rillo/
shapes.rs

1/*
2    This Source Code Form is subject to the terms of the Mozilla Public
3    License, v. 2.0. If a copy of the MPL was not distributed with this
4    file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6    Copyright (c) 2022 Ebert Charles Matthee. All rights reserved.
7*/
8
9use crossterm::{
10    cursor,
11    style::{self, Stylize},
12    terminal, QueueableCommand,
13};
14use std::io;
15use std::io::stdout;
16
17pub struct Rectangle {
18    // TODO Color as field
19    pub origin: (u16, u16),
20    pub size_col: u16,
21    pub size_row: u16,
22    pub char_corner_top_left: char,
23    pub char_corner_top_right: char,
24    pub char_corner_bottom_left: char,
25    pub char_corner_bottom_right: char,
26    pub char_horizontal_top: char,
27    pub char_horizontal_bottom: char,
28    pub char_verticle_left: char,
29    pub char_verticle_right: char,
30    pub char_fill: char,
31}
32
33impl Rectangle {
34    pub fn draw(&self) -> io::Result<()> {
35        let mut stdout = stdout();
36
37        let col = self.size_col - 1;
38        let row = self.size_row - 1;
39
40        let term_max = match terminal::size() {
41            Ok(i) => i,
42            Err(..) => (1, 1),
43        };
44
45        // Check if drawing will on current terminal screen size
46        if (self.origin.0 + col > term_max.0) || (self.origin.1 + row > term_max.1) {
47            // TODO Error handling
48            return Ok(());
49        };
50
51        // Sides
52        for x in 1..col {
53            stdout.queue(cursor::MoveTo(self.origin.0 + x, self.origin.1))?;
54            stdout.queue(style::PrintStyledContent(
55                self.char_horizontal_top.clone().magenta(),
56            ))?;
57
58            stdout.queue(cursor::MoveTo(self.origin.0 + x, self.origin.1 + row))?;
59            stdout.queue(style::PrintStyledContent(
60                self.char_horizontal_bottom.clone().magenta(),
61            ))?;
62        }
63
64        for y in 1..row {
65            stdout.queue(cursor::MoveTo(self.origin.0, self.origin.1 + y))?;
66            stdout.queue(style::PrintStyledContent(
67                self.char_verticle_left.clone().magenta(),
68            ))?;
69
70            stdout.queue(cursor::MoveTo(self.origin.0 + col, self.origin.1 + y))?;
71            stdout.queue(style::PrintStyledContent(
72                self.char_verticle_right.clone().magenta(),
73            ))?;
74        }
75
76        // Corners
77        stdout.queue(cursor::MoveTo(self.origin.0, self.origin.1))?;
78        stdout.queue(style::PrintStyledContent(
79            self.char_corner_top_left.clone().magenta(),
80        ))?;
81
82        stdout.queue(cursor::MoveTo(self.origin.0 + col, self.origin.1))?;
83        stdout.queue(style::PrintStyledContent(
84            self.char_corner_top_right.clone().magenta(),
85        ))?;
86
87        stdout.queue(cursor::MoveTo(self.origin.0, self.origin.1 + row))?;
88        stdout.queue(style::PrintStyledContent(
89            self.char_corner_bottom_left.clone().magenta(),
90        ))?;
91
92        stdout.queue(cursor::MoveTo(self.origin.0 + col, self.origin.1 + row))?;
93        stdout.queue(style::PrintStyledContent(
94            self.char_corner_bottom_right.clone().magenta(),
95        ))?;
96
97        Ok(())
98    }
99
100    pub fn fill(&self) -> io::Result<()> {
101        todo!();
102    }
103
104    pub fn erase(&self) -> io::Result<()> {
105        todo!();
106    }
107
108    pub fn erase_fill(&self) -> io::Result<()> {
109        todo!();
110    }
111}
112
113pub struct Line {
114    start_point: (u16, u16),
115    end_point: (u16, u16),
116    char_line: char,
117}
118
119impl Line {
120    // add code here
121    pub fn draw(&self) -> io::Result<()> {
122        todo!();
123    }
124
125    pub fn erase(&self) -> io::Result<()> {
126        todo!();
127    }
128}