mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-06 19:45:23 +00:00
feat(channels): new channel for directories and associated transitions (#130)
This commit is contained in:
parent
c0c790cb48
commit
18c5213e83
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -2985,7 +2985,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television"
|
name = "television"
|
||||||
version = "0.7.0"
|
version = "0.7.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"better-panic",
|
"better-panic",
|
||||||
"clap",
|
"clap",
|
||||||
@ -3018,7 +3018,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television-channels"
|
name = "television-channels"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
@ -3039,7 +3039,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television-derive"
|
name = "television-derive"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -3048,7 +3048,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television-fuzzy"
|
name = "television-fuzzy"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nucleo",
|
"nucleo",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
@ -3056,7 +3056,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television-previewers"
|
name = "television-previewers"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"devicons",
|
"devicons",
|
||||||
@ -3077,7 +3077,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television-screen"
|
name = "television-screen"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"ratatui",
|
"ratatui",
|
||||||
@ -3090,7 +3090,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "television-utils"
|
name = "television-utils"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bat",
|
"bat",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
|
14
Cargo.toml
14
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television"
|
name = "television"
|
||||||
version = "0.7.0"
|
version = "0.7.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "The revolution will be televised."
|
description = "The revolution will be televised."
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@ -56,12 +56,12 @@ name = "tv"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
# workspace dependencies
|
# workspace dependencies
|
||||||
|
|
||||||
television-fuzzy = { path = "crates/television-fuzzy", version = "0.0.11" }
|
television-fuzzy = { path = "crates/television-fuzzy", version = "0.0.12" }
|
||||||
television-derive = { path = "crates/television-derive", version = "0.0.11" }
|
television-derive = { path = "crates/television-derive", version = "0.0.12" }
|
||||||
television-screen = { path = "crates/television-screen", version = "0.0.11" }
|
television-screen = { path = "crates/television-screen", version = "0.0.12" }
|
||||||
television-channels = { path = "crates/television-channels", version = "0.0.11" }
|
television-channels = { path = "crates/television-channels", version = "0.0.12" }
|
||||||
television-previewers = { path = "crates/television-previewers", version = "0.0.11" }
|
television-previewers = { path = "crates/television-previewers", version = "0.0.12" }
|
||||||
television-utils = { path = "crates/television-utils", version = "0.0.11" }
|
television-utils = { path = "crates/television-utils", version = "0.0.12" }
|
||||||
|
|
||||||
# external dependencies
|
# external dependencies
|
||||||
better-panic = "0.3.0"
|
better-panic = "0.3.0"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television-channels"
|
name = "television-channels"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
description.workspace = true
|
description.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
@ -13,9 +13,9 @@ edition.workspace = true
|
|||||||
rust-version.workspace = true
|
rust-version.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
television-fuzzy = { path = "../television-fuzzy", version = "0.0.11" }
|
television-fuzzy = { path = "../television-fuzzy", version = "0.0.12" }
|
||||||
television-utils = { path = "../television-utils", version = "0.0.11" }
|
television-utils = { path = "../television-utils", version = "0.0.12" }
|
||||||
television-derive = { path = "../television-derive", version = "0.0.11" }
|
television-derive = { path = "../television-derive", version = "0.0.12" }
|
||||||
devicons = "0.6.11"
|
devicons = "0.6.11"
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
eyre = "0.6.12"
|
eyre = "0.6.12"
|
||||||
|
@ -4,6 +4,7 @@ use television_derive::{Broadcast, ToCliChannel, ToUnitChannel};
|
|||||||
|
|
||||||
mod alias;
|
mod alias;
|
||||||
mod cable;
|
mod cable;
|
||||||
|
mod dirs;
|
||||||
mod env;
|
mod env;
|
||||||
mod files;
|
mod files;
|
||||||
mod git_repos;
|
mod git_repos;
|
||||||
@ -120,6 +121,10 @@ pub enum TelevisionChannel {
|
|||||||
///
|
///
|
||||||
/// This channel allows to search through git repositories.
|
/// This channel allows to search through git repositories.
|
||||||
GitRepos(git_repos::Channel),
|
GitRepos(git_repos::Channel),
|
||||||
|
/// The dirs channel.
|
||||||
|
///
|
||||||
|
/// This channel allows to search through directories.
|
||||||
|
Dirs(dirs::Channel),
|
||||||
/// The text channel.
|
/// The text channel.
|
||||||
///
|
///
|
||||||
/// This channel allows to search through the contents of text files.
|
/// This channel allows to search through the contents of text files.
|
||||||
@ -170,6 +175,9 @@ macro_rules! variant_to_module {
|
|||||||
(Text) => {
|
(Text) => {
|
||||||
text::Channel
|
text::Channel
|
||||||
};
|
};
|
||||||
|
(Dirs) => {
|
||||||
|
dirs::Channel
|
||||||
|
};
|
||||||
(GitRepos) => {
|
(GitRepos) => {
|
||||||
git_repos::Channel
|
git_repos::Channel
|
||||||
};
|
};
|
||||||
@ -279,5 +287,6 @@ macro_rules! define_transitions {
|
|||||||
define_transitions! {
|
define_transitions! {
|
||||||
Text => [Files, Text],
|
Text => [Files, Text],
|
||||||
Files => [Files, Text],
|
Files => [Files, Text],
|
||||||
GitRepos => [Files, Text],
|
Dirs => [Files, Text, Dirs],
|
||||||
|
GitRepos => [Files, Text, Dirs],
|
||||||
}
|
}
|
||||||
|
159
crates/television-channels/src/channels/dirs.rs
Normal file
159
crates/television-channels/src/channels/dirs.rs
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
use crate::channels::{OnAir, TelevisionChannel};
|
||||||
|
use crate::entry::{Entry, PreviewCommand, PreviewType};
|
||||||
|
use devicons::FileIcon;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use television_fuzzy::matcher::{config::Config, injector::Injector, Matcher};
|
||||||
|
use television_utils::files::{walk_builder, DEFAULT_NUM_THREADS};
|
||||||
|
|
||||||
|
pub struct Channel {
|
||||||
|
matcher: Matcher<String>,
|
||||||
|
crawl_handle: tokio::task::JoinHandle<()>,
|
||||||
|
// PERF: cache results (to make deleting characters smoother) with
|
||||||
|
// a shallow stack of sub-patterns as keys (e.g. "a", "ab", "abc")
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Channel {
|
||||||
|
pub fn new(paths: Vec<PathBuf>) -> Self {
|
||||||
|
let matcher = Matcher::new(Config::default().match_paths(true));
|
||||||
|
// start loading files in the background
|
||||||
|
let crawl_handle = tokio::spawn(load_dirs(paths, matcher.injector()));
|
||||||
|
Channel {
|
||||||
|
matcher,
|
||||||
|
crawl_handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Channel {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new(vec![std::env::current_dir().unwrap()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&mut TelevisionChannel> for Channel {
|
||||||
|
fn from(value: &mut TelevisionChannel) -> Self {
|
||||||
|
match value {
|
||||||
|
c @ TelevisionChannel::GitRepos(_) => {
|
||||||
|
let entries = c.results(c.result_count(), 0);
|
||||||
|
Self::new(
|
||||||
|
entries
|
||||||
|
.iter()
|
||||||
|
.map(|entry| PathBuf::from(entry.name.clone()))
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
c @ TelevisionChannel::Dirs(_) => {
|
||||||
|
let entries = c.results(c.result_count(), 0);
|
||||||
|
Self::new(
|
||||||
|
entries
|
||||||
|
.iter()
|
||||||
|
.map(|entry| PathBuf::from(&entry.name))
|
||||||
|
.collect::<HashSet<_>>()
|
||||||
|
.into_iter()
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
const PREVIEW_COMMAND: &str = "ls -la --color=always {}";
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
const PREVIEW_COMMAND: &str = "dir /Q {}";
|
||||||
|
|
||||||
|
impl OnAir for Channel {
|
||||||
|
fn find(&mut self, pattern: &str) {
|
||||||
|
self.matcher.find(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn results(&mut self, num_entries: u32, offset: u32) -> Vec<Entry> {
|
||||||
|
self.matcher.tick();
|
||||||
|
self.matcher
|
||||||
|
.results(num_entries, offset)
|
||||||
|
.into_iter()
|
||||||
|
.map(|item| {
|
||||||
|
let path = item.matched_string;
|
||||||
|
Entry::new(
|
||||||
|
path.clone(),
|
||||||
|
PreviewType::Command(PreviewCommand::new(
|
||||||
|
PREVIEW_COMMAND,
|
||||||
|
" ",
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.with_name_match_ranges(item.match_indices)
|
||||||
|
.with_icon(FileIcon::from(&path))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_result(&self, index: u32) -> Option<Entry> {
|
||||||
|
self.matcher.get_result(index).map(|item| {
|
||||||
|
let path = item.matched_string;
|
||||||
|
Entry::new(
|
||||||
|
path.clone(),
|
||||||
|
PreviewType::Command(PreviewCommand::new(
|
||||||
|
PREVIEW_COMMAND,
|
||||||
|
" ",
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.with_icon(FileIcon::from(&path))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn result_count(&self) -> u32 {
|
||||||
|
self.matcher.matched_item_count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn total_count(&self) -> u32 {
|
||||||
|
self.matcher.total_item_count
|
||||||
|
}
|
||||||
|
|
||||||
|
fn running(&self) -> bool {
|
||||||
|
self.matcher.status.running
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shutdown(&self) {
|
||||||
|
self.crawl_handle.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unused_async)]
|
||||||
|
async fn load_dirs(paths: Vec<PathBuf>, injector: Injector<String>) {
|
||||||
|
if paths.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let current_dir = std::env::current_dir().unwrap();
|
||||||
|
let mut builder =
|
||||||
|
walk_builder(&paths[0], *DEFAULT_NUM_THREADS, None, None);
|
||||||
|
paths[1..].iter().for_each(|path| {
|
||||||
|
builder.add(path);
|
||||||
|
});
|
||||||
|
let walker = builder.build_parallel();
|
||||||
|
|
||||||
|
walker.run(|| {
|
||||||
|
let injector = injector.clone();
|
||||||
|
let current_dir = current_dir.clone();
|
||||||
|
Box::new(move |result| {
|
||||||
|
if let Ok(entry) = result {
|
||||||
|
if entry.file_type().unwrap().is_dir() {
|
||||||
|
let dir_path = &entry
|
||||||
|
.path()
|
||||||
|
.strip_prefix(¤t_dir)
|
||||||
|
.unwrap_or(entry.path())
|
||||||
|
.to_string_lossy();
|
||||||
|
if dir_path == "" {
|
||||||
|
return ignore::WalkState::Continue;
|
||||||
|
}
|
||||||
|
let () = injector.push(dir_path.to_string(), |e, cols| {
|
||||||
|
cols[0] = e.clone().into();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ignore::WalkState::Continue
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
@ -63,6 +63,17 @@ impl From<&mut TelevisionChannel> for Channel {
|
|||||||
.collect(),
|
.collect(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
c @ TelevisionChannel::Dirs(_) => {
|
||||||
|
let entries = c.results(c.result_count(), 0);
|
||||||
|
Self::new(
|
||||||
|
entries
|
||||||
|
.iter()
|
||||||
|
.map(|entry| PathBuf::from(&entry.name))
|
||||||
|
.collect::<HashSet<_>>()
|
||||||
|
.into_iter()
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,6 +149,15 @@ impl From<&mut TelevisionChannel> for Channel {
|
|||||||
let entries = c.results(c.result_count(), 0);
|
let entries = c.results(c.result_count(), 0);
|
||||||
Self::from_text_entries(entries)
|
Self::from_text_entries(entries)
|
||||||
}
|
}
|
||||||
|
c @ TelevisionChannel::Dirs(_) => {
|
||||||
|
let entries = c.results(c.result_count(), 0);
|
||||||
|
Self::new(
|
||||||
|
entries
|
||||||
|
.iter()
|
||||||
|
.map(|entry| PathBuf::from(&entry.name))
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television-derive"
|
name = "television-derive"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
description.workspace = true
|
description.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television-fuzzy"
|
name = "television-fuzzy"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
description.workspace = true
|
description.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television-previewers"
|
name = "television-previewers"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
description.workspace = true
|
description.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
@ -14,8 +14,8 @@ rust-version.workspace = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
syntect = "5.2.0"
|
syntect = "5.2.0"
|
||||||
television-channels = { path = "../television-channels", version = "0.0.11" }
|
television-channels = { path = "../television-channels", version = "0.0.12" }
|
||||||
television-utils = { path = "../television-utils", version = "0.0.11" }
|
television-utils = { path = "../television-utils", version = "0.0.12" }
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
parking_lot = "0.12.3"
|
parking_lot = "0.12.3"
|
||||||
tokio = "1.41.1"
|
tokio = "1.41.1"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television-screen"
|
name = "television-screen"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
description.workspace = true
|
description.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
@ -15,8 +15,8 @@ rust-version.workspace = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
ratatui = "0.29.0"
|
ratatui = "0.29.0"
|
||||||
serde = "1.0.215"
|
serde = "1.0.215"
|
||||||
television-utils = { path = "../television-utils", version = "0.0.11" }
|
television-utils = { path = "../television-utils", version = "0.0.12" }
|
||||||
television-channels = { path = "../television-channels", version = "0.0.11" }
|
television-channels = { path = "../television-channels", version = "0.0.12" }
|
||||||
television-previewers = { path = "../television-previewers", version = "0.0.11" }
|
television-previewers = { path = "../television-previewers", version = "0.0.12" }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
syntect = "5.2.0"
|
syntect = "5.2.0"
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "television-utils"
|
name = "television-utils"
|
||||||
version = "0.0.11"
|
version = "0.0.12"
|
||||||
description.workspace = true
|
description.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user