-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering.rs
67 lines (64 loc) · 2.04 KB
/
rendering.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
60
61
62
63
64
65
66
67
use crossterm::{
cursor::{MoveDown, MoveLeft, MoveToColumn, MoveUp},
queue,
style::{ContentStyle, PrintStyledContent, StyledContent, Stylize},
terminal,
};
use std::io::{self, Write};
pub(crate) fn render_boxed_word<T: Write, I>(mut stdout: T, word: &str, styles: I) -> io::Result<()>
where
I: Iterator<Item = ContentStyle>,
{
let x = (terminal::size()?.0 / 2) - (4 * word.len() as u16 - 1) / 2;
for ((ci, c), style) in word.char_indices().zip(styles) {
let s = &mut stdout;
let cx = x + (ci as u16 * 4);
queue!(s, MoveToColumn(cx))?;
draw_charbox(s, c, style)?;
}
queue!(stdout, MoveDown(3))
}
pub(crate) fn render_message_centered<T: Write>(
mut stdout: T,
message: StyledContent<&str>,
) -> io::Result<()> {
queue!(
stdout,
MoveToColumn(terminal::size()?.0 / 2 - message.content().len() as u16 / 2),
PrintStyledContent(message)
)
}
fn draw_charbox<T: Write>(mut stdout: T, c: char, style: ContentStyle) -> io::Result<()> {
match style {
ContentStyle {
background_color: None,
..
} => queue!(
stdout,
PrintStyledContent(style.apply("\u{250c}\u{2500}\u{2510}")),
MoveDown(1),
MoveLeft(3),
PrintStyledContent(style.apply("\u{2502} \u{2502}")),
MoveDown(1),
MoveLeft(3),
PrintStyledContent(style.apply("\u{2514}\u{2500}\u{2518}")),
MoveUp(1),
MoveLeft(2),
PrintStyledContent(style.apply(c)),
MoveUp(1),
MoveLeft(1),
),
_ => queue!(
stdout,
PrintStyledContent(style.black().negative().apply("\u{2584}\u{2584}\u{2584}")),
MoveDown(1),
MoveLeft(3),
PrintStyledContent(style.apply(format!(" {} ", c))),
MoveDown(1),
MoveLeft(3),
PrintStyledContent(style.black().negative().apply("\u{2580}\u{2580}\u{2580}")),
MoveUp(2),
MoveLeft(2),
),
}
}