You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
helix-plus/helix-view/src/info.rs

61 lines
1.9 KiB
Rust

3 years ago
use crate::input::KeyEvent;
use helix_core::unicode::width::UnicodeWidthStr;
3 years ago
use std::fmt::Write;
#[derive(Debug)]
/// Info box used in editor. Rendering logic will be in other crate.
pub struct Info {
/// Title shown at top.
pub title: String,
/// Text body, should contain newlines.
3 years ago
pub text: String,
/// Body width.
pub width: u16,
/// Body height.
pub height: u16,
}
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);
3 years ago
let keymaps_width: u16 = body
.iter()
.map(|r| r.1.iter().map(|e| e.width() as u16 + 2).sum::<u16>() - 2)
3 years ago
.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];
3 years ago
let mut left = keymaps_width - keyevent.width() as u16;
for _ in 0..lpad {
text.push(' ');
}
3 years ago
write!(text, "{}", keyevent).ok();
for keyevent in &keyevents[1..] {
3 years ago
write!(text, ", {}", keyevent).ok();
left -= 2 + keyevent.width() as u16;
}
for _ in 0..left + mpad {
3 years ago
text.push(' ');
}
let desc = desc.trim();
let w = lpad + keymaps_width + mpad + (desc.width() as u16) + rpad;
if w > width {
width = w;
3 years ago
}
writeln!(text, "{}", desc).ok();
3 years ago
}
Info {
title: title.to_string(),
3 years ago
text,
width,
height,
}
}
}