Skip to content

Commit

Permalink
vector2_col_sums to vector2_col_max
Browse files Browse the repository at this point in the history
The original intention was never to sum up all the lengths, I just
forgot what I was supposed to implement I guess.
  • Loading branch information
Xithrius committed Sep 12, 2021
1 parent 5498884 commit 7df81bc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
29 changes: 11 additions & 18 deletions src/ui/keybinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tui::{

use crate::{
handlers::config::CompleteConfig,
utils::{colors::WindowStyles, text::vector2_col_sums},
utils::{colors::WindowStyles, text::vector2_col_max},
};

pub fn draw_keybinds_ui<T>(frame: &mut Frame<T>, config: CompleteConfig) -> Result<()>
Expand All @@ -17,28 +17,25 @@ where
{
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(10)
.constraints([Constraint::Min(1)].as_ref())
.margin(5)
.constraints([Constraint::Percentage(100)].as_ref())
.split(frame.size());

let mut keybinds = vec![
vec!["Description", "Keybind"],
vec!["Bring up the chat window", config.keybinds.chat.as_str()],
vec![
"Bring up the table for keybinds",
config.keybinds.keybinds.as_str(),
],
vec!["Keybinds help", config.keybinds.keybinds.as_str()],
vec!["See all the users in chat", config.keybinds.users.as_str()],
vec!["Quit this application", config.keybinds.quit.as_str()],
];

let (maximum_description_width, maximum_keybind_width) = vector2_col_sums(keybinds.clone());
let (maximum_description_width, maximum_keybind_width) = vector2_col_max(keybinds.clone());

let column_names = keybinds.remove(0);

let table_widths = vec![
Constraint::Length(maximum_description_width.clone()),
Constraint::Length(maximum_keybind_width.clone()),
Constraint::Min(maximum_description_width.clone()),
Constraint::Min(maximum_keybind_width.clone()),
];

let table = Table::new(
Expand All @@ -47,15 +44,11 @@ where
.map(|k| Row::new(k.clone()))
.collect::<Vec<Row>>(),
)
.header(Row::new(column_names.clone()).style(WindowStyles::new(WindowStyles::ColumnTitle)))
.block(Block::default().borders(Borders::ALL).title("[ Keybinds ]"))
.column_spacing(1)
.style(WindowStyles::new(WindowStyles::BoarderName))
.header(
Row::new(column_names.clone())
.style(WindowStyles::new(WindowStyles::ColumnTitle))
.bottom_margin(0),
)
.widths(&table_widths);
.widths(&table_widths)
.column_spacing(2)
.style(WindowStyles::new(WindowStyles::BoarderName));

frame.render_widget(table, vertical_chunks[0]);

Expand Down
37 changes: 34 additions & 3 deletions src/utils/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,40 @@ pub fn align_text(text: &str, alignment: &str, maximum_length: &u16) -> String {
}
}

pub fn vector2_col_sums(vec2: Vec<Vec<&str>>) -> (u16, u16) {
let col0 = vec2.iter().map(|v| v[0].len()).sum::<usize>();
let col1 = vec2.iter().map(|v| v[1].len()).sum::<usize>();
pub fn vector2_col_max<T>(vec2: Vec<Vec<T>>) -> (u16, u16)
where
T: AsRef<str>,
{
let col0 = vec2.iter().map(|v| v[0].as_ref().len()).max().unwrap();
let col1 = vec2.iter().map(|v| v[1].as_ref().len()).max().unwrap();

(col0 as u16, col1 as u16)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_reference_string_vec2() {
let vec2 = vec![vec!["", "s"], vec!["longer string", "lll"]];

let (col0, col1) = vector2_col_max(vec2);

assert_eq!(col0, 13);
assert_eq!(col1, 3);
}

#[test]
fn test_string_vec2() {
let vec2 = vec![
vec!["".to_string(), "another".to_string()],
vec!["".to_string(), "the last string".to_string()],
];

let (col0, col1) = vector2_col_max(vec2);

assert_eq!(col0, 0);
assert_eq!(col1, 15);
}
}

0 comments on commit 7df81bc

Please sign in to comment.