From ec11d699812bb3c17244e8f58fbdb5a3cc6ece6a Mon Sep 17 00:00:00 2001 From: alexpasmantier Date: Tue, 8 Oct 2024 21:39:30 +0200 Subject: [PATCH] things in progress --- src/app.rs | 16 +++++--- src/components/finders/env.rs | 2 +- src/components/previewers/cache.rs | 1 + src/components/previewers/files.rs | 59 +++++++++++++++--------------- src/components/television.rs | 4 +- src/main.rs | 4 +- 6 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/app.rs b/src/app.rs index bc5f856..ed13fe2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -93,11 +93,12 @@ pub enum Mode { } impl App { - pub async fn new(channel: UnitTvChannel, tick_rate: f64, frame_rate: f64) -> Result { + pub fn new(channel: UnitTvChannel, tick_rate: f64, frame_rate: f64) -> Result { let (action_tx, action_rx) = mpsc::unbounded_channel(); let (render_tx, _) = mpsc::unbounded_channel(); - let event_loop = EventLoop::new(tick_rate, true); - let television = Arc::new(Mutex::new(Television::new(channel).await)); + let (_, event_rx) = mpsc::unbounded_channel(); + let (event_abort_tx, _) = mpsc::unbounded_channel(); + let television = Arc::new(Mutex::new(Television::new(channel))); Ok(Self { tick_rate, @@ -109,14 +110,19 @@ impl App { mode: Mode::Input, action_tx, action_rx, - event_rx: event_loop.rx, - event_abort_tx: event_loop.abort_tx, + event_rx, + event_abort_tx, render_tx, }) } pub async fn run(&mut self) -> Result> { let mut tui = Tui::new()?.frame_rate(self.frame_rate); + info!("Starting event loop"); + let event_loop = EventLoop::new(self.tick_rate, true); + info!("Started event loop"); + self.event_rx = event_loop.rx; + self.event_abort_tx = event_loop.abort_tx; // Rendering loop let (render_tx, render_rx) = mpsc::unbounded_channel(); diff --git a/src/components/finders/env.rs b/src/components/finders/env.rs index c823b94..fe433a1 100644 --- a/src/components/finders/env.rs +++ b/src/components/finders/env.rs @@ -85,7 +85,7 @@ impl Finder for EnvVarFinder { } let mut col_indices = Vec::new(); let mut matcher = MATCHER.lock(); - let icon = self.file_icon.clone(); + let icon = self.file_icon; snapshot .matched_items(offset..(num_entries + offset).min(snapshot.matched_item_count())) diff --git a/src/components/previewers/cache.rs b/src/components/previewers/cache.rs index 11ce937..8a7d575 100644 --- a/src/components/previewers/cache.rs +++ b/src/components/previewers/cache.rs @@ -7,6 +7,7 @@ use tracing::debug; use crate::components::previewers::Preview; +/// TODO: add unit tests /// A ring buffer that also keeps track of the keys it contains to avoid duplicates. /// /// I'm planning on using this as a backend LRU-cache for the preview cache. diff --git a/src/components/previewers/files.rs b/src/components/previewers/files.rs index 3105b88..7caf5b9 100644 --- a/src/components/previewers/files.rs +++ b/src/components/previewers/files.rs @@ -27,42 +27,44 @@ pub struct FilePreviewer { cache: Arc>, syntax_set: Arc, syntax_theme: Arc, - image_picker: Arc>, + //image_picker: Arc>, } impl FilePreviewer { pub fn new() -> Self { let syntax_set = SyntaxSet::load_defaults_nonewlines(); let theme_set = ThemeSet::load_defaults(); - let image_picker = get_image_picker(); + info!("getting image picker"); + //let image_picker = get_image_picker(); + info!("got image picker"); FilePreviewer { cache: Arc::new(Mutex::new(PreviewCache::default())), syntax_set: Arc::new(syntax_set), syntax_theme: Arc::new(theme_set.themes["base16-ocean.dark"].clone()), - image_picker: Arc::new(Mutex::new(image_picker)), + //image_picker: Arc::new(Mutex::new(image_picker)), } } - async fn compute_image_preview(&self, entry: &finders::Entry) { - let cache = self.cache.clone(); - let picker = self.image_picker.clone(); - let entry_c = entry.clone(); - tokio::spawn(async move { - info!("Loading image: {:?}", entry_c.name); - if let Ok(dyn_image) = ImageReader::open(entry_c.name.clone()).unwrap().decode() { - let image = picker.lock().await.new_resize_protocol(dyn_image); - let preview = Arc::new(Preview::new( - entry_c.name.clone(), - PreviewContent::Image(image), - )); - cache - .lock() - .await - .insert(entry_c.name.clone(), preview.clone()); - } - }); - } + //async fn compute_image_preview(&self, entry: &finders::Entry) { + // let cache = self.cache.clone(); + // let picker = self.image_picker.clone(); + // let entry_c = entry.clone(); + // tokio::spawn(async move { + // info!("Loading image: {:?}", entry_c.name); + // if let Ok(dyn_image) = ImageReader::open(entry_c.name.clone()).unwrap().decode() { + // let image = picker.lock().await.new_resize_protocol(dyn_image); + // let preview = Arc::new(Preview::new( + // entry_c.name.clone(), + // PreviewContent::Image(image), + // )); + // cache + // .lock() + // .await + // .insert(entry_c.name.clone(), preview.clone()); + // } + // }); + //} async fn compute_highlighted_text_preview(&self, entry: &finders::Entry, lines: Vec) { let cache = self.cache.clone(); @@ -199,7 +201,7 @@ impl FilePreviewer { self.cache_preview(entry.name.clone(), preview.clone()) .await; // compute the image preview in the background - self.compute_image_preview(entry).await; + //self.compute_image_preview(entry).await; return preview; } FileType::Other => { @@ -221,11 +223,10 @@ impl FilePreviewer { } fn get_image_picker() -> Picker { - //let mut picker = match Picker::from_termios() { - // Ok(p) => p, - // Err(_) => Picker::new((7, 14)), - //}; - let mut picker = Picker::new((7, 14)); + let mut picker = match Picker::from_termios() { + Ok(p) => p, + Err(_) => Picker::new((7, 14)), + }; picker.guess_protocol(); picker.background_color = Some(Rgb::([255, 0, 255])); picker @@ -289,7 +290,7 @@ fn compute_highlights<'a>( highlighted_lines.push( hl_regions .iter() - .map(|(style, text)| (*style, text.to_string())) + .map(|(style, text)| (*style, (*text).to_string())) .collect(), ); } diff --git a/src/components/television.rs b/src/components/television.rs index ce413e3..09538f0 100644 --- a/src/components/television.rs +++ b/src/components/television.rs @@ -61,7 +61,7 @@ pub struct Television { const EMPTY_STRING: &str = ""; impl Television { - pub async fn new(channel: UnitTvChannel) -> Self { + pub fn new(channel: UnitTvChannel) -> Self { let mut tv_channel = get_tv_channel(channel); tv_channel.find(EMPTY_STRING); @@ -141,7 +141,7 @@ impl Television { self.picker_view_offset = (self .channel .result_count() - .saturating_sub(self.results_area_height as u32 - 2)) + .saturating_sub(self.results_area_height - 2)) as usize; self.picker_state.select(Some( (self.channel.result_count() as usize).saturating_sub(1), diff --git a/src/main.rs b/src/main.rs index 6e35809..e7e980d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,11 @@ async fn main() -> Result<()> { crate::logging::init()?; let args = Cli::parse(); - let mut app = App::new(args.channel, args.tick_rate, args.frame_rate).await?; + let mut app = App::new(args.channel, args.tick_rate, args.frame_rate)?; if let Some(entry) = app.run().await? { // print entry to stdout info!("{:?}", entry); - stdout().write(entry.stdout_repr().as_bytes())?; + stdout().write_all(entry.stdout_repr().as_bytes())?; } Ok(()) }