This commit is contained in:
2025-11-11 10:38:15 -07:00
parent 794c96bff4
commit ca3ddf9f03
4 changed files with 37 additions and 35 deletions

View File

@@ -42,8 +42,6 @@ impl Widget for &App {
} }
} }
let x_max = if area.width / cell_length > len { len - 1 } else { area.width / cell_length }; let x_max = if area.width / cell_length > len { len - 1 } else { area.width / cell_length };
let y_max = if area.height / cell_height > len { len - 1 } else { area.height / cell_height }; let y_max = if area.height / cell_height > len { len - 1 } else { area.height / cell_height };
@@ -91,7 +89,7 @@ impl Widget for &App {
const ORANGE2: Color = Color::Rgb(180, 130, 0); const ORANGE2: Color = Color::Rgb(180, 130, 0);
match (x == 0, y == 0) { match (x == 0, y == 0) {
// 0,0 dead space // 0,0 vi mode
(true, true) => { (true, true) => {
display = self.mode.to_string(); display = self.mode.to_string();
style = self.mode.get_style(); style = self.mode.get_style();

View File

@@ -4,8 +4,7 @@ use evalexpr::*;
use crate::app::ctx; use crate::app::ctx;
// if this is very large at all it will overflow the stack pub const LEN: usize = 100;
pub const LEN: usize = 10;
pub struct Grid { pub struct Grid {
// a b c ... // a b c ...

View File

@@ -1,7 +1,12 @@
use std::{cmp::{max, min}, fmt::Display}; use std::{
cmp::{max, min},
fmt::Display,
};
use ratatui::{ use ratatui::{
prelude, style::{Color, Style}, widgets::{Paragraph, Widget} prelude,
style::{Color, Style},
widgets::{Paragraph, Widget},
}; };
use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage}; use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage};
@@ -27,7 +32,6 @@ impl Display for Mode {
} }
impl Mode { impl Mode {
pub fn get_style(&self) -> Style { pub fn get_style(&self) -> Style {
match self { match self {
// Where you are typing // Where you are typing
@@ -40,7 +44,6 @@ 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']
@@ -48,7 +51,7 @@ impl Mode {
let args = cmd.split_ascii_whitespace().collect::<Vec<&str>>(); let args = cmd.split_ascii_whitespace().collect::<Vec<&str>>();
// we are guaranteed at least 1 arg // we are guaranteed at least 1 arg
if args.is_empty() { if args.is_empty() {
return return;
} }
match args[0] { match args[0] {
@@ -74,10 +77,8 @@ impl Mode {
let value = parts[1]; let value = parts[1];
app.vars.insert(key.to_owned(), value.to_owned()); app.vars.insert(key.to_owned(), value.to_owned());
} }
app.error_msg = ErrorMessage::new("set <key>=<value>") app.error_msg = ErrorMessage::new("set <key>=<value>")
} }
_ => {} _ => {}
} }
@@ -95,7 +96,7 @@ impl Mode {
} }
// v // v
'j' => { 'j' => {
app.grid.selected_cell.1 = min(app.grid.selected_cell.1.saturating_add(1), LEN); app.grid.selected_cell.1 = min(app.grid.selected_cell.1.saturating_add(1), LEN - 1);
return; return;
} }
// ^ // ^
@@ -105,7 +106,7 @@ impl Mode {
} }
// > // >
'l' => { 'l' => {
app.grid.selected_cell.0 = min(app.grid.selected_cell.0.saturating_add(1), LEN); app.grid.selected_cell.0 = min(app.grid.selected_cell.0.saturating_add(1), LEN - 1);
return; return;
} }
'0' => { '0' => {
@@ -116,8 +117,7 @@ impl Mode {
'i' | 'a' | 'r' => { '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(Chord::from(val)); app.mode = Mode::Insert(Chord::from(val));
} }
@@ -128,10 +128,27 @@ impl Mode {
'v' => app.mode = Mode::Visual(app.grid.selected_cell), 'v' => app.mode = Mode::Visual(app.grid.selected_cell),
':' => app.mode = Mode::Command(Chord::new(':')), ':' => app.mode = Mode::Command(Chord::new(':')),
// loose chars will put you into chord mode // loose chars will put you into chord mode
c => app.mode = Mode::Chord(Chord::new(c)), c => {
if let Mode::Normal = app.mode {
app.mode = Mode::Chord(Chord::new(c))
}
}
}
if let Mode::Visual((x1, y1)) = app.mode {
// TODO visual copy, paste, etc
let (x2, y2) = app.grid.selected_cell;
let (low_x, hi_x) = if x1 < x2 { (x1, x2) } else { (x2, x1) };
let (low_y, hi_y) = if y1 < y2 { (y1, y2) } else { (y2, y1) };
if key == 'd' {
for x in low_x..=hi_x {
for y in low_y..=hi_y {
app.grid.set_cell_raw((x, y), String::new());
}
}
app.mode = Mode::Normal
} }
if let Mode::Visual(v) = app.mode {
// TODO visual delete, copy, paste, etc
} }
} }
Mode::Chord(chord) => { Mode::Chord(chord) => {
@@ -185,7 +202,9 @@ pub struct Chord {
impl From<String> for Chord { impl From<String> for Chord {
fn from(value: String) -> Self { fn from(value: String) -> Self {
let b = value.as_bytes().iter().map(|f| *f as char).collect(); let b = value.as_bytes().iter().map(|f| *f as char).collect();
Chord { buf: b } Chord {
buf: b,
}
} }
} }

View File

@@ -5,20 +5,6 @@ use crate::app::{app::App};
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
let term = ratatui::init(); let term = ratatui::init();
let mut app = App::new(); let mut app = App::new();
app.grid.set_cell("A0", "Apples".to_string());
app.grid.set_cell("A1", 10.);
app.grid.set_cell("B0", "Bananas".to_string());
app.grid.set_cell("B1", 10.);
app.grid.set_cell("C0", "Fruit".to_string());
app.grid.set_cell("C1", "=A1+B1".to_string());
app.grid.set_cell("D0", "x2".to_string());
app.grid.set_cell("D1", "=C1*2".to_string());
app.grid.set_cell("A4", "Recursive references don't break anything!".to_string());
app.grid.set_cell("A5", "=B5".to_string());
app.grid.set_cell("B5", "=A5".to_string());
let res = app.run(term); let res = app.run(term);
ratatui::restore(); ratatui::restore();
return res; return res;