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
use {
crate::*,
anyhow::Result,
crokey::CroKey,
crossbeam::channel::{
bounded,
select,
},
termimad::crossterm::event::Event,
termimad::EventSource,
};
/// Run the mission and return the reference to the next
/// job to run, if any
pub fn run(
w: &mut W,
mission: Mission,
event_source: &EventSource,
) -> Result<Option<JobRef>> {
let keybindings = mission.settings.keybindings.clone();
let mut ignorer = time!(Info, mission.ignorer());
let (watch_sender, watch_receiver) = bounded(0);
let mut watcher =
notify::recommended_watcher(move |res: notify::Result<notify::Event>| match res {
Ok(we) => {
debug!("notify event: {we:?}");
if let Some(ignorer) = ignorer.as_mut() {
match time!(Info, ignorer.excludes_all(&we.paths)) {
Ok(true) => {
debug!("all excluded");
return;
}
Ok(false) => {
debug!("at least one is included");
}
Err(e) => {
warn!("exclusion check failed: {e}");
}
}
}
if let Err(e) = watch_sender.send(()) {
debug!("error when notifying on inotify event: {}", e);
}
}
Err(e) => warn!("watch error: {:?}", e),
})?;
mission.add_watchs(&mut watcher)?;
let executor = Executor::new(&mission)?;
let mut state = AppState::new(mission)?;
state.computation_starts();
state.draw(w)?;
executor.start(state.new_task())?; // first computation
let user_events = event_source.receiver();
let mut next_job: Option<JobRef> = None;
#[allow(unused_mut)]
loop {
let mut action: Option<&Action> = None;
select! {
recv(user_events) -> user_event => {
match user_event?.event {
Event::Resize(mut width, mut height) => {
// I don't know why but Crossterm seems to always report an
// understimated size on Windows
#[cfg(windows)]
{
width += 1;
height += 1;
}
state.resize(width, height);
}
Event::Key(key_event) => {
debug!("key pressed: {}", CroKey::from(key_event));
action = keybindings.get(key_event);
}
_ => {}
}
event_source.unblock(false);
}
recv(watch_receiver) -> _ => {
if let Err(e) = executor.start(state.new_task()) {
debug!("error sending task: {}", e);
} else {
state.computation_starts();
}
}
recv(executor.line_receiver) -> info => {
match info? {
CommandExecInfo::Line(line) => {
state.add_line(line);
}
CommandExecInfo::End { status } => {
info!("execution finished with status: {:?}", status);
// computation finished
if let Some(output) = state.take_output() {
let cmd_result = CommandResult::new(output, status)?;
state.set_result(cmd_result);
action = state.action();
} else {
warn!("a computation finished but didn't start?");
state.computation_stops();
}
}
CommandExecInfo::Error(e) => {
warn!("error in computation: {}", e);
state.computation_stops();
break;
}
CommandExecInfo::Interruption => {
debug!("command was interrupted (by us)");
}
}
}
}
if let Some(action) = action.take() {
debug!("requested action: {action:?}");
match action {
Action::Internal(internal) => match internal {
Internal::Back => {
if !state.close_help() {
next_job = Some(JobRef::Previous);
break;
}
}
Internal::Help => {
state.toggle_help();
}
Internal::Quit => {
break;
}
Internal::ReRun => {
if let Err(e) = executor.start(state.new_task()) {
debug!("error sending task on re-rerun: {}", e);
} else {
state.computation_starts();
}
}
Internal::ToggleRawOutput => {
state.toggle_raw_output();
}
Internal::ToggleSummary => {
state.toggle_summary_mode();
}
Internal::ToggleWrap => {
state.toggle_wrap_mode();
}
Internal::ToggleBacktrace => {
state.toggle_backtrace();
if let Err(e) = executor.start(state.new_task()) {
debug!("error sending task: {}", e);
} else {
state.computation_starts();
}
}
Internal::Scroll(scroll_command) => {
let scroll_command = *scroll_command;
state.apply_scroll_command(scroll_command);
}
},
Action::Job(job_ref) => {
next_job = Some((*job_ref).clone());
break;
}
}
}
state.draw(w)?;
}
executor.die()?;
Ok(next_job)
}