Debian Codegraph @

helix-view/src/info.rs

raw symbols SCIP
1 use crate::register::Registers;
2 use helix_core::unicode::width::UnicodeWidthStr;
3 use std::{borrow::Cow, fmt::Write};
4
5 #[derive(Debug)]
6 /// Info box used in editor. Rendering logic will be in other crate.
7 pub struct Info {
8 /// Title shown at top.
9 pub title: Cow<'static, str>,
10 /// Text body, should contain newlines.
11 pub text: String,
12 /// Body width.
13 pub width: u16,
14 /// Body height.
15 pub height: u16,
16 }
17
18 impl Info {
19 pub fn new<T, K, V>(title: T, body: &[(K, V)]) -> Self
20 where
21 T: Into<Cow<'static, str>>,
22 K: AsRef<str>,
23 V: AsRef<str>,
24 {
25 let title = title.into();
26 if body.is_empty() {
27 return Self {
28 height: 1,
29 width: title.len() as u16,
30 text: "".to_string(),
31 title,
32 };
33 }
34
35 let item_width = body
36 .iter()
37 .map(|(item, _)| item.as_ref().width())
38 .max()
39 .unwrap();
40 let mut text = String::new();
41
42 for (item, desc) in body {
43 let _ = writeln!(
44 text,
45 "{:width$} {}",
46 item.as_ref(),
47 desc.as_ref(),
48 width = item_width
49 );
50 }
51
52 Self {
53 title,
54 width: text.lines().map(|l| l.width()).max().unwrap() as u16,
55 height: body.len() as u16,
56 text,
57 }
58 }
59
60 pub fn from_registers(title: impl Into<Cow<'static, str>>, registers: &Registers) -> Self {
61 let body: Vec<_> = registers
62 .iter_preview()
63 .map(|(ch, preview)| (ch.to_string(), preview))
64 .collect();
65
66 let mut infobox = Self::new(title, &body);
67 infobox.width = 30; // copied content could be very long
68 infobox
69 }
70 }