Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/info.rs')
| -rw-r--r-- | helix-view/src/info.rs | 58 |
1 files changed, 18 insertions, 40 deletions
diff --git a/helix-view/src/info.rs b/helix-view/src/info.rs index d1e90b5a..629a3112 100644 --- a/helix-view/src/info.rs +++ b/helix-view/src/info.rs @@ -1,12 +1,12 @@ -use crate::register::Registers; +use crate::input::KeyEvent; use helix_core::unicode::width::UnicodeWidthStr; -use std::{borrow::Cow, fmt::Write}; +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: Cow<'static, str>, + pub title: String, /// Text body, should contain newlines. pub text: String, /// Body width. @@ -16,55 +16,33 @@ pub struct Info { } impl Info { - pub fn new<T, K, V>(title: T, body: &[(K, V)]) -> Self - where - T: Into<Cow<'static, str>>, - K: AsRef<str>, - V: AsRef<str>, - { - let title = title.into(); - if body.is_empty() { - return Self { - height: 1, - width: title.len() as u16, - text: "".to_string(), - title, - }; - } + pub fn new(title: &str, body: Vec<(&str, Vec<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 item_width = body - .iter() - .map(|(item, _)| item.as_ref().width()) - .max() - .unwrap(); + let keymaps_width = body.iter().map(|r| r.1.len()).max().unwrap(); let mut text = String::new(); - for (item, desc) in body { + for (desc, keyevents) in &body { let _ = writeln!( text, "{:width$} {}", - item.as_ref(), - desc.as_ref(), - width = item_width + keyevents, + desc, + width = keymaps_width ); } - Self { - title, + Info { + title: title.to_string(), width: text.lines().map(|l| l.width()).max().unwrap() as u16, height: body.len() as u16, text, } } - - pub fn from_registers(title: impl Into<Cow<'static, str>>, registers: &Registers) -> Self { - let body: Vec<_> = registers - .iter_preview() - .map(|(ch, preview)| (ch.to_string(), preview)) - .collect(); - - let mut infobox = Self::new(title, &body); - infobox.width = 30; // copied content could be very long - infobox - } } |