diff --git a/src/app/app.rs b/src/app/app.rs index 8c49576..7f0e752 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -12,7 +12,7 @@ use ratatui::{ use crate::app::{ calc::{Grid, LEN}, error_msg::ErrorMessage, - mode::Mode, + mode::{Chord, Mode}, }; pub struct App { @@ -229,21 +229,21 @@ impl App { self.mode = Mode::Normal; } event::KeyCode::Enter => { - let v = editor.buf.trim().to_string(); + let v = editor.as_string(); if let Ok(v) = v.parse::() { - self.grid.set_cell_raw(editor.location, v); + self.grid.set_cell_raw(self.grid.selected_cell, v); } else { - self.grid.set_cell_raw(editor.location, v); + self.grid.set_cell_raw(self.grid.selected_cell, v); } self.mode = Mode::Normal; } event::KeyCode::Backspace => { - editor.buf.pop(); + editor.backspace(); } event::KeyCode::Char(c) => { - editor.buf += &c.to_string(); + editor.add_char(c); } _ => {} }, @@ -295,3 +295,14 @@ impl App { 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); +} diff --git a/src/app/mode.rs b/src/app/mode.rs index ab35865..7dd4cec 100644 --- a/src/app/mode.rs +++ b/src/app/mode.rs @@ -7,7 +7,7 @@ use ratatui::{ use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage}; pub enum Mode { - Insert(Editor), + Insert(Chord), Chord(Chord), Normal, Command(Chord), @@ -44,15 +44,28 @@ impl Mode { pub fn process_cmd(app: &mut App) { if let Mode::Command(editor) = &mut app.mode { // [':', 'q'] - match editor.as_string().as_bytes()[1] as char { - 'w' => { + let cmd = &editor.as_string()[1..]; + let args = cmd.split_ascii_whitespace().collect::>(); + // we are guaranteed at least 1 arg + if args.is_empty() { + return + } + + match args[0] { + "w" => { if let Some(file) = &app.file { unimplemented!("Figure out how we want to save Grid to a csv or something") } else { + if let Some(arg) = args.get(1) { + unimplemented!("Saving a file") + } 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; } // edit cell - 'i' | 'a' => { + 'i' | 'a' | 'r' => { let (x, y) = app.grid.selected_cell; let val = 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 */ } '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 { buf: Vec, } +impl From for Chord { + fn from(value: String) -> Self { + let b= value.as_bytes().iter().map(|f| *f as char).collect(); + Chord { buf: b } + } +} + impl Chord { pub fn new(inital: char) -> Self { let mut buf = Vec::new();