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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! Textual's 3-row big digits for displaying numbers, time, and other character sequences
//! with visual emphasis. Each digit occupies 3 cells wide by 3 rows tall.
//!
//! ```
//! use tuile::prelude::*;
//! # let area = Rect::new(0, 0, 20, 3);
//! # let mut buf = Buffer::empty(area);
//! let theme = theme::current();
//! Digits::new("12:34:56").color(theme.primary).render(area, &mut buf);
//! ```
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::{Alignment, Rect};
use ratatui_core::widgets::Widget;
use crate::draw::{put, put_centered, st};
use crate::theme::{self, Rgb, Theme, Variant};
// Textual's DIGITS3X3: " 0123456789+-^x:ABCDEF$£€()"
// Each glyph is 3 rows of 3 cells (some are narrower like ":" at 1 cell)
const GLYPHS: &str = " 0123456789+-^x:ABCDEF$£€()";
const GLYPH_DATA: &[&str] = &[
// space (3x3)
" ",
" ",
" ",
// 0
"╭─╮",
"│ │",
"╰─╯",
// 1
"╶╮ ",
" │ ",
"╶┴╴",
// 2
"╶─╮",
"┌─┘",
"╰─╴",
// 3
"╶─╮",
" ─┤",
"╶─╯",
// 4
"╷ ╷",
"╰─┤",
" ╵",
// 5
"╭─╴",
"╰─╮",
"╶─╯",
// 6
"╭─╴",
"├─╮",
"╰─╯",
// 7
"╶─┐",
" │",
" ╵",
// 8
"╭─╮",
"├─┤",
"╰─╯",
// 9
"╭─╮",
"╰─┤",
"╶─╯",
// +
" ",
"╶┼╴",
" ",
// -
" ",
"╶─╴",
" ",
// ^
" ^ ",
" ",
" ",
// x
" ",
" × ",
" ",
// : (1 cell wide)
" ",
":",
" ",
// A
"╭─╮",
"├─┤",
"╵ ╵",
// B
"┌─╮",
"├─┤",
"└─╯",
// C
"╭─╮",
"│ ",
"╰─╯",
// D
"┌─╮",
"│ │",
"└─╯",
// E
"╭─╴",
"├─ ",
"╰─╴",
// F
"╭─╴",
"├─ ",
"╵ ",
// $
"╭╫╮",
"╰╫╮",
"╰╫╯",
// £
"╭─╮",
"╪═ ",
"┷━╸",
// €
"╭─╮",
"╪═ ",
"╰─╯",
// (
"╭╴ ",
"│ ",
"╰╴ ",
// )
" ╶╮",
" │",
" ╶╯",
];
/// Big 3-row digits widget (Textual `Digits` renderable).
pub struct Digits {
text: String,
color: Option<Rgb>,
variant: Option<Variant>,
align: Alignment,
bold: bool,
bg: Option<Rgb>,
theme: Option<Theme>,
}
impl Digits {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
color: None,
variant: None,
align: Alignment::Left,
bold: false,
bg: None,
theme: None,
}
}
/// Display width in cells (3 per digit, 1 for colon).
pub fn width(text: &str) -> u16 {
text.chars().map(|c| if c == ':' { 1 } else { 3 }).sum()
}
pub fn color(mut self, c: Rgb) -> Self {
self.color = Some(c);
self
}
pub fn variant(mut self, v: Variant) -> Self {
self.variant = Some(v);
self
}
pub fn align(mut self, a: Alignment) -> Self {
self.align = a;
self
}
pub fn bold(mut self, b: bool) -> Self {
self.bold = b;
self
}
pub fn bg(mut self, b: Rgb) -> Self {
self.bg = Some(b);
self
}
pub fn theme(mut self, t: &Theme) -> Self {
self.theme = Some(*t);
self
}
}
impl Widget for Digits {
fn render(self, area: Rect, buf: &mut Buffer) {
let th = self.theme.unwrap_or_else(theme::current);
if area.height < 3 {
return;
}
let fg = if let Some(c) = self.color {
c
} else if let Some(v) = self.variant {
th.variant(v)
} else {
th.primary
};
let bg = self.bg.unwrap_or(th.surface);
let mut style = st(fg, bg);
if self.bold {
style = style.add_modifier(ratatui_core::style::Modifier::BOLD);
}
let w = Self::width(&self.text);
let start_x = match self.align {
Alignment::Left => area.x,
Alignment::Center => area.x + (area.width.saturating_sub(w)) / 2,
Alignment::Right => area.x + area.width.saturating_sub(w),
};
let mut x = start_x;
for c in self.text.chars() {
if x >= area.x + area.width {
break;
}
let idx = GLYPHS.chars().position(|g| g == c);
let char_w = if c == ':' { 1 } else { 3 };
if let Some(i) = idx {
let base = i * 3;
let lines = [GLYPH_DATA[base], GLYPH_DATA[base + 1], GLYPH_DATA[base + 2]];
for (row, line) in lines.iter().enumerate() {
let y = area.y + row as u16;
if y >= area.y + area.height {
break;
}
put(buf, x, y, line, char_w, style);
}
} else {
// Fallback: center the char in a 3x3 box
let char_rect = Rect {
x,
y: area.y + 1,
width: 3,
height: 1,
};
put_centered(buf, char_rect, &c.to_string(), style);
}
x += char_w;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn digit_width() {
assert_eq!(Digits::width("123"), 9);
assert_eq!(Digits::width("12:34"), 13); // 3+3+1+3+3
assert_eq!(Digits::width(":"), 1);
}
#[test]
fn glyph_lookup() {
assert_eq!(GLYPH_DATA[3], "╭─╮"); // 0 first line
assert_eq!(GLYPH_DATA[4], "│ │"); // 0 second line
assert_eq!(GLYPH_DATA[5], "╰─╯"); // 0 third line
}
}