diff --git a/crates/squid-tui/src/app.rs b/crates/squid-tui/src/app.rs index 1aa682a..28e0a95 100644 --- a/crates/squid-tui/src/app.rs +++ b/crates/squid-tui/src/app.rs @@ -1111,9 +1111,9 @@ impl App { let message = format!( "{} · {}", if trashed { - self.strings.t( - &format!("{removed_count} to Trash"), - &format!("移入废纸篓 {removed_count}"), + self.strings.bin_t( + &format!("{removed_count} to {{bin}}"), + &format!("移入{{bin}} {removed_count}"), ) } else { self.strings.t( @@ -1490,7 +1490,7 @@ impl App { } let size: Bytes = chosen.iter().map(|item| item.on_disk()).sum(); let mode = if self.delete_mode == DeleteMode::Trash { - self.strings.t("moved to Trash", "移入废纸篓") + self.strings.bin_t("moved to {bin}", "移入{bin}") } else { self.strings.t("deleted permanently", "永久删除") }; @@ -1638,7 +1638,7 @@ impl App { DeleteMode::Permanent => DeleteMode::Trash, }; let text = match self.delete_mode { - DeleteMode::Trash => self.strings.t("Trash", "废纸篓"), + DeleteMode::Trash => self.strings.bin().to_string(), DeleteMode::Permanent => self.strings.t("Permanent", "永久删除"), }; self.toast( @@ -1686,8 +1686,8 @@ impl App { Some(session) => vec![ format!("{} · {}", session.command, session.freed().human_long()), self.strings.t( - &format!("{} items will come back from Trash", session.entries.len()), - &format!("{} 项将从废纸篓还原", session.entries.len()), + &format!("{} items will come back from {{bin}}", session.entries.len()), + &format!("{} 项将从{{bin}}还原", session.entries.len()), ), ], None => vec![id.clone()], diff --git a/crates/squid-tui/src/theme.rs b/crates/squid-tui/src/theme.rs index 13674d8..0d84f55 100644 --- a/crates/squid-tui/src/theme.rs +++ b/crates/squid-tui/src/theme.rs @@ -17,6 +17,10 @@ pub enum Lang { #[derive(Clone, Debug)] pub struct Strings { pub lang: Lang, + /// What the host OS calls the place deleted files wait in. Copy about + /// where a file went, or how to get it back, is a claim about the operating + /// system, not about the language. + pub bin_noun: squid_platform::BinNoun, } impl Strings { @@ -26,13 +30,17 @@ impl Strings { Locale::Zh => Lang::Zh, Locale::En => Lang::En, }, + bin_noun: squid_platform::current().bin_noun(), } } /// Strings in a chosen language, whatever the terminal and settings say. /// The layout tests need to measure both languages on one machine. pub fn for_lang(lang: Lang) -> Strings { - Strings { lang } + Strings { + lang, + bin_noun: squid_platform::current().bin_noun(), + } } pub fn t(&self, en: &str, zh: &str) -> String { @@ -42,6 +50,22 @@ impl Strings { } } + /// The host's own word for the recycle bin, in the current language. + pub fn bin(&self) -> &'static str { + match self.lang { + Lang::En => self.bin_noun.en(), + Lang::Zh => self.bin_noun.zh(), + } + } + + /// `t`, with every `{bin}` replaced by the host's word for the recycle + /// bin. Windows calls it 回收站 / "Recycle Bin"; promising a Windows user + /// that they can 「清空废纸篓」 to get space back names a control that does + /// not exist on their machine. + pub fn bin_t(&self, en: &str, zh: &str) -> String { + self.t(en, zh).replace("{bin}", self.bin()) + } + pub fn cleanup(&self) -> String { self.t("Cleanup", "清理") } @@ -93,8 +117,14 @@ impl Strings { /// sidebar is the one place where the interface looked untranslated. pub fn category(&self, category: squid_core::Category) -> String { use squid_core::Category as C; - match self.lang { - Lang::En => category.title().to_string(), + (match self.lang { + // The recycle bin is the one group named after an OS feature + // instead of after our own rules, so in either language it takes + // the operating system's word for it. + Lang::En => match category { + C::Trash => self.bin(), + _ => category.title(), + }, Lang::Zh => match category { C::SystemJunk => "系统垃圾", C::AppCaches => "应用缓存", @@ -102,12 +132,12 @@ impl Strings { C::Logs => "日志与崩溃报告", C::Developer => "开发文件", C::ProjectArtifacts => "构建产物", - C::Trash => "废纸篓", C::LanguagePacks => "多余语言包", C::Leftovers => "卸载残留", - } - .to_string(), - } + C::Trash => self.bin(), + }, + }) + .to_string() } pub fn free(&self) -> String { @@ -586,7 +616,12 @@ pub fn flat_block() -> ratatui::widgets::Block<'static> { /// Checkbox glyph, mirroring the round toggles of the reference app. pub fn checkbox(selected: bool, locked: bool) -> (&'static str, Style) { if locked { - ("🔒", Palette::locked()) + // U+1F512 looks right and is the one glyph the real Windows console + // cannot carry: any astral-plane character comes back out of a + // ConPTY/conhost session as U+FFFD plus the glyph, i.e. one stray + // cell that shifts the whole row. `⊘` is in the BMP, one cell wide, + // and was measured to survive the same session. + ("⊘", Palette::locked()) } else if selected { ( "●", @@ -977,8 +1012,8 @@ mod tests { /// nobody has acted on yet was read as a claim about the past. #[test] fn counts_and_labels_cannot_be_mistaken_for_a_size() { - let zh = Strings { lang: Lang::Zh }; - let en = Strings { lang: Lang::En }; + let zh = Strings::for_lang(Lang::Zh); + let en = Strings::for_lang(Lang::En); assert_eq!(zh.count(1634), "1,634 项"); assert_eq!(en.count(1634), "1,634"); assert_eq!(zh.count(5), "5 项"); @@ -991,8 +1026,8 @@ mod tests { #[test] fn every_group_has_a_name_in_its_own_language() { use squid_core::Category; - let zh = Strings { lang: Lang::Zh }; - let en = Strings { lang: Lang::En }; + let zh = Strings::for_lang(Lang::Zh); + let en = Strings::for_lang(Lang::En); for category in Category::ALL { let name = zh.category(category); assert!(!name.is_empty(), "{category:?} lost its name"); @@ -1004,9 +1039,44 @@ mod tests { unicode_width::UnicodeWidthStr::width(name.as_str()) <= 24, "{name} will not fit the group row" ); - assert_eq!(en.category(category), category.title()); + let expected_en = match category { + // One group is named after an OS feature, so even the English + // name comes from the operating system: a Windows user has a + // Recycle Bin and no Trash to open. + Category::Trash => squid_platform::current().bin_noun().en().to_string(), + _ => category.title().to_string(), + }; + assert_eq!(en.category(category), expected_en); } assert_eq!(zh.category(Category::Developer), "开发文件"); assert_eq!(zh.category(Category::LanguagePacks), "多余语言包"); } + + /// Where a deleted file went, and how to get it back, is a claim about the + /// operating system. Windows has no Trash folder, so copy that promises + /// 「清空废纸篓」 there sends the user looking for something that is not there. + #[test] + fn the_recycle_bin_is_called_what_the_os_calls_it() { + let en = Strings::for_lang(Lang::En); + let zh = Strings::for_lang(Lang::Zh); + let noun = squid_platform::current().bin_noun(); + assert_eq!(en.bin(), noun.en()); + assert_eq!(zh.bin(), noun.zh()); + assert_eq!( + en.bin_t("moved to {bin}", "移入{bin}"), + format!("moved to {}", noun.en()) + ); + assert_eq!( + zh.bin_t("· 清空{bin}后才真正释放", "· 清空{bin}后才真正释放"), + format!("· 清空{}后才真正释放", noun.zh()) + ); + // The group row is 24 columns; the longest name the OS can hand us has + // to survive it in both languages. + for name in [noun.en(), noun.zh()] { + assert!( + unicode_width::UnicodeWidthStr::width(name) <= 24, + "{name} will not fit the group row" + ); + } + } } diff --git a/crates/squid-tui/src/views.rs b/crates/squid-tui/src/views.rs index 27b243f..1984a16 100644 --- a/crates/squid-tui/src/views.rs +++ b/crates/squid-tui/src/views.rs @@ -16,6 +16,7 @@ use ratatui::text::{Line, Span}; use ratatui::widgets::{Block, Borders, Clear, Gauge, List, ListState, Paragraph, Sparkline, Wrap}; use ratatui::Frame; use squid_core::{Bytes, Category, CleanupItem}; +use squid_platform::Device; use unicode_width::UnicodeWidthStr; // ------------------------------------------------------------------- helpers @@ -55,7 +56,16 @@ fn spans_width(spans: &[Span<'static>]) -> usize { } /// Left-aligned spans, then filler, then right-aligned spans. +/// +/// When the two halves do not fit, the left one gives ground. A row that spills +/// puts them back to back and the terminal cuts off whatever came last, so the +/// right half — the state the user is acting on, like the delete mode — is the +/// part that disappears. That is how "empties only with the Recycle Bin" +/// swallowed "delete to · Recycle Bin" on a Windows box at 100 columns: the +/// operating system's name for the recycle bin is longer there than on macOS. fn joined(left: Vec>, right: Vec>, width: usize) -> Line<'static> { + let room = width.saturating_sub(spans_width(&right) + 1); + let left = clip_left(left, room); let gap = width.saturating_sub(spans_width(&left) + spans_width(&right)); let mut spans = left; spans.push(Span::raw(" ".repeat(gap))); @@ -64,6 +74,31 @@ fn joined(left: Vec>, right: Vec>, width: usize) -> Line::from(spans) } +/// Fit the left half of a row into `room` columns, giving up whole trailing +/// spans before cutting one in half, so an ellipsis lands between words where +/// it can. +fn clip_left(left: Vec>, room: usize) -> Vec> { + if spans_width(&left) <= room { + return left; + } + let mut kept: Vec> = Vec::new(); + let mut used = 0; + for span in left { + let wide = span.content.width(); + if used + wide > room { + let tail = room - used; + if tail > 1 { + let cut = crate::theme::truncate(&span.content, tail, false); + kept.push(Span::styled(cut, span.style)); + } + break; + } + used += wide; + kept.push(span); + } + kept +} + /// Give the unstyled spans of a row the row's own style. /// /// The columns of a row are separated by plain `Span::raw(" ")`, which inherit @@ -234,7 +269,7 @@ fn draw_header(frame: &mut Frame, area: Rect, app: &App) { ); let mode = match app.delete_mode { - squid_core::DeleteMode::Trash => app.strings.t("Trash", "废纸篓"), + squid_core::DeleteMode::Trash => app.strings.bin().to_string(), squid_core::DeleteMode::Permanent => app.strings.t("Permanent", "永久删除"), }; let mut bottom_right = vec![Span::styled( @@ -269,7 +304,7 @@ fn draw_header(frame: &mut Frame, area: Rect, app: &App) { let pending = app.to_trash; let all_trash = !pending.is_zero() && pending == app.reclaimed; let label = if all_trash { - app.strings.t("moved to Trash", "移入废纸篓") + app.strings.bin_t("moved to {bin}", "移入{bin}") } else { app.strings.t("reclaimed", "已释放") }; @@ -283,21 +318,32 @@ fn draw_header(frame: &mut Frame, area: Rect, app: &App) { ), Span::styled(format!(" {label} · {}", app.sessions), Palette::muted()), ]; + let mut note = None; if !pending.is_zero() { - let note = if all_trash { + let text = if all_trash { app.strings - .t("· empties only with the Trash", "· 清空废纸篓后才真正释放") + .bin_t("· empties only with the {bin}", "· 清空{bin}后才真正释放") } else { app.strings.t( - &format!("· {} still in Trash", pending.human_fixed()), - &format!("· {} 仍在废纸篓", pending.human_fixed()), + &format!("· {} still in {{bin}}", pending.human_fixed()), + &format!("· {} 仍在{{bin}}", pending.human_fixed()), ) }; - bottom_left.push(Span::styled( - format!(" {note}"), + note = Some(Span::styled( + format!(" {text}"), Style::default().fg(Palette::warn()), )); } + // The note explains the number; the right half states what the user is + // about to do. Given a row too narrow for both, the explanation yields and + // says nothing rather than pushing the delete mode off the card. + if let Some(note) = note { + if spans_width(&bottom_left) + spans_width(&bottom_right) + note.content.width() + <= inner.width as usize + { + bottom_left.push(note); + } + } let bottom = joined(bottom_left, bottom_right, inner.width as usize); frame.render_widget( Paragraph::new(vec![top, bottom]).style(Palette::base()), @@ -691,17 +737,29 @@ fn draw_scan( // Nothing on the list is either "still working" or "nothing to find", // and the two need to be told apart: one asks for patience, the other // for a key press. + // 「this Mac」 on a Windows box is a copy leak rather than a + // translation problem, so the noun comes from the platform and each + // platform owns the spacing of its own phrase. + let (scanning, scanned) = match app.engine.platform().device() { + Device::Mac => ("Scanning this Mac…", "press s to scan this Mac"), + Device::Pc => ("Scanning this PC…", "press s to scan this PC"), + Device::Machine => ("Scanning this machine…", "press s to scan this machine"), + }; + let (scanning_zh, scanned_zh) = match app.engine.platform().device() { + Device::Mac => ("正在扫描这台 Mac…", "按 s 扫描这台 Mac"), + Device::Pc => ("正在扫描这台电脑…", "按 s 扫描这台电脑"), + Device::Machine => ("正在扫描这台机器…", "按 s 扫描这台机器"), + }; let (title, hint) = if filling { ( - app.strings.t("Scanning this Mac…", "正在扫描这台 Mac…"), + app.strings.t(scanning, scanning_zh), app.strings .t("hold on, or press c to cancel", "请稍候,或按 c 取消"), ) } else { ( app.strings.t("Nothing scanned yet", "尚未扫描"), - app.strings - .t("press s to scan this Mac", "按 s 扫描这台 Mac"), + app.strings.t(scanned, scanned_zh), ) }; frame.render_widget(empty_hint(&title, &hint), inner); @@ -1543,9 +1601,9 @@ fn draw_history(frame: &mut Frame, area: Rect, app: &App) { ); if trashed { heading.pop(); - heading.push_str(&app.strings.t( - " · frees once Trash is emptied ", - " · 清空废纸篓后才真正释放 ", + heading.push_str(&app.strings.bin_t( + " · frees once {bin} is emptied ", + " · 清空{bin}后才真正释放 ", )); } let card = block(&heading); @@ -1624,7 +1682,7 @@ fn draw_history(frame: &mut Frame, area: Rect, app: &App) { app.strings.t("restored", "已还原") } else { match session.mode { - squid_core::DeleteMode::Trash => app.strings.t("trash", "废纸篓"), + squid_core::DeleteMode::Trash => app.strings.bin().to_string(), squid_core::DeleteMode::Permanent => { app.strings.t("deleted for good", "已清除") } @@ -1730,10 +1788,10 @@ fn draw_help(frame: &mut Frame, area: Rect, app: &App) { "d".to_string(), s.t("clean what is checked", "清理已勾选项"), ), - ("m".to_string(), s.t("Trash ⇄ permanent", "废纸篓 ⇄ 永久")), + ("m".to_string(), s.bin_t("{bin} ⇄ permanent", "{bin} ⇄ 永久")), ( "u".to_string(), - s.t("restore last Trash session", "还原上次废纸篓会话"), + s.bin_t("restore last {bin} session", "还原上次{bin}会话"), ), ("f".to_string(), s.t("filter applications", "筛选应用")), (