i18n_ui 0.2.126

Terminal UI for translation progress with interactive concurrent panels and line-by-line fallback
Documentation
//! Events sent from the translation side to the render thread

use std::time::Duration;

use clean_text::one_line;

use crate::text::ARROW;

/// Static metadata of a single `(endpoint, token)` slot
#[derive(Debug, Clone)]
pub struct SlotMeta {
  pub host: String,
  pub nth: u16,
}

/// Live adaptive phase of a single slot
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotState {
  Idle,
  Fast,
  Probe,
  Cruise,
  Cool { wait: Duration },
  Recover,
  Dead { code: u16 },
}

/// Live telemetry row for a single slot
#[derive(Debug, Clone, Copy)]
pub struct SlotRow {
  pub slot: usize,
  pub in_flight: u16,
  pub cwnd: u16,
  pub state: SlotState,
  pub tok: u64,
}

/// Global concurrency pool metrics
#[derive(Debug, Clone, Copy, Default)]
pub struct PoolStat {
  pub in_flight: u16,
  pub cap: u16,
  pub queued: usize,
  pub tok: u64,
}

/// Task name: `"{from} → {out}"` built in one allocation and sliced at `from_n`
#[derive(Clone)]
pub(crate) struct Name {
  tag: String,
  from_n: usize,
}

impl Name {
  pub(crate) fn new(from: &str, out: &str) -> Self {
    let from = one_line(from);
    let out = one_line(out);
    let mut tag = String::with_capacity(from.len() + ARROW.len() + out.len());
    tag.push_str(&from);
    tag.push_str(ARROW);
    tag.push_str(&out);
    Self {
      tag,
      from_n: from.len(),
    }
  }

  /// Combined display label `"{from} → {out}"`
  #[inline]
  pub(crate) fn tag(&self) -> &str {
    &self.tag
  }

  /// Source language
  #[inline]
  pub(crate) fn from(&self) -> &str {
    // SAFETY: `from_n` is the byte length of `from` copied verbatim at the start of `tag`
    unsafe { self.tag.get_unchecked(..self.from_n) }
  }

  /// Target file display path (including the target language directory)
  #[inline]
  pub(crate) fn out(&self) -> &str {
    // SAFETY: `ARROW` sits immediately after `from`, so `from_n + ARROW.len()` is a valid UTF-8 boundary
    unsafe { self.tag.get_unchecked(self.from_n + ARROW.len()..) }
  }
}

pub(crate) enum Event {
  /// Source files discovered by the directory walk so far (absolute total)
  ScanFound(usize),
  /// Source files whose cache state is checked so far (absolute total) and confirmed cache hits
  ScanChecked { checked: usize, cache: usize },
  /// Plan: number of model tasks and cache hits confirmed during preflight
  Plan { job: usize, cache: usize },
  /// Register all `(endpoint, token)` slots for the pool panel
  Pool(Vec<SlotMeta>),
  /// Update a single slot row and global pool statistics
  Slot { row: SlotRow, stat: PoolStat },
  /// All cache hits, model not called
  Hit,
  /// Model work discovered during execution that the precheck did not count
  Grow,
  /// Planned but unprocessed this round (source file vanished, early failure, etc.), consumes one planned progress unit
  Skip,
  /// Local non-job failure counted toward the summary failure total
  Fail,
  /// Non-model disk/cache mutation (offline render, deleted orphan, or manual-edit sync) requiring full summary
  Change,
  /// Start calling the model (slot acquired)
  Start { id: u32, name: Name },
  /// Bind or switch the active slot serving a running task
  JobSlot { id: u32, slot: usize },
  /// Accumulate tokens consumed by a running task
  JobTok { id: u32, tok: u32 },
  /// Model streaming output; `reply` is `false` for thinking, `true` for the reply body
  Text { id: u32, text: String, reply: bool },
  /// Request failed, about to retry with another token / endpoint; `nth`/`total` are current and total rounds, `wait` is cooldown
  Retry {
    id: u32,
    nth: usize,
    total: usize,
    wait: Duration,
    base: String,
    err: String,
  },
  /// Task finished; `err` is the failure reason, `active` is effective model call time excluding retry cooldowns
  End {
    id: u32,
    err: Option<String>,
    active: Duration,
  },
  /// Informational message
  Log(String),
  /// Close the UI and print the summary
  Close,
}