diff --git a/src/main.rs b/src/main.rs index 5057aac..b1d1adf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,6 @@ use std::path::PathBuf; use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; -use command::CommandResult; use draw::run_tui; use model::Model; use serde_json::Value; @@ -314,30 +313,32 @@ fn get_import_data(paths: &[PathBuf]) -> Option { // ── Headless command execution ─────────────────────────────────────────────── fn run_headless_commands(cmds: &[String], file: &Option) -> Result<()> { - let mut model = get_initial_model(file)?; + use crossterm::event::{KeyCode, KeyModifiers}; + + let model = get_initial_model(file)?; + let mut app = ui::app::App::new(model, file.clone()); let mut exit_code = 0; for line in cmds { - let parsed = match command::parse_line(line) { - Ok(cmds) => cmds, + match command::parse_line(line) { + Ok(parsed_cmds) => { + for cmd in &parsed_cmds { + let effects = { + let ctx = app.cmd_context(KeyCode::Null, KeyModifiers::NONE); + cmd.execute(&ctx) + }; + app.apply_effects(effects); + } + } Err(e) => { - let r = CommandResult::err(format!("Parse error: {e}")); - println!("{}", serde_json::to_string(&r)?); - exit_code = 1; - continue; - } - }; - for cmd in &parsed { - let result = command::dispatch(&mut model, cmd); - if !result.ok { + eprintln!("Parse error: {e}"); exit_code = 1; } - println!("{}", serde_json::to_string(&result)?); } } if let Some(path) = file { - persistence::save(&model, path)?; + persistence::save(&app.model, path)?; } std::process::exit(exit_code); diff --git a/src/ui/app.rs b/src/ui/app.rs index 4e718fd..81df986 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -99,7 +99,7 @@ impl App { } } - fn cmd_context(&self, key: KeyCode, mods: KeyModifiers) -> CmdContext<'_> { + pub fn cmd_context(&self, key: KeyCode, mods: KeyModifiers) -> CmdContext<'_> { let view = self.model.active_view(); CmdContext { model: &self.model,