-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ratatui.rs
59 lines (55 loc) · 2.27 KB
/
ratatui.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Example demonstrating integration with the `ratatui` crate.
use std::io::{self, stdout};
use catppuccin::PALETTE;
use ratatui::{
backend::CrosstermBackend,
layout::Rect,
style::Stylize as _,
text::{Line, Span},
widgets::{Paragraph, Widget},
Terminal, TerminalOptions, Viewport,
};
fn main() -> io::Result<()> {
let mut terminal = Terminal::with_options(
CrosstermBackend::new(stdout()),
TerminalOptions {
viewport: Viewport::Inline(0),
},
)?;
for flavor in &PALETTE {
terminal.insert_before(8, |buf| {
let analogous: Vec<Span> = flavor
.colors
.into_iter()
.filter(|c| c.accent)
.map(|c| "██".fg(*c)) // fg accepts any type that implements Into<Color>
.collect::<Vec<Span>>();
let monochromatic: Vec<Span> = flavor
.colors
.into_iter()
.filter(|c| !c.accent)
.map(|c| "██".fg(*c)) // fg accepts any type that implements Into<Color>
.collect();
let ansi_normals: Vec<Span> = flavor
.ansi_colors
.into_iter()
.filter(|c| c.code < 8)
.map(|c| "██".fg(*c)) // fg accepts any type that implements Into<Color>
.collect::<Vec<Span>>();
let ansi_brights: Vec<Span> = flavor
.ansi_colors
.into_iter()
.filter(|c| c.code >= 8)
.map(|c| "██".fg(*c)) // fg accepts any type that implements Into<Color>
.collect::<Vec<Span>>();
let width = buf.area.width;
Paragraph::new(flavor.name.to_string()).render(Rect::new(0, 0, width, 1), buf);
Paragraph::new(Line::from(analogous)).render(Rect::new(0, 1, width, 1), buf);
Paragraph::new(Line::from(monochromatic)).render(Rect::new(0, 2, width, 1), buf);
Paragraph::new(format!("{} ANSI", flavor.name)).render(Rect::new(0, 4, width, 1), buf);
Paragraph::new(Line::from(ansi_normals)).render(Rect::new(0, 5, width, 1), buf);
Paragraph::new(Line::from(ansi_brights)).render(Rect::new(0, 6, width, 1), buf);
})?;
}
Ok(())
}