open files with space

This commit is contained in:
🪞👃🪞 2025-03-09 04:47:44 +02:00
parent 8cc9418272
commit 41c5686d67
5 changed files with 137 additions and 33 deletions

View file

@ -1,9 +1,10 @@
use crate::*;
use opener::open;
macro_rules! press {
($key:tt) => {
($($key:tt)+) => {
Event::Key(KeyEvent {
code: KeyCode::$key,
code: KeyCode::$($key)+,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE
@ -16,14 +17,15 @@ impl Handle<TuiIn> for Taggart {
let x_min = self.offset;
let x_max = self.offset + self.size.h().saturating_sub(1);
match &*input.event() {
press!(Up) => { self.cursor = self.cursor.saturating_sub(1); },
press!(Down) => { self.cursor = self.cursor + 1; },
press!(PageUp) => { self.cursor = self.cursor.saturating_sub(PAGE_SIZE); },
press!(PageDown) => { self.cursor += PAGE_SIZE; },
press!(Left) => { self.column = self.column.saturating_sub(1); },
press!(Right) => { self.column = self.column + 1; },
press!(Enter) => { self.editing = Some((self.cursor, self.column)); },
press!(Esc) => { self.editing = None; },
press!(Up) => { self.cursor = self.cursor.saturating_sub(1); },
press!(Down) => { self.cursor = self.cursor + 1; },
press!(PageUp) => { self.cursor = self.cursor.saturating_sub(PAGE_SIZE); },
press!(PageDown) => { self.cursor += PAGE_SIZE; },
press!(Left) => { self.column = self.column.saturating_sub(1); },
press!(Right) => { self.column = self.column + 1; },
press!(Enter) => { self.editing = Some((self.cursor, self.column)); },
press!(Esc) => { self.editing = None; },
press!(Char(' ')) => { open(&self.paths[self.cursor].path)?; }
_ => {}
}
if self.cursor < x_min {

View file

@ -2,7 +2,7 @@
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};
use std::env::current_dir;
use std::env::{current_dir, set_current_dir};
use std::fs::read;
use tek_tui::*;
@ -30,34 +30,23 @@ fn cli () -> clap::Command {
}
fn main () -> Usually<()> {
let args = cli().get_matches();
let path = args.get_one::<PathBuf>("path");
let state = Arc::new(RwLock::new(Taggart::new(path)?));
let args = cli().get_matches();
let path = if let Some(path) = args.get_one::<PathBuf>("path") {
path.into()
} else {
current_dir()?
};
set_current_dir(&path);
let state = Arc::new(RwLock::new(Taggart::new(&path)?));
Tui::new()?.run(&state)
}
pub struct Taggart {
pub root: PathBuf,
pub paths: Vec<Entry>,
pub cursor: usize,
pub offset: usize,
pub column: usize,
pub columns: Columns<Entry>,
pub size: Measure<TuiOut>,
pub editing: Option<(usize, usize)>,
}
impl Taggart {
fn new (root: Option<&impl AsRef<Path>>) -> Usually<Self> {
let root = if let Some(root) = root {
root.as_ref().into()
} else {
current_dir()?
};
fn new (root: &impl AsRef<Path>) -> Usually<Self> {
Ok(Self {
paths: Self::collect(&root)?,
root,
root: root.as_ref().into(),
paths: Self::collect(root)?,
cursor: 0,
offset: 0,
column: 0,

View file

@ -3,6 +3,17 @@ use walkdir::DirEntry;
use id3::{Tag, TagLike};
use std::io::Read;
pub struct Taggart {
pub root: PathBuf,
pub paths: Vec<Entry>,
pub cursor: usize,
pub offset: usize,
pub column: usize,
pub columns: Columns<Entry>,
pub size: Measure<TuiOut>,
pub editing: Option<(usize, usize)>,
}
#[derive(Ord, Eq, PartialEq, PartialOrd)]
pub struct Entry {
pub path: PathBuf,