2021-06-19 17:54:37 +02:00
|
|
|
use crate::input::KeyEvent;
|
2021-06-29 18:17:16 +02:00
|
|
|
use helix_core::unicode::width::UnicodeWidthStr;
|
2021-06-19 17:54:37 +02:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// Info box used in editor. Rendering logic will be in other crate.
|
|
|
|
pub struct Info {
|
2021-07-26 18:07:13 +02:00
|
|
|
/// Title shown at top.
|
|
|
|
pub title: String,
|
|
|
|
/// Text body, should contain newlines.
|
2021-06-19 17:54:37 +02:00
|
|
|
pub text: String,
|
|
|
|
/// Body width.
|
|
|
|
pub width: u16,
|
|
|
|
/// Body height.
|
|
|
|
pub height: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Info {
|
2021-07-26 18:07:13 +02:00
|
|
|
// 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 {
|
2021-07-03 18:12:02 +02:00
|
|
|
let (lpad, mpad, rpad) = (1, 2, 1);
|
2021-06-19 17:54:37 +02:00
|
|
|
let keymaps_width: u16 = body
|
|
|
|
.iter()
|
2021-07-26 18:07:13 +02:00
|
|
|
.map(|r| r.1.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
|
2021-06-19 17:54:37 +02:00
|
|
|
.max()
|
|
|
|
.unwrap();
|
|
|
|
let mut text = String::new();
|
|
|
|
let mut width = 0;
|
|
|
|
let height = body.len() as u16;
|
2021-07-26 18:07:13 +02:00
|
|
|
for (desc, keyevents) in body {
|
2021-07-02 03:46:28 +02:00
|
|
|
let keyevent = keyevents[0];
|
2021-06-19 17:54:37 +02:00
|
|
|
let mut left = keymaps_width - keyevent.width() as u16;
|
2021-07-03 18:12:02 +02:00
|
|
|
for _ in 0..lpad {
|
|
|
|
text.push(' ');
|
|
|
|
}
|
2021-06-19 17:54:37 +02:00
|
|
|
write!(text, "{}", keyevent).ok();
|
2021-07-02 03:46:28 +02:00
|
|
|
for keyevent in &keyevents[1..] {
|
2021-06-19 17:54:37 +02:00
|
|
|
write!(text, ", {}", keyevent).ok();
|
|
|
|
left -= 2 + keyevent.width() as u16;
|
|
|
|
}
|
2021-07-03 18:12:02 +02:00
|
|
|
for _ in 0..left + mpad {
|
2021-06-19 17:54:37 +02:00
|
|
|
text.push(' ');
|
|
|
|
}
|
2021-07-03 18:12:02 +02:00
|
|
|
let desc = desc.trim();
|
|
|
|
let w = lpad + keymaps_width + mpad + (desc.width() as u16) + rpad;
|
|
|
|
if w > width {
|
|
|
|
width = w;
|
2021-06-19 17:54:37 +02:00
|
|
|
}
|
2021-07-03 18:12:02 +02:00
|
|
|
writeln!(text, "{}", desc).ok();
|
2021-06-19 17:54:37 +02:00
|
|
|
}
|
|
|
|
Info {
|
2021-07-26 18:07:13 +02:00
|
|
|
title: title.to_string(),
|
2021-06-19 17:54:37 +02:00
|
|
|
text,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|