use std::{fmt::Write as _, iter::repeat_n, ops::Range, time::Duration};
use i18n_progress::{View, eta_text};
use itoa::Buffer;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use crate::i18n::T;
pub(crate) const ARROW: &str = " → ";
const ELLIPSIS: char = '…';
const TENTH_MS: u128 = 100;
const HALF_TENTH_MS: u128 = 50;
const DUR_W: usize = 6;
fn char_w(c: char) -> usize {
c.width().unwrap_or(0)
}
pub(crate) fn one_line(s: &str) -> String {
clean_text::one_line(s).into_owned()
}
pub(crate) fn width(s: &str) -> usize {
s.width()
}
pub(crate) fn fit(s: &str, max: usize) -> String {
if width(s) <= max {
return s.to_owned();
}
let mut out = String::new();
let mut w = 0;
for c in s.chars() {
let cw = char_w(c);
if w + cw + 1 > max {
break;
}
w += cw;
out.push(c);
}
if max > 0 {
out.push(ELLIPSIS);
}
out
}
pub(crate) fn fit_end(s: &str, max: usize) -> String {
if width(s) <= max {
return s.to_owned();
}
let mut start = s.len();
let mut w = 0;
for (i, c) in s.char_indices().rev() {
let cw = char_w(c);
if w + cw + 1 > max {
break;
}
w += cw;
start = i;
}
let tail = &s[start..];
let mut out = String::with_capacity(tail.len() + ELLIPSIS.len_utf8());
if max > 0 {
out.push(ELLIPSIS);
}
out.push_str(tail);
out
}
pub(crate) fn tail(s: &str, col: usize, row: usize) -> Vec<String> {
if col == 0 || row == 0 {
return Vec::new();
}
let budget = col * row;
let mut rev_li = Vec::new();
let mut w = 0;
let mut space = false;
for c in s.trim().chars().rev() {
if c.is_whitespace() {
if space || rev_li.is_empty() {
continue;
}
space = true;
rev_li.push(' ');
w += 1;
} else {
space = false;
rev_li.push(c);
w += char_w(c);
}
if w >= budget {
break;
}
}
if rev_li.is_empty() {
return Vec::new();
}
let mut range_li: Vec<Range<usize>> = Vec::with_capacity(row.min(rev_li.len()));
let mut start = 0;
let mut lw = 0;
for (i, &c) in rev_li.iter().enumerate() {
let cw = char_w(c);
if lw > 0 && lw + cw > col {
range_li.push(start..i);
start = i;
if range_li.len() == row {
break;
}
lw = 0;
}
lw += cw;
}
if range_li.len() < row {
range_li.push(start..rev_li.len());
}
let mut line_li: Vec<String> = range_li
.into_iter()
.rev()
.map(|r| rev_li[r].iter().rev().collect())
.collect();
if let Some(first) = line_li.first_mut() {
let trimmed = first.trim_start().len();
first.drain(..first.len() - trimmed);
if first.is_empty() {
line_li.remove(0);
}
}
line_li
}
pub(crate) fn dur(sec: f32) -> String {
let mut out = String::new();
if sec < 60.0 {
let _ = write!(out, "{sec:.1}s");
} else {
let s = sec as u64;
if s >= 3600 {
let _ = write!(out, "{}h{:02}m", s / 3600, s % 3600 / 60);
} else {
let _ = write!(out, "{}m{:02}s", s / 60, s % 60);
}
}
out
}
pub(crate) fn dur_col(sec: f32) -> String {
let mut out = dur(sec);
let w = width(&out);
if w < DUR_W {
out.extend(repeat_n(' ', DUR_W - w));
}
out
}
pub(crate) fn tenth_sec(rem: Duration, out: &mut String) {
let tenth = (rem.as_millis().saturating_add(HALF_TENTH_MS) / TENTH_MS).max(1);
let mut buf = Buffer::new();
out.push_str(buf.format(tenth / 10));
out.push('.');
out.push((b'0' + (tenth % 10) as u8) as char);
out.push('s');
}
pub(crate) fn percent(view: &View) -> String {
let mut out = String::with_capacity(6);
let mut buf = Buffer::new();
out.push_str(buf.format(view.permille / 10));
out.push('.');
out.push((b'0' + (view.permille % 10) as u8) as char);
out.push('%');
out
}
pub(crate) fn count(view: &View) -> String {
let mut out = String::with_capacity(12);
let mut buf = Buffer::new();
out.push_str(buf.format(view.done_job));
out.push('/');
out.push_str(buf.format(view.total_job));
out
}
pub(crate) fn eta(view: &View) -> String {
if view.eta.is_some_and(|eta| eta.is_zero()) {
return String::new();
}
let eta = match view.eta {
Some(eta) => eta_text(eta),
None => T.ui.estimating().to_owned(),
};
T.ui.eta(eta).into_owned()
}
pub(crate) fn summary(ok: usize, hit: usize, fail: usize, sec: f32) -> String {
T.ui.summary(ok, hit, fail, dur(sec)).into_owned()
}