sheetsui/src/ui/mod.rs

514 lines
17 KiB
Rust
Raw Normal View History

2024-10-29 19:47:50 -04:00
//! Ui rendering logic
2024-11-16 10:51:38 -05:00
use std::{path::PathBuf, process::ExitCode};
2024-10-29 19:47:50 -04:00
2024-11-16 10:51:38 -05:00
use crate::book::Book;
2024-10-29 19:47:50 -04:00
2024-11-16 10:51:38 -05:00
use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
2024-10-29 19:47:50 -04:00
use ratatui::{
buffer::Buffer,
layout::{Constraint, Flex, Layout, Rect},
style::{Modifier, Style},
2024-11-25 22:09:57 -05:00
widgets::{Block, Widget},
2024-10-29 19:47:50 -04:00
};
use tui_prompts::{State, Status, TextPrompt, TextState};
2024-11-03 14:08:46 -05:00
use tui_textarea::{CursorMove, TextArea};
mod cmd;
2024-11-23 21:46:11 -05:00
pub mod render;
#[cfg(test)]
mod test;
use cmd::Cmd;
use render::{viewport::ViewportState, Viewport};
2024-11-22 14:57:08 -05:00
#[derive(Default, Debug, PartialEq, Clone)]
2024-10-30 14:34:45 -04:00
pub enum Modality {
#[default]
Navigate,
CellEdit,
Command,
2024-11-22 14:57:08 -05:00
Dialog,
2024-10-30 14:34:45 -04:00
}
2024-11-22 14:57:08 -05:00
#[derive(Debug)]
pub struct AppState<'ws> {
2024-11-22 14:57:08 -05:00
pub modality_stack: Vec<Modality>,
pub viewport_state: ViewportState,
pub command_state: TextState<'ws>,
dirty: bool,
2024-11-23 21:46:11 -05:00
popup: Vec<String>,
2024-10-30 14:34:45 -04:00
}
2024-11-22 14:57:08 -05:00
impl<'ws> Default for AppState<'ws> {
fn default() -> Self {
AppState {
modality_stack: vec![Modality::default()],
viewport_state: Default::default(),
command_state: Default::default(),
dirty: Default::default(),
popup: Default::default(),
2024-11-22 14:57:08 -05:00
}
}
}
impl<'ws> AppState<'ws> {
pub fn modality(&'ws self) -> &'ws Modality {
self.modality_stack.last().unwrap()
}
pub fn pop_modality(&mut self) {
if self.modality_stack.len() > 1 {
self.modality_stack.pop();
}
}
}
2024-11-16 10:51:38 -05:00
// TODO(jwall): This should probably move to a different module.
/// The Address in a Table.
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub struct Address {
pub row: usize,
pub col: usize,
}
impl Address {
pub fn new(row: usize, col: usize) -> Self {
Self { row, col }
}
}
impl Default for Address {
fn default() -> Self {
Address::new(1, 1)
}
}
/// A workspace defining our UI state.
2024-11-02 21:42:17 -04:00
pub struct Workspace<'ws> {
2024-11-03 14:08:46 -05:00
name: PathBuf,
2024-11-16 10:51:38 -05:00
book: Book,
state: AppState<'ws>,
2024-11-02 21:42:17 -04:00
text_area: TextArea<'ws>,
2024-10-29 19:47:50 -04:00
}
2024-11-02 21:42:17 -04:00
impl<'ws> Workspace<'ws> {
/// Constructs a new Workspace from an `Book` with a path for the name.
2024-11-16 10:51:38 -05:00
pub fn new(book: Book, name: PathBuf) -> Self {
2024-11-02 21:42:17 -04:00
let mut ws = Self {
2024-11-16 10:51:38 -05:00
book,
2024-11-03 14:08:46 -05:00
name,
2024-10-30 14:34:45 -04:00
state: AppState::default(),
2024-11-02 21:42:17 -04:00
text_area: reset_text_area("".to_owned()),
};
ws.handle_movement_change();
ws
2024-10-29 19:47:50 -04:00
}
/// Loads a workspace from a path.
2024-11-16 10:51:38 -05:00
pub fn load(path: &PathBuf, locale: &str, tz: &str) -> Result<Self> {
let book = load_book(path, locale, tz)?;
2024-11-16 10:51:38 -05:00
Ok(Workspace::new(book, path.clone()))
2024-10-30 14:34:45 -04:00
}
/// Loads a new `Book` into a `Workspace` from a path.
pub fn load_into<P: Into<PathBuf>>(&mut self, path: P) -> Result<()> {
let path: PathBuf = path.into();
// FIXME(zaphar): This should be managed better.
let book = load_book(&path, "en", "America/New_York")?;
self.book = book;
self.name = path;
Ok(())
}
/// Move a row down in the current sheet.
2024-10-30 14:34:45 -04:00
pub fn move_down(&mut self) -> Result<()> {
2024-11-16 10:51:38 -05:00
let mut loc = self.book.location.clone();
if loc.row < render::viewport::LAST_ROW {
2024-10-30 14:34:45 -04:00
loc.row += 1;
self.book.move_to(&loc)?;
2024-10-30 14:34:45 -04:00
}
Ok(())
}
2024-11-02 21:42:17 -04:00
/// Move a row up in the current sheet.
2024-10-30 14:34:45 -04:00
pub fn move_up(&mut self) -> Result<()> {
2024-11-16 10:51:38 -05:00
let mut loc = self.book.location.clone();
2024-11-18 18:00:16 -05:00
if loc.row > 1 {
2024-10-30 14:34:45 -04:00
loc.row -= 1;
self.book.move_to(&loc)?;
2024-10-30 14:34:45 -04:00
}
Ok(())
}
2024-11-02 21:42:17 -04:00
/// Move a column to the left in the current sheet.
2024-10-30 14:34:45 -04:00
pub fn move_left(&mut self) -> Result<()> {
2024-11-16 10:51:38 -05:00
let mut loc = self.book.location.clone();
2024-11-18 18:00:16 -05:00
if loc.col > 1 {
2024-10-30 14:34:45 -04:00
loc.col -= 1;
self.book.move_to(&loc)?;
2024-10-30 14:34:45 -04:00
}
Ok(())
}
2024-11-02 21:42:17 -04:00
/// Move a column to the left in the current sheet.
2024-10-30 14:34:45 -04:00
pub fn move_right(&mut self) -> Result<()> {
2024-11-16 10:51:38 -05:00
let mut loc = self.book.location.clone();
if loc.col < render::viewport::LAST_COLUMN {
2024-11-02 21:42:17 -04:00
loc.col += 1;
self.book.move_to(&loc)?;
2024-10-30 14:34:45 -04:00
}
Ok(())
2024-10-29 19:47:50 -04:00
}
2024-10-30 14:34:45 -04:00
/// Handle input in our ui loop.
pub fn handle_input(&mut self, evt: Event) -> Result<Option<ExitCode>> {
if let Event::Key(key) = evt {
2024-11-22 14:57:08 -05:00
let result = match self.state.modality() {
2024-11-03 14:08:46 -05:00
Modality::Navigate => self.handle_navigation_input(key)?,
Modality::CellEdit => self.handle_edit_input(key)?,
Modality::Command => self.handle_command_input(key)?,
2024-11-22 14:57:08 -05:00
Modality::Dialog => self.handle_dialog_input(key)?,
2024-11-03 14:08:46 -05:00
};
return Ok(result);
2024-10-30 14:34:45 -04:00
}
Ok(None)
}
2024-11-22 15:58:28 -05:00
fn render_help_text(&self) -> Vec<String> {
match self.state.modality() {
Modality::Navigate => vec![
"Navigate Mode:".to_string(),
"* e: Enter edit mode for current cell".to_string(),
"* h,j,k,l: vim style navigation".to_string(),
"* CTRl-r: Add a row".to_string(),
"* CTRl-c: Add a column".to_string(),
2024-11-23 21:55:25 -05:00
"* CTRl-l: Grow column width by 1".to_string(),
"* CTRl-h: Shrink column width by 1".to_string(),
2024-11-22 15:58:28 -05:00
"* q exit".to_string(),
"* Ctrl-S Save sheet".to_string(),
],
Modality::CellEdit => vec![
"Edit Mode:".to_string(),
"* ESC: Exit edit mode".to_string(),
"Otherwise edit as normal".to_string(),
],
Modality::Command => vec![
"Command Mode:".to_string(),
"* ESC: Exit command mode".to_string(),
],
2024-11-23 21:46:11 -05:00
_ => vec!["General help".to_string()],
2024-11-22 15:58:28 -05:00
}
2024-11-05 15:35:51 -05:00
}
fn handle_command_input(&mut self, key: event::KeyEvent) -> Result<Option<ExitCode>> {
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Esc | KeyCode::Enter => self.exit_command_mode()?,
_ => {
// NOOP
}
}
}
self.state.command_state.handle_key_event(key);
Ok(None)
}
2024-11-22 14:57:08 -05:00
fn handle_dialog_input(&mut self, key: event::KeyEvent) -> Result<Option<ExitCode>> {
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q') => self.exit_dialog_mode()?,
_ => {
// NOOP
}
}
}
Ok(None)
}
2024-11-03 14:08:46 -05:00
fn handle_edit_input(&mut self, key: event::KeyEvent) -> Result<Option<ExitCode>> {
2024-10-30 14:34:45 -04:00
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Char('?') => {
2024-11-22 15:58:28 -05:00
self.enter_dialog_mode(self.render_help_text());
}
KeyCode::Esc | KeyCode::Enter => self.exit_edit_mode()?,
_ => {
// NOOP
}
2024-10-30 14:34:45 -04:00
}
}
2024-11-03 14:08:46 -05:00
// TODO(zaphar): Some specialized editing keybinds
// * Select All
// * Copy
// * Paste
2024-11-02 21:42:17 -04:00
if self.text_area.input(key) {
self.state.dirty = true;
2024-11-02 21:42:17 -04:00
}
2024-10-30 14:34:45 -04:00
Ok(None)
}
fn handle_command(&mut self, cmd_text: String) -> Result<bool> {
if cmd_text.is_empty() {
return Ok(true);
2024-11-18 18:22:36 -05:00
}
match cmd::parse(&cmd_text) {
Ok(Some(Cmd::Edit(path))) => {
self.load_into(path)?;
Ok(true)
}
Ok(Some(Cmd::Help(_maybe_topic))) => {
2024-11-22 15:58:28 -05:00
self.enter_dialog_mode(vec!["TODO help topic".to_owned()]);
Ok(true)
}
Ok(Some(Cmd::Write(maybe_path))) => {
if let Some(path) = maybe_path {
self.save_to(path)?;
} else {
self.save_file()?;
}
Ok(true)
}
Ok(Some(Cmd::InsertColumns(count))) => {
self.book.insert_columns(self.book.location.col, count)?;
self.book.evaluate();
Ok(true)
2024-11-23 21:46:11 -05:00
}
Ok(Some(Cmd::InsertRow(count))) => {
self.book.insert_rows(self.book.location.row, count)?;
self.book.evaluate();
Ok(true)
2024-11-23 21:46:11 -05:00
}
Ok(Some(Cmd::Quit)) => {
// TODO(zaphar): We probably need to do better than this
std::process::exit(0);
2024-11-23 21:46:11 -05:00
}
2024-11-22 14:57:08 -05:00
Ok(None) => {
2024-11-22 15:58:28 -05:00
self.enter_dialog_mode(vec![format!("Unrecognized commmand {}", cmd_text)]);
2024-11-22 14:57:08 -05:00
Ok(false)
2024-11-23 21:46:11 -05:00
}
2024-11-22 14:57:08 -05:00
Err(msg) => {
2024-11-22 15:58:28 -05:00
self.enter_dialog_mode(vec![msg.to_owned()]);
Ok(false)
}
}
2024-11-18 18:22:36 -05:00
}
2024-11-03 14:08:46 -05:00
fn handle_navigation_input(&mut self, key: event::KeyEvent) -> Result<Option<ExitCode>> {
2024-10-30 14:34:45 -04:00
if key.kind == KeyEventKind::Press {
match key.code {
2024-11-20 21:30:09 -05:00
KeyCode::Char('e') | KeyCode::Char('i') => {
self.enter_edit_mode();
}
KeyCode::Char(':') => {
self.enter_command_mode();
2024-11-02 21:42:17 -04:00
}
2024-11-03 14:08:46 -05:00
KeyCode::Char('s') if key.modifiers == KeyModifiers::CONTROL => {
self.save_file()?;
}
KeyCode::Char('?') => {
self.enter_dialog_mode(self.render_help_text());
}
2024-11-23 21:46:11 -05:00
KeyCode::Char('s')
if key.modifiers == KeyModifiers::HYPER
|| key.modifiers == KeyModifiers::SUPER =>
{
self.save_file()?;
}
KeyCode::Char('l') if key.modifiers == KeyModifiers::CONTROL => {
let Address { row: _, col } = &self.book.location;
self.book
.set_col_size(*col, self.book.get_col_size(*col)? + 1)?;
}
KeyCode::Char('h') if key.modifiers == KeyModifiers::CONTROL => {
let Address { row: _, col } = &self.book.location;
let curr_size = self.book.get_col_size(*col)?;
if curr_size > 1 {
self.book.set_col_size(*col, curr_size - 1)?;
}
}
2024-11-05 16:34:30 -05:00
KeyCode::Char('r') if key.modifiers == KeyModifiers::CONTROL => {
2024-11-16 20:25:35 -05:00
let (row_count, _) = self.book.get_size()?;
self.book.update_entry(
&Address {
row: row_count + 1,
col: 1,
},
"",
)?;
2024-11-16 20:25:35 -05:00
let (row, _) = self.book.get_size()?;
2024-11-16 10:51:38 -05:00
let mut loc = self.book.location.clone();
2024-11-16 20:25:35 -05:00
if loc.row < row as usize {
loc.row = row as usize;
self.book.move_to(&loc)?;
}
self.handle_movement_change();
2024-11-05 16:34:30 -05:00
}
2024-11-18 18:00:16 -05:00
KeyCode::Char('t') if key.modifiers == KeyModifiers::CONTROL => {
2024-11-16 20:25:35 -05:00
let (_, col_count) = self.book.get_size()?;
self.book.update_entry(
&Address {
row: 1,
col: col_count + 1,
},
"",
)?;
2024-11-05 16:34:30 -05:00
}
2024-10-30 14:34:45 -04:00
KeyCode::Char('q') => {
return Ok(Some(ExitCode::SUCCESS));
2024-11-02 21:42:17 -04:00
}
2024-11-23 21:46:11 -05:00
KeyCode::Char('j') | KeyCode::Down if key.modifiers != KeyModifiers::CONTROL => {
2024-10-30 14:34:45 -04:00
self.move_down()?;
2024-11-02 21:42:17 -04:00
self.handle_movement_change();
}
2024-11-23 21:46:11 -05:00
KeyCode::Char('k') | KeyCode::Up if key.modifiers != KeyModifiers::CONTROL => {
2024-10-30 14:34:45 -04:00
self.move_up()?;
2024-11-02 21:42:17 -04:00
self.handle_movement_change();
}
2024-11-23 21:46:11 -05:00
KeyCode::Char('h') | KeyCode::Left if key.modifiers != KeyModifiers::CONTROL => {
2024-10-30 14:34:45 -04:00
self.move_left()?;
2024-11-02 21:42:17 -04:00
self.handle_movement_change();
}
2024-11-23 21:46:11 -05:00
KeyCode::Char('l') | KeyCode::Right if key.modifiers != KeyModifiers::CONTROL => {
2024-10-30 14:34:45 -04:00
self.move_right()?;
2024-11-02 21:42:17 -04:00
self.handle_movement_change();
}
2024-10-30 14:34:45 -04:00
_ => {
// noop
}
}
}
return Ok(None);
}
fn enter_navigation_mode(&mut self) {
2024-11-22 14:57:08 -05:00
self.state.modality_stack.push(Modality::Navigate);
}
fn enter_command_mode(&mut self) {
2024-11-22 14:57:08 -05:00
self.state.modality_stack.push(Modality::Command);
self.state.command_state.truncate();
*self.state.command_state.status_mut() = Status::Pending;
self.state.command_state.focus();
}
2024-11-22 15:58:28 -05:00
fn enter_dialog_mode(&mut self, msg: Vec<String>) {
self.state.popup = msg;
2024-11-22 14:57:08 -05:00
self.state.modality_stack.push(Modality::Dialog);
}
fn enter_edit_mode(&mut self) {
2024-11-22 14:57:08 -05:00
self.state.modality_stack.push(Modality::CellEdit);
self.text_area
.set_cursor_line_style(Style::default().add_modifier(Modifier::UNDERLINED));
self.text_area
.set_cursor_style(Style::default().add_modifier(Modifier::SLOW_BLINK));
self.text_area.move_cursor(CursorMove::Bottom);
self.text_area.move_cursor(CursorMove::End);
}
fn exit_command_mode(&mut self) -> Result<()> {
let cmd = self.state.command_state.value().to_owned();
self.state.command_state.blur();
*self.state.command_state.status_mut() = Status::Done;
2024-11-22 14:57:08 -05:00
self.state.pop_modality();
self.handle_command(cmd)?;
Ok(())
}
2024-11-22 14:57:08 -05:00
fn exit_dialog_mode(&mut self) -> Result<()> {
self.state.pop_modality();
Ok(())
}
2024-11-23 21:46:11 -05:00
fn exit_edit_mode(&mut self) -> Result<()> {
self.text_area.set_cursor_line_style(Style::default());
self.text_area.set_cursor_style(Style::default());
let contents = self.text_area.lines().join("\n");
if self.state.dirty {
self.book.edit_current_cell(contents)?;
self.book.evaluate();
self.state.dirty = false;
}
self.enter_navigation_mode();
Ok(())
}
2024-11-02 21:42:17 -04:00
fn handle_movement_change(&mut self) {
2024-11-16 10:51:38 -05:00
let contents = self
.book
.get_current_cell_contents()
.expect("Unexpected failure getting current cell contents");
2024-11-02 21:42:17 -04:00
self.text_area = reset_text_area(contents);
}
2024-11-03 14:08:46 -05:00
fn save_file(&self) -> Result<()> {
self.book
.save_to_xlsx(&self.name.to_string_lossy().to_string())?;
2024-11-03 14:08:46 -05:00
Ok(())
}
fn save_to<S: Into<String>>(&self, path: S) -> Result<()> {
self.book.save_to_xlsx(path.into().as_str())?;
Ok(())
}
fn get_render_parts(
&mut self,
area: Rect,
) -> Vec<(Rect, Box<dyn Fn(Rect, &mut Buffer, &mut Self)>)> {
use ratatui::widgets::StatefulWidget;
let mut cs = vec![Constraint::Fill(4), Constraint::Fill(30)];
let mut rs: Vec<Box<dyn Fn(Rect, &mut Buffer, &mut Self)>> = vec![
Box::new(|rect: Rect, buf: &mut Buffer, ws: &mut Self| ws.text_area.render(rect, buf)),
Box::new(move |rect: Rect, buf: &mut Buffer, ws: &mut Self| {
let sheet_name = ws.book.get_sheet_name().unwrap_or("Unknown");
let table_block = Block::bordered().title_top(sheet_name);
let viewport = Viewport::new(&ws.book).with_selected(ws.book.location.clone()).block(table_block);
StatefulWidget::render(viewport, rect, buf, &mut ws.state.viewport_state);
}),
];
2024-11-22 14:57:08 -05:00
if self.state.modality() == &Modality::Command {
cs.push(Constraint::Max(1));
rs.push(Box::new(|rect: Rect, buf: &mut Buffer, ws: &mut Self| {
StatefulWidget::render(
TextPrompt::from("Command"),
rect,
buf,
&mut ws.state.command_state,
)
}));
}
let rects: Vec<Rect> = Vec::from(
Layout::vertical(cs)
.vertical_margin(2)
.horizontal_margin(2)
.flex(Flex::Legacy)
.split(area.clone())
.as_ref(),
);
rects
.into_iter()
.zip(rs.into_iter())
.map(|(rect, f)| (rect, f))
.collect()
}
2024-11-02 21:42:17 -04:00
}
fn load_book(path: &PathBuf, locale: &str, tz: &str) -> Result<Book, anyhow::Error> {
let book = if path.exists() {
Book::new_from_xlsx_with_locale(&path.to_string_lossy().to_string(), locale, tz)?
} else {
Book::default()
};
Ok(book)
}
2024-11-02 21:42:17 -04:00
fn reset_text_area<'a>(content: String) -> TextArea<'a> {
let mut text_area = TextArea::from(content.lines());
text_area.set_cursor_line_style(Style::default());
text_area.set_cursor_style(Style::default());
text_area.set_block(Block::bordered());
2024-11-02 21:42:17 -04:00
text_area
2024-10-29 19:47:50 -04:00
}