refactor(main): update headless mode to use App and new command system
Update headless command execution to use the new architecture: - Removed CommandResult import (no longer needed) - Headless mode now creates an App instance and uses cmd_context() - Commands are parsed and executed via the registry, effects applied through app.apply_effects() instead of command::dispatch() - Made cmd_context() public so headless mode can access it - Updated persistence save to use app.model instead of direct model Tests updated to use ExecuteCommand instead of QuitCmd, with proper buffer setup for command parsing. Co-Authored-By: fiddlerwoaroof/git-smart-commit (unsloth/Qwen3.5-35B-A3B-GGUF:Q5_K_M)
This commit is contained in:
31
src/main.rs
31
src/main.rs
@ -14,7 +14,6 @@ use std::path::PathBuf;
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
use command::CommandResult;
|
|
||||||
use draw::run_tui;
|
use draw::run_tui;
|
||||||
use model::Model;
|
use model::Model;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
@ -314,30 +313,32 @@ fn get_import_data(paths: &[PathBuf]) -> Option<Value> {
|
|||||||
// ── Headless command execution ───────────────────────────────────────────────
|
// ── Headless command execution ───────────────────────────────────────────────
|
||||||
|
|
||||||
fn run_headless_commands(cmds: &[String], file: &Option<PathBuf>) -> Result<()> {
|
fn run_headless_commands(cmds: &[String], file: &Option<PathBuf>) -> 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;
|
let mut exit_code = 0;
|
||||||
|
|
||||||
for line in cmds {
|
for line in cmds {
|
||||||
let parsed = match command::parse_line(line) {
|
match command::parse_line(line) {
|
||||||
Ok(cmds) => cmds,
|
Ok(parsed_cmds) => {
|
||||||
Err(e) => {
|
for cmd in &parsed_cmds {
|
||||||
let r = CommandResult::err(format!("Parse error: {e}"));
|
let effects = {
|
||||||
println!("{}", serde_json::to_string(&r)?);
|
let ctx = app.cmd_context(KeyCode::Null, KeyModifiers::NONE);
|
||||||
exit_code = 1;
|
cmd.execute(&ctx)
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
for cmd in &parsed {
|
app.apply_effects(effects);
|
||||||
let result = command::dispatch(&mut model, cmd);
|
}
|
||||||
if !result.ok {
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Parse error: {e}");
|
||||||
exit_code = 1;
|
exit_code = 1;
|
||||||
}
|
}
|
||||||
println!("{}", serde_json::to_string(&result)?);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(path) = file {
|
if let Some(path) = file {
|
||||||
persistence::save(&model, path)?;
|
persistence::save(&app.model, path)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::process::exit(exit_code);
|
std::process::exit(exit_code);
|
||||||
|
|||||||
@ -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();
|
let view = self.model.active_view();
|
||||||
CmdContext {
|
CmdContext {
|
||||||
model: &self.model,
|
model: &self.model,
|
||||||
|
|||||||
Reference in New Issue
Block a user