wip: properly trimmed text, add some libs

This commit is contained in:
🪞👃🪞 2025-03-16 21:13:24 +02:00
parent fc24c80521
commit f0dc66a3e8
6 changed files with 569 additions and 91 deletions

77
src/view/column.rs Normal file
View file

@ -0,0 +1,77 @@
use crate::*;
pub struct Column<T> {
pub title: Arc<str>,
pub width: usize,
pub value: Box<dyn Fn(&T)->Option<Arc<str>> + Send + Sync>,
}
impl<T> Column<T> {
pub fn new (
title: &impl AsRef<str>,
width: usize,
value: impl Fn(&T)->Option<Arc<str>> + Send + Sync + 'static
) -> Self {
Self { width, value: Box::new(value), title: title.as_ref().into() }
}
}
pub struct Columns<T>(pub Vec<Column<T>>);
impl Default for Columns<Entry> {
fn default () -> Self {
Self(vec![
Column::new(&"HASH", 16, |entry: &Entry|entry.hash()),
Column::new(&"FILE", 80, |entry: &Entry|entry.name()),
Column::new(&"ARTIST", 30, |entry: &Entry|entry.artist()),
Column::new(&"RELEASE", 30, |entry: &Entry|entry.album()),
Column::new(&"TRACK", 5, |entry: &Entry|entry.track()),
Column::new(&"TITLE", 80, |entry: &Entry|entry.title()),
])
}
}
impl<T> Columns<T> {
pub fn header (&self) -> Arc<str> {
let mut output = String::new();
for Column { width, title, .. } in self.0.iter() {
let cell = title.pad_to_width(*width);
output = format!("{output}{cell}");
}
output.into()
}
pub fn row (&self, entry: &T) -> Arc<str> {
let mut output = String::new();
for Column { width, value, .. } in self.0.iter() {
let cell = value(entry).unwrap_or_default().pad_to_width(*width);
output = format!("{output}{cell}");
}
output.into()
}
pub fn row_content (&self, entry: &T) -> impl Content<TuiOut> where T: Send + Sync {
Map::new(
||self.0.iter(),
|Column { width, value, .. }, index|map_east(
0u16,
*width as u16,
value(entry).map(|x|TrimString(*width as u16, x))
)
)
}
pub fn xw (&self, column: usize) -> (u16, u16) {
let mut x: u16 = 0;
for (index, Column { width, .. }) in self.0.iter().enumerate() {
let w = *width as u16 + 1;
if index == column {
if x > 0 {
return (x - 1, w + 1)
} else {
return (x, w)
}
} else {
x += w;
}
}
(0, 0)
}
}

49
src/view/string.rs Normal file
View file

@ -0,0 +1,49 @@
use crate::*;
use crate::ratatui::prelude::Position;
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
/// Displays an owned [str]-like with fixed maximum width.
///
/// Width is computed using [unicode_width].
pub struct TrimString<T: AsRef<str>>(pub u16, pub T);
impl<'a, T: AsRef<str>> TrimString<T> {
fn as_ref (&self) -> TrimStringRef<'_, T> {
TrimStringRef(self.0, &self.1)
}
}
impl<'a, T: AsRef<str>> Content<TuiOut> for TrimString<T> {
fn layout (&self, to: [u16; 4]) -> [u16;4] {
Content::layout(&self.as_ref(), to)
}
fn render (&self, to: &mut TuiOut) {
Content::render(&self.as_ref(), to)
}
}
/// Displays a borrowed [str]-like with fixed maximum width
///
/// Width is computed using [unicode_width].
pub struct TrimStringRef<'a, T: AsRef<str>>(pub u16, pub &'a T);
impl<T: AsRef<str>> Content<TuiOut> for TrimStringRef<'_, T> {
fn layout (&self, to: [u16; 4]) -> [u16;4] {
[to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h()]
}
fn render (&self, target: &mut TuiOut) {
let area = target.area();
let mut width: u16 = 0;
let mut chars = self.1.as_ref().chars();
while let Some(c) = chars.next() {
width += c.width().unwrap_or(0) as u16;
if width > self.0 || width > area.w() {
break
}
if let Some(cell) = target.buffer.cell_mut(Position {
x: area.x() + width,
y: area.y()
}) {
cell.set_char(c);
}
}
}
}