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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use std::process::{Command, Stdio};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent};
use ratatui::{
prelude::{Alignment, Constraint, Direction, Frame, Layout},
style::{Color, Style},
widgets::Paragraph,
};
use crate::menus::MenuContainer;
use crate::mouse_input::MouseInput;
use crate::user_options::UserOptions;
use crate::menus::{
OpenMenu,
job_actions::JobActions,
message::{Message, MessageKind},
confirmation::Confirmation,};
use crate::job::{Job, JobStatus};
use crate::joblist::{JobList, JobListAction};
/// At the end of each tick, the app will handle the action that was set
/// during the tick. This enum represents the possible actions that can be
/// taken.
#[derive(Debug, Default, Clone)]
pub enum Action {
#[default]
None,
/// Opens a confirmation dialog to quit the application
Quit,
/// This action always quits the application
ConfirmedQuit,
/// Opens a selected menu
OpenMenu(OpenMenu),
/// Remove Salloc Entry (Confirmation Dialog)
RemoveSallocEntryDialog,
/// Remove Salloc Entry (Confirmed)
RemoveSallocEntry,
/// Updates the user options from the user options menu
UpdateUserOptions,
/// Updates the joblist (e.g. job selection, job sorting, etc.)
UpdateJobList(JobListAction),
/// Handles a job action (e.g. kill, open log)
JobOption(JobActions),
/// Start the salloc command with the parameters
StartSalloc(String),
}
/// The main application struct that holds all information and
/// states of the application.
pub struct App {
/// The current action that should be taken
pub action: Action,
// booleans of actions that are handled in the main loop
/// If true, the application should quit
pub should_quit: bool,
/// If true, the application should reset the frame rate
pub should_set_frame_rate: bool,
/// If true, the application should redraw the tui
pub should_redraw: bool,
/// If true, the application should execute the given command
pub should_execute_command: bool,
/// To open vim, tui must be closed. Hence they must be handled in
/// the main loop
pub open_vim: bool,
/// The path to the file that should be opened in vim
vim_path: Option<String>,
// This command will be written to a given file (for execution after
// closing stama)
pub exit_command: Option<String>,
/// A buffer for a command
command: String,
/// The user options
pub user_options: UserOptions,
// The joblist is the main data structure that holds all the jobs
pub joblist: JobList,
// All the menus and dialogs
pub menus: MenuContainer,
// Mouse input
pub mouse_input: MouseInput,
}
// ===================================================================
// CONSTRUCTOR
// ===================================================================
impl App {
pub fn new() -> Self {
// loading user options from config file
let user_options = UserOptions::load();
// create the joblist
let mut joblist = JobList::new();
// start the main joblist thread to update the jobs
joblist.update_jobs(&user_options);
let menus = MenuContainer::new(&user_options, &joblist);
// create the app
Self {
action: Action::None,
should_quit: false,
should_set_frame_rate: false,
should_redraw: true,
should_execute_command: false,
open_vim: false,
vim_path: None,
exit_command: None,
command: "".to_string(),
user_options,
joblist,
menus,
mouse_input: MouseInput::new(),
}
}
}
// ===================================================================
// METHODS
// ===================================================================
impl App {
/// Handles the action that was set during the tick
pub fn handle_action(&mut self) {
match &self.action {
Action::Quit => {
self.quit();
}
Action::ConfirmedQuit => {
self.confirmed_quit();
}
Action::OpenMenu(menu) => {
self.menus.activate_menu(menu.clone(), &self.joblist);
}
Action::UpdateUserOptions => {
self.update_user_options();
}
Action::UpdateJobList(change) => {
self.update_job_list(change.clone());
}
Action::JobOption(action) => {
self.handle_job_action(action.clone());
}
Action::StartSalloc(cmd) => {
self.should_execute_command = true;
self.command = cmd.to_string();
}
Action::RemoveSallocEntryDialog => {
self.open_remove_salloc_entry_dialog();
}
Action::RemoveSallocEntry => {
self.menus.salloc_menu.delete_current_entry();
}
_ => {}
};
// reset the action
self.action = Action::None;
}
/// Either opens a confirmation dialog to quit the application
/// or quits the application directly if the user options are set
/// to not confirm
pub fn quit(&mut self) {
if self.user_options.confirm_before_quit {
self.menus.confirmation = Confirmation::new(
"Quit?", Action::ConfirmedQuit);
} else {
self.should_quit = true;
}
}
/// Quits the application. Always.
fn confirmed_quit(&mut self) {
self.should_quit = true;
}
/// Updates the user options from the user options menu
fn update_user_options(&mut self) {
// save the old refresh rate to check if it has changed
let old_rate = self.user_options.refresh_rate;
// update the user options
self.user_options = self.menus.user_options_menu.to_user_option();
let new_rate = self.user_options.refresh_rate;
// update the job overview refresh rate if it has changed
if old_rate != new_rate {
self.menus.job_overview.refresh_rate = new_rate;
self.should_set_frame_rate = true;
}
}
/// Updates the joblist (e.g. job selection, job sorting, etc.)
fn update_job_list(&mut self, change: JobListAction) {
self.joblist.handle_joblist_action(change);
}
/// Handles a job action (e.g. kill, open log)
fn handle_job_action(&mut self, action: JobActions) {
match action {
JobActions::Kill(job) => self.open_kill_confirmation(&job),
JobActions::KillConfirmed(job) => self.kill_job(&job),
JobActions::OpenLog(_) => self.open_log(),
JobActions::OpenSubmission(_) => self.open_submissions(),
JobActions::GoWorkDir(_) => self.go_workdir(),
JobActions::SSH(_) => self.ssh_to_node(),
}
}
/// Open an error message
fn open_error_message(&mut self, msg: &str) {
self.menus.message = Message::new(msg);
self.menus.message.kind = MessageKind::Error;
}
/// Open remove salloc entry dialog
fn open_remove_salloc_entry_dialog(&mut self) {
self.menus.confirmation = Confirmation::new(
"Are your sure you want to remove the entry?",
Action::RemoveSallocEntry);
}
}
// ===================================================================
// JOB ACTIONS FUNCTIONS
// ===================================================================
impl App {
/// Opens a confirmation dialog to kill the selected job
fn open_kill_confirmation(&mut self, job: &Job) {
if self.user_options.confirm_before_kill {
let job_name = job.get_jobname();
let msg = format!("Kill job {} ({})?", job_name, job.id);
self.menus.confirmation = Confirmation::new(
&msg, Action::JobOption(
JobActions::KillConfirmed(job.clone())));
} else {
self.kill_job(job);
}
}
/// Kills the selected job with the "scancel" command
/// If the user has no permission to kill the job, an error Message
/// will be shown.
fn kill_job(&mut self, job: &Job) {
// perform the kill command
let command_status = Command::new("scancel")
.arg(job.id.to_string())
.output();
// check if the command was successful. This will check if the command
// could be executed. It will not check if the job was actually killed.
match command_status {
Ok(output) => {
// Check the exit status of the command.
// If it was not successful, show an error message.
if !output.status.success() {
let error_msg = String::from_utf8_lossy(&output.stderr);
self.open_error_message(
&format!("Error killing job: {}", error_msg));
}
}
Err(e) => {
// If the command could not be executed, show an error message.
self.open_error_message(
&format!("Error killing job: {}", e));
}
}
}
/// Opens the log file of the selected job in vim (or the
/// user defined editor)
/// If no log file is found, an error message will be shown.
fn open_log(&mut self) {
// get the current job
let job = match self.joblist.get_job() {
Some(job) => job,
None => {
// if no job is selected, show an error message
self.open_error_message("No job selected");
return;
}
};
// try to get the log file path from the job
let output = job.get_stdout();
let log_path = if let Some(log_path) = &output {
log_path
} else {
self.open_error_message("No log file found");
return;
};
// set the vim path and set the open_vim flag to true
self.vim_path = Some(log_path.clone());
self.open_vim = true;
}
/// Opens the submission script of the selected job in vim (or the
/// user defined editor)
fn open_submissions(&mut self) {
let job = match self.joblist.get_job() {
Some(job) => job,
None => {
self.open_error_message("No job selected");
return;
}
};
// if the sumbission command is "(null)", show an error message
if job.command == "(null)" {
self.open_error_message("No submission script found");
return;
}
// if the job command is empty (only whitespace),
// show an error message
if job.command.trim().is_empty() {
self.open_error_message("No submission script found");
return;
}
// For completed jobs, the command is for example:
// "sbin/sbatch relative/path/to/job.sh"
// Or all the sbatch options are given in the command.
// At the moment, I don't know how to decode it to the file path.
// So I just show the command.
if job.is_completed() {
let mes = format!("Job was submitted with: \n {}", &job.command);
self.menus.message = Message::new(&mes);
return;
}
// Finally, if all checks are passed, open the submission script
// by setting the vim path and the open_vim flag to true.
self.vim_path = Some(job.command.clone());
self.open_vim = true;
}
/// Opens file in external editor
/// This function is called from the main loop when the open_vim
/// flag is set to true. (see main.rs)
pub fn open_file_in_editor(&mut self) {
let editor = self.user_options.external_editor.as_str();
match &self.vim_path {
Some(path) => {
let mut parts = editor.trim().split_whitespace();
let program = parts.next().unwrap_or(" ");
let args: Vec<&str> = parts.collect();
let mut child = Command::new(program)
.args(args)
.arg(path)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn().expect("Failed to execute command");
// Wait for the process to finish
child.wait().expect("Failed to wait on child");
self.open_vim = false;
self.vim_path = None;
}
None => {}
}
}
/// Opens the working directory of the selected job in the terminal
/// This only works if the exit command is executed in the terminal
fn go_workdir(&mut self) {
// first get the current job
let job = match self.joblist.get_job() {
Some(job) => job,
None => {
// if no job is selected, show an error message
self.open_error_message("No job selected");
return;
}
};
// set the exit command to "cd <workdir>"
let command = format!("cd {}", job.workdir);
self.exit_command = Some(command);
// set the should_quit flag to true to exit the application
self.should_quit = true;
}
/// Get the first node in the node list and create a ssh command to it:
/// "ssh <node>"
/// The command will only be executed in the terminal after closing stama
/// if a wrapper script is used around stama.
fn ssh_to_node(&mut self) {
// get the current job
let job = match self.joblist.get_job() {
Some(job) => job,
None => {
// if no job is selected, show an error message
self.open_error_message("No job selected");
return;
}
};
// check if the job is running
// if not, there will be no node to ssh to
if job.status != JobStatus::Running {
// print an error message if the job is not running
self.open_error_message("Job not running");
return;
}
// get the node list of the job
let com_stat = Command::new("squeue")
.arg("-j")
.arg(&job.id)
.arg("--Format=NodeList")
.arg("--noheader")
.output();
// check if the command was successful
match com_stat {
Ok(output) => {
if !output.status.success() {
// print an error message if the command was not successful
let error_msg = String::from_utf8_lossy(&output.stderr);
self.open_error_message(
&format!("Error getting node list: {}", error_msg));
return;
}
// format the node list such that only the first node is taken
// assume format l[42314-42434], or l[42314,42316,42319]
let node_list = String::from_utf8_lossy(&output.stdout);
// remove the brackets
let node_list = node_list.trim().replace("[", "");
// discard everything after the first comma or dash
let mut node = node_list.split("-").collect::<Vec<&str>>()[0];
node = node.split(",").collect::<Vec<&str>>()[0];
// create the ssh command
let command = format!("ssh {}", node);
// set the exit command to the ssh command and set the
// exit flag to true
self.exit_command = Some(command);
self.should_quit = true;
}
Err(e) => {
// print an error message if the squeue command to get the
// node list could not be executed
self.open_error_message(
&format!("Error getting node list: {}", e));
}
}
}
/// Start the Salloc Command
pub fn start_salloc(&mut self) {
println!("{}", self.command);
let mut parts = self.command.trim().split_whitespace();
let program = parts.next().unwrap_or(" ");
let args: Vec<&str> = parts.collect();
let output_status = Command::new(program)
.args(args)
.arg("--no-shell")
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn(); //.expect("Failed to execute command");
// open a error dialog if the command could not be executed
if output_status.is_err() {
let msg = format!("Error starting salloc command: {}",
output_status.err().unwrap());
self.open_error_message(&msg);
} else {
let mut child = output_status.unwrap();
// Wait for the process to finish
child.wait().expect("Failed to wait on child");
}
self.should_execute_command = false;
}
}
// ===================================================================
// Events
// ===================================================================
impl App {
/// Updates the joblist
pub fn update_jobs(&mut self) {
self.joblist.update_jobs(&self.user_options);
}
/// Handle keyboard input
pub fn input(&mut self, key_event: KeyEvent) {
// Ctrl + C should always quit, regardless of the input mode
match key_event.code {
KeyCode::Char('c') | KeyCode::Char('C') => {
if key_event.modifiers == KeyModifiers::CONTROL {
self.quit();
return
}
}
_ => {}
};
// pass the key event to the app menus
self.menus.input(&mut self.action, key_event);
self.handle_action();
}
/// Handles mouse input
pub fn mouse_input(&mut self, mouse_event: MouseEvent) {
self.menus.mouse_input(
&mut self.action,
&mut self.mouse_input,
mouse_event,
);
self.handle_action();
}
/// Render the UI
pub fn render(&mut self, f: &mut Frame) {
let outer_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Min(3),
Constraint::Length(1),
]
.as_ref(),
)
.split(f.size());
// make a info text at the bottom
f.render_widget(
Paragraph::new("Press `Ctrl-C` or `q` for exit, `?` for help")
.style(Style::default().fg(Color::LightCyan))
.alignment(Alignment::Center),
outer_layout[1],
);
// render the windows
self.menus.render(f, &outer_layout[0], &self.joblist);
}
}