Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lazyslurm"
version = "0.1.6"
version = "0.1.7"
edition = "2024"
description = "A terminal UI for monitoring and managing slurm jobs"
license = "MIT"
Expand Down
34 changes: 25 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::Parser;
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
event::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, KeyModifiers,
},
execute,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
Expand Down Expand Up @@ -119,26 +121,40 @@ async fn run_app(
continue;
}

match key.code {
KeyCode::Char('q') => return Ok(()),
KeyCode::Char('r') => {
match (key.code, key.modifiers) {
(KeyCode::Char('q'), _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
return Ok(());
}
(KeyCode::Char('r'), _) => {
app.refresh_jobs().await?;
}
KeyCode::Up => {
(KeyCode::Up, _) => {
app.select_previous_job();
}
KeyCode::Down => {
(KeyCode::Down, _) => {
app.select_next_job();
}
KeyCode::Char('c') => {
if let Err(e) = app.cancel_selected_job().await {
app.error_message = Some(format!("Failed to cancel job: {}", e));
(KeyCode::Char('c'), _) => {
if app.selected_job.is_some() {
app.show_confirm_popup = true;
app.confirm_action = false;
}
}
(KeyCode::Char('y'), _) if app.show_confirm_popup => {
app.confirm_action = true;
app.show_confirm_popup = false;
}
(KeyCode::Char('n'), _) | (KeyCode::Esc, _) if app.show_confirm_popup => {
app.show_confirm_popup = false;
app.confirm_action = false;
}

_ => {}
}
}

app.handle_confirm_action().await?;

// Auto refresh if needed
if app.should_refresh() {
app.refresh_jobs().await?;
Expand Down
14 changes: 14 additions & 0 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct App {
pub error_message: Option<String>,
pub event_sender: mpsc::UnboundedSender<AppEvent>,
pub event_receiver: mpsc::UnboundedReceiver<AppEvent>,
pub show_confirm_popup: bool,
pub confirm_action: bool,
}

impl App {
Expand All @@ -43,6 +45,8 @@ impl App {
error_message: None,
event_sender,
event_receiver,
show_confirm_popup: false,
confirm_action: false,
}
}

Expand Down Expand Up @@ -134,6 +138,16 @@ impl App {
self.job_list.completed_jobs()
}

pub async fn handle_confirm_action(&mut self) -> Result<()> {
if self.confirm_action && self.selected_job.is_some() {
if let Err(e) = self.cancel_selected_job().await {
self.error_message = Some(format!("Failed to cancel job: {}", e));
}
self.confirm_action = false;
}
Ok(())
}

pub async fn cancel_selected_job(&mut self) -> Result<()> {
if let Some(job) = &self.selected_job {
SlurmCommands::scancel(&job.job_id).await?;
Expand Down
42 changes: 41 additions & 1 deletion src/ui/components.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use ratatui::{
Frame,
layout::{Constraint, Direction, Layout, Rect},
prelude::Alignment,
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, List, ListItem, Paragraph, Wrap},
widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap},
};
use std::fs;

Expand Down Expand Up @@ -54,6 +55,25 @@ pub fn render_app(frame: &mut Frame, app: &App) {

// Render help bar
render_help_bar(frame, chunks[2]);

if app.show_confirm_popup {
let popup_area = centered_rect(30, 7, frame.area());

frame.render_widget(Clear, popup_area);

let popup = Paragraph::new("Cancel selected job? (y/n)")
.style(Style::default().fg(Color::White))
.block(
Block::default()
.borders(Borders::ALL)
.title("Confirm")
.style(Style::default().fg(Color::Yellow)),
)
.wrap(Wrap { trim: true })
.alignment(Alignment::Center);

frame.render_widget(popup, popup_area);
}
}

fn render_status_bar(frame: &mut Frame, app: &App, area: Rect) {
Expand Down Expand Up @@ -309,3 +329,23 @@ fn truncate(s: &str, max_len: usize) -> String {
format!("{}...", &s[..max_len.saturating_sub(3)])
}
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
])
.split(r);

Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
])
.split(popup_layout[1])[1]
}
Loading