rust_hdl/docs/vcd2svg/
text_frame.rs

1// Inspired heavily by dwfv
2
3use crate::docs::vcd2svg::symbols;
4
5#[derive(Debug, Clone, Default)]
6pub struct TextFrame {
7    buffers: Vec<Vec<char>>,
8    columns: usize,
9}
10
11impl TextFrame {
12    pub fn new(columns: usize) -> Self {
13        Self {
14            buffers: vec![],
15            columns,
16        }
17    }
18    pub fn row(&mut self, row: usize) -> &mut [char] {
19        if row < self.buffers.len() {
20            &mut self.buffers[row]
21        } else {
22            while self.buffers.len() <= row {
23                self.buffers.push(vec![' '; self.columns])
24            }
25            &mut self.buffers[row]
26        }
27    }
28    pub fn write(&mut self, row: usize, col: usize, msg: &str) {
29        for (ndx, char) in msg.chars().enumerate() {
30            if ndx + col < self.columns {
31                self.row(row)[ndx + col] = char;
32            }
33        }
34    }
35    pub fn put(&mut self, row: usize, col: usize, x: char) {
36        if col < self.columns {
37            self.row(row)[col] = x;
38        }
39    }
40}
41
42impl ToString for TextFrame {
43    fn to_string(&self) -> String {
44        self.buffers
45            .iter()
46            .map(|x| x.iter().collect::<String>())
47            .collect::<Vec<_>>()
48            .join("\n")
49    }
50}
51
52#[test]
53fn test_frame_draw_stuff() {
54    let mut x = TextFrame::new(16);
55    x.write(0, 0, "hello world!");
56    assert_eq!(x.to_string(), "hello world!    ");
57    x.write(1, 0, "foo!");
58    assert_eq!(x.to_string(), "hello world!    \nfoo!            ");
59    x.put(1, 1, symbols::HORIZONTAL);
60    assert_eq!(x.to_string(), "hello world!    \nf─o!            ");
61}