Skip to content

Commit

Permalink
things in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 8, 2024
1 parent f2351bf commit ec11d69
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 39 deletions.
16 changes: 11 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ pub enum Mode {
}

impl App {
pub async fn new(channel: UnitTvChannel, tick_rate: f64, frame_rate: f64) -> Result<Self> {
pub fn new(channel: UnitTvChannel, tick_rate: f64, frame_rate: f64) -> Result<Self> {
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,
Expand All @@ -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<Option<Entry>> {
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();
Expand Down
2 changes: 1 addition & 1 deletion src/components/finders/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
1 change: 1 addition & 0 deletions src/components/previewers/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 30 additions & 29 deletions src/components/previewers/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,44 @@ pub struct FilePreviewer {
cache: Arc<Mutex<PreviewCache>>,
syntax_set: Arc<SyntaxSet>,
syntax_theme: Arc<Theme>,
image_picker: Arc<Mutex<Picker>>,
//image_picker: Arc<Mutex<Picker>>,
}

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<String>) {
let cache = self.cache.clone();
Expand Down Expand Up @@ -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 => {
Expand All @@ -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::<u8>([255, 0, 255]));
picker
Expand Down Expand Up @@ -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(),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit ec11d69

Please sign in to comment.