i18n_ui 0.2.126

Terminal UI for translation progress with interactive concurrent panels and line-by-line fallback
Documentation
//! Shared TUI geometry helpers, spinner frames, zero-allocation padding, bars, and section dividers

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;

/// Braille spinner; one step per frame
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 = "░░░░░░░░";

/// Section head marker: a phosphor bar before the title
const SEC_MARK: &str = " ▍";

/// Prebuilt dashes for zero-allocation section dividers and off-bars up to 256 columns
const DASH_STR: &str = concat!(
  "────────────────────────────────────────────────────────────────",
  "────────────────────────────────────────────────────────────────",
  "────────────────────────────────────────────────────────────────",
  "────────────────────────────────────────────────────────────────",
);

/// Prebuilt spaces for zero-allocation padding up to 256 columns
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 {
  // SAFETY: `step % SPIN_LI.len()` is always `< SPIN_LI.len()`
  unsafe { SPIN_LI.get_unchecked(step % SPIN_LI.len()) }
}

/// Slice `n` bar cells from a static prebuilt bar string
#[inline]
pub(super) fn bar_str(buf: &'static str, n: usize) -> &'static str {
  &buf[..n * BAR_CHAR_LEN]
}

/// Zero-allocation dashed divider up to 256 columns
#[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))
  }
}

/// Zero-allocation space padding at common terminal widths
#[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))
  }
}

/// Row `y` slice within `area`
#[inline]
pub(super) fn row_rect(area: Rect, y: u16) -> Rect {
  Rect::new(area.x, y, area.width, 1)
}

/// Draw section header with leading phosphor marker and trailing dashed divider
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);
}