remove editor in favor of chord

This commit is contained in:
2025-11-11 09:38:49 -07:00
parent e00e28cea8
commit c09df8eb1f
2 changed files with 43 additions and 35 deletions

View File

@@ -12,7 +12,7 @@ use ratatui::{
use crate::app::{ use crate::app::{
calc::{Grid, LEN}, calc::{Grid, LEN},
error_msg::ErrorMessage, error_msg::ErrorMessage,
mode::Mode, mode::{Chord, Mode},
}; };
pub struct App { pub struct App {
@@ -229,21 +229,21 @@ impl App {
self.mode = Mode::Normal; self.mode = Mode::Normal;
} }
event::KeyCode::Enter => { event::KeyCode::Enter => {
let v = editor.buf.trim().to_string(); let v = editor.as_string();
if let Ok(v) = v.parse::<f64>() { if let Ok(v) = v.parse::<f64>() {
self.grid.set_cell_raw(editor.location, v); self.grid.set_cell_raw(self.grid.selected_cell, v);
} else { } else {
self.grid.set_cell_raw(editor.location, v); self.grid.set_cell_raw(self.grid.selected_cell, v);
} }
self.mode = Mode::Normal; self.mode = Mode::Normal;
} }
event::KeyCode::Backspace => { event::KeyCode::Backspace => {
editor.buf.pop(); editor.backspace();
} }
event::KeyCode::Char(c) => { event::KeyCode::Char(c) => {
editor.buf += &c.to_string(); editor.add_char(c);
} }
_ => {} _ => {}
}, },
@@ -295,3 +295,14 @@ impl App {
Ok(()) Ok(())
} }
} }
#[test]
fn test_quit_cmd() {
let mut app = App::new();
assert!(!app.exit);
app.mode = Mode::Command(Chord::from(":q".to_string()));
Mode::process_cmd(&mut app);
assert!(app.exit);
}

View File

@@ -7,7 +7,7 @@ use ratatui::{
use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage}; use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage};
pub enum Mode { pub enum Mode {
Insert(Editor), Insert(Chord),
Chord(Chord), Chord(Chord),
Normal, Normal,
Command(Chord), Command(Chord),
@@ -44,15 +44,28 @@ impl Mode {
pub fn process_cmd(app: &mut App) { pub fn process_cmd(app: &mut App) {
if let Mode::Command(editor) = &mut app.mode { if let Mode::Command(editor) = &mut app.mode {
// [':', 'q'] // [':', 'q']
match editor.as_string().as_bytes()[1] as char { let cmd = &editor.as_string()[1..];
'w' => { let args = cmd.split_ascii_whitespace().collect::<Vec<&str>>();
// we are guaranteed at least 1 arg
if args.is_empty() {
return
}
match args[0] {
"w" => {
if let Some(file) = &app.file { if let Some(file) = &app.file {
unimplemented!("Figure out how we want to save Grid to a csv or something") unimplemented!("Figure out how we want to save Grid to a csv or something")
} else { } else {
if let Some(arg) = args.get(1) {
unimplemented!("Saving a file")
}
app.error_msg = ErrorMessage::new("No file selected"); app.error_msg = ErrorMessage::new("No file selected");
} }
} }
'q' => app.exit = true, "q" => app.exit = true,
"set" => {
// TODO solve issue #8
}
_ => {} _ => {}
} }
} }
@@ -87,13 +100,13 @@ impl Mode {
return; return;
} }
// edit cell // edit cell
'i' | 'a' => { 'i' | 'a' | 'r' => {
let (x, y) = app.grid.selected_cell; let (x, y) = app.grid.selected_cell;
let val = let val =
app.grid.get_cell_raw(x, y).as_ref().map(|f| f.to_string()).unwrap_or(String::new()); app.grid.get_cell_raw(x, y).as_ref().map(|f| f.to_string()).unwrap_or(String::new());
app.mode = Mode::Insert(Editor::new(val, (x, y))); app.mode = Mode::Insert(Chord::from(val));
} }
'I' => { /* insert col before */ } 'I' => { /* insert col before */ }
'A' => { /* insert col after */ } 'A' => { /* insert col after */ }
@@ -152,33 +165,17 @@ impl Mode {
} }
} }
pub struct Editor {
pub buf: String,
cursor: usize,
pub location: (usize, usize),
}
impl Editor {
fn new(value: String, loc: (usize, usize)) -> Self {
Self {
buf: value.to_string(),
cursor: value.len(),
location: loc,
}
}
}
impl Widget for &Editor {
fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) {
// TODO add visual cursor
Paragraph::new(self.buf.clone()).render(area, buf);
}
}
pub struct Chord { pub struct Chord {
buf: Vec<char>, buf: Vec<char>,
} }
impl From<String> for Chord {
fn from(value: String) -> Self {
let b= value.as_bytes().iter().map(|f| *f as char).collect();
Chord { buf: b }
}
}
impl Chord { impl Chord {
pub fn new(inital: char) -> Self { pub fn new(inital: char) -> Self {
let mut buf = Vec::new(); let mut buf = Vec::new();