on 2158366 help sign in
48 lines 1.3 kB history raw
1use crate::input::KeyEvent;
2use helix_core::unicode::width::UnicodeWidthStr;
3use std::fmt::Write;
5#[derive(Debug)]
6/// Info box used in editor. Rendering logic will be in other crate.
7pub struct Info {
8 /// Title shown at top.
9 pub title: String,
10 /// Text body, should contain newlines.
11 pub text: String,
12 /// Body width.
13 pub width: u16,
14 /// Body height.
15 pub height: u16,
18impl Info {
19 pub fn new(title: &str, body: Vec<(&str, Vec<KeyEvent>)>) -> Info {
20 let body = body
21 .into_iter()
22 .map(|(desc, events)| {
23 let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
24 (desc, events.join(", "))
25 })
26 .collect::<Vec<_>>();
28 let keymaps_width = body.iter().map(|r| r.1.len()).max().unwrap();
29 let mut text = String::new();
31 for (desc, keyevents) in &body {
32 let _ = writeln!(
33 text,
34 "{:width$} {}",
35 keyevents,
36 desc,
37 width = keymaps_width
38 );
39 }
41 Info {
42 title: title.to_string(),
43 width: text.lines().map(|l| l.width()).max().unwrap() as u16,
44 height: body.len() as u16,
45 text,
46 }
47 }