use super::manager::WindowManager;
use crate::rendering::{Cell, Theme, VideoBuffer};
const ASCII_DIGITS: [&[&str]; 10] = [
&[
" ",
" ",
" ",
" ",
" ",
" ",
],
&[
" \u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" ",
],
&[
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" ",
],
&[
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" ",
],
&[
"\u{2588}\u{2588} \u{2588}\u{2588} ",
"\u{2588}\u{2588} \u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" ",
],
&[
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" ",
],
&[
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} ",
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" ",
],
&[
"\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" ",
],
&[
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" ",
],
&[
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
"\u{2588}\u{2588} \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" \u{2588}\u{2588} ",
" \u{2588}\u{2588}\u{2588}\u{2588}\u{2588} ",
" ",
],
];
const MIN_WIDTH_FOR_ASCII: u16 = 12;
const MIN_HEIGHT_FOR_ASCII: u16 = 10;
fn extract_number_from_title(title: &str) -> Option<usize> {
if let Some(rest) = title.strip_prefix("Terminal ") {
let num_str: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
num_str.parse().ok()
} else {
None
}
}
pub fn render_window_numbers(
buffer: &mut VideoBuffer,
window_manager: &WindowManager,
theme: &Theme,
) {
let positions = window_manager.get_window_positions();
for (_, x, y, width, height, is_minimized, title) in &positions {
if *is_minimized {
continue;
}
let number = match extract_number_from_title(title) {
Some(n) if (1..=9).contains(&n) => n,
_ => continue, };
if *width >= MIN_WIDTH_FOR_ASCII && *height >= MIN_HEIGHT_FOR_ASCII {
render_ascii_number(buffer, *x, *y, *width, *height, number, theme);
} else {
render_single_digit(buffer, *x, *y, *width, *height, number, theme);
}
}
}
fn render_ascii_number(
buffer: &mut VideoBuffer,
win_x: u16,
win_y: u16,
win_width: u16,
win_height: u16,
number: usize,
theme: &Theme,
) {
let digit = &ASCII_DIGITS[number];
let digit_width = 9u16;
let digit_height = 6u16;
let content_x = win_x + 1;
let content_y = win_y + 1; let content_width = win_width.saturating_sub(2); let content_height = win_height.saturating_sub(2);
let start_x = content_x + content_width.saturating_sub(digit_width) / 2;
let start_y = content_y + content_height.saturating_sub(digit_height) / 2;
let fg = theme.overlay_number_fg;
let bg = theme.overlay_number_bg;
for (row_idx, row) in digit.iter().enumerate() {
for (col_idx, ch) in row.chars().enumerate() {
let x = start_x + col_idx as u16;
let y = start_y + row_idx as u16;
if x >= content_x
&& x < content_x + content_width
&& y >= content_y
&& y < content_y + content_height
{
buffer.set(x, y, Cell::new_unchecked(ch, fg, bg));
}
}
}
}
fn render_single_digit(
buffer: &mut VideoBuffer,
win_x: u16,
win_y: u16,
win_width: u16,
win_height: u16,
number: usize,
theme: &Theme,
) {
let content_x = win_x + 1;
let content_y = win_y + 1;
let content_width = win_width.saturating_sub(2);
let content_height = win_height.saturating_sub(2);
let center_x = content_x + content_width / 2;
let center_y = content_y + content_height / 2;
let fg = theme.overlay_number_fg;
let bg = theme.overlay_number_bg;
let digit_char = char::from_digit(number as u32, 10).unwrap_or('?');
buffer.set(center_x, center_y, Cell::new_unchecked(digit_char, fg, bg));
}