This commit is contained in:
2025-11-10 13:10:26 -07:00
parent bd5c6cc106
commit 9998756b0d
2 changed files with 207 additions and 89 deletions

3
rustfmt.toml Normal file
View File

@@ -0,0 +1,3 @@
use_small_heuristics = "Max"
max_width = 120
struct_lit_single_line = false

View File

@@ -1,14 +1,11 @@
// #![feature(impl_trait_in_bindings)]
mod calc; mod calc;
mod ctx; mod ctx;
use std::io; use std::{fmt::Display, io, path::PathBuf};
use ratatui::{ use ratatui::{
crossterm::event, crossterm::event,
layout::{Constraint, Layout}, layout::{Constraint, Layout},
text::*,
widgets::{Paragraph, Widget}, widgets::{Paragraph, Widget},
*, *,
}; };
@@ -50,17 +47,64 @@ fn main() -> Result<(), std::io::Error> {
return res; return res;
} }
enum Mode {
Insert(Editor),
Chord(Chord),
Normal,
Command(Chord),
}
impl Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mode::Insert(_) => write!(f, "- INSERT -"),
Mode::Chord(_) => write!(f, "- CHORD -"),
Mode::Normal => write!(f, "- NORMAL -"),
Mode::Command(_) => write!(f, "- COMMAND -"),
}
}
}
impl Mode {
fn process_key(app: &mut App, key: char) {
match key {
// <
'h' => app.grid.selected_cell.0 = app.grid.selected_cell.0.saturating_sub(1),
// v
'j' => app.grid.selected_cell.1 = app.grid.selected_cell.1.saturating_add(1),
// ^
'k' => app.grid.selected_cell.1 = app.grid.selected_cell.1.saturating_sub(1),
// >
'l' => app.grid.selected_cell.0 = app.grid.selected_cell.0.saturating_add(1),
// edit cell
'i' | 'a' => {
let (x, y) = app.grid.selected_cell;
let val = app.grid.get_cell_raw(x, y).as_ref().map(|f| f.as_raw_string()).unwrap_or(String::new());
app.mode = Mode::Insert(Editor::new(val, (x, y)));
}
'I' => { /* insert col before */ }
'A' => { /* insert col after */ }
'o' => { /* insert row below */ }
'O' => { /* insert row above */ }
':' => app.mode = Mode::Command(Chord::new(':')),
// loose chars will put you into chord mode
c => app.mode = Mode::Chord(Chord::new(c)),
}
}
}
struct App { struct App {
exit: bool, exit: bool,
grid: Grid, grid: Grid,
/// Buffer for key-chords mode: Mode,
chord_buf: String, file: Option<PathBuf>,
editor: Option<Editor>,
} }
impl Widget for &App { impl Widget for &App {
fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) { fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) {
Paragraph::new("Status").render(area, buf); Paragraph::new(self.mode.to_string()).render(area, buf);
} }
} }
@@ -69,8 +113,8 @@ impl App {
Self { Self {
exit: false, exit: false,
grid: Grid::new(), grid: Grid::new(),
chord_buf: String::new(), mode: Mode::Normal,
editor: None, file: None,
} }
} }
@@ -81,98 +125,134 @@ impl App {
} }
Ok(()) Ok(())
} }
fn draw(&self, frame: &mut Frame) {
fn draw(&self, frame: &mut Frame) {
let layout = Layout::default() let layout = Layout::default()
.direction(layout::Direction::Vertical) .direction(layout::Direction::Vertical)
.constraints([ .constraints([Constraint::Length(1), Constraint::Min(1), Constraint::Length(1)])
Constraint::Length(1),
Constraint::Min(1),
Constraint::Length(1),
])
.split(frame.area()); .split(frame.area());
if let Some(editor) = &self.editor { let bottom_split = Layout::default()
frame.render_widget(editor, layout[0]); .direction(layout::Direction::Horizontal)
} else { .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
frame.render_widget(Paragraph::new("sc_rs"), layout[0]); .split(layout[2]);
match &self.mode {
Mode::Insert(editor) => {
frame.render_widget(editor, layout[0]);
}
Mode::Command(editor) => {
frame.render_widget(editor, bottom_split[0]);
}
Mode::Chord(chord) => frame.render_widget(chord, bottom_split[0]),
Mode::Normal => frame.render_widget(
Paragraph::new({
let (x, y) = self.grid.selected_cell;
let cell = self.grid.get_cell_raw(x, y).as_ref().map(|f| f.as_raw_string()).unwrap_or_default();
cell
}),
layout[0],
),
} }
frame.render_widget(&self.grid, layout[1]); frame.render_widget(&self.grid, layout[1]);
frame.render_widget(self, layout[2]); frame.render_widget(self, bottom_split[1]);
} }
fn handle_events(&mut self) -> io::Result<()> { fn handle_events(&mut self) -> io::Result<()> {
match event::read()? { match &mut self.mode {
event::Event::Key(key_event) => match key_event.code { Mode::Chord(chord) => match event::read()? {
event::KeyCode::Enter => { event::Event::Key(key) => match key.code {
if let Some(editor) = &self.editor { event::KeyCode::Esc => self.mode = Mode::Normal,
let loc= self.grid.selected_cell; event::KeyCode::Char(c) => {
chord.buf.push(c);
let val = editor.buf.trim().to_string(); match chord.as_string()[0..chord.buf.len() - 1].parse::<usize>() {
Ok(num) => match c {
// insert as number if at all possible 'G' => {
if let Ok(val) = val.parse::<f64>() { let sel = self.grid.selected_cell;
self.grid.set_cell_raw(loc, val); self.grid.selected_cell = (sel.0, num);
} else { self.mode = Mode::Normal;
self.grid.set_cell_raw(loc, val); }
}; _ => {
if c.is_alphabetic() {
self.editor = None; self.mode = Mode::Normal;
} for _ in 0..num {
} Mode::process_key(self, c);
event::KeyCode::Backspace => { }
if let Some(editor) = &mut self.editor { }
editor.buf.pop(); }
} },
} _ => {}
event::KeyCode::F(_) => todo!(),
event::KeyCode::Char(c) => {
if let Some(editor) = &mut self.editor {
editor.buf += &c.to_string();
return Ok(());
}
if !self.chord_buf.is_empty() {}
match c {
'q' => self.exit = true,
// <
'h' => self.grid.selected_cell.0 = self.grid.selected_cell.0.saturating_sub(1),
// v
'j' => self.grid.selected_cell.1 = self.grid.selected_cell.1.saturating_add(1),
// ^
'k' => self.grid.selected_cell.1 = self.grid.selected_cell.1.saturating_sub(1),
// >
'l' => self.grid.selected_cell.0 = self.grid.selected_cell.0.saturating_add(1),
// edit cell
'i' | 'a' => {
let (x,y) = self.grid.selected_cell;
let starting_val = if let Some(val) = self.grid.get_cell_raw(x, y) {
val.as_raw_string()
} else {
String::new()
};
self.editor = Some(Editor::from(starting_val))
},
'I' => {/* insert col before */}
'A' => {/* insert col after */}
'o' => {/* insert row below */}
'O' => {/* insert row above */}
':' => {/* enter command mode */}
c => {
// start entering c for words
self.chord_buf += &c.to_string();
} }
} }
_ => {}
},
_ => {}
},
Mode::Insert(editor) => match event::read()? {
event::Event::Key(key) => match key.code {
event::KeyCode::Esc => {
// just cancel the operation
self.mode = Mode::Normal;
}
event::KeyCode::Enter => {
let v = editor.buf.trim().to_string();
if let Ok(v) = v.parse::<f64>() {
self.grid.set_cell_raw(editor.location, v);
} else {
self.grid.set_cell_raw(editor.location, v);
}
self.mode = Mode::Normal;
}
event::KeyCode::Backspace => {
editor.buf.pop();
}
event::KeyCode::Char(c) => {
editor.buf += &c.to_string();
}
_ => {}
},
_ => {}
},
Mode::Normal => match event::read()? {
event::Event::Key(key_event) => match key_event.code {
event::KeyCode::F(_) => todo!(),
event::KeyCode::Char(c) => {
Mode::process_key(self, c);
}
_ => todo!(),
},
_ => todo!(),
},
Mode::Command(editor) => match event::read()? {
event::Event::Key(key) => match key.code {
event::KeyCode::Esc => {
// just cancel the operation
self.mode = Mode::Normal;
}
event::KeyCode::Enter => {
// [':', 'q']
match editor.buf[1] {
'w' => {}
'q' => self.exit = true,
_ => {}
}
self.mode = Mode::Normal;
}
event::KeyCode::Backspace => {
editor.buf.pop();
}
event::KeyCode::Char(c) => {
editor.add_char(c);
}
_ => {}
}, },
_ => {} _ => {}
}, },
_ => {}
event::Event::Paste(_) => todo!(),
event::Event::Resize(_, _) => todo!(),
} }
Ok(()) Ok(())
} }
} }
@@ -180,19 +260,54 @@ impl App {
struct Editor { struct Editor {
buf: String, buf: String,
cursor: usize, cursor: usize,
location: (usize, usize),
} }
impl Editor {
impl From<String> for Editor { fn new(value: String, loc: (usize, usize)) -> Self {
fn from(value: String) -> Self {
Self { Self {
buf: value.to_string(), buf: value.to_string(),
cursor: value.len(), cursor: value.len(),
location: loc,
} }
} }
} }
impl Widget for &Editor { impl Widget for &Editor {
fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) { fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) {
// TODO add visual cursor
Paragraph::new(self.buf.clone()).render(area, buf); Paragraph::new(self.buf.clone()).render(area, buf);
} }
} }
struct Chord {
buf: Vec<char>,
}
impl Chord {
fn new(inital: char) -> Self {
let mut buf = Vec::new();
buf.push(inital);
Self {
buf,
}
}
fn backspace(&mut self) {
self.buf.pop();
}
fn add_char(&mut self, c: char) {
self.buf.push(c)
}
fn as_string(&self) -> String {
self.buf.iter().collect()
}
}
impl Widget for &Chord {
fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) {
Paragraph::new(self.buf.iter().collect::<String>()).render(area, buf);
}
}