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
68
69
70
71
72
use std::io::{Stdout, Write};
use crossterm::{queue, terminal, cursor, style, Result};
use buffy;
pub trait Wiget {
fn new(x: u16, y: u16, width: u16, height: u16) -> Self;
fn set_text(&mut self, location: u16, text: &str);
fn set_pos(&mut self, x: u16, y: u16);
fn set_size(&mut self, width: u16, height: u16);
fn draw(&self, stdout: &mut Stdout);
}
pub struct Group {
}
pub struct Window {
x: u16,
y: u16,
buffer: buffy::Buffer,
}
impl Wiget for Window {
fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
Self {
x, y, buffer: buffy::Buffer::new(width as usize, height as usize, ' '),
}
}
fn set_text(&mut self, location: u16, text: &str) {
self.buffer.insert_from_str(location as usize, text);
}
fn set_pos(&mut self, x: u16, y: u16) {
self.x = x;
self.y = y;
}
fn set_size(&mut self, width: u16, height: u16) {
self.buffer.set_size(width as usize, height as usize);
}
fn draw(&self, stdout: &mut Stdout) {
for (idx, line) in self.buffer.get_lines().iter().enumerate() {
queue!(
stdout,
cursor::MoveTo(self.x, self.y + idx as u16),
style::Print(line.to_string()),
).expect("Draw Error");
}
}
}
pub fn clear(stdout: &mut Stdout) {
queue!(stdout, terminal::Clear(terminal::ClearType::All)).expect("Clear disn't work");
}