use std::fmt::Display;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Lang {
#[default]
EnUs,
PtBr,
}
pub const LANGS: [Lang; 2] = [Lang::EnUs, Lang::PtBr];
impl Lang {
pub fn code(self) -> &'static str {
match self {
Lang::EnUs => "en-US",
Lang::PtBr => "pt-BR",
}
}
pub fn name(self) -> &'static str {
match self {
Lang::EnUs => "English",
Lang::PtBr => "Português",
}
}
pub fn parse(text: &str) -> Option<Lang> {
let base = text.split(['.', '@']).next().unwrap_or("");
let normalized = base.replace('_', "-").to_ascii_lowercase();
match normalized.split('-').next().unwrap_or("") {
"pt" => Some(Lang::PtBr),
"en" => Some(Lang::EnUs),
_ => None,
}
}
pub fn next(self) -> Lang {
match self {
Lang::EnUs => Lang::PtBr,
Lang::PtBr => Lang::EnUs,
}
}
pub fn is_one(self, n: u64) -> bool {
match self {
Lang::EnUs => n == 1,
Lang::PtBr => n <= 1,
}
}
}
pub fn detect(
flag: Option<&str>,
env: &dyn Fn(&str) -> Option<String>,
saved: Option<&str>,
system: Option<&str>,
) -> Lang {
let non_empty = |name: &str| env(name).filter(|v| !v.is_empty());
let candidates = [
flag.map(str::to_owned),
non_empty("TABA_CLI_LANG"),
saved.map(str::to_owned),
non_empty("LC_ALL"),
non_empty("LC_MESSAGES"),
non_empty("LANG"),
system.map(str::to_owned),
];
candidates
.into_iter()
.flatten()
.find_map(|c| Lang::parse(&c))
.unwrap_or_default()
}
pub fn tr(lang: Lang, key: &str) -> String {
let text = crate::_rust_i18n_translate(lang.code(), key).into_owned();
let missing = |code: &str| text == format!("{code}.{key}");
if missing(lang.code()) || missing(Lang::EnUs.code()) {
key.to_owned()
} else {
text
}
}
pub fn tr_with(lang: Lang, key: &str, args: &[(&str, &dyn Display)]) -> String {
let mut text = tr(lang, key);
for (name, value) in args {
text = text.replace(&format!("%{{{name}}}"), &value.to_string());
}
text
}
pub fn tr_n(lang: Lang, key: &str, n: u64) -> String {
let form = if lang.is_one(n) { "one" } else { "other" };
tr_with(lang, &format!("{key}.{form}"), &[("n", &group(lang, n))])
}
pub fn variables(text: &str) -> Vec<String> {
let mut found = Vec::new();
let mut rest = text;
while let Some(start) = rest.find("%{") {
let after = &rest[start + 2..];
let Some(end) = after.find('}') else { break };
found.push(after[..end].to_owned());
rest = &after[end + 1..];
}
found.sort();
found.dedup();
found
}
pub fn group(lang: Lang, n: u64) -> String {
let digits = n.to_string();
let separator = match lang {
Lang::EnUs => ',',
Lang::PtBr => '.',
};
let mut out = String::new();
for (i, c) in digits.chars().enumerate() {
if i > 0 && (digits.len() - i).is_multiple_of(3) {
out.push(separator);
}
out.push(c);
}
out
}
pub fn decimal(lang: Lang, value: f64, places: usize) -> String {
let text = format!("{value:.places$}");
match lang {
Lang::EnUs => text,
Lang::PtBr => text.replace('.', ","),
}
}
pub fn bytes(lang: Lang, n: u64) -> String {
const UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
let mut value = n as f64;
let mut unit = 0;
while value >= 1024.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
if unit == 0 {
format!("{n} B")
} else {
format!("{} {}", decimal(lang, value, 1), UNITS[unit])
}
}
pub fn duration(secs: u64) -> String {
let (d, h, m, s) = (secs / 86_400, secs / 3600 % 24, secs / 60 % 60, secs % 60);
if d > 0 {
format!("{d}d {h:02}h")
} else if h > 0 {
format!("{h}h {m:02}m")
} else if m > 0 {
format!("{m}m {s:02}s")
} else {
format!("{s}s")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Civil {
pub year: i64,
pub month: u32,
pub day: u32,
pub hour: u32,
pub minute: u32,
pub second: u32,
}
fn days_from_civil(year: i64, month: u32, day: u32) -> i64 {
let y = if month <= 2 { year - 1 } else { year };
let era = y.div_euclid(400);
let yoe = y.rem_euclid(400);
let mp = (i64::from(month) + 9) % 12;
let doy = (153 * mp + 2) / 5 + i64::from(day) - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
era * 146_097 + doe - 719_468
}
pub fn civil_from_ms(ms: i64) -> Civil {
let secs = ms.div_euclid(1000);
let days = secs.div_euclid(86_400);
let rem = secs.rem_euclid(86_400);
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
let year = yoe + era * 400 + i64::from(month <= 2);
Civil {
year,
month,
day,
hour: (rem / 3600) as u32,
minute: (rem / 60 % 60) as u32,
second: (rem % 60) as u32,
}
}
pub fn parse_instant(text: &str) -> Option<i64> {
let (date, time) = text.split_once(['T', 't', ' '])?;
let mut d = date.split('-');
let (y, m, day) = (
d.next()?.parse::<i64>().ok()?,
d.next()?.parse::<u32>().ok()?,
d.next()?.parse::<u32>().ok()?,
);
if !(1..=12).contains(&m) || !(1..=31).contains(&day) {
return None;
}
let split = time.find(['Z', 'z', '+', '-']).unwrap_or(time.len());
let (clock, zone) = time.split_at(split);
let mut c = clock.split(':');
let hour = c.next()?.parse::<i64>().ok()?;
let minute = c.next()?.parse::<i64>().ok()?;
let seconds = c.next().unwrap_or("0");
let (whole, fraction) = seconds.split_once('.').unwrap_or((seconds, ""));
let second = whole.parse::<i64>().ok()?;
let millis = format!("{fraction:0<3}")[..3].parse::<i64>().ok()?;
let offset = match zone.chars().next() {
Some(sign @ ('+' | '-')) => {
let (h, m) = zone[1..].split_once(':').unwrap_or((&zone[1..], "0"));
let minutes = h.parse::<i64>().ok()? * 60 + m.parse::<i64>().ok()?;
if sign == '+' {
minutes
} else {
-minutes
}
}
_ => 0,
};
let days = days_from_civil(y, m, day);
Some(((days * 86_400 + hour * 3600 + minute * 60 + second - offset * 60) * 1000) + millis)
}
pub fn date_time(lang: Lang, ms: i64) -> String {
let c = civil_from_ms(ms);
let clock = format!("{:02}:{:02}:{:02}", c.hour, c.minute, c.second);
match lang {
Lang::EnUs => format!("{:04}-{:02}-{:02} {clock}", c.year, c.month, c.day),
Lang::PtBr => format!("{:02}/{:02}/{:04} {clock}", c.day, c.month, c.year),
}
}
pub fn clock(ms: i64) -> String {
let c = civil_from_ms(ms);
format!("{:02}:{:02}:{:02}", c.hour, c.minute, c.second)
}
#[cfg(test)]
mod tests;