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

89 lines
2.4 KiB
Rust

3 years ago
use crate::input::KeyEvent;
use helix_core::{register::Registers, 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<T, U>(title: &str, body: &[(T, U)]) -> Self
where
T: AsRef<str>,
U: AsRef<str>,
{
if body.is_empty() {
return Self {
title: title.to_string(),
height: 1,
width: title.len() as u16,
text: "".to_string(),
};
}
let item_width = body
.iter()
.map(|(item, _)| item.as_ref().width())
.max()
.unwrap();
3 years ago
let mut text = String::new();
for (item, desc) in body {
let _ = writeln!(
text,
"{:width$} {}",
item.as_ref(),
desc.as_ref(),
width = item_width
);
3 years ago
}
Self {
title: title.to_string(),
width: text.lines().map(|l| l.width()).max().unwrap() as u16,
height: body.len() as u16,
3 years ago
text,
}
}
pub fn from_keymap(title: &str, body: Vec<(BTreeSet<KeyEvent>, &str)>) -> Self {
let body: Vec<_> = body
.into_iter()
.map(|(events, desc)| {
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
.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("Registers", &body);
infobox.width = 30; // copied content could be very long
infobox
}
3 years ago
}