use std::borrow::Cow;
use ratatui::{
buffer::Buffer,
layout::Rect,
text::{Line, Span},
widgets::Widget,
};
use super::fx::{ACCENT, BOLD, FAINT};
use crate::text::{fit, width};
pub(super) const COL_GAP: &str = " ";
pub(super) const COL_GAP_W: usize = COL_GAP.len();
pub(super) const RIGHT_PAD: usize = 1;
pub(super) const SPIN_LI: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
pub(super) const BAR_CHAR_LEN: usize = '━'.len_utf8();
pub(super) const BAR_ON: &str = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━";
pub(super) const BAR_DOT: &str = "░░░░░░░░";
const SEC_MARK: &str = " ▍";
const DASH_STR: &str = concat!(
"────────────────────────────────────────────────────────────────",
"────────────────────────────────────────────────────────────────",
"────────────────────────────────────────────────────────────────",
"────────────────────────────────────────────────────────────────",
);
const SPACE_STR: &str = concat!(
" ",
" ",
" ",
" ",
);
const _: () = {
assert!('━'.len_utf8() == '─'.len_utf8());
assert!('━'.len_utf8() == '░'.len_utf8());
assert!(BAR_ON.len() == 32 * BAR_CHAR_LEN);
assert!(BAR_DOT.len() == 8 * BAR_CHAR_LEN);
assert!(DASH_STR.len() == 256 * BAR_CHAR_LEN);
assert!(SPACE_STR.len() == 256);
};
#[inline]
pub(super) fn spin_at(step: usize) -> &'static str {
unsafe { SPIN_LI.get_unchecked(step % SPIN_LI.len()) }
}
#[inline]
pub(super) fn bar_str(buf: &'static str, n: usize) -> &'static str {
&buf[..n * BAR_CHAR_LEN]
}
#[inline]
pub(super) fn dash_str(n: usize) -> Cow<'static, str> {
if n <= 256 {
Cow::Borrowed(bar_str(DASH_STR, n))
} else {
Cow::Owned("─".repeat(n))
}
}
#[inline]
pub(super) fn pad(n: usize) -> Cow<'static, str> {
if n <= SPACE_STR.len() {
Cow::Borrowed(&SPACE_STR[..n])
} else {
Cow::Owned(" ".repeat(n))
}
}
#[inline]
pub(super) fn row_rect(area: Rect, y: u16) -> Rect {
Rect::new(area.x, y, area.width, 1)
}
pub(super) fn draw_sec_head(area: Rect, buf: &mut Buffer, title: &str, w: usize) {
let inner = w.saturating_sub(2);
if inner == 0 {
return;
}
let title_fit = fit(title, inner.saturating_sub(4));
let used = width(SEC_MARK) + width(&title_fit) + 1;
let fill = w.saturating_sub(used + RIGHT_PAD);
Line::from(vec![
Span::styled(SEC_MARK, ACCENT),
Span::styled(title_fit, BOLD),
Span::raw(" "),
Span::styled(dash_str(fill), FAINT),
])
.render(area, buf);
}