Skip to content

Commit

Permalink
nicely wrap and split on colon for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Sep 23, 2024
1 parent 5948f67 commit b326e0b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 54 deletions.
10 changes: 10 additions & 0 deletions src/components/finders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ pub struct Entry {
pub line_number: Option<usize>,
}

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.
///
Expand Down
1 change: 1 addition & 0 deletions src/components/previewers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use env::EnvVarPreviewer;
#[derive(Debug, Clone)]
pub enum PreviewContent {
PlainText(String),
PlainTextWrapped(String),
HighlightedText(String),
Empty,
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/previewers/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
130 changes: 77 additions & 53 deletions src/components/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -26,6 +26,7 @@ use crate::{
};

use super::{
finders::ENTRY_PLACEHOLDER,
input::{backend::EventHandler, Input, InputRequest, StateChanged},
pickers::{get_tv_channel, TvChannel},
};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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)),
}
}
}

Expand Down

0 comments on commit b326e0b

Please sign in to comment.