aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/info.rs
diff options
context:
space:
mode:
authorIvan Tham2021-06-19 15:54:37 +0000
committerBlaž Hrastnik2021-07-04 09:01:59 +0000
commit8985c58fd328cde512df0ac7bc577bd8a8c949a0 (patch)
tree09e82861faa30009a75859ed17746544af4eb5d7 /helix-view/src/info.rs
parent6ccfa229ed61552e243519534c8659c0974ef62e (diff)
Add infobox
Diffstat (limited to 'helix-view/src/info.rs')
-rw-r--r--helix-view/src/info.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/helix-view/src/info.rs b/helix-view/src/info.rs
new file mode 100644
index 00000000..eef8d3a1
--- /dev/null
+++ b/helix-view/src/info.rs
@@ -0,0 +1,51 @@
+use crate::input::KeyEvent;
+use std::fmt::Write;
+use unicode_width::UnicodeWidthStr;
+
+#[derive(Debug)]
+/// Info box used in editor. Rendering logic will be in other crate.
+pub struct Info {
+ /// Title kept as static str for now.
+ pub title: &'static str,
+ /// Text body, should contains newline.
+ pub text: String,
+ /// Body width.
+ pub width: u16,
+ /// Body height.
+ pub height: u16,
+}
+
+impl Info {
+ pub fn key(title: &'static str, body: Vec<(Vec<KeyEvent>, &'static str)>) -> Info {
+ let keymaps_width: u16 = body
+ .iter()
+ .map(|r| r.0.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
+ .max()
+ .unwrap();
+ let mut text = String::new();
+ let mut width = 0;
+ let height = body.len() as u16;
+ for (mut keyevents, desc) in body {
+ let keyevent = keyevents.remove(0);
+ let mut left = keymaps_width - keyevent.width() as u16;
+ write!(text, "{}", keyevent).ok();
+ for keyevent in keyevents {
+ write!(text, ", {}", keyevent).ok();
+ left -= 2 + keyevent.width() as u16;
+ }
+ for _ in 0..left {
+ text.push(' ');
+ }
+ if keymaps_width + 2 + (desc.width() as u16) > width {
+ width = keymaps_width + 2 + desc.width() as u16;
+ }
+ writeln!(text, " {}", &desc).ok();
+ }
+ Info {
+ title,
+ text,
+ width,
+ height,
+ }
+ }
+}