Decouple number of columns from paragraphs
Some checks failed
Cargo CI / Build and Test (pull_request) Successful in 1m43s
Cargo CI / Lint (pull_request) Failing after 1m15s

This commit is contained in:
Wojciech Kozlowski 2024-02-10 23:17:40 +01:00
parent 64b93c837e
commit 78fcbafc04

View File

@ -347,25 +347,41 @@ impl<'a, 'b> TrackState<'a, 'b> {
} }
} }
struct Minibuffer; struct Minibuffer<'a> {
paragraphs: Vec<Paragraph<'a>>,
columns: u16,
}
impl Minibuffer { impl Minibuffer<'_> {
fn paragraphs(state: &AppPublicState) -> Vec<Paragraph> { fn paragraphs(state: &AppPublicState) -> Self {
match state { match state {
AppState::Browse(_) => vec![ AppState::Browse(_) => Minibuffer {
paragraphs: vec![
Paragraph::new("m: show info overlay"), Paragraph::new("m: show info overlay"),
Paragraph::new("g: show reload menu"), Paragraph::new("g: show reload menu"),
], ],
AppState::Info(_) => vec![Paragraph::new("m: hide info overlay")], columns: 3,
AppState::Reload(_) => vec![ },
AppState::Info(_) => Minibuffer {
paragraphs: vec![Paragraph::new("m: hide info overlay")],
columns: 3,
},
AppState::Reload(_) => Minibuffer {
paragraphs: vec![
Paragraph::new("g: hide reload menu"),
Paragraph::new("d: reload database"), Paragraph::new("d: reload database"),
Paragraph::new("l: reload library"), Paragraph::new("l: reload library"),
Paragraph::new("g: hide reload menu"),
], ],
AppState::Error(_) => { columns: 3,
vec![Paragraph::new("Press any key to dismiss the error message")] },
} AppState::Error(_) => Minibuffer {
AppState::Critical(_) => vec![Paragraph::new("Press ctrl+c to terminate the program")], paragraphs: vec![Paragraph::new("Press any key to dismiss the error message...")],
columns: 1,
},
AppState::Critical(_) => Minibuffer {
paragraphs: vec![Paragraph::new("Press ctrl+c to terminate the program...")],
columns: 1,
},
} }
} }
} }
@ -472,10 +488,13 @@ impl Ui {
); );
} }
fn columns(paragraphs: Vec<Paragraph>, area: Rect) -> Vec<Column> { fn columns(paragraphs: Vec<Paragraph>, min: u16, area: Rect) -> Vec<Column> {
let mut x = area.x; let mut x = area.x;
let mut width = area.width; let mut width = area.width;
let mut remaining = paragraphs.len() as u16; let mut remaining = paragraphs.len() as u16;
if remaining < min {
remaining = min;
}
let mut blocks = vec![]; let mut blocks = vec![];
for paragraph in paragraphs.into_iter() { for paragraph in paragraphs.into_iter() {
@ -501,11 +520,12 @@ impl Ui {
fn render_columns<B: Backend>( fn render_columns<B: Backend>(
paragraphs: Vec<Paragraph>, paragraphs: Vec<Paragraph>,
min: u16,
active: bool, active: bool,
area: Rect, area: Rect,
frame: &mut Frame<'_, B>, frame: &mut Frame<'_, B>,
) { ) {
for column in Self::columns(paragraphs, area).into_iter() { for column in Self::columns(paragraphs, min, area).into_iter() {
frame.render_widget( frame.render_widget(
column column
.paragraph .paragraph
@ -531,7 +551,9 @@ impl Ui {
} }
fn render_minibuffer<B: Backend>(state: &AppPublicState, ar: Rect, fr: &mut Frame<'_, B>) { fn render_minibuffer<B: Backend>(state: &AppPublicState, ar: Rect, fr: &mut Frame<'_, B>) {
let paragraphs = Minibuffer::paragraphs(state) let mut mb = Minibuffer::paragraphs(state);
mb.paragraphs = mb
.paragraphs
.into_iter() .into_iter()
.map(|p| p.alignment(Alignment::Center)) .map(|p| p.alignment(Alignment::Center))
.collect(); .collect();
@ -544,7 +566,7 @@ impl Ui {
}; };
Self::render_info_widget("Minibuffer", Paragraph::new(""), false, ar, fr); Self::render_info_widget("Minibuffer", Paragraph::new(""), false, ar, fr);
Self::render_columns(paragraphs, false, area, fr); Self::render_columns(mb.paragraphs, mb.columns, false, area, fr);
} }
fn render_main_frame<B: Backend>( fn render_main_frame<B: Backend>(