refactor: split cmd.rs

This commit is contained in:
Edward Langley
2026-04-09 02:25:23 -07:00
parent 767d524d4b
commit fd69126cdc
15 changed files with 4391 additions and 4300 deletions

203
src/command/cmd/commit.rs Normal file
View File

@ -0,0 +1,203 @@
use crate::model::cell::{CellKey, CellValue};
use crate::ui::app::AppMode;
use crate::ui::effect::{self, Effect};
use super::core::{Cmd, CmdContext};
use super::navigation::{viewport_effects, CursorState, EnterAdvance};
// ── Commit commands (mode-specific buffer consumers) ────────────────────────
/// Commit a cell value: for synthetic records keys, stage in drill pending edits
/// or apply directly; for real keys, write to the model.
fn commit_cell_value(key: &CellKey, value: &str, effects: &mut Vec<Box<dyn Effect>>) {
if let Some((record_idx, col_name)) = crate::view::synthetic_record_info(key) {
effects.push(Box::new(effect::SetDrillPendingEdit {
record_idx,
col_name,
new_value: value.to_string(),
}));
} else if value.is_empty() {
effects.push(Box::new(effect::ClearCell(key.clone())));
effects.push(effect::mark_dirty());
} else if let Ok(n) = value.parse::<f64>() {
effects.push(Box::new(effect::SetCell(key.clone(), CellValue::Number(n))));
effects.push(effect::mark_dirty());
} else {
effects.push(Box::new(effect::SetCell(
key.clone(),
CellValue::Text(value.to_string()),
)));
effects.push(effect::mark_dirty());
}
}
/// Direction to advance after committing a cell edit.
#[derive(Debug, Clone, Copy)]
pub enum AdvanceDir {
/// Move down (typewriter-style, wraps to next column at bottom).
Down,
/// Move right (clamps at rightmost column).
Right,
}
/// Commit a cell edit, advance the cursor, and re-enter edit mode.
/// Subsumes the old `CommitCellEdit` (Down) and `CommitAndAdvanceRight` (Right).
#[derive(Debug)]
pub struct CommitAndAdvance {
pub key: CellKey,
pub value: String,
pub advance: AdvanceDir,
pub cursor: CursorState,
}
impl Cmd for CommitAndAdvance {
fn name(&self) -> &'static str {
match self.advance {
AdvanceDir::Down => "commit-cell-edit",
AdvanceDir::Right => "commit-and-advance-right",
}
}
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let mut effects: Vec<Box<dyn Effect>> = Vec::new();
commit_cell_value(&self.key, &self.value, &mut effects);
match self.advance {
AdvanceDir::Down => {
let adv = EnterAdvance {
cursor: self.cursor.clone(),
};
effects.extend(adv.execute(ctx));
}
AdvanceDir::Right => {
let col_max = self.cursor.col_count.saturating_sub(1);
let nc = (self.cursor.col + 1).min(col_max);
effects.extend(viewport_effects(
self.cursor.row,
nc,
self.cursor.row_offset,
self.cursor.col_offset,
self.cursor.visible_rows,
self.cursor.visible_cols,
));
}
}
effects.push(Box::new(effect::EnterEditAtCursor));
effects
}
}
/// Commit a formula from the formula edit buffer.
#[derive(Debug)]
pub struct CommitFormula;
impl Cmd for CommitFormula {
fn name(&self) -> &'static str {
"commit-formula"
}
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let buf = ctx.buffers.get("formula").cloned().unwrap_or_default();
let first_cat = ctx
.model
.regular_category_names()
.into_iter()
.next()
.map(String::from);
let mut effects: Vec<Box<dyn Effect>> = Vec::new();
if let Some(cat) = first_cat {
effects.push(Box::new(effect::AddFormula {
raw: buf,
target_category: cat,
}));
effects.push(effect::mark_dirty());
effects.push(effect::set_status("Formula added"));
} else {
effects.push(effect::set_status("Add at least one category first."));
}
effects.push(effect::change_mode(AppMode::FormulaPanel));
effects
}
}
/// Shared helper: read a buffer, trim it, and if non-empty, produce add + dirty
/// + status + clear-buffer effects. If empty, return to CategoryPanel.
fn commit_add_from_buffer(
ctx: &CmdContext,
buffer_name: &str,
add_effect: impl FnOnce(&str) -> Option<Box<dyn Effect>>,
status_msg: impl FnOnce(&str) -> String,
) -> Vec<Box<dyn Effect>> {
let buf = ctx.buffers.get(buffer_name).cloned().unwrap_or_default();
let trimmed = buf.trim().to_string();
if trimmed.is_empty() {
return vec![effect::change_mode(AppMode::CategoryPanel)];
}
let Some(add) = add_effect(&trimmed) else {
return vec![];
};
vec![
add,
effect::mark_dirty(),
effect::set_status(status_msg(&trimmed)),
Box::new(effect::SetBuffer {
name: buffer_name.to_string(),
value: String::new(),
}),
]
}
/// Commit adding a category, staying in CategoryAdd mode for the next entry.
#[derive(Debug)]
pub struct CommitCategoryAdd;
impl Cmd for CommitCategoryAdd {
fn name(&self) -> &'static str {
"commit-category-add"
}
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
commit_add_from_buffer(
ctx,
"category",
|name| Some(Box::new(effect::AddCategory(name.to_string()))),
|name| format!("Added category \"{name}\""),
)
}
}
/// Commit adding an item, staying in ItemAdd mode for the next entry.
#[derive(Debug)]
pub struct CommitItemAdd;
impl Cmd for CommitItemAdd {
fn name(&self) -> &'static str {
"commit-item-add"
}
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let category = if let AppMode::ItemAdd { category, .. } = ctx.mode {
category.clone()
} else {
return vec![];
};
commit_add_from_buffer(
ctx,
"item",
|name| {
Some(Box::new(effect::AddItem {
category: category.clone(),
item: name.to_string(),
}))
},
|name| format!("Added \"{name}\""),
)
}
}
/// Commit an export from the export buffer.
#[derive(Debug)]
pub struct CommitExport;
impl Cmd for CommitExport {
fn name(&self) -> &'static str {
"commit-export"
}
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let buf = ctx.buffers.get("export").cloned().unwrap_or_default();
vec![
Box::new(effect::ExportCsv(std::path::PathBuf::from(buf))),
effect::change_mode(AppMode::Normal),
]
}
}