Functional help bar
This commit is contained in:
parent
eb7a3fdf60
commit
94c231bf2c
1 changed files with 37 additions and 5 deletions
42
src/main.rs
42
src/main.rs
|
|
@ -2,7 +2,10 @@ use colored::*;
|
|||
use mosaic_tile::*;
|
||||
|
||||
#[derive(Default)]
|
||||
struct State(usize, String);
|
||||
struct State {
|
||||
lines: Vec<String>,
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue