2021-06-19 17:54:37 +02:00
|
|
|
use crate::input::KeyEvent;
|
2021-11-04 19:33:31 +01:00
|
|
|
use helix_core::{register::Registers, unicode::width::UnicodeWidthStr};
|
2021-11-01 15:50:12 +01:00
|
|
|
use std::{collections::BTreeSet, fmt::Write};
|
2021-06-19 17:54:37 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// Info box used in editor. Rendering logic will be in other crate.
|
|
|
|
pub struct Info {
|
2021-07-26 18:07:13 +02:00
|
|
|
/// Title shown at top.
|
|
|
|
pub title: String,
|
|
|
|
/// Text body, should contain newlines.
|
2021-06-19 17:54:37 +02:00
|
|
|
pub text: String,
|
|
|
|
/// Body width.
|
|
|
|
pub width: u16,
|
|
|
|
/// Body height.
|
|
|
|
pub height: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Info {
|
2021-11-04 19:33:31 +01:00
|
|
|
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(),
|
|
|
|
};
|
|
|
|
}
|
2021-08-17 02:25:48 +02:00
|
|
|
|
2021-11-04 19:33:31 +01:00
|
|
|
let item_width = body.iter().map(|(item, _)| item.width()).max().unwrap();
|
2021-06-19 17:54:37 +02:00
|
|
|
let mut text = String::new();
|
2021-08-17 02:25:48 +02:00
|
|
|
|
2021-11-04 19:33:31 +01:00
|
|
|
for (item, desc) in &body {
|
|
|
|
let _ = writeln!(text, "{:width$} {}", item, desc, width = item_width);
|
2021-06-19 17:54:37 +02:00
|
|
|
}
|
2021-08-17 02:25:48 +02:00
|
|
|
|
2022-01-14 04:25:59 +01:00
|
|
|
Self {
|
2021-07-26 18:07:13 +02:00
|
|
|
title: title.to_string(),
|
2021-08-17 02:25:48 +02:00
|
|
|
width: text.lines().map(|l| l.width()).max().unwrap() as u16,
|
|
|
|
height: body.len() as u16,
|
2021-06-19 17:54:37 +02:00
|
|
|
text,
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 19:33:31 +01:00
|
|
|
|
|
|
|
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)| {
|
2022-02-05 07:35:51 +01:00
|
|
|
let content = reg
|
|
|
|
.read()
|
|
|
|
.get(0)
|
|
|
|
.and_then(|s| s.lines().next())
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_default();
|
2021-11-04 19:33:31 +01:00
|
|
|
(ch.to_string(), content)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut infobox = Self::new("Registers", body);
|
|
|
|
infobox.width = 30; // copied content could be very long
|
|
|
|
infobox
|
|
|
|
}
|
2021-06-19 17:54:37 +02:00
|
|
|
}
|