Skip to content

Commit

Permalink
refactor: Accommodate latest changes in ratatui
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Mar 14, 2024
1 parent 586c0a9 commit 15dfc62
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 60 deletions.
19 changes: 11 additions & 8 deletions src/app/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,17 @@ mod tests {
content: Borrowed("quits the application\n"),
style: Style::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed(":quit\n"),
style: Style::default().fg(Color::Red),
}],
alignment: None,
..Default::default()
},
],
..Default::default()
},
key_binding.get_description_text(Style::default().fg(Color::Red))
);
Expand All @@ -329,23 +330,24 @@ mod tests {
content: Borrowed("[q] [esc] "),
style: Style::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed(" └─quit"),
style: Style::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed(" "),
style: Style::default(),
}],
alignment: None,
..Default::default()
},
],
..Default::default()
}),
key_binding.as_list_item(false, false)
);
Expand Down Expand Up @@ -403,7 +405,7 @@ mod tests {
},
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![
Expand All @@ -422,10 +424,11 @@ mod tests {
},
},
],
alignment: None,
..Default::default()
},
Line::default(),
]
],
..Default::default()
}),
key_binding.as_list_item(true, true)
);
Expand Down
57 changes: 19 additions & 38 deletions src/app/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::app::style;
use crate::app::tab::Tab;
use crate::widget::row::RowItem;
use crate::widget::table::TableSize;
use ratatui::backend::Backend;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::terminal::Frame;
Expand All @@ -22,7 +21,7 @@ use unicode_width::UnicodeWidthStr;
const KEYS_ROW_LENGTH: (u16, u16) = (31, 55);

/// Renders all the widgets thus the user interface.
pub fn render<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
pub fn render(app: &mut App, frame: &mut Frame) {
let rect = frame.size();
if app.keys_table.state.minimize_threshold != 0 {
app.keys_table.state.size.set_minimized(
Expand Down Expand Up @@ -50,11 +49,7 @@ pub fn render<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>) {
}

/// Renders the splash screen.
fn render_splash_screen<B: Backend>(
app: &mut App,
frame: &mut Frame<'_, B>,
rect: Rect,
) {
fn render_splash_screen(app: &mut App, frame: &mut Frame, rect: Rect) {
app.state.show_splash = app.splash_screen.step != 0;
let data = app.splash_screen.get(app.state.style.is_colored());
frame.render_widget(
Expand Down Expand Up @@ -82,11 +77,7 @@ fn render_splash_screen<B: Backend>(
}

/// Renders the command prompt.
fn render_command_prompt<B: Backend>(
app: &mut App,
frame: &mut Frame<'_, B>,
rect: Rect,
) {
fn render_command_prompt(app: &mut App, frame: &mut Frame, rect: Rect) {
frame.render_widget(
Paragraph::new(Line::from(if !app.prompt.text.is_empty() {
vec![Span::raw(format!(
Expand Down Expand Up @@ -165,11 +156,7 @@ fn render_command_prompt<B: Backend>(
}

/// Renders the help tab.
fn render_help_tab<B: Backend>(
app: &mut App,
frame: &mut Frame<'_, B>,
rect: Rect,
) {
fn render_help_tab(app: &mut App, frame: &mut Frame, rect: Rect) {
frame.render_widget(
Block::default()
.borders(Borders::ALL)
Expand Down Expand Up @@ -323,11 +310,7 @@ fn render_help_tab<B: Backend>(
}

/// Renders the options menu.
fn render_options_menu<B: Backend>(
app: &mut App,
frame: &mut Frame<'_, B>,
rect: Rect,
) {
fn render_options_menu(app: &mut App, frame: &mut Frame, rect: Rect) {
let items = app
.options
.items
Expand Down Expand Up @@ -394,24 +377,26 @@ fn render_options_menu<B: Backend>(
}

/// Renders the table of keys.
fn render_keys_table<B: Backend>(
app: &mut App,
frame: &mut Frame<'_, B>,
rect: Rect,
) {
fn render_keys_table(app: &mut App, frame: &mut Frame, rect: Rect) {
let keys_row_length = if app.keys_table.state.size != TableSize::Normal {
KEYS_ROW_LENGTH.0
} else {
KEYS_ROW_LENGTH.1
};
frame.render_stateful_widget(
Table::new(get_keys_table_rows(
app,
rect.width
.checked_sub(keys_row_length + 7)
.unwrap_or(rect.width),
rect.height.checked_sub(2).unwrap_or(rect.height),
))
Table::new(
get_keys_table_rows(
app,
rect.width
.checked_sub(keys_row_length + 7)
.unwrap_or(rect.width),
rect.height.checked_sub(2).unwrap_or(rect.height),
),
&[
Constraint::Min(keys_row_length),
Constraint::Percentage(100),
],
)
.style(Style::default().fg(app.state.color))
.highlight_style(if app.state.style.is_colored() {
Style::default().add_modifier(Modifier::BOLD)
Expand All @@ -426,10 +411,6 @@ fn render_keys_table<B: Backend>(
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::DarkGray)),
)
.widths(&[
Constraint::Min(keys_row_length),
Constraint::Percentage(100),
])
.column_spacing(1),
rect,
&mut app.keys_table.state.tui,
Expand Down
31 changes: 17 additions & 14 deletions src/app/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ mod tests {
content: Borrowed(""),
style: TuiStyle::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![
Expand Down Expand Up @@ -306,23 +306,24 @@ mod tests {
style: TuiStyle::default(),
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed(" └─(2020-07-29)"),
style: TuiStyle::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed("\t\t"),
style: TuiStyle::default(),
}],
alignment: None,
..Default::default()
},
],
..Default::default()
},
get_colored_table_row(&row_data, false)
);
Expand All @@ -344,7 +345,7 @@ mod tests {
content: Borrowed(""),
style: TuiStyle::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![
Expand Down Expand Up @@ -389,7 +390,7 @@ mod tests {
style: TuiStyle::default(),
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![
Expand All @@ -409,14 +410,14 @@ mod tests {
style: TuiStyle::default(),
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed("\t├─][ test"),
style: TuiStyle::default(),
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![
Expand Down Expand Up @@ -461,16 +462,17 @@ mod tests {
style: TuiStyle::default(),
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
content: Borrowed("\t\t\t\t"),
style: TuiStyle::default(),
}],
alignment: None,
..Default::default()
},
],
..Default::default()
},
get_colored_table_row(&row_data, false)
);
Expand Down Expand Up @@ -501,7 +503,7 @@ mod tests {
},
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![
Expand All @@ -527,7 +529,7 @@ mod tests {
},
},
],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
Expand All @@ -537,7 +539,7 @@ mod tests {
..TuiStyle::default()
},
}],
alignment: None,
..Default::default()
},
Line {
spans: vec![Span {
Expand All @@ -547,9 +549,10 @@ mod tests {
..TuiStyle::default()
},
}],
alignment: None,
..Default::default()
},
],
..Default::default()
},
get_colored_info(
"test: xyz \n\
Expand Down

0 comments on commit 15dfc62

Please sign in to comment.