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/helix-view/src/info.rs

49 lines
1.3 KiB
Rust

3 years ago
use crate::input::KeyEvent;
use helix_core::unicode::width::UnicodeWidthStr;
use std::{collections::BTreeSet, fmt::Write};
3 years ago
#[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 {
pub fn new(title: &str, body: Vec<(&str, BTreeSet<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();
3 years ago
let mut text = String::new();
for (desc, keyevents) in &body {
let _ = writeln!(
text,
"{:width$} {}",
keyevents,
desc,
width = keymaps_width
);
3 years ago
}
3 years ago
Info {
title: title.to_string(),
width: text.lines().map(|l| l.width()).max().unwrap() as u16,
height: body.len() as u16,
3 years ago
text,
}
}
}