i18n_ui 0.2.126

Terminal UI for translation progress with interactive concurrent panels and line-by-line fallback
Documentation
//! Retry badge, endpoint host deduplication, countdown bar, and retry row rendering

use std::time::Instant;

use itoa::Buffer as ItoaBuffer;
use ratatui::text::{Line, Span};

use super::{
  super::{BAR_ON, COL_GAP, COL_GAP_W, DIM, FAINT, WARN, bar_str, dash_str, pad},
  MARK_RETRY, MID_MIN_W, trunk,
};
use crate::{
  board::RetryState,
  i18n::T,
  text::{fit, fit_owned, tenth_sec, width},
};

const WAIT_BAR_W: usize = 6;
const WAIT_BAR_MIN_W: usize = 68;

pub(super) fn retry_badge(r: &RetryState) -> String {
  let mut out = T.ui.retry_badge(r.nth, r.total).into_owned();
  if r.streak > 1 {
    out.push_str(" · ×");
    out.push_str(ItoaBuffer::new().format(r.streak));
  }
  out
}

/// Whether the endpoint URL only repeats `host`, already printed as the tree root
pub(in crate::tty) fn same_host(base: &str, host: &str) -> bool {
  let rest = base.split_once("://").map_or(base, |(_, rest)| rest);
  let found = rest.split_once('/').map_or(rest, |(h, _)| h);
  !found.is_empty() && found == host
}

fn countdown(r: &RetryState, now: Instant, show_bar: bool) -> (Vec<Span<'static>>, usize) {
  if r.wait.is_zero() {
    return (Vec::new(), 0);
  }
  let rem = r.rem(now);
  if rem.is_zero() {
    let reconnect = T.ui.reconnect();
    return (vec![Span::styled(reconnect, DIM)], width(reconnect));
  }
  let mut num = String::with_capacity(8);
  tenth_sec(rem, &mut num);
  let wait_suffix = T.ui.retry_after();
  let mut used = num.len() + 1 + width(wait_suffix);
  let mut span_li = Vec::with_capacity(6);
  if show_bar {
    let total_ms = r.wait.as_millis().max(1);
    let rem_ms = rem.as_millis().min(total_ms);
    let lit = ((rem_ms * WAIT_BAR_W as u128).div_ceil(total_ms) as usize).clamp(1, WAIT_BAR_W);
    span_li.push(Span::styled(bar_str(BAR_ON, lit), WARN));
    span_li.push(Span::styled(dash_str(WAIT_BAR_W - lit), FAINT));
    span_li.push(Span::raw(" "));
    used += WAIT_BAR_W + 1;
  }
  span_li.push(Span::styled(num, WARN));
  span_li.push(Span::raw(" "));
  span_li.push(Span::styled(wait_suffix, DIM));
  (span_li, used)
}

/// Retry row: badge, detail, and a right-aligned countdown; returns whether the detail fit
pub(super) fn retry_line(
  last: bool,
  badge: String,
  mid_src: &str,
  r: &RetryState,
  now: Instant,
  col: usize,
  w: usize,
) -> (Line<'static>, bool) {
  let badge_w = width(&badge);
  let (right_li, right_w) = countdown(r, now, w >= WAIT_BAR_MIN_W);
  let right_gap = if right_w == 0 { 0 } else { COL_GAP_W };
  let mut span_li = Vec::with_capacity(6 + right_li.len());
  span_li.push(trunk(last));
  span_li.push(Span::styled(MARK_RETRY, WARN));

  if !mid_src.is_empty()
    && let Some(mid_max) = col.checked_sub(badge_w + COL_GAP_W + right_gap + right_w)
    && mid_max >= MID_MIN_W
  {
    let mid = fit(mid_src, mid_max);
    let gap = mid_max - width(&mid) + right_gap;
    span_li.push(Span::styled(badge, WARN));
    span_li.push(Span::raw(COL_GAP));
    span_li.push(Span::styled(mid.into_owned(), DIM));
    span_li.push(Span::raw(pad(gap)));
    span_li.extend(right_li);
    return (Line::from(span_li), true);
  }

  if let Some(gap) = col.checked_sub(badge_w + right_w) {
    span_li.push(Span::styled(badge, WARN));
    span_li.push(Span::raw(pad(gap)));
    span_li.extend(right_li);
    return (Line::from(span_li), false);
  }

  let mut merged = badge;
  for s in &right_li {
    merged.push_str(s.content.as_ref());
  }
  span_li.push(Span::styled(fit_owned(merged, col), WARN));
  (Line::from(span_li), false)
}