now compiles again; but at what cost

not sure if saving cursor inside the state is a good approach, im not
sure why i decided for such refactoring when i touched the project the
last time
This commit is contained in:
YK 2024-07-17 09:10:29 +03:00
parent 007398c576
commit 0631db171f
2 changed files with 101 additions and 92 deletions

View File

@ -13,15 +13,15 @@ mod td;
mod filter; mod filter;
mod main; mod main;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Default, Copy, PartialEq, Eq)]
pub struct Cursor { pub struct Cursor {
row: usize, row: usize,
col: usize, col: usize,
} }
impl Cursor { // impl Cursor {
fn // fn
} // }
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct App { pub struct App {
@ -39,10 +39,11 @@ pub enum Screen {
impl Default for Screen { impl Default for Screen {
fn default () -> Self { fn default () -> Self {
Self::Main(MainScreen::default()) Self::Main { inner: MainScreen::default(), cursor: Default::default() }
} }
} }
#[derive(Debug, Default, PartialEq, Eq, FromRepr, EnumCount, EnumIter, Clone, Copy, Display)] #[derive(Debug, Default, PartialEq, Eq, FromRepr, EnumCount, EnumIter, Clone, Copy, Display)]
pub enum MainScreen { pub enum MainScreen {
#[default] #[default]
@ -55,6 +56,14 @@ pub enum MainScreen {
} }
impl MainScreen {
pub fn with_default_cursor (self) -> Screen {
Screen::Main {
inner: self,
cursor: Default::default(),
}
}
}
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -99,46 +108,45 @@ impl App {
fn handle_key_event (&mut self, k: KeyEvent) -> Result<bool> { fn handle_key_event (&mut self, k: KeyEvent) -> Result<bool> {
match &self.screen { match &mut self.screen {
Screen::Main (screen) => { Screen::Main { inner, cursor } => {
match self.mode { match self.mode {
Mode::Normal => { Mode::Normal => {
match k.code { match k.code {
KeyCode::Char('q') => self.screen = Screen::Exiting, KeyCode::Char('q') => self.screen = Screen::Exiting,
KeyCode::Tab => { KeyCode::Tab => {
let next = *screen as usize + 1; let next = *inner as usize + 1;
self.cursor = (0, 0); // self.cursor = (0, 0);
self.selection.clear(); self.selection.clear();
self.screen = Screen::Main(MainScreen::from_repr(next % MainScreen::COUNT).unwrap_or_default()); self.screen = MainScreen::from_repr(next % MainScreen::COUNT).unwrap_or_default().with_default_cursor();
}, },
KeyCode::BackTab => { KeyCode::BackTab => {
let prev = (*screen as usize).checked_sub(1).unwrap_or(MainScreen::COUNT - 1); let prev = (*inner as usize).checked_sub(1).unwrap_or(MainScreen::COUNT - 1);
self.cursor = (0, 0);
self.selection.clear(); self.selection.clear();
self.screen = Screen::Main(MainScreen::from_repr(prev).unwrap_or_default()); self.screen = MainScreen::from_repr(prev % MainScreen::COUNT).unwrap_or_default().with_default_cursor();
}, },
KeyCode::Up => { KeyCode::Up => {
if self.cursor.0 > 0 { if cursor.row > 0 {
self.cursor.0 -= 1; cursor.row -= 1;
} }
}, },
KeyCode::Left => { KeyCode::Left => {
if self.cursor.1 > 0 { if cursor.col > 0 {
self.cursor.1 -= 1; cursor.col -= 1;
} }
}, },
KeyCode::Down => { KeyCode::Down => {
self.cursor.0 += 1; cursor.row += 1;
}, },
KeyCode::Right => { KeyCode::Right => {
self.cursor.1 += 1; cursor.col += 1;
}, },
KeyCode::Char(' ') => { KeyCode::Char(' ') => {
if self.selection.contains(&self.cursor.0) { if self.selection.contains(&cursor.row) {
self.selection.remove(&self.cursor.0); self.selection.remove(&cursor.row);
} else { } else {
self.selection.insert(self.cursor.0); self.selection.insert(cursor.row);
} }
} }
@ -153,7 +161,7 @@ impl App {
Screen::Exiting => { Screen::Exiting => {
match k.code { match k.code {
KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('q') => return Ok(true), KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('q') => return Ok(true),
KeyCode::Esc | KeyCode::Char('n') => self.screen = Screen::Main(MainScreen::default()), KeyCode::Esc | KeyCode::Char('n') => self.screen = Screen::default(),
_ => () _ => ()
} }
} }
@ -174,7 +182,7 @@ impl Widget for &App {
impl Screen { impl Screen {
fn render (&self, app: &App, area: Rect, buf: &mut Buffer) -> () { fn render (&self, app: &App, area: Rect, buf: &mut Buffer) -> () {
match self { match self {
Self::Main(ms) => main::screen(app, *ms, area, buf), Self::Main { inner, cursor } => main::screen(app, *inner, *cursor, area, buf),
_ => () _ => ()
} }
} }

View File

@ -1,4 +1,4 @@
use crate::prelude::*; use crate::{app::{Cursor, Screen}, prelude::*};
use super::MainScreen; use super::MainScreen;
use ratatui::{ use ratatui::{
@ -12,7 +12,7 @@ use utils::truncate_with_ellipsis as tc;
use itertools::Itertools; use itertools::Itertools;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) { pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut Buffer) {
let layout = Layout::default() let layout = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints(vec![ .constraints(vec![
@ -36,7 +36,6 @@ pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) {
).centered()).block(Block::default().borders(Borders::BOTTOM).padding(Padding::top(1))).render(layout[0], buf); ).centered()).block(Block::default().borders(Borders::BOTTOM).padding(Padding::top(1))).render(layout[0], buf);
let mut list_items = Vec::<ListItem>::new(); let mut list_items = Vec::<ListItem>::new();
let w = area.width; let w = area.width;
macro_rules! wx { macro_rules! wx {
@ -54,7 +53,9 @@ pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) {
(wx!(10), "Kind", super::td::kind) (wx!(10), "Kind", super::td::kind)
]; ];
let th = |col: usize| if col == app.cursor.1 % cw.len() { Style::default().bold().bg(Color::from_u32(0x253325)) } else { Style::default() };
let th = |col: usize| if col == cursor.col % cw.len() { Style::default().bold().bg(Color::from_u32(0x253325)) } else { Style::default() };
let delimiter = || Span::styled("|", Style::default()); let delimiter = || Span::styled("|", Style::default());
@ -64,8 +65,8 @@ pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) {
let item_count = iterator().count(); let item_count = iterator().count();
let td = |row: usize, col: usize| match (row, col) { let td = |row: usize, col: usize| match (row, col) {
(r, c) if r == app.cursor.0 % item_count && c == app.cursor.1 % cw.len() => Style::default().bg(Color::from_u32(0x007700)), (r, c) if r == cursor.row % item_count && c == cursor.col % cw.len() => Style::default().bg(Color::from_u32(0x007700)),
(r, _) if r == app.cursor.0 % item_count => Style::default().bg(Color::from_u32(0x005500)), (r, _) if r == cursor.row % item_count => Style::default().bg(Color::from_u32(0x005500)),
// (_, c) if c == app.cursor.1 % cw.len() => Style::default().bg(Color::from_u32(0x001500)), // (_, c) if c == app.cursor.1 % cw.len() => Style::default().bg(Color::from_u32(0x001500)),
_ => Style::default() _ => Style::default()
}; };
@ -80,7 +81,7 @@ pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) {
let td = tr(idx); let td = tr(idx);
let bg = if app.selection.contains(&idx) { let bg = if app.selection.contains(&idx) {
Color::from_u32(0x000044) Color::from_u32(0x000044)
} else if idx == app.cursor.0 % item_count { } else if idx == cursor.row % item_count {
Color::from_u32(0x002200) Color::from_u32(0x002200)
} else { } else {
Color::default() Color::default()