| 1 | use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, Utc, Weekday}; |
| 2 | |
| 3 | use crate::theme; |
| 4 | |
| 5 | const BADGE_CAP: u32 = 99; |
| 6 | const WEEKDAY_WINDOW_DAYS: i64 = 6; |
| 7 | |
| 8 | pub fn palette_index(key: &str) -> usize { |
| 9 | let mut hash: u32 = 0x811c_9dc5; |
| 10 | for byte in key.bytes() { |
| 11 | hash ^= u32::from(byte); |
| 12 | hash = hash.wrapping_mul(0x0100_0193); |
| 13 | } |
| 14 | hash as usize % theme::avatar_palette_len() |
| 15 | } |
| 16 | |
| 17 | pub fn initials(name: &str) -> String { |
| 18 | let words: Vec<&str> = name |
| 19 | .split(|character: char| !character.is_alphanumeric()) |
| 20 | .filter(|word| !word.is_empty()) |
| 21 | .collect(); |
| 22 | let letters: Vec<char> = match words.as_slice() { |
| 23 | [] => return "?".to_owned(), |
| 24 | [single] => single.chars().take(2).collect(), |
| 25 | [first, .., last] => first.chars().take(1).chain(last.chars().take(1)).collect(), |
| 26 | }; |
| 27 | letters.into_iter().flat_map(char::to_uppercase).collect() |
| 28 | } |
| 29 | |
| 30 | pub fn first_name(name: &str) -> &str { |
| 31 | name.split_whitespace().next().unwrap_or(name) |
| 32 | } |
| 33 | |
| 34 | pub fn badge_text(count: u32) -> String { |
| 35 | if count > BADGE_CAP { |
| 36 | format!("{BADGE_CAP}+") |
| 37 | } else { |
| 38 | count.to_string() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn weekday_label(weekday: Weekday) -> &'static str { |
| 43 | match weekday { |
| 44 | Weekday::Mon => "Mon", |
| 45 | Weekday::Tue => "Tue", |
| 46 | Weekday::Wed => "Wed", |
| 47 | Weekday::Thu => "Thu", |
| 48 | Weekday::Fri => "Fri", |
| 49 | Weekday::Sat => "Sat", |
| 50 | Weekday::Sun => "Sun", |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn file_size_label(bytes: u64) -> String { |
| 55 | const KIB: f64 = 1024.; |
| 56 | let value = bytes as f64; |
| 57 | if value < KIB { |
| 58 | format!("{bytes} B") |
| 59 | } else if value < KIB * KIB { |
| 60 | format!("{:.0} KB", value / KIB) |
| 61 | } else { |
| 62 | format!("{:.1} MB", value / (KIB * KIB)) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub fn short_version(build_id: &str, package_version: &str) -> String { |
| 67 | let commit = build_id.split('-').next().unwrap_or(build_id); |
| 68 | format!("v{package_version} ({commit})") |
| 69 | } |
| 70 | |
| 71 | pub fn list_time_label(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> String { |
| 72 | let local = time.with_timezone(&offset); |
| 73 | let day = local.date_naive(); |
| 74 | let days_ago = (today - day).num_days(); |
| 75 | if days_ago <= 0 { |
| 76 | local.format("%H:%M").to_string() |
| 77 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 78 | weekday_label(local.weekday()).to_owned() |
| 79 | } else { |
| 80 | local.format("%b %-d").to_string() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn post_time_label(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> String { |
| 85 | let local = time.with_timezone(&offset); |
| 86 | let day = local.date_naive(); |
| 87 | let days_ago = (today - day).num_days(); |
| 88 | let clock = local.format("%H:%M"); |
| 89 | if days_ago <= 0 { |
| 90 | clock.to_string() |
| 91 | } else if days_ago == 1 { |
| 92 | format!("Yesterday {clock}") |
| 93 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 94 | format!("{} {clock}", weekday_label(local.weekday())) |
| 95 | } else if day.year() == today.year() { |
| 96 | format!("{} {clock}", local.format("%b %-d")) |
| 97 | } else { |
| 98 | format!("{} {clock}", local.format("%b %-d, %Y")) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn day_label(day: NaiveDate, today: NaiveDate) -> String { |
| 103 | if day == today { |
| 104 | "Today".to_owned() |
| 105 | } else if today.pred_opt() == Some(day) { |
| 106 | "Yesterday".to_owned() |
| 107 | } else { |
| 108 | format!( |
| 109 | "{}, {}", |
| 110 | weekday_label(day.weekday()), |
| 111 | day.format("%b %-d, %Y") |
| 112 | ) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[cfg(test)] |
| 117 | mod tests { |
| 118 | use chrono::TimeZone; |
| 119 | |
| 120 | use super::*; |
| 121 | |
| 122 | fn utc(day: u32, hour: u32) -> DateTime<Utc> { |
| 123 | Utc.with_ymd_and_hms(2026, 10, day, hour, 5, 0).unwrap() |
| 124 | } |
| 125 | |
| 126 | fn today() -> NaiveDate { |
| 127 | NaiveDate::from_ymd_opt(2026, 10, 7).unwrap() |
| 128 | } |
| 129 | |
| 130 | fn zero() -> FixedOffset { |
| 131 | FixedOffset::east_opt(0).unwrap() |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn palette_index_is_stable_and_in_range() { |
| 136 | assert_eq!(palette_index("user-1"), palette_index("user-1")); |
| 137 | for key in [ |
| 138 | "a", |
| 139 | "b", |
| 140 | "demo-ada", |
| 141 | "", |
| 142 | "00000000-0000-0000-0000-000000000001", |
| 143 | ] { |
| 144 | assert!(palette_index(key) < theme::avatar_palette_len()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn palette_index_spreads_over_several_colors() { |
| 150 | let used: std::collections::HashSet<usize> = (0..40) |
| 151 | .map(|number| palette_index(&format!("user-{number}"))) |
| 152 | .collect(); |
| 153 | assert!(used.len() >= 4); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn initials_use_first_and_last_word() { |
| 158 | assert_eq!(initials("Mara Lindqvist"), "ML"); |
| 159 | assert_eq!(initials("Ada"), "AD"); |
| 160 | assert_eq!(initials("anna maria von Berg"), "AB"); |
| 161 | assert_eq!(initials(" "), "?"); |
| 162 | assert_eq!(initials("Release planning"), "RP"); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn first_name_takes_the_first_word() { |
| 167 | assert_eq!(first_name("Priya Nair"), "Priya"); |
| 168 | assert_eq!(first_name("Priya"), "Priya"); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn file_sizes_use_binary_units() { |
| 173 | assert_eq!(file_size_label(900), "900 B"); |
| 174 | assert_eq!(file_size_label(48_213), "47 KB"); |
| 175 | assert_eq!(file_size_label(5 * 1024 * 1024 + 300_000), "5.3 MB"); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn badge_caps_at_99_plus() { |
| 180 | assert_eq!(badge_text(1), "1"); |
| 181 | assert_eq!(badge_text(99), "99"); |
| 182 | assert_eq!(badge_text(100), "99+"); |
| 183 | assert_eq!(badge_text(4000), "99+"); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn list_time_is_clock_today_weekday_this_week_date_before() { |
| 188 | assert_eq!(list_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 189 | assert_eq!(list_time_label(utc(5, 13), today(), zero()), "Mon"); |
| 190 | assert_eq!(list_time_label(utc(6, 9), today(), zero()), "Tue"); |
| 191 | assert_eq!(list_time_label(utc(1, 13), today(), zero()), "Thu"); |
| 192 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 13, 0, 0).unwrap(); |
| 193 | assert_eq!(list_time_label(september, today(), zero()), "Sep 30"); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn list_time_respects_the_offset_for_the_day_boundary() { |
| 198 | let plus_two = FixedOffset::east_opt(2 * 3600).unwrap(); |
| 199 | assert_eq!(list_time_label(utc(6, 23), today(), plus_two), "01:05"); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn short_version_keeps_the_commit_only() { |
| 204 | assert_eq!( |
| 205 | short_version("687e2dc-20261007T124355Z", "0.1.0"), |
| 206 | "v0.1.0 (687e2dc)" |
| 207 | ); |
| 208 | assert_eq!(short_version("dev", "0.1.0"), "v0.1.0 (dev)"); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn post_time_adds_the_day_when_not_today() { |
| 213 | assert_eq!(post_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 214 | assert_eq!(post_time_label(utc(6, 9), today(), zero()), "Yesterday 09:05"); |
| 215 | assert_eq!(post_time_label(utc(5, 13), today(), zero()), "Mon 13:05"); |
| 216 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 8, 42, 0).unwrap(); |
| 217 | assert_eq!(post_time_label(september, today(), zero()), "Sep 30 08:42"); |
| 218 | let last_year = Utc.with_ymd_and_hms(2020, 5, 22, 8, 42, 0).unwrap(); |
| 219 | assert_eq!(post_time_label(last_year, today(), zero()), "May 22, 2020 08:42"); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn day_labels() { |
| 224 | assert_eq!(day_label(today(), today()), "Today"); |
| 225 | assert_eq!(day_label(today().pred_opt().unwrap(), today()), "Yesterday"); |
| 226 | assert_eq!( |
| 227 | day_label(NaiveDate::from_ymd_opt(2026, 10, 1).unwrap(), today()), |
| 228 | "Thu, Oct 1, 2026" |
| 229 | ); |
| 230 | } |
| 231 | } |