Skip to content

Commit

Permalink
wip: extract SearchPrompt out of SearchFilterPrompt
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Feb 10, 2024
1 parent 36e66eb commit d30d427
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ lazy_static = "1.4.0"
libc = "0.2.148"
num-format = "0.4.4"
pretty_assertions = "1.4.0"
ratatui = { version = "0.26.0", features = ["serde", "macros"] }
ratatui = { version = "0.26.1-alpha.0", features = ["serde", "macros"] }
ratatui-macros = "0.2.3"
serde = { version = "1.0.188", features = ["derive"] }
serde_with = "3.5.0"
Expand Down
3 changes: 3 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ use crate::{

mod search_page;
use search_page::SearchPage;
mod search_prompt;
// TODO switch to use search_prompt::SearchPrompt
use search_prompt::*;

use self::search_page::SearchMode;

Expand Down
98 changes: 98 additions & 0 deletions src/app/search_prompt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use ratatui::{layout::Constraint::*, layout::Position, prelude::*, widgets::*};

use crate::{app::Mode, command::Command, config};

#[derive(Debug, Clone)]
pub struct SearchPrompt {
input: tui_input::Input,
sort: crates_io_api::Sort,
}

#[derive(Debug, Clone)]
pub struct SearchPromptState {
pub cursor_position: Option<Position>,
}

const MARGIN: Margin = Margin::new(2, 2);

impl StatefulWidget for &SearchPrompt {
type State = SearchPromptState;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let [input_area, meta_area] = Layout::horizontal([Percentage(75), Fill(0)]).areas(area);

self.input_block().render(area, buf);

let sort_area = meta_area.inner(&MARGIN);
self.sort_by_info().render(sort_area, buf);

self.input_text(input_area.width as usize)
.render(input_area.inner(&MARGIN), buf);

state.cursor_position = Some(self.cursor_position(area));
}
}

impl SearchPrompt {
fn cursor_position(&self, area: Rect) -> Position {
// TODO base this on the actual area used rather than the area of the whole so we don't have to
// calculate it twice
let width = ((area.width as f64 * 0.75) as u16).saturating_sub(2);
let x = (area.x + MARGIN.horizontal + self.input.cursor() as u16).min(width);
let y = area.y + MARGIN.vertical;
Position::new(x, y)
}

fn input_block(&self) -> Block {
let border_color = config::get().color.base0a;
let help_key = self.help_command_key();
let toggle_sort_key = self.toggle_sort_key();
let search_title = Line::from(vec!["Search: ".into(), "Enter".bold(), " to submit".into()]);
let toggle_sort_title = Line::from(vec![toggle_sort_key.bold(), " to toggle sort".into()]);
let help_title = Line::from(vec![help_key.bold(), " for help".into()]);
Block::bordered()
.fg(config::get().color.base05)
.border_style(border_color)
.title_top(search_title)
.title_top(toggle_sort_title.right_aligned())
.title_bottom(help_title.right_aligned())
}

// TODO make this a method on KeyBindings
fn help_command_key(&self) -> String {
config::get()
.key_bindings
.get_config_for_command(Mode::Search, Command::SwitchMode(Mode::Help))
.into_iter()
.next()
.unwrap_or_default()
}

fn toggle_sort_key(&self) -> String {
config::get()
.key_bindings
.get_config_for_command(
Mode::Search,
Command::ToggleSortBy {
reload: false,
forward: true,
},
)
.into_iter()
.next()
.unwrap_or_default()
}

fn sort_by_info(&self) -> impl Widget {
Line::from(vec![
"Sort By: ".into(),
format!("{:?}", self.sort.clone()).fg(config::get().color.base0d),
])
.right_aligned()
}

fn input_text(&self, width: usize) -> impl Widget + '_ {
let scroll = self.input.cursor().saturating_sub(width.saturating_sub(4));
let text = Line::from(vec![self.input.value().into()]);
Paragraph::new(text).scroll((0, scroll as u16))
}
}

0 comments on commit d30d427

Please sign in to comment.