From a09b8b55737e2de3b26e7d6c7a3e51d556e6512b Mon Sep 17 00:00:00 2001 From: SW Date: Mon, 15 Jun 2026 13:18:42 +0200 Subject: [PATCH] Run commands in current terminal by default; 'o' opens in new window --- src/app.rs | 2 ++ src/events.rs | 19 ++++++++++++++++++- src/main.rs | 22 ++++++++++++++++------ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/app.rs b/src/app.rs index 05b0764..e01997c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,6 +19,7 @@ pub struct App { pub file_index: usize, pub section_index: usize, pub focus: Focus, + pub pending_command: Option>, pub message: Option, } @@ -34,6 +35,7 @@ impl App { file_index: 0, section_index: 0, focus: Focus::Groups, + pending_command: None, message: None, } } diff --git a/src/events.rs b/src/events.rs index 8476146..d6404f3 100644 --- a/src/events.rs +++ b/src/events.rs @@ -41,11 +41,28 @@ pub fn handle_event(app: &mut App, event: AppEvent) -> bool { edit_file(&file.path, &app.terminal, &app.shell); } } - KeyCode::Enter => { + KeyCode::Char('o') => { if let (Some(section), Some(file)) = (app.current_section(), app.current_file()) { run_section(section, file, &app.terminal, &app.shell, &app.data_dir); } } + KeyCode::Enter => { + if let (Some(section), Some(file)) = (app.current_section(), app.current_file()) { + let commands = if section.is_run_all() { + file.sections + .iter() + .filter(|s| !s.is_run_all()) + .flat_map(|s| s.commands.clone()) + .collect() + } else { + section.commands.clone() + }; + if !commands.is_empty() { + app.pending_command = Some(commands); + return false; + } + } + } _ => {} } true diff --git a/src/main.rs b/src/main.rs index d3256be..4c4d287 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod ui; use std::io; use std::path::PathBuf; +use std::process::Command; use std::time::Duration; use clap::Parser; @@ -40,7 +41,11 @@ fn main() -> io::Result<()> { // Ensure data directory exists if let Err(e) = std::fs::create_dir_all(&config.data_dir) { - eprintln!("Nepodařilo se vytvořit datovou složku {}: {}", config.data_dir.display(), e); + eprintln!( + "Nepodařilo se vytvořit datovou složku {}: {}", + config.data_dir.display(), + e + ); } let app = App::new(config.data_dir, config.terminal, config.shell); @@ -51,7 +56,7 @@ fn main() -> io::Result<()> { let backend = CrosstermBackend::new(stdout); let mut terminal = Terminal::new(backend)?; - let res = run_app(&mut terminal, app); + let app = run_app(&mut terminal, app)?; disable_raw_mode()?; execute!( @@ -61,14 +66,14 @@ fn main() -> io::Result<()> { )?; terminal.show_cursor()?; - if let Err(err) = res { - eprintln!("{}", err); + if let Some(commands) = app.pending_command { + run_in_current_terminal(&commands, &app.shell); } Ok(()) } -fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> +fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result where io::Error: From<::Error>, { @@ -80,7 +85,12 @@ where if let Some(event) = poll_event(tick_rate)? && !handle_event(&mut app, event) { - return Ok(()); + return Ok(app); } } } + +fn run_in_current_terminal(commands: &[String], shell: &str) { + let script = commands.join("\n"); + let _ = Command::new(shell).arg("-c").arg(script).status(); +}