clean up rendering function

This commit is contained in:
2025-11-14 09:33:11 -07:00
parent ef4429a38f
commit 9c44db0d92
3 changed files with 58 additions and 42 deletions

View File

@@ -210,27 +210,7 @@ impl App {
Ok(()) Ok(())
} }
fn draw(&self, frame: &mut Frame) { fn file_name_display(&self) -> String {
let layout = Layout::default()
.direction(layout::Direction::Vertical)
.constraints([Constraint::Length(1), Constraint::Min(1)])
.split(frame.area());
let cmd_line = layout[0];
let body = layout[1];
let len = match &self.mode {
Mode::Insert(edit) | Mode::Command(edit) | Mode::Chord(edit) => edit.len(),
Mode::Normal => {
let (x, y) = self.grid.cursor();
let cell = self.grid.get_cell_raw(x, y).as_ref().map(|f| f.to_string().len()).unwrap_or_default();
cell
}
Mode::Visual(_) => 0,
};
// min 20 chars, expand if needed
let len = max(len as u16 + 1, 20);
let file_name_status = { let file_name_status = {
let mut file_name = "[No Name]"; let mut file_name = "[No Name]";
let mut icon = ""; let mut icon = "";
@@ -246,6 +226,23 @@ impl App {
} }
format!("{file_name}{icon}") format!("{file_name}{icon}")
}; };
file_name_status
}
fn draw(&self, frame: &mut Frame) {
let (x, y) = self.grid.cursor();
let current_cell = self.grid.get_cell_raw(x, y);
let len = self.mode.chars_to_display(current_cell);
let file_name_status = self.file_name_display();
// layout
// ======================================================
let layout = Layout::default()
.direction(layout::Direction::Vertical)
.constraints([Constraint::Length(1), Constraint::Min(1)])
.split(frame.area());
let cmd_line = layout[0];
let body = layout[1];
let cmd_line_split = Layout::default() let cmd_line_split = Layout::default()
.direction(layout::Direction::Horizontal) .direction(layout::Direction::Horizontal)
@@ -256,29 +253,14 @@ impl App {
let cmd_line_status = cmd_line_split[1]; let cmd_line_status = cmd_line_split[1];
let cmd_line_right = cmd_line_split[2]; let cmd_line_right = cmd_line_split[2];
let cmd_line_debug = cmd_line_split[3]; let cmd_line_debug = cmd_line_split[3];
// ======================================================
match &self.mode { self.mode.render(frame, cmd_line_left, current_cell);
Mode::Insert(editor) => {
frame.render_widget(editor, cmd_line_left);
}
Mode::Command(editor) => {
frame.render_widget(editor, cmd_line_left);
}
Mode::Chord(chord) => frame.render_widget(chord, cmd_line_left),
Mode::Normal => frame.render_widget(
Paragraph::new({
let (x, y) = self.grid.cursor();
let cell = self.grid.get_cell_raw(x, y).as_ref().map(|f| f.to_string()).unwrap_or_default();
cell
}),
cmd_line_left,
),
Mode::Visual(_) => {}
}
frame.render_widget(self, body); frame.render_widget(self, body);
frame.render_widget(&self.msg, cmd_line_right); frame.render_widget(&self.msg, cmd_line_right);
frame.render_widget(Paragraph::new(file_name_status), cmd_line_status); frame.render_widget(Paragraph::new(file_name_status), cmd_line_status);
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
frame.render_widget( frame.render_widget(
Paragraph::new(format!( Paragraph::new(format!(

View File

@@ -12,7 +12,6 @@ use crate::app::{
cell::{CSV_DELIMITER, CellType}, cell::{CSV_DELIMITER, CellType},
ctx, ctx,
}, },
mode::Mode,
}; };
#[cfg(test)] #[cfg(test)]

View File

@@ -1,4 +1,4 @@
use std::{cmp::min, fmt::Display, path::PathBuf}; use std::{cmp::{max, min}, fmt::Display, path::PathBuf};
use ratatui::{ use ratatui::{
prelude, prelude,
@@ -9,7 +9,7 @@ use ratatui::{
use crate::app::{ use crate::app::{
app::App, app::App,
error_msg::StatusMessage, error_msg::StatusMessage,
logic::calc::LEN, logic::{calc::LEN, cell::CellType},
}; };
pub enum Mode { pub enum Mode {
@@ -298,6 +298,41 @@ impl Mode {
Mode::Command(_chord) => {}, Mode::Command(_chord) => {},
} }
} }
pub fn chars_to_display(&self, cell: &Option<CellType>) -> u16 {
let len = match &self {
Mode::Insert(edit) | Mode::Command(edit) | Mode::Chord(edit) => edit.len(),
Mode::Normal => {
let len = cell.as_ref().map(|f| f.to_string().len()).unwrap_or_default();
len
}
Mode::Visual(_) => 0,
};
// min 20 chars, expand if needed
let len = max(len as u16 + 1, 20);
len
}
pub fn render(&self, f: &mut ratatui::Frame, area: prelude::Rect, cell: &Option<CellType>) {
match &self {
Mode::Insert(editor) => {
f.render_widget(editor, area);
}
Mode::Command(editor) => {
f.render_widget(editor, area);
}
Mode::Chord(chord) => f.render_widget(chord, area),
Mode::Normal => f.render_widget(
Paragraph::new({
let cell = cell.as_ref().map(|f| f.to_string()).unwrap_or_default();
cell
}),
area,
),
Mode::Visual(_) => {}
}
}
} }
pub struct Chord { pub struct Chord {