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
//TODO: Needs Key access

//! eztui is a crate to make UI in the terminal easier.  It is super early in development but I
//! think eztui will not have a event loop and let the user create it.  Eztui will have
//! Windows: (has a buffer with a width height and a location on screen, can not hold a
//! window),
//! Labels: (Not sure),
//! Groups: (Will hold any strcut that has a Wiget trait.),
//!
use std::io::{Stdout, Write};
use crossterm::{queue, terminal, cursor, style, Result};
use buffy;

/// Base group of methods.
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);
}

/// Group Widget's together in a Group
pub struct Group {
}

/// Window holds a buffer 
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;
    }

    /// Sets size of buffer.
    fn set_size(&mut self, width: u16, height: u16) {
        self.buffer.set_size(width as usize, height as usize);
    }

    /// Drawing does not flush.
    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");
        }
    }
}

/// This is just for testing
pub fn clear(stdout: &mut Stdout) {
    queue!(stdout, terminal::Clear(terminal::ClearType::All)).expect("Clear disn't work");
}