Skip to content

Commit

Permalink
nearly there
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Sep 22, 2024
1 parent d028163 commit 5948f67
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .config/config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"<ctrl-down>": "GoToPaneDown", // Move to the pane below
"<ctrl-left>": "GoToPaneLeft", // Move to the pane to the left
"<ctrl-right>": "GoToPaneRight", // Move to the pane to the right
"<ctrl-j>": "SelectNextEntry", // Move to the next entry
"<ctrl-k>": "SelectPrevEntry", // Move to the previous entry
"<ctrl-n>": "SelectNextEntry", // Move to the next entry
"<ctrl-p>": "SelectPrevEntry", // Move to the previous entry
},
}
}
4 changes: 2 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use std::sync::{Arc, Mutex};
use color_eyre::Result;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tracing::debug;
use tracing::{debug, info};

use crate::{
action::Action,
Expand Down Expand Up @@ -161,6 +161,7 @@ impl App {
fn convert_event_to_action(&self, event: Event<Key>) -> Action {
match event {
Event::Input(keycode) => {
info!("{:?}", keycode);
// if the current component is the television
// and the mode is input, we want to handle
if self.television.lock().unwrap().is_input_focused() {
Expand Down
1 change: 0 additions & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use tokio::sync::mpsc::UnboundedSender;

use crate::{action::Action, config::Config};

mod display;
mod finders;
pub mod fps;
pub mod home;
Expand Down
17 changes: 0 additions & 17 deletions src/components/display.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/finders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod env;
pub use env::EnvVarFinder;
use rust_devicons::FileIcon;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Entry {
pub name: String,
pub preview: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions src/components/finders/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{cmp::max, collections::HashMap, env::vars, path::Path};

use fuzzy_matcher::skim::SkimMatcherV2;
use rust_devicons::{icon_for_file, File, FileIcon};
use tracing::info;

use crate::components::finders::{Entry, Finder};

Expand Down
28 changes: 27 additions & 1 deletion src/components/pickers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,39 @@ pub trait Picker {
/// Clear all entries.
fn clear(&mut self);
/// Get a preview of the currently selected entry.
fn get_preview(&self, entry: Entry) -> Option<previewers::Preview>;
fn get_preview(&mut self, entry: &Entry) -> previewers::Preview;
}

pub enum TvChannel {
Env(env::EnvVarPicker),
}

impl TvChannel {
pub fn load_entries(&mut self, pattern: &str) -> Result<()> {
match self {
TvChannel::Env(picker) => picker.load_entries(pattern),
}
}

pub fn entries(&self) -> &Vec<Entry> {
match self {
TvChannel::Env(picker) => picker.entries(),
}
}

pub fn clear(&mut self) {
match self {
TvChannel::Env(picker) => picker.clear(),
}
}

pub fn get_preview(&mut self, entry: &Entry) -> previewers::Preview {
match self {
TvChannel::Env(picker) => picker.get_preview(entry),
}
}
}

pub fn get_tv_channel(channel: UnitTvChannel) -> TvChannel {
match channel {
UnitTvChannel::ENV => TvChannel::Env(env::EnvVarPicker::new()),
Expand Down
4 changes: 2 additions & 2 deletions src/components/pickers/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl super::Picker for EnvVarPicker {
self.entries.clear();
}

fn get_preview(&self, entry: finders::Entry) -> Option<previewers::Preview> {
Some(self.previewer.preview(entry))
fn get_preview(&mut self, entry: &finders::Entry) -> previewers::Preview {
self.previewer.preview(entry)
}
}
15 changes: 12 additions & 3 deletions src/components/previewers.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
use super::{display::StyledString, finders::Entry};
use super::finders::Entry;

mod env;

// previewer types
pub use env::EnvVarPreviewer;

#[derive(Debug, Clone)]
pub enum PreviewContent {
PlainText(String),
HighlightedText(String),
Empty,
}

/// A preview of an entry.
///
/// # Fields
/// - `title`: The title of the preview.
/// - `content`: The content of the preview.
#[derive(Debug, Clone)]
pub struct Preview {
pub title: String,
pub content: Vec<StyledString>,
// maybe images too and other formats
pub content: PreviewContent,
}

/// A trait for a previewer that can preview entries.
///
/// # Methods
/// - `preview`: Preview an entry and produce a preview.
pub trait Previewer {
fn preview(&self, e: Entry) -> Preview;
fn preview(&mut self, e: &Entry) -> Preview;
}
40 changes: 28 additions & 12 deletions src/components/previewers/env.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
use crate::components::display;
use std::collections::HashMap;

use crate::components::finders;
use crate::components::previewers;
use crate::components::previewers::{Preview, PreviewContent, Previewer};

pub struct EnvVarPreviewer {}
pub struct EnvVarPreviewer {
cache: HashMap<finders::Entry, Preview>,
}

impl EnvVarPreviewer {
pub fn new() -> Self {
EnvVarPreviewer {}
EnvVarPreviewer {
cache: HashMap::new(),
}
}
}

impl previewers::Previewer for EnvVarPreviewer {
fn preview(&self, e: finders::Entry) -> previewers::Preview {
previewers::Preview {
title: e.name.clone(),
content: vec![display::StyledString {
text: e.preview.unwrap().clone(),
style: display::StyleGroup::Entry,
}],
impl Previewer for EnvVarPreviewer {
fn preview(&mut self, entry: &finders::Entry) -> Preview {
// check if we have that preview in the cache
if let Some(preview) = self.cache.get(entry) {
return preview.clone();
}
let preview = Preview {
title: entry.name.clone(),
content: if let Some(preview) = &entry.preview {
PreviewContent::PlainText(add_newline_after_colon(&preview))
} else {
PreviewContent::Empty
},
};
self.cache.insert(entry.clone(), preview.clone());
preview
}
}

fn add_newline_after_colon(s: &str) -> String {
s.replace(":", ":\n")
}
Loading

0 comments on commit 5948f67

Please sign in to comment.