diff --git a/crates/telemetry/src/account_store.rs b/crates/telemetry/src/account_store.rs index c7c5e3d29..055a60898 100644 --- a/crates/telemetry/src/account_store.rs +++ b/crates/telemetry/src/account_store.rs @@ -204,10 +204,46 @@ pub fn list_text(conn: &Connection) -> Result { Ok(out.trim_end().to_string()) } +/// The masked header text for `key`. An unnamed email key shows only `@` and its first +/// domain label, which keeps the address out of screenshots. When two unnamed +/// keys cut to the same text, each gets its place among them, so cycling +/// accounts still changes the header. +pub(crate) fn header_label(key: &str, name: &str, choices: &[(String, String)]) -> String { + let Some(label) = email_cut(key, name) else { + return name.to_string(); + }; + let clashing: Vec<&str> = choices + .iter() + .filter(|(k, n)| email_cut(k, n).as_deref() == Some(label.as_str())) + .map(|(k, _)| k.as_str()) + .collect(); + match clashing.iter().position(|k| *k == key) { + Some(at) if clashing.len() > 1 => format!("{label} ({})", at + 1), + _ => label, + } +} + +/// `None` for a named account or a key with no local part to hide. +fn email_cut(key: &str, name: &str) -> Option { + if name != key { + return None; + } + let (local, domain) = key.split_once('@')?; + if local.is_empty() { + return None; + } + let host = domain + .split('.') + .find(|part| !part.is_empty()) + .unwrap_or(""); + Some(format!("@{host}")) +} + /// A store with four events on two accounts and one skill record, for tests. #[cfg(test)] pub(crate) mod tests { use super::*; + use crate::account::UNKNOWN_ACCOUNT; use crate::db::open_db; use crate::util::scratch_dir; @@ -283,4 +319,36 @@ pub(crate) mod tests { ); assert!(text.contains("work@x.com\n 3 events, $7.00"), "{text}"); } + + fn label(key: &str) -> String { + header_label(key, key, &[(key.to_string(), key.to_string())]) + } + + #[test] + fn an_unnamed_email_account_shows_only_its_domain_name() { + assert_eq!(label("dev@example.com.invalid"), "@example"); + assert_eq!(label("a@example.com"), "@example"); + assert_eq!(label("dev@.com"), "@com"); + assert_eq!(label("dev@"), "@"); + } + + #[test] + fn named_plain_and_unknown_accounts_show_unchanged() { + assert_eq!(header_label("dev@example.com.invalid", "Work", &[]), "Work"); + assert_eq!(label("home"), "home"); + assert_eq!(label(UNKNOWN_ACCOUNT), UNKNOWN_ACCOUNT); + assert_eq!(label("@example.com"), "@example.com"); + } + + #[test] + fn unnamed_emails_on_one_domain_get_their_place() { + let choices: Vec<(String, String)> = + ["dev@example.com", "ana@example.com", "bo@work.invalid"] + .map(|k| (k.to_string(), k.to_string())) + .to_vec(); + let shown = |key: &str| header_label(key, key, &choices); + assert_eq!(shown("dev@example.com"), "@example (1)"); + assert_eq!(shown("ana@example.com"), "@example (2)"); + assert_eq!(shown("bo@work.invalid"), "@work"); + } } diff --git a/crates/telemetry/src/run.rs b/crates/telemetry/src/run.rs index 37d19722d..7a2e15f7d 100644 --- a/crates/telemetry/src/run.rs +++ b/crates/telemetry/src/run.rs @@ -152,7 +152,7 @@ pub fn run_summary( if let Some(key) = &key { let shown = super::account_store::display(&choices, key); let shown = if mask.is_some() { - super::tui::account::header_label(key, &shown, &choices) + super::account_store::header_label(key, &shown, &choices) } else { shown }; diff --git a/crates/telemetry/src/summary.rs b/crates/telemetry/src/summary.rs index a432fafcd..19bb1fc20 100644 --- a/crates/telemetry/src/summary.rs +++ b/crates/telemetry/src/summary.rs @@ -53,7 +53,7 @@ pub type MaskChoices<'a> = Option<&'a [(String, String)]>; fn mask_accounts(groups: &mut [Group], choices: &[(String, String)]) { for group in groups { let name = crate::account_store::display(choices, &group.key); - group.key = crate::tui::account::header_label(&group.key, &name, choices); + group.key = crate::account_store::header_label(&group.key, &name, choices); } } diff --git a/crates/telemetry/src/tui/account.rs b/crates/telemetry/src/tui/account.rs index f5ce0487b..5020fbf8a 100644 --- a/crates/telemetry/src/tui/account.rs +++ b/crates/telemetry/src/tui/account.rs @@ -6,6 +6,7 @@ //! so a second login does not add to the first one's numbers. use crate::account::UNKNOWN_ACCOUNT; +use crate::account_store::header_label; use crate::{ToolInvocationEvent, UsageEvent}; use playbook_store::Connection; @@ -112,41 +113,6 @@ pub fn view(conn: &Connection, sel: &AccountSel) -> Result }) } -/// The masked header text for `key`. An unnamed email key shows only `@` and its first -/// domain label, which keeps the address out of screenshots. When two unnamed -/// keys cut to the same text, each gets its place among them, so cycling -/// accounts still changes the header. -pub(crate) fn header_label(key: &str, name: &str, choices: &[(String, String)]) -> String { - let Some(label) = email_cut(key, name) else { - return name.to_string(); - }; - let clashing: Vec<&str> = choices - .iter() - .filter(|(k, n)| email_cut(k, n).as_deref() == Some(label.as_str())) - .map(|(k, _)| k.as_str()) - .collect(); - match clashing.iter().position(|k| *k == key) { - Some(at) if clashing.len() > 1 => format!("{label} ({})", at + 1), - _ => label, - } -} - -/// `None` for a named account or a key with no local part to hide. -fn email_cut(key: &str, name: &str) -> Option { - if name != key { - return None; - } - let (local, domain) = key.split_once('@')?; - if local.is_empty() { - return None; - } - let host = domain - .split('.') - .find(|part| !part.is_empty()) - .unwrap_or(""); - Some(format!("@{host}")) -} - /// Drops the events of other accounts. `None` keeps everything. pub fn keep(events: &mut Vec, key: Option<&str>) { if let Some(key) = key { @@ -171,26 +137,6 @@ mod tests { .to_vec() } - fn label(key: &str) -> String { - header_label(key, key, &[(key.to_string(), key.to_string())]) - } - - #[test] - fn an_unnamed_email_account_shows_only_its_domain_name() { - assert_eq!(label("dev@example.com.br"), "@example"); - assert_eq!(label("a@example.com"), "@example"); - assert_eq!(label("dev@.com"), "@com"); - assert_eq!(label("dev@"), "@"); - } - - #[test] - fn named_plain_and_unknown_accounts_show_unchanged() { - assert_eq!(header_label("dev@example.com.br", "Work", &[]), "Work"); - assert_eq!(label("home"), "home"); - assert_eq!(label(UNKNOWN_ACCOUNT), UNKNOWN_ACCOUNT); - assert_eq!(label("@x.com"), "@x.com"); - } - #[test] fn the_view_label_is_the_full_email_unless_the_mask_is_on() { let view = AccountsView { @@ -203,18 +149,6 @@ mod tests { assert_eq!(view.label(true), "@example"); } - #[test] - fn unnamed_emails_on_one_domain_get_their_place() { - let choices: Vec<(String, String)> = - ["dev@example.com", "ana@example.com", "bo@work.invalid"] - .map(|k| (k.to_string(), k.to_string())) - .to_vec(); - let shown = |key: &str| header_label(key, key, &choices); - assert_eq!(shown("dev@example.com"), "@example (1)"); - assert_eq!(shown("ana@example.com"), "@example (2)"); - assert_eq!(shown("bo@work.invalid"), "@work"); - } - #[test] fn the_newest_account_is_the_default_and_unknown_never_wins_it() { assert_eq!(