1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
use zellij_tile::prelude::*;
use std::{collections::BTreeMap, sync::Arc};
use zjstatus::frames;
#[derive(Default, Debug, Clone)]
pub struct ZellijState {
pub mode: ModeInfo,
pub panes: PaneManifest,
pub tabs: Vec<TabInfo>,
}
#[derive(Default)]
struct State {
pending_events: Vec<Event>,
got_permissions: bool,
state: ZellijState,
hide_frame_for_single_pane: bool,
hide_frame_except_for_search: bool,
hide_frame_except_for_fullscreen: bool,
err: Option<anyhow::Error>,
}
#[cfg(not(test))]
register_plugin!(State);
#[cfg(feature = "tracing")]
fn init_tracing() {
use std::fs::File;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
let file = File::create("/host/.zjframes.log");
let file = match file {
Ok(file) => file,
Err(error) => panic!("Error: {:?}", error),
};
let debug_log = tracing_subscriber::fmt::layer().with_writer(Arc::new(file));
tracing_subscriber::registry().with(debug_log).init();
tracing::info!("tracing initialized");
}
impl ZellijPlugin for State {
fn load(&mut self, configuration: BTreeMap<String, String>) {
#[cfg(feature = "tracing")]
init_tracing();
// we need the ReadApplicationState permission to receive the ModeUpdate and TabUpdate
// events
// we need the RunCommands permission to run "cargo test" in a floating window
request_permission(&[
PermissionType::ReadApplicationState,
PermissionType::ChangeApplicationState,
]);
subscribe(&[
EventType::ModeUpdate,
EventType::PaneUpdate,
EventType::PermissionRequestResult,
EventType::TabUpdate,
EventType::SessionUpdate,
]);
self.hide_frame_for_single_pane = match configuration.get("hide_frame_for_single_pane") {
Some(toggle) => toggle == "true",
None => false,
};
self.hide_frame_except_for_search = match configuration.get("hide_frame_except_for_search")
{
Some(toggle) => toggle == "true",
None => false,
};
self.hide_frame_except_for_fullscreen =
match configuration.get("hide_frame_except_for_fullscreen") {
Some(toggle) => toggle == "true",
None => false,
};
self.pending_events = Vec::new();
self.got_permissions = false;
self.state = ZellijState::default();
}
#[tracing::instrument(skip_all, fields(event_type))]
fn update(&mut self, event: Event) -> bool {
if let Event::PermissionRequestResult(PermissionStatus::Granted) = event {
self.got_permissions = true;
while !self.pending_events.is_empty() {
tracing::debug!("processing cached event");
let ev = self.pending_events.pop();
self.handle_event(ev.unwrap());
}
}
if !self.got_permissions {
tracing::debug!("caching event");
self.pending_events.push(event);
return false;
}
self.handle_event(event)
}
#[tracing::instrument(skip_all)]
fn render(&mut self, _rows: usize, _cols: usize) {
if !self.got_permissions {
return;
}
if let Some(err) = &self.err {
println!("Error: {:?}", err);
return;
}
print!("Please load this plugin in the background");
}
}
impl State {
fn handle_event(&mut self, event: Event) -> bool {
let mut should_render = false;
match event {
Event::ModeUpdate(mode_info) => {
tracing::Span::current().record("event_type", "Event::ModeUpdate");
tracing::debug!(mode = ?mode_info.mode);
tracing::debug!(mode = ?mode_info.session_name);
self.state.mode = mode_info;
frames::hide_frames_conditionally(
&frames::FrameConfig::new(
self.hide_frame_for_single_pane,
self.hide_frame_except_for_search,
self.hide_frame_except_for_fullscreen,
),
&self.state.tabs,
&self.state.panes,
&self.state.mode,
get_plugin_ids(),
true,
);
}
Event::PaneUpdate(pane_info) => {
tracing::Span::current().record("event_type", "Event::PaneUpdate");
tracing::debug!(pane_count = ?pane_info.panes.len());
self.state.panes = pane_info;
frames::hide_frames_conditionally(
&frames::FrameConfig::new(
self.hide_frame_for_single_pane,
self.hide_frame_except_for_search,
self.hide_frame_except_for_fullscreen,
),
&self.state.tabs,
&self.state.panes,
&self.state.mode,
get_plugin_ids(),
true,
);
}
Event::PermissionRequestResult(result) => {
tracing::Span::current().record("event_type", "Event::PermissionRequestResult");
tracing::debug!(result = ?result);
set_selectable(false);
should_render = true;
}
Event::SessionUpdate(session_info, _) => {
tracing::Span::current().record("event_type", "Event::SessionUpdate");
let current_session = session_info.iter().find(|s| s.is_current_session);
if let Some(current_session) = current_session {
frames::hide_frames_conditionally(
&frames::FrameConfig::new(
self.hide_frame_for_single_pane,
self.hide_frame_except_for_search,
self.hide_frame_except_for_fullscreen,
),
¤t_session.tabs,
¤t_session.panes,
&self.state.mode,
get_plugin_ids(),
true,
);
}
}
Event::TabUpdate(tab_info) => {
tracing::Span::current().record("event_type", "Event::TabUpdate");
tracing::debug!(tab_count = ?tab_info.len());
self.state.tabs = tab_info;
}
_ => (),
};
should_render
}
}
|