Skip to content

Commit

Permalink
unsupported previews
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Sep 24, 2024
1 parent 4292573 commit 98b5830
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 45 deletions.
118 changes: 106 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ serde_json = "1.0.125"
signal-hook = "0.3.17"
strip-ansi-escapes = "0.2.0"
strum = { version = "0.26.3", features = ["derive"] }
syntect = "5.2.0"
tokio = { version = "1.39.3", features = ["full"] }
tokio-stream = "0.1.16"
tokio-util = "0.7.11"
Expand Down
12 changes: 12 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# tasks
- [ ] preview navigation
- [ ] add a way to open the selected file in the default editor
- [ ] maybe filter out image types etc. for now

## bugs
- [ ] sanitize input (tabs, \0, etc)

## improvements
- [ ] async finder initialization
- [ ] async finder search

## feature ideas
- [x] environment variables
- [ ] aliases
Expand Down
3 changes: 3 additions & 0 deletions src/components/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ impl TvChannel {
}

pub fn get_preview(&mut self, entry: &Entry) -> previewers::Preview {
if entry.name.is_empty() {
return previewers::Preview::default();
}
match self {
TvChannel::Env(picker) => picker.get_preview(entry),
TvChannel::Files(picker) => picker.get_preview(entry),
Expand Down
15 changes: 10 additions & 5 deletions src/components/finders/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{

use fuzzy_matcher::skim::SkimMatcherV2;
use ignore::{types::TypesBuilder, WalkBuilder};
use tracing::info;

use crate::{
components::finders::{Entry, Finder},
Expand All @@ -31,6 +30,8 @@ impl FileFinder {
}
}

const FUZZY_THRESHOLD: i64 = 2;

impl Finder for FileFinder {
fn find(&mut self, pattern: &str) -> impl Iterator<Item = Entry> {
let mut results: Vec<Entry> = Vec::new();
Expand All @@ -39,11 +40,16 @@ impl Finder for FileFinder {
results.extend(entries.iter().cloned());
} else {
for file in &self.files {
let file_name = file.file_name().unwrap().to_string_lossy().to_string();
let rel_path = file.strip_prefix(&self.current_directory).unwrap();
let rel_path_str = rel_path.to_string_lossy();
if !pattern.is_empty() {
if let Some((score, indices)) = self.matcher.fuzzy(&file_name, pattern, true) {
if let Some((score, indices)) = self.matcher.fuzzy(&rel_path_str, pattern, true)
{
if score < FUZZY_THRESHOLD {
continue;
}
results.push(Entry {
name: file_name.clone(),
name: rel_path_str.to_string(),
display_name: None,
preview: None,
score,
Expand All @@ -70,7 +76,6 @@ fn load_files(path: &Path) -> Vec<PathBuf> {
let tx = tx.clone();
Box::new(move |result| {
if let Ok(entry) = result {
info!("found file: {:?}", entry.path());
if entry.file_type().unwrap().is_file() {
tx.send(entry.path().to_path_buf()).unwrap();
}
Expand Down
13 changes: 12 additions & 1 deletion src/components/previewers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ mod files;
// previewer types
pub use env::EnvVarPreviewer;
pub use files::FilePreviewer;
use syntect::highlighting::Style;

#[derive(Debug, Clone)]
pub enum PreviewContent {
PlainText(String),
PlainTextWrapped(String),
HighlightedText(String),
HighlightedText(Vec<Vec<(Style, String)>>),
NotSupported,
Empty,
}

Expand All @@ -27,6 +29,15 @@ pub struct Preview {
pub content: PreviewContent,
}

impl Default for Preview {
fn default() -> Self {
Preview {
title: String::new(),
content: PreviewContent::Empty,
}
}
}

/// A trait for a previewer that can preview entries.
///
/// # Methods
Expand Down
Loading

0 comments on commit 98b5830

Please sign in to comment.