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:
parent
007398c576
commit
0631db171f
56
src/app.rs
56
src/app.rs
@ -13,15 +13,15 @@ mod td;
|
||||
mod filter;
|
||||
mod main;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq)]
|
||||
pub struct Cursor {
|
||||
row: usize,
|
||||
col: usize,
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
fn
|
||||
}
|
||||
// impl Cursor {
|
||||
// fn
|
||||
// }
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct App {
|
||||
@ -39,10 +39,11 @@ pub enum Screen {
|
||||
|
||||
impl Default for Screen {
|
||||
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)]
|
||||
pub enum MainScreen {
|
||||
#[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)]
|
||||
@ -99,46 +108,45 @@ impl App {
|
||||
fn handle_key_event (&mut self, k: KeyEvent) -> Result<bool> {
|
||||
|
||||
|
||||
match &self.screen {
|
||||
Screen::Main (screen) => {
|
||||
match &mut self.screen {
|
||||
Screen::Main { inner, cursor } => {
|
||||
match self.mode {
|
||||
Mode::Normal => {
|
||||
match k.code {
|
||||
KeyCode::Char('q') => self.screen = Screen::Exiting,
|
||||
KeyCode::Tab => {
|
||||
let next = *screen as usize + 1;
|
||||
self.cursor = (0, 0);
|
||||
let next = *inner as usize + 1;
|
||||
// self.cursor = (0, 0);
|
||||
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 => {
|
||||
let prev = (*screen as usize).checked_sub(1).unwrap_or(MainScreen::COUNT - 1);
|
||||
self.cursor = (0, 0);
|
||||
let prev = (*inner as usize).checked_sub(1).unwrap_or(MainScreen::COUNT - 1);
|
||||
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 => {
|
||||
if self.cursor.0 > 0 {
|
||||
self.cursor.0 -= 1;
|
||||
if cursor.row > 0 {
|
||||
cursor.row -= 1;
|
||||
}
|
||||
},
|
||||
KeyCode::Left => {
|
||||
if self.cursor.1 > 0 {
|
||||
self.cursor.1 -= 1;
|
||||
if cursor.col > 0 {
|
||||
cursor.col -= 1;
|
||||
}
|
||||
},
|
||||
KeyCode::Down => {
|
||||
self.cursor.0 += 1;
|
||||
cursor.row += 1;
|
||||
},
|
||||
KeyCode::Right => {
|
||||
self.cursor.1 += 1;
|
||||
cursor.col += 1;
|
||||
},
|
||||
|
||||
KeyCode::Char(' ') => {
|
||||
if self.selection.contains(&self.cursor.0) {
|
||||
self.selection.remove(&self.cursor.0);
|
||||
if self.selection.contains(&cursor.row) {
|
||||
self.selection.remove(&cursor.row);
|
||||
} else {
|
||||
self.selection.insert(self.cursor.0);
|
||||
self.selection.insert(cursor.row);
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,7 +161,7 @@ impl App {
|
||||
Screen::Exiting => {
|
||||
match k.code {
|
||||
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 {
|
||||
fn render (&self, app: &App, area: Rect, buf: &mut Buffer) -> () {
|
||||
match self {
|
||||
Self::Main(ms) => main::screen(app, *ms, area, buf),
|
||||
Self::Main { inner, cursor } => main::screen(app, *inner, *cursor, area, buf),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::prelude::*;
|
||||
use crate::{app::{Cursor, Screen}, prelude::*};
|
||||
use super::MainScreen;
|
||||
|
||||
use ratatui::{
|
||||
@ -12,7 +12,7 @@ use utils::truncate_with_ellipsis as tc;
|
||||
use itertools::Itertools;
|
||||
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()
|
||||
.direction(Direction::Vertical)
|
||||
.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);
|
||||
let mut list_items = Vec::<ListItem>::new();
|
||||
|
||||
|
||||
let w = area.width;
|
||||
|
||||
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)
|
||||
];
|
||||
|
||||
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());
|
||||
|
||||
@ -64,8 +65,8 @@ pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) {
|
||||
let item_count = iterator().count();
|
||||
|
||||
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, _) if r == app.cursor.0 % item_count => Style::default().bg(Color::from_u32(0x005500)),
|
||||
(r, c) if r == cursor.row % item_count && c == cursor.col % cw.len() => Style::default().bg(Color::from_u32(0x007700)),
|
||||
(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)),
|
||||
_ => Style::default()
|
||||
};
|
||||
@ -80,7 +81,7 @@ pub fn screen (app: &App, ms: MainScreen, area: Rect, buf: &mut Buffer) {
|
||||
let td = tr(idx);
|
||||
let bg = if app.selection.contains(&idx) {
|
||||
Color::from_u32(0x000044)
|
||||
} else if idx == app.cursor.0 % item_count {
|
||||
} else if idx == cursor.row % item_count {
|
||||
Color::from_u32(0x002200)
|
||||
} else {
|
||||
Color::default()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user