i18n_ui 0.2.126

Terminal UI for translation progress with interactive concurrent panels and line-by-line fallback
Documentation
//! Slot pool state and snapshot updates for [`Board`]

use std::time::{Duration, Instant};

use super::Board;
use crate::{
  event::{PoolStat, SlotMeta, SlotRow, SlotState},
  text::slot_tag,
};

/// Single `(endpoint, token)` slot entry on the UI board
pub(crate) struct SlotEntry {
  /// `host #nth`, shown by the pool panel and the completed feed
  pub(crate) label: String,
  /// Byte length of the host prefix inside `label`
  pub(crate) host_n: usize,
  /// First slot index sharing this host: the tree rank of its running tasks
  pub(crate) root: usize,
  pub(crate) in_flight: u16,
  pub(crate) cwnd: u16,
  pub(crate) state: SlotState,
  pub(crate) cool_at: Option<Instant>,
  pub(crate) tok: u64,
}

impl SlotEntry {
  /// Live remaining cooldown for countdown rendering
  pub(crate) fn cool_rem(&self, now: Instant) -> Option<Duration> {
    let SlotState::Cool { wait } = self.state else {
      return None;
    };
    let at = self.cool_at?;
    let rem = wait.saturating_sub(now.saturating_duration_since(at));
    (!rem.is_zero()).then_some(rem)
  }

  /// Endpoint host, the tree root that groups running tasks
  pub(crate) fn host(&self) -> &str {
    // SAFETY: `host_n` is the byte length of the host copied verbatim at the head of `label`
    unsafe { self.label.get_unchecked(..self.host_n) }
  }

  /// Token ordinal tag `#nth`, the part of `label` after the host and its space
  pub(crate) fn tag(&self) -> &str {
    // SAFETY: `label` is `host #nth`, so `host_n + 1` is the ASCII `#` boundary
    unsafe { self.label.get_unchecked(self.host_n + 1..) }
  }
}

impl Board {
  pub(super) fn init_pool(&mut self, meta_li: Vec<SlotMeta>) {
    self.slot_li = Vec::with_capacity(meta_li.len());
    for meta in meta_li {
      let root = self
        .slot_li
        .iter()
        .position(|s| s.host() == meta.host)
        .unwrap_or(self.slot_li.len());
      self.slot_li.push(SlotEntry {
        label: slot_tag(&meta.host, meta.nth),
        host_n: meta.host.len(),
        root,
        in_flight: 0,
        cwnd: 1,
        state: SlotState::Idle,
        cool_at: None,
        tok: 0,
      });
    }
    if !self.slot_li.is_empty() && self.pool_stat.cap == 0 {
      self.pool_stat.cap = self.slot_li.len().min(usize::from(u16::MAX)) as u16;
      self.progress.set_concurrency(self.pool_stat.cap);
    }
  }

  pub(super) fn on_slot(&mut self, row: SlotRow, stat: PoolStat) {
    if let Some(entry) = self.slot_li.get_mut(row.slot) {
      entry.in_flight = row.in_flight;
      entry.cwnd = row.cwnd;
      entry.state = row.state;
      entry.cool_at = match row.state {
        SlotState::Cool { wait } if !wait.is_zero() => Some(Instant::now()),
        _ => None,
      };
      entry.tok = row.tok;
    }
    self.pool_stat = stat;
    self.progress.set_concurrency(stat.cap.max(1));
  }

  pub(crate) fn slot(&self, slot: Option<usize>) -> Option<&SlotEntry> {
    slot.and_then(|i| self.slot_li.get(i))
  }
}