more highlights, prevent overscroll

This commit is contained in:
🪞👃🪞 2025-03-03 18:39:21 +02:00
parent 2b855f43d7
commit b3a52c171b
3 changed files with 61 additions and 31 deletions

View file

@ -1,8 +1,12 @@
use crate::*; use crate::*;
const PAGE_SIZE: usize = 10;
impl Handle<TuiIn> for Taggart { impl Handle<TuiIn> for Taggart {
fn handle (&mut self, input: &TuiIn) -> Perhaps<bool> { fn handle (&mut self, input: &TuiIn) -> Perhaps<bool> {
Ok(match &*input.event() { let x_min = self.offset;
let x_max = self.offset + self.size.h().saturating_sub(1);
match &*input.event() {
Event::Key(KeyEvent { Event::Key(KeyEvent {
code: KeyCode::Up, code: KeyCode::Up,
kind: KeyEventKind::Press, kind: KeyEventKind::Press,
@ -10,7 +14,6 @@ impl Handle<TuiIn> for Taggart {
state: KeyEventState::NONE state: KeyEventState::NONE
}) => { }) => {
self.cursor = self.cursor.saturating_sub(1); self.cursor = self.cursor.saturating_sub(1);
None
}, },
Event::Key(KeyEvent { Event::Key(KeyEvent {
code: KeyCode::Down, code: KeyCode::Down,
@ -19,7 +22,6 @@ impl Handle<TuiIn> for Taggart {
state: KeyEventState::NONE state: KeyEventState::NONE
}) => { }) => {
self.cursor = self.cursor + 1; self.cursor = self.cursor + 1;
None
}, },
Event::Key(KeyEvent { Event::Key(KeyEvent {
code: KeyCode::PageUp, code: KeyCode::PageUp,
@ -27,8 +29,7 @@ impl Handle<TuiIn> for Taggart {
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE state: KeyEventState::NONE
}) => { }) => {
self.offset = self.offset.saturating_sub(5); self.cursor = self.cursor.saturating_sub(PAGE_SIZE);
None
}, },
Event::Key(KeyEvent { Event::Key(KeyEvent {
code: KeyCode::PageDown, code: KeyCode::PageDown,
@ -36,11 +37,20 @@ impl Handle<TuiIn> for Taggart {
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE state: KeyEventState::NONE
}) => { }) => {
self.offset += 5; self.cursor += PAGE_SIZE;
None
}, },
_ => None _ => {}
}) }
if self.cursor < x_min {
self.offset = self.cursor;
}
if self.cursor > x_max {
self.offset += self.cursor - x_max;
}
if self.cursor >= self.paths.len() {
self.cursor = self.paths.len().saturating_sub(1)
}
Ok(None)
} }
} }

View file

@ -25,6 +25,8 @@ struct Taggart {
paths: Vec<Entry>, paths: Vec<Entry>,
cursor: usize, cursor: usize,
offset: usize, offset: usize,
column: usize,
size: Measure<TuiOut>,
} }
#[derive(Ord, Eq, PartialEq, PartialOrd, Default)] #[derive(Ord, Eq, PartialEq, PartialOrd, Default)]
struct Entry { struct Entry {
@ -64,6 +66,11 @@ impl Taggart {
} }
let depth = entry.depth(); let depth = entry.depth();
let path = entry.into_path(); let path = entry.into_path();
let (is_dir, is_mus, is_img) = if path.is_dir() {
(true, false, false)
} else {
(false, false, false)
};
paths.push(Entry { paths.push(Entry {
path: path.strip_prefix(&root)?.into(), path: path.strip_prefix(&root)?.into(),
is_dir: path.is_dir(), is_dir: path.is_dir(),
@ -78,6 +85,8 @@ impl Taggart {
paths, paths,
cursor: 0, cursor: 0,
offset: 0, offset: 0,
column: 0,
size: Measure::new(),
}) })
} }
} }

View file

@ -1,25 +1,44 @@
use crate::*; use crate::*;
use tek_tui::ratatui::{style::{Color, Style}, prelude::Stylize};
use pad::PadStr; use pad::PadStr;
fn table_row (label: &str, artist: &str, album: &str, title: &str) -> String {
format!("{label:60} {artist:20} {album:20} {title:20}")
}
impl Content<TuiOut> for Taggart { impl Content<TuiOut> for Taggart {
fn layout (&self, area: [u16;4]) -> [u16;4] { fn content (&self) -> impl Render<TuiOut> {
[area.x(), area.y(), 20, area.h()] let sizer = Fill::xy(&self.size);
let size = format!("{}x{}", self.size.w(), self.size.h());
let size_bar = Fill::x(Align::e(Tui::bold(true, Tui::fg_bg(Color::Rgb(0,0,0), Color::Rgb(255,255,255), size))));
let titlebar = Fill::x(Align::w(Tui::bold(true, Tui::fg_bg(Color::Rgb(0,0,0), Color::Rgb(255,255,255), table_row("FILE", "ARTIST", "ALBUM", "TITLE")))));
let table = Fill::xy(TreeTable(self));
Bsp::n(size_bar, Bsp::s(titlebar, Bsp::b(sizer, table)))
} }
}
struct TreeTable<'a>(&'a Taggart);
impl<'a> Content<TuiOut> for TreeTable<'a> {
fn render (&self, to: &mut TuiOut) { fn render (&self, to: &mut TuiOut) {
let area = to.area(); let area = to.area();
let Taggart { offset, paths, cursor, .. } = self.0;
for (i, y) in area.iter_y().enumerate() { for (i, y) in area.iter_y().enumerate() {
let i_offset = i + self.offset; let i_offset = i + offset;
if let Some(entry) = self.paths.get(i_offset) { let selected = *cursor == i_offset;
if entry.depth > 0 { if let Some(entry) = paths.get(i_offset) {
for (index, fragment) in entry.path.iter().enumerate() { for (index, fragment) in entry.path.iter().enumerate() {
if index == entry.depth - 1 { if index == entry.depth - 1 {
let cursor = if self.cursor == i_offset { ">" } else { " " }; let cursor = if selected { ">" } else { " " };
let icon = if entry.is_dir {"+"} else {" "}; let icon = if entry.is_dir {"+"} else {" "};
let name = fragment.display(); let name = fragment.display();
let indent = "".pad_to_width((entry.depth - 1) * 2); let indent = "".pad_to_width((entry.depth - 1) * 2);
let label = format!("{cursor} {indent}{icon} {name}"); let label = table_row(&format!("{cursor} {indent}{icon} {name}"), "", "", "");
let label = format!("{label:80} ARTIST ALBUM TITLE"); to.blit(&label, area.x(), y, if entry.is_dir { None } else {
to.blit(&label, area.x(), y, None); Some(Style::default().bold())
});
if selected {
to.fill_bg([area.x(), y, area.w(), 1], Color::Rgb(0, 0, 0));
} }
} }
} }
@ -29,11 +48,3 @@ impl Content<TuiOut> for Taggart {
} }
} }
} }
fn depth_of (path: &PathBuf) -> usize {
let mut depth = 0;
for _ in path.iter() {
depth += 1;
}
depth
}