Skip to content

Commit

Permalink
split Bottom Bar draw function
Browse files Browse the repository at this point in the history
  • Loading branch information
konradsz committed Nov 1, 2023
1 parent 8412a91 commit 441eee2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use std::path::PathBuf;
pub struct App {
search_config: SearchConfig,
ig: Ig,
theme: Box<dyn Theme>,
result_list: ResultList,
context_viewer: ContextViewer,
theme: Box<dyn Theme>,
search_popup: SearchPopup,
keymap_popup: KeymapPopup,
}
Expand All @@ -41,9 +41,9 @@ impl App {
Self {
search_config,
ig: Ig::new(editor_command),
theme,
result_list: ResultList::default(),
context_viewer: ContextViewer::default(),
theme,
search_popup: SearchPopup::default(),
keymap_popup: KeymapPopup::default(),
}
Expand Down
112 changes: 74 additions & 38 deletions src/ui/bottom_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ pub fn draw(
ig: &Ig,
input_handler: &InputHandler,
theme: &dyn Theme,
) {
let selected_info_text = render_selected_info_text(result_list);

let hsplit = Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Length(12),
Constraint::Min(1),
Constraint::Length(2),
Constraint::Length(selected_info_text.len() as u16),
]
.as_ref(),
)
.split(area);

draw_app_status(frame, hsplit[0], ig, theme);
draw_search_result_summary(frame, hsplit[1], ig, result_list, theme);
draw_current_input(frame, hsplit[2], input_handler, theme);
draw_selected_info(frame, hsplit[3], selected_info_text, theme);
}

fn draw_app_status(
frame: &mut Frame<CrosstermBackend<std::io::Stdout>>,
area: Rect,
ig: &Ig,
theme: &dyn Theme,
) {
let (app_status_text, app_status_style) = if ig.is_searching() {
("SEARCHING", theme.searching_state_style())
Expand All @@ -32,6 +59,21 @@ pub fn draw(
};
let app_status = Span::styled(app_status_text, app_status_style);

frame.render_widget(
Paragraph::new(app_status)
.style(Style::default().bg(app_status_style.bg.expect("Background not set")))
.alignment(Alignment::Center),
area,
);
}

fn draw_search_result_summary(
frame: &mut Frame<CrosstermBackend<std::io::Stdout>>,
area: Rect,
ig: &Ig,
result_list: &ResultList,
theme: &dyn Theme,
) {
let search_result = Span::raw(if ig.is_searching() {
"".into()
} else if let Some(err) = ig.last_error() {
Expand Down Expand Up @@ -61,6 +103,20 @@ pub fn draw(
}
});

frame.render_widget(
Paragraph::new(search_result)
.style(theme.bottom_bar_style())
.alignment(Alignment::Left),
area,
);
}

fn draw_current_input(
frame: &mut Frame<CrosstermBackend<std::io::Stdout>>,
area: Rect,
input_handler: &InputHandler,
theme: &dyn Theme,
) {
let (current_input_content, current_input_color) = match input_handler.get_state() {
InputState::Valid => (String::default(), theme.bottom_bar_font_color()),
InputState::Incomplete(input) => (input.to_owned(), theme.bottom_bar_font_color()),
Expand All @@ -73,53 +129,33 @@ pub fn draw(
.fg(current_input_color),
);

let current_no_of_matches = result_list.get_current_number_of_matches();
let current_match_index = result_list.get_current_match_index();
let selected_info_text = {
let width = current_no_of_matches.to_string().len();
format!(" | {current_match_index: >width$}/{current_no_of_matches} ")
};
let selected_info_length = selected_info_text.len();
let selected_info = Span::styled(selected_info_text, theme.bottom_bar_style());

let hsplit = Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Length(12),
Constraint::Min(1),
Constraint::Length(2),
Constraint::Length(selected_info_length as u16),
]
.as_ref(),
)
.split(area);

frame.render_widget(
Paragraph::new(app_status)
.style(Style::default().bg(app_status_style.bg.expect("Background not set")))
.alignment(Alignment::Center),
hsplit[0],
);

frame.render_widget(
Paragraph::new(search_result)
.style(theme.bottom_bar_style())
.alignment(Alignment::Left),
hsplit[1],
);

frame.render_widget(
Paragraph::new(current_input)
.style(theme.bottom_bar_style())
.alignment(Alignment::Right),
hsplit[2],
area,
);
}

fn render_selected_info_text(result_list: &ResultList) -> String {
let current_no_of_matches = result_list.get_current_number_of_matches();
let current_match_index = result_list.get_current_match_index();
let width = current_no_of_matches.to_string().len();
format!(" | {current_match_index: >width$}/{current_no_of_matches} ")
}

fn draw_selected_info(
frame: &mut Frame<CrosstermBackend<std::io::Stdout>>,
area: Rect,
selected_info_text: String,
theme: &dyn Theme,
) {
let selected_info = Span::styled(selected_info_text, theme.bottom_bar_style());

frame.render_widget(
Paragraph::new(selected_info)
.style(theme.bottom_bar_style())
.alignment(Alignment::Right),
hsplit[3],
area,
);
}

0 comments on commit 441eee2

Please sign in to comment.