Run commands in current terminal by default; 'o' opens in new window

This commit is contained in:
SW 2026-06-15 13:18:42 +02:00
parent 378b6d3645
commit a09b8b5573
3 changed files with 36 additions and 7 deletions

View file

@ -19,6 +19,7 @@ pub struct App {
pub file_index: usize,
pub section_index: usize,
pub focus: Focus,
pub pending_command: Option<Vec<String>>,
pub message: Option<String>,
}
@ -34,6 +35,7 @@ impl App {
file_index: 0,
section_index: 0,
focus: Focus::Groups,
pending_command: None,
message: None,
}
}

View file

@ -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

View file

@ -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<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()>
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<App>
where
io::Error: From<<B as Backend>::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();
}