aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
authorGokul Soumya2021-08-17 00:25:48 +0000
committerGitHub2021-08-17 00:25:48 +0000
commit14c08e855f6d0ebfbf1d7cb5d7010507ad3a5db6 (patch)
treea9a0ea3d0f9fbb61bc258be87e7f51a965126471 /helix-view
parent27616153bc5117153f246e538bf32de226fa8fc9 (diff)
Refactor infobox rendering and parsing (#579)
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/info.rs56
1 files changed, 22 insertions, 34 deletions
diff --git a/helix-view/src/info.rs b/helix-view/src/info.rs
index 70e934cd..629a3112 100644
--- a/helix-view/src/info.rs
+++ b/helix-view/src/info.rs
@@ -16,45 +16,33 @@ pub struct Info {
}
impl Info {
- // body is a BTreeMap instead of a HashMap because keymaps are represented
- // with nested hashmaps with no ordering, and each invocation of infobox would
- // show different orders of items
- pub fn key(title: &str, body: Vec<(&str, Vec<KeyEvent>)>) -> Info {
- let (lpad, mpad, rpad) = (1, 2, 1);
- let keymaps_width: u16 = body
- .iter()
- .map(|r| r.1.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
- .max()
- .unwrap();
+ pub fn new(title: &str, body: Vec<(&str, Vec<KeyEvent>)>) -> Info {
+ let body = body
+ .into_iter()
+ .map(|(desc, events)| {
+ let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
+ (desc, events.join(", "))
+ })
+ .collect::<Vec<_>>();
+
+ let keymaps_width = body.iter().map(|r| r.1.len()).max().unwrap();
let mut text = String::new();
- let mut width = 0;
- let height = body.len() as u16;
- for (desc, keyevents) in body {
- let keyevent = keyevents[0];
- let mut left = keymaps_width - keyevent.width() as u16;
- for _ in 0..lpad {
- text.push(' ');
- }
- write!(text, "{}", keyevent).ok();
- for keyevent in &keyevents[1..] {
- write!(text, ", {}", keyevent).ok();
- left -= 2 + keyevent.width() as u16;
- }
- for _ in 0..left + mpad {
- text.push(' ');
- }
- let desc = desc.trim();
- let w = lpad + keymaps_width + mpad + (desc.width() as u16) + rpad;
- if w > width {
- width = w;
- }
- writeln!(text, "{}", desc).ok();
+
+ for (desc, keyevents) in &body {
+ let _ = writeln!(
+ text,
+ "{:width$} {}",
+ keyevents,
+ desc,
+ width = keymaps_width
+ );
}
+
Info {
title: title.to_string(),
+ width: text.lines().map(|l| l.width()).max().unwrap() as u16,
+ height: body.len() as u16,
text,
- width,
- height,
}
}
}