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
|
use std::ops::Sub;
use chrono::{Duration, Local};
use crate::{
config::ZellijState,
widgets::{command::TIMESTAMP_FORMAT, notification},
};
/// Parses the line protocol and updates the state accordingly
///
/// The protocol is as follows:
///
/// zjstatus::command_name::args
///
/// It first starts with `zjstatus` as a prefix to indicate that the line is
/// used for the line protocol and zjstatus should parse it. It is followed
/// by the command name and then the arguments. The following commands are
/// available:
///
/// - `rerun` - Reruns the command with the given name (like in the config) as
/// argument. E.g. `zjstatus::rerun::command_1`
///
/// The function returns a boolean indicating whether the state has been
/// changed and the UI should be re-rendered.
#[tracing::instrument(skip(state))]
pub fn parse_protocol(state: &mut ZellijState, input: &str) -> bool {
tracing::debug!("parsing protocol");
let lines = input.split('\n').collect::<Vec<&str>>();
let mut should_render = false;
for line in lines {
let line_renders = process_line(state, line);
if line_renders {
should_render = true;
}
}
should_render
}
#[tracing::instrument(skip_all)]
fn process_line(state: &mut ZellijState, line: &str) -> bool {
let parts = line.split("::").collect::<Vec<&str>>();
if parts.len() < 3 {
return false;
}
if parts[0] != "zjstatus" {
return false;
}
tracing::debug!("command: {}", parts[1]);
let mut should_render = false;
#[allow(clippy::single_match)]
match parts[1] {
"rerun" => {
rerun_command(state, parts[2]);
should_render = true;
}
"notify" => {
notify(state, parts[2]);
should_render = true;
}
"pipe" => {
if parts.len() < 4 {
return false;
}
pipe(state, parts[2], parts[3]);
should_render = true;
}
_ => {}
}
should_render
}
fn pipe(state: &mut ZellijState, name: &str, content: &str) {
tracing::debug!("saving pipe result {name} {content}");
state
.pipe_results
.insert(name.to_owned(), content.to_owned());
}
fn notify(state: &mut ZellijState, message: &str) {
state.incoming_notification = Some(notification::Message {
body: message.to_string(),
received_at: Local::now(),
});
}
fn rerun_command(state: &mut ZellijState, command_name: &str) {
invalidate_command_result(state, command_name);
}
/// Backdates the stored timestamp of a command result so the next render
/// re-runs the command.
pub fn invalidate_command_result(state: &mut ZellijState, command_name: &str) {
let command_result = state.command_results.get(command_name);
if command_result.is_none() {
return;
}
let mut command_result = command_result.unwrap().clone();
let ts = Sub::<Duration>::sub(Local::now(), Duration::try_days(1).unwrap());
command_result.context.insert(
"timestamp".to_string(),
ts.format(TIMESTAMP_FORMAT).to_string(),
);
state.command_results.remove(command_name);
state
.command_results
.insert(command_name.to_string(), command_result.clone());
}
|