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.rs48
1 files changed, 33 insertions, 15 deletions
diff --git a/helix-view/src/info.rs b/helix-view/src/info.rs
index d1e90b5a..3080cf8e 100644
--- a/helix-view/src/info.rs
+++ b/helix-view/src/info.rs
@@ -1,12 +1,12 @@
-use crate::register::Registers;
-use helix_core::unicode::width::UnicodeWidthStr;
-use std::{borrow::Cow, fmt::Write};
+use crate::input::KeyEvent;
+use helix_core::{register::Registers, unicode::width::UnicodeWidthStr};
+use std::{collections::BTreeSet, 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,19 +16,17 @@ pub struct Info {
}
impl Info {
- pub fn new<T, K, V>(title: T, body: &[(K, V)]) -> Self
+ pub fn new<T, U>(title: &str, body: &[(T, U)]) -> Self
where
- T: Into<Cow<'static, str>>,
- K: AsRef<str>,
- V: AsRef<str>,
+ T: AsRef<str>,
+ U: AsRef<str>,
{
- let title = title.into();
if body.is_empty() {
return Self {
+ title: title.to_string(),
height: 1,
width: title.len() as u16,
text: "".to_string(),
- title,
};
}
@@ -50,20 +48,40 @@ impl Info {
}
Self {
- title,
+ 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 {
+ pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
+ let body: Vec<_> = body
+ .into_iter()
+ .map(|(desc, events)| {
+ let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
+ (events.join(", "), desc)
+ })
+ .collect();
+
+ Self::new(title, &body)
+ }
+
+ pub fn from_registers(registers: &Registers) -> Self {
let body: Vec<_> = registers
- .iter_preview()
- .map(|(ch, preview)| (ch.to_string(), preview))
+ .inner()
+ .iter()
+ .map(|(ch, reg)| {
+ let content = reg
+ .read()
+ .get(0)
+ .and_then(|s| s.lines().next())
+ .unwrap_or_default();
+ (ch.to_string(), content)
+ })
.collect();
- let mut infobox = Self::new(title, &body);
+ let mut infobox = Self::new("Registers", &body);
infobox.width = 30; // copied content could be very long
infobox
}