From b326e0b95b32363d1bbb556a5104d23746f8d9dc Mon Sep 17 00:00:00 2001 From: alexpasmantier Date: Mon, 23 Sep 2024 14:21:56 +0200 Subject: [PATCH] nicely wrap and split on colon for paths --- src/components/finders.rs | 10 +++ src/components/previewers.rs | 1 + src/components/previewers/env.rs | 2 +- src/components/television.rs | 130 ++++++++++++++++++------------- 4 files changed, 89 insertions(+), 54 deletions(-) diff --git a/src/components/finders.rs b/src/components/finders.rs index f2d613f..dfd03e0 100644 --- a/src/components/finders.rs +++ b/src/components/finders.rs @@ -15,6 +15,16 @@ pub struct Entry { pub line_number: Option, } +pub const ENTRY_PLACEHOLDER: Entry = Entry { + name: String::new(), + preview: None, + score: 0, + name_match_ranges: None, + preview_match_ranges: None, + icon: None, + line_number: None, +}; + /// A trait for a finder that can find entries based on a pattern. /// The `find` method takes a pattern and returns an iterator over the entries found. /// diff --git a/src/components/previewers.rs b/src/components/previewers.rs index 6263075..dd51f19 100644 --- a/src/components/previewers.rs +++ b/src/components/previewers.rs @@ -8,6 +8,7 @@ pub use env::EnvVarPreviewer; #[derive(Debug, Clone)] pub enum PreviewContent { PlainText(String), + PlainTextWrapped(String), HighlightedText(String), Empty, } diff --git a/src/components/previewers/env.rs b/src/components/previewers/env.rs index bbd4603..f1caaa4 100644 --- a/src/components/previewers/env.rs +++ b/src/components/previewers/env.rs @@ -24,7 +24,7 @@ impl Previewer for EnvVarPreviewer { let preview = Preview { title: entry.name.clone(), content: if let Some(preview) = &entry.preview { - PreviewContent::PlainText(add_newline_after_colon(&preview)) + PreviewContent::PlainTextWrapped(add_newline_after_colon(&preview)) } else { PreviewContent::Empty }, diff --git a/src/components/television.rs b/src/components/television.rs index bb49e21..bf31a2f 100644 --- a/src/components/television.rs +++ b/src/components/television.rs @@ -5,7 +5,7 @@ use ratatui::{ text::{Line, Span, Text}, widgets::{ block::{Position, Title}, - Block, BorderType, Borders, List, ListDirection, ListState, Padding, Paragraph, + Block, BorderType, Borders, List, ListDirection, ListState, Padding, Paragraph, Wrap, }, Frame, }; @@ -26,6 +26,7 @@ use crate::{ }; use super::{ + finders::ENTRY_PLACEHOLDER, input::{backend::EventHandler, Input, InputRequest, StateChanged}, pickers::{get_tv_channel, TvChannel}, }; @@ -82,6 +83,13 @@ impl Television { Ok(()) } + fn get_selected_entry(&self) -> Option<&Entry> { + self.results_list + .state + .selected() + .and_then(|i| self.results_list.results.get(i)) + } + pub fn select_prev_entry(&mut self) { let selected = self.results_list.state.selected().unwrap_or(0); let total_search_results = self.results_list.results.len(); @@ -365,48 +373,48 @@ impl Component for Television { } // top right block: preview title - // TODO: update this to always show the preview pane - if let Some(selected) = self.results_list.state.selected() { - let entry = &self.results_list.results[selected]; - let preview = self.channel.get_preview(entry); - let preview_title = Paragraph::new(Line::from(preview.title.clone())) - .block( - Block::default() - .borders(Borders::ALL) - .border_type(BorderType::Rounded) - .border_style(self.get_border_style(false)), - ) - .style(Style::default().fg(DEFAULT_PREVIEW_TITLE_FG)) - .alignment(Alignment::Left); - frame.render_widget(preview_title, preview_title_area); - - // file preview - let preview_outer_block = Block::default() - .title( - Title::from(" Preview ") - .position(Position::Top) - .alignment(Alignment::Center), - ) - .borders(Borders::ALL) - .border_type(BorderType::Rounded) - .border_style(self.get_border_style(Pane::Preview == self.current_pane)) - .style(Style::default()) - .padding(Padding::right(1)); - - let preview_inner_block = Block::default().style(Style::default()).padding(Padding { - top: 0, - right: 1, - bottom: 0, - left: 1, - }); - let inner = preview_outer_block.inner(preview_area); - frame.render_widget(preview_outer_block, preview_area); - - //app.preview_pane_height = inner.height; - let preview = self.build_preview(preview_inner_block, preview); - - frame.render_widget(preview, inner); - } + let selected_entry = self.get_selected_entry(); + + let preview = self + .channel + .get_preview(&selected_entry.unwrap_or(&ENTRY_PLACEHOLDER).clone()); + let preview_title = Paragraph::new(Line::from(preview.title.clone())) + .block( + Block::default() + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(self.get_border_style(false)), + ) + .style(Style::default().fg(DEFAULT_PREVIEW_TITLE_FG)) + .alignment(Alignment::Left); + frame.render_widget(preview_title, preview_title_area); + + // file preview + let preview_outer_block = Block::default() + .title( + Title::from(" Preview ") + .position(Position::Top) + .alignment(Alignment::Center), + ) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(self.get_border_style(Pane::Preview == self.current_pane)) + .style(Style::default()) + .padding(Padding::right(1)); + + let preview_inner_block = Block::default().style(Style::default()).padding(Padding { + top: 0, + right: 1, + bottom: 0, + left: 1, + }); + let inner = preview_outer_block.inner(preview_area); + frame.render_widget(preview_outer_block, preview_area); + + //app.preview_pane_height = inner.height; + let preview = self.build_preview(preview_inner_block, preview); + + frame.render_widget(preview, inner); Ok(()) } @@ -534,30 +542,46 @@ impl Television { preview: Preview, ) -> Paragraph<'a> { let preview_content = preview.content.clone(); - let text = match preview_content { + match preview_content { PreviewContent::PlainText(content) => { - let mut spans = Vec::new(); + let mut lines = Vec::new(); for line in content.lines() { - spans.push(Span::styled( + lines.push(Line::styled( line.to_string(), Style::default().fg(DEFAULT_RESULT_PREVIEW_FG), )); } - Text::from(Line::from(spans)) + let text = Text::from(lines); + Paragraph::new(text).block(preview_block) + } + PreviewContent::PlainTextWrapped(content) => { + let mut lines = Vec::new(); + for line in content.lines() { + lines.push(Line::styled( + line.to_string(), + Style::default().fg(DEFAULT_RESULT_PREVIEW_FG), + )); + } + let text = Text::from(lines); + Paragraph::new(text) + .block(preview_block) + .wrap(Wrap { trim: true }) } PreviewContent::HighlightedText(content) => { - let mut spans = Vec::new(); + let mut lines = Vec::new(); for line in content.lines() { - spans.push(Span::styled( + lines.push(Line::styled( line.to_string(), Style::default().fg(DEFAULT_RESULT_PREVIEW_FG), )); } - Text::from(Line::from(spans)) + let text = Text::from(lines); + Paragraph::new(text) + .block(preview_block) + .wrap(Wrap { trim: true }) } - PreviewContent::Empty => Text::raw(EMPTY_STRING), - }; - Paragraph::new(text).block(preview_block) + PreviewContent::Empty => Paragraph::new(Text::raw(EMPTY_STRING)), + } } }