aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/info.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2022-02-13 09:31:51 +0000
committerBlaž Hrastnik2022-02-13 09:31:51 +0000
commitbd549d8a20cce98e24c8653a4a86107c786cbaa3 (patch)
tree0780b58d41b6181e69023265cdb54517e2953778 /helix-view/src/info.rs
parent7ad8eaaef0b292f4be6c66298cea40d2b928e172 (diff)
parent7083b98a388b30e0b61caac9bf6ccc1d79eadf81 (diff)
Merge remote-tracking branch 'origin/master' into debug
Diffstat (limited to 'helix-view/src/info.rs')
-rw-r--r--helix-view/src/info.rs65
1 files changed, 46 insertions, 19 deletions
diff --git a/helix-view/src/info.rs b/helix-view/src/info.rs
index b5a002fa..5ad6a60c 100644
--- a/helix-view/src/info.rs
+++ b/helix-view/src/info.rs
@@ -1,5 +1,5 @@
use crate::input::KeyEvent;
-use helix_core::unicode::width::UnicodeWidthStr;
+use helix_core::{register::Registers, unicode::width::UnicodeWidthStr};
use std::{collections::BTreeSet, fmt::Write};
#[derive(Debug)]
@@ -16,33 +16,60 @@ pub struct Info {
}
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<_>>();
+ pub fn new(title: &str, body: Vec<(String, String)>) -> Self {
+ if body.is_empty() {
+ return Self {
+ title: title.to_string(),
+ height: 1,
+ width: title.len() as u16,
+ text: "".to_string(),
+ };
+ }
- let keymaps_width = body.iter().map(|r| r.1.len()).max().unwrap();
+ let item_width = body.iter().map(|(item, _)| item.width()).max().unwrap();
let mut text = String::new();
- for (desc, keyevents) in &body {
- let _ = writeln!(
- text,
- "{:width$} {}",
- keyevents,
- desc,
- width = keymaps_width
- );
+ for (item, desc) in &body {
+ let _ = writeln!(text, "{:width$} {}", item, desc, width = item_width);
}
- Info {
+ Self {
title: title.to_string(),
width: text.lines().map(|l| l.width()).max().unwrap() as u16,
height: body.len() as u16,
text,
}
}
+
+ pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
+ let body = body
+ .into_iter()
+ .map(|(desc, events)| {
+ let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
+ (events.join(", "), desc.to_string())
+ })
+ .collect();
+
+ Self::new(title, body)
+ }
+
+ pub fn from_registers(registers: &Registers) -> Self {
+ let body = registers
+ .inner()
+ .iter()
+ .map(|(ch, reg)| {
+ let content = reg
+ .read()
+ .get(0)
+ .and_then(|s| s.lines().next())
+ .map(String::from)
+ .unwrap_or_default();
+ (ch.to_string(), content)
+ })
+ .collect();
+
+ let mut infobox = Self::new("Registers", body);
+ infobox.width = 30; // copied content could be very long
+ infobox
+ }
}