From 94c231bf2c2ab51e329cd8e384213f1f0598db15 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Mon, 11 Jan 2021 21:35:49 +0000 Subject: [PATCH] Functional help bar --- src/main.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index b820a18d..7557b066 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,10 @@ use colored::*; use mosaic_tile::*; #[derive(Default)] -struct State(usize, String); +struct State { + lines: Vec, + page: usize, +} register_tile!(State); @@ -12,12 +15,41 @@ impl MosaicTile for State { } fn draw(&mut self, _rows: usize, cols: usize) { - let line = format!("{}: {}", self.0, self.1); - println!("{:cols$}", line.reversed(), cols = cols); + let mut width = 0; + let more_msg = ", More"; + self.lines = vec![String::new()]; + for item in get_help() { + if width + item.len() > cols - more_msg.len() { + self.lines.last_mut().unwrap().push_str(more_msg); + width = item.len(); + self.lines.push(item); + } else { + let line = self.lines.last_mut().unwrap(); + if !line.is_empty() { + line.push_str(", "); + } + line.push_str(&item); + width += item.len() + 2; + } + } + let line = format!( + "{}{}", + self.lines[self.page], + if self.page > 0 && self.lines.len() == self.page + 1 { + ", Back" + } else { + "" + } + ); + println!("{}", line.italic()); } fn handle_global_key(&mut self, key: Key) { - self.0 += 1; - self.1 = format!("{:?}", key); + if self.lines.len() > 1 { + if let Key::Char('?') = key { + self.page += 1; + self.page %= self.lines.len(); + } + } } }