bracket_terminal/consoles/text/
gui_helpers.rs

1use crate::prelude::{to_cp437, Console};
2use bracket_color::prelude::RGBA;
3
4/// Draws a box, starting at x/y with the extents width/height using CP437 line characters
5pub fn draw_box(
6    console: &mut dyn Console,
7    sx: i32,
8    sy: i32,
9    width: i32,
10    height: i32,
11    fg: RGBA,
12    bg: RGBA,
13) {
14    for y in sy..sy + height {
15        for x in sx..sx + width {
16            console.set(
17                x,
18                y,
19                RGBA::from_f32(1.0, 1.0, 1.0, fg.a),
20                RGBA::from_f32(0.0, 0.0, 0.0, fg.a),
21                32,
22            );
23        }
24    }
25
26    console.set(sx, sy, fg, bg, to_cp437('┌'));
27    console.set(sx + width, sy, fg, bg, to_cp437('┐'));
28    console.set(sx, sy + height, fg, bg, to_cp437('└'));
29    console.set(sx + width, sy + height, fg, bg, to_cp437('┘'));
30    for x in sx + 1..sx + width {
31        console.set(x, sy, fg, bg, to_cp437('─'));
32        console.set(x, sy + height, fg, bg, to_cp437('─'));
33    }
34    for y in sy + 1..sy + height {
35        console.set(sx, y, fg, bg, to_cp437('│'));
36        console.set(sx + width, y, fg, bg, to_cp437('│'));
37    }
38}
39
40/// Draw a single-lined box without filling in the middle
41pub fn draw_hollow_box(
42    console: &mut dyn Console,
43    sx: i32,
44    sy: i32,
45    width: i32,
46    height: i32,
47    fg: RGBA,
48    bg: RGBA,
49) {
50    console.set(sx, sy, fg, bg, to_cp437('┌'));
51    console.set(sx + width, sy, fg, bg, to_cp437('┐'));
52    console.set(sx, sy + height, fg, bg, to_cp437('└'));
53    console.set(sx + width, sy + height, fg, bg, to_cp437('┘'));
54    for x in sx + 1..sx + width {
55        console.set(x, sy, fg, bg, to_cp437('─'));
56        console.set(x, sy + height, fg, bg, to_cp437('─'));
57    }
58    for y in sy + 1..sy + height {
59        console.set(sx, y, fg, bg, to_cp437('│'));
60        console.set(sx + width, y, fg, bg, to_cp437('│'));
61    }
62}
63
64/// Draws a box, starting at x/y with the extents width/height using CP437 line characters
65pub fn draw_box_double(
66    console: &mut dyn Console,
67    sx: i32,
68    sy: i32,
69    width: i32,
70    height: i32,
71    fg: RGBA,
72    bg: RGBA,
73) {
74    for y in sy..sy + height {
75        for x in sx..sx + width {
76            console.set(
77                x,
78                y,
79                RGBA::from_f32(1.0, 1.0, 1.0, fg.a),
80                RGBA::from_f32(0.0, 0.0, 0.0, fg.a),
81                32,
82            );
83        }
84    }
85
86    console.set(sx, sy, fg, bg, to_cp437('╔'));
87    console.set(sx + width, sy, fg, bg, to_cp437('╗'));
88    console.set(sx, sy + height, fg, bg, to_cp437('╚'));
89    console.set(sx + width, sy + height, fg, bg, to_cp437('╝'));
90    for x in sx + 1..sx + width {
91        console.set(x, sy, fg, bg, to_cp437('═'));
92        console.set(x, sy + height, fg, bg, to_cp437('═'));
93    }
94    for y in sy + 1..sy + height {
95        console.set(sx, y, fg, bg, to_cp437('║'));
96        console.set(sx + width, y, fg, bg, to_cp437('║'));
97    }
98}
99
100/// Draws a box, starting at x/y with the extents width/height using CP437 line characters
101pub fn draw_hollow_box_double(
102    console: &mut dyn Console,
103    sx: i32,
104    sy: i32,
105    width: i32,
106    height: i32,
107    fg: RGBA,
108    bg: RGBA,
109) {
110    console.set(sx, sy, fg, bg, to_cp437('╔'));
111    console.set(sx + width, sy, fg, bg, to_cp437('╗'));
112    console.set(sx, sy + height, fg, bg, to_cp437('╚'));
113    console.set(sx + width, sy + height, fg, bg, to_cp437('╝'));
114    for x in sx + 1..sx + width {
115        console.set(x, sy, fg, bg, to_cp437('═'));
116        console.set(x, sy + height, fg, bg, to_cp437('═'));
117    }
118    for y in sy + 1..sy + height {
119        console.set(sx, y, fg, bg, to_cp437('║'));
120        console.set(sx + width, y, fg, bg, to_cp437('║'));
121    }
122}
123
124/// Draws a horizontal progress bar
125#[allow(clippy::too_many_arguments)]
126pub fn draw_bar_horizontal(
127    console: &mut dyn Console,
128    sx: i32,
129    sy: i32,
130    width: i32,
131    n: i32,
132    max: i32,
133    fg: RGBA,
134    bg: RGBA,
135) {
136    let percent = n as f32 / max as f32;
137    let fill_width = (percent * width as f32) as i32;
138    for x in 0..width {
139        if x <= fill_width {
140            console.set(sx + x, sy, fg, bg, to_cp437('▓'));
141        } else {
142            console.set(sx + x, sy, fg, bg, to_cp437('░'));
143        }
144    }
145}
146
147/// Draws a vertical progress bar
148#[allow(clippy::too_many_arguments)]
149pub fn draw_bar_vertical(
150    console: &mut dyn Console,
151    sx: i32,
152    sy: i32,
153    height: i32,
154    n: i32,
155    max: i32,
156    fg: RGBA,
157    bg: RGBA,
158) {
159    let percent = n as f32 / max as f32;
160    let fill_height = height - ((percent * height as f32) as i32);
161    for y in 0..height {
162        if y >= fill_height {
163            console.set(sx, sy + y, fg, bg, to_cp437('▓'));
164        } else {
165            console.set(sx, sy + y, fg, bg, to_cp437('░'));
166        }
167    }
168}