Commit d059576965
Unregistered key
Layout: unified · split
config.full.toml +8
| @@ -20,6 +20,9 @@ password_file = "/path/to/password" | |||
| 20 | url = "https://matrix-client.matrix.org" | 20 | url = "https://matrix-client.matrix.org" |
| 21 | 21 | ||
| 22 | [settings] | 22 | [settings] |
| 23 | # strftime format for the date separator. See | ||
| 24 | # https://docs.rs/chrono/latest/chrono/format/strftime/index.html | ||
| 25 | # date_format = "%A, %B %d %Y" | ||
| 23 | default_markup = "markdown" | 26 | default_markup = "markdown" |
| 24 | default_register = "\"" | 27 | default_register = "\"" |
| 25 | default_room = "#iamb-users:0x.badd.cafe" | 28 | default_room = "#iamb-users:0x.badd.cafe" |
| @@ -39,6 +42,11 @@ read_receipt_send = true | |||
| 39 | room_labels = ["membership", "muted", "tags"] | 42 | room_labels = ["membership", "muted", "tags"] |
| 40 | request_timeout = 10000 | 43 | request_timeout = 10000 |
| 41 | sync_delay_ms = 1000 | 44 | sync_delay_ms = 1000 |
| 45 | # strftime format for the per-message time gutter (same specifier reference as | ||
| 46 | # date_format). This string is the full gutter text, including brackets and | ||
| 47 | # spacing. Column width follows the rendered format. An empty string disables | ||
| 48 | # the time display. | ||
| 49 | # time_format = " [%T]" | ||
| 42 | typing_notice_display = true | 50 | typing_notice_display = true |
| 43 | typing_notice_send = true | 51 | typing_notice_send = true |
| 44 | user_gutter_width = 30 | 52 | user_gutter_width = 30 |
docs/iamb.5 +20
| @@ -142,6 +142,13 @@ Specifying this object will replace the upstream defaults and possibly lead to u | |||
| 142 | Possible keys and their effect are documented at: | 142 | Possible keys and their effect are documented at: |
| 143 | .br | 143 | .br |
| 144 | .Lk https://docs.rs/matrix-sdk/latest/matrix_sdk/media/struct.MediaRetentionPolicy.html#fields | 144 | .Lk https://docs.rs/matrix-sdk/latest/matrix_sdk/media/struct.MediaRetentionPolicy.html#fields |
| 145 | .It Sy date_format | ||
| 146 | A strftime format string for the date separator between messages from different days. | ||
| 147 | See | ||
| 148 | .Lk https://docs.rs/chrono/latest/chrono/format/strftime/index.html | ||
| 149 | for the conversion specifiers. | ||
| 150 | Defaults to | ||
| 151 | .Dq "%A, %B %d %Y" . | ||
| 145 | .It Sy default_markup | 152 | .It Sy default_markup |
| 146 | Controls how text in the message bar is interpreted by default. | 153 | Controls how text in the message bar is interpreted by default. |
| 147 | Possible values are | 154 | Possible values are |
| @@ -331,6 +338,19 @@ Subsection for configuring how specific terminal features are used (or not used) | |||
| 331 | See | 338 | See |
| 332 | .Sx TERMINAL | 339 | .Sx TERMINAL |
| 333 | for more details. | 340 | for more details. |
| 341 | .It Sy time_format | ||
| 342 | A strftime format string for the time shown beside each message. | ||
| 343 | See | ||
| 344 | .Lk https://docs.rs/chrono/latest/chrono/format/strftime/index.html | ||
| 345 | for the conversion specifiers. | ||
| 346 | The value is the full gutter text, including any brackets or spacing. | ||
| 347 | Its display width, measured by rendering the format for a sample timestamp, | ||
| 348 | is the width of the time column and does not change from message to message. | ||
| 349 | An empty string disables the time display and reserves no columns. | ||
| 350 | Defaults to | ||
| 351 | .Dq " [%T]" , | ||
| 352 | which renders as | ||
| 353 | .Dq " [HH:MM:SS]" . | ||
| 334 | .It Sy typing_notice_send | 354 | .It Sy typing_notice_send |
| 335 | Defines whether or not the typing state is sent. | 355 | Defines whether or not the typing state is sent. |
| 336 | .It Sy typing_notice_display | 356 | .It Sy typing_notice_display |
src/config/mod.rs +153
| @@ -7,6 +7,8 @@ use std::process; | |||
| 7 | use std::sync::Arc; | 7 | use std::sync::Arc; |
| 8 | 8 | ||
| 9 | use anyhow::anyhow; | 9 | use anyhow::anyhow; |
| 10 | use chrono::format::{Item, StrftimeItems}; | ||
| 11 | use chrono::{DateTime, Local}; | ||
| 10 | use clap::Parser; | 12 | use clap::Parser; |
| 11 | use indexmap::IndexMap; | 13 | use indexmap::IndexMap; |
| 12 | use lazy_static::lazy_static; | 14 | use lazy_static::lazy_static; |
| @@ -90,6 +92,37 @@ const DEFAULT_ICON_UNKNOWN: Cow<'static, str> = Cow::Borrowed("[?] "); | |||
| 90 | 92 | ||
| 91 | const DEFAULT_LOG_LEVEL: &str = "off"; | 93 | const DEFAULT_LOG_LEVEL: &str = "off"; |
| 92 | 94 | ||
| 95 | /// Default date separator, matching the historical hard-coded `"%A, %B %d %Y"`. | ||
| 96 | const DEFAULT_DATE_FORMAT: &str = "%A, %B %d %Y"; | ||
| 97 | |||
| 98 | /// Default time gutter, preserving the historical ` [HH:MM:SS]` rendering. | ||
| 99 | const DEFAULT_TIME_FORMAT: &str = " [%T]"; | ||
| 100 | |||
| 101 | /// Unix seconds of the timestamp used to measure time-gutter width. | ||
| 102 | /// | ||
| 103 | /// 2024-01-17 13:25:30 UTC, so numeric fields have their usual widths. | ||
| 104 | const TIME_GUTTER_SAMPLE_UNIX: i64 = 1_705_497_930; | ||
| 105 | |||
| 106 | fn time_gutter_sample() -> DateTime<Local> { | ||
| 107 | DateTime::from_timestamp(TIME_GUTTER_SAMPLE_UNIX, 0) | ||
| 108 | .unwrap_or_default() | ||
| 109 | .with_timezone(&Local) | ||
| 110 | } | ||
| 111 | |||
| 112 | /// Display columns reserved for the per-message time gutter. | ||
| 113 | /// | ||
| 114 | /// The width is the display width of `format` applied to one fixed sample | ||
| 115 | /// timestamp, so a configuration keeps a constant column. An empty format | ||
| 116 | /// reserves nothing and hides the time. | ||
| 117 | pub fn time_gutter_width(format: &str) -> usize { | ||
| 118 | if format.is_empty() { | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | let rendered = time_gutter_sample().format(format).to_string(); | ||
| 123 | UnicodeWidthStr::width(rendered.as_str()) | ||
| 124 | } | ||
| 125 | |||
| 93 | fn is_profile_char(c: char) -> bool { | 126 | fn is_profile_char(c: char) -> bool { |
| 94 | c.is_ascii_alphanumeric() || c == '.' || c == '-' | 127 | c.is_ascii_alphanumeric() || c == '.' || c == '-' |
| 95 | } | 128 | } |
| @@ -170,6 +203,21 @@ where | |||
| 170 | Ok(Some(r)) | 203 | Ok(Some(r)) |
| 171 | } | 204 | } |
| 172 | 205 | ||
| 206 | fn deserialize_strftime<'de, D>(deserializer: D) -> Result<Option<String>, D::Error> | ||
| 207 | where | ||
| 208 | D: Deserializer<'de>, | ||
| 209 | { | ||
| 210 | let value = String::deserialize(deserializer)?; | ||
| 211 | |||
| 212 | // `DelayedFormat`'s `Display` impl returns `fmt::Error` for a bad specifier, | ||
| 213 | // and `.to_string()` then panics. Reject those strings while loading config. | ||
| 214 | if StrftimeItems::new(&value).any(|item| matches!(item, Item::Error)) { | ||
| 215 | return Err(D::Error::custom(format!("invalid strftime format string: {value:?}"))); | ||
| 216 | } | ||
| 217 | |||
| 218 | Ok(Some(value)) | ||
| 219 | } | ||
| 220 | |||
| 173 | /// Load themes from disk, and merge in the configured overrides. | 221 | /// Load themes from disk, and merge in the configured overrides. |
| 174 | fn load_themes( | 222 | fn load_themes( |
| 175 | themes_dir: &Path, | 223 | themes_dir: &Path, |
| @@ -942,6 +990,11 @@ pub struct TunableValues { | |||
| 942 | pub ssl_verify: bool, | 990 | pub ssl_verify: bool, |
| 943 | pub cache_policy: MediaRetentionPolicy, | 991 | pub cache_policy: MediaRetentionPolicy, |
| 944 | pub send_on_enter: bool, | 992 | pub send_on_enter: bool, |
| 993 | pub date_format: String, | ||
| 994 | pub time_format: String, | ||
| 995 | /// Display columns reserved for the time gutter, computed from | ||
| 996 | /// `time_format` once when the settings are loaded. | ||
| 997 | pub time_gutter_width: usize, | ||
| 945 | } | 998 | } |
| 946 | 999 | ||
| 947 | #[derive(Clone, Debug, Default, Deserialize)] | 1000 | #[derive(Clone, Debug, Default, Deserialize)] |
| @@ -1002,6 +1055,13 @@ pub struct Tunables { | |||
| 1002 | pub ssl_verify: Option<bool>, | 1055 | pub ssl_verify: Option<bool>, |
| 1003 | pub cache_policy: Option<MediaRetentionPolicy>, | 1056 | pub cache_policy: Option<MediaRetentionPolicy>, |
| 1004 | pub send_on_enter: Option<bool>, | 1057 | pub send_on_enter: Option<bool>, |
| 1058 | #[serde(default, deserialize_with = "deserialize_strftime")] | ||
| 1059 | pub date_format: Option<String>, | ||
| 1060 | /// strftime string for the per-message time gutter, including any brackets | ||
| 1061 | /// or spacing. The column is as wide as a sample rendering of this string. | ||
| 1062 | /// An empty string hides the time. | ||
| 1063 | #[serde(default, deserialize_with = "deserialize_strftime")] | ||
| 1064 | pub time_format: Option<String>, | ||
| 1005 | } | 1065 | } |
| 1006 | 1066 | ||
| 1007 | impl Tunables { | 1067 | impl Tunables { |
| @@ -1062,10 +1122,15 @@ impl Tunables { | |||
| 1062 | ssl_verify: self.ssl_verify.or(other.ssl_verify), | 1122 | ssl_verify: self.ssl_verify.or(other.ssl_verify), |
| 1063 | cache_policy: self.cache_policy.or(other.cache_policy), | 1123 | cache_policy: self.cache_policy.or(other.cache_policy), |
| 1064 | send_on_enter: self.send_on_enter.or(other.send_on_enter), | 1124 | send_on_enter: self.send_on_enter.or(other.send_on_enter), |
| 1125 | date_format: self.date_format.or(other.date_format), | ||
| 1126 | time_format: self.time_format.or(other.time_format), | ||
| 1065 | } | 1127 | } |
| 1066 | } | 1128 | } |
| 1067 | 1129 | ||
| 1068 | fn values(self) -> TunableValues { | 1130 | fn values(self) -> TunableValues { |
| 1131 | let time_format = self.time_format.unwrap_or_else(|| DEFAULT_TIME_FORMAT.to_owned()); | ||
| 1132 | let time_gutter_width = time_gutter_width(&time_format); | ||
| 1133 | |||
| 1069 | TunableValues { | 1134 | TunableValues { |
| 1070 | encryption: self.encryption.values(), | 1135 | encryption: self.encryption.values(), |
| 1071 | proxy: self.proxy.unwrap_or_default().values(), | 1136 | proxy: self.proxy.unwrap_or_default().values(), |
| @@ -1116,6 +1181,9 @@ impl Tunables { | |||
| 1116 | ssl_verify: self.ssl_verify.unwrap_or(true), | 1181 | ssl_verify: self.ssl_verify.unwrap_or(true), |
| 1117 | cache_policy: self.cache_policy.unwrap_or_default(), | 1182 | cache_policy: self.cache_policy.unwrap_or_default(), |
| 1118 | send_on_enter: self.send_on_enter.unwrap_or(true), | 1183 | send_on_enter: self.send_on_enter.unwrap_or(true), |
| 1184 | date_format: self.date_format.unwrap_or_else(|| DEFAULT_DATE_FORMAT.to_owned()), | ||
| 1185 | time_format, | ||
| 1186 | time_gutter_width, | ||
| 1119 | } | 1187 | } |
| 1120 | } | 1188 | } |
| 1121 | } | 1189 | } |
| @@ -1806,6 +1874,91 @@ mod tests { | |||
| 1806 | assert_eq!(res.users, Some(users.into_iter().collect())); | 1874 | assert_eq!(res.users, Some(users.into_iter().collect())); |
| 1807 | } | 1875 | } |
| 1808 | 1876 | ||
| 1877 | fn parse_settings_toml(input: &str) -> Result<Tunables, toml::de::Error> { | ||
| 1878 | #[derive(Deserialize)] | ||
| 1879 | struct SettingsToml { | ||
| 1880 | settings: Tunables, | ||
| 1881 | } | ||
| 1882 | |||
| 1883 | toml::from_str::<SettingsToml>(input).map(|doc| doc.settings) | ||
| 1884 | } | ||
| 1885 | |||
| 1886 | #[test] | ||
| 1887 | fn test_parse_tunables_date_time_format() { | ||
| 1888 | let settings = parse_settings_toml( | ||
| 1889 | r#" | ||
| 1890 | [settings] | ||
| 1891 | date_format = "%Y-%m-%d" | ||
| 1892 | time_format = "%H:%M" | ||
| 1893 | "#, | ||
| 1894 | ) | ||
| 1895 | .unwrap(); | ||
| 1896 | assert_eq!(settings.date_format.as_deref(), Some("%Y-%m-%d")); | ||
| 1897 | assert_eq!(settings.time_format.as_deref(), Some("%H:%M")); | ||
| 1898 | |||
| 1899 | let values = settings.values(); | ||
| 1900 | assert_eq!(values.date_format, "%Y-%m-%d"); | ||
| 1901 | assert_eq!(values.time_format, "%H:%M"); | ||
| 1902 | assert_eq!(values.time_gutter_width, 5); | ||
| 1903 | |||
| 1904 | // Absent keys keep today's formats. | ||
| 1905 | let settings = parse_settings_toml("[settings]\n").unwrap(); | ||
| 1906 | assert_eq!(settings.date_format, None); | ||
| 1907 | assert_eq!(settings.time_format, None); | ||
| 1908 | let values = settings.values(); | ||
| 1909 | assert_eq!(values.date_format, "%A, %B %d %Y"); | ||
| 1910 | assert_eq!(values.time_format, " [%T]"); | ||
| 1911 | assert_eq!(values.time_gutter_width, 12); | ||
| 1912 | |||
| 1913 | // An empty format is valid and disables the time column. | ||
| 1914 | let settings = parse_settings_toml( | ||
| 1915 | r#" | ||
| 1916 | [settings] | ||
| 1917 | time_format = "" | ||
| 1918 | "#, | ||
| 1919 | ) | ||
| 1920 | .unwrap(); | ||
| 1921 | assert_eq!(settings.time_format.as_deref(), Some("")); | ||
| 1922 | let values = settings.values(); | ||
| 1923 | assert_eq!(values.time_format, ""); | ||
| 1924 | assert_eq!(values.time_gutter_width, 0); | ||
| 1925 | |||
| 1926 | // Brackets and spacing are literal gutter text, not added later. | ||
| 1927 | let settings = parse_settings_toml( | ||
| 1928 | r#" | ||
| 1929 | [settings] | ||
| 1930 | time_format = " [%H:%M]" | ||
| 1931 | "#, | ||
| 1932 | ) | ||
| 1933 | .unwrap(); | ||
| 1934 | let values = settings.values(); | ||
| 1935 | assert_eq!(values.time_format, " [%H:%M]"); | ||
| 1936 | assert_eq!(values.time_gutter_width, 9); | ||
| 1937 | } | ||
| 1938 | |||
| 1939 | #[test] | ||
| 1940 | fn test_parse_tunables_date_time_format_invalid() { | ||
| 1941 | let err = parse_settings_toml( | ||
| 1942 | r#" | ||
| 1943 | [settings] | ||
| 1944 | date_format = "%Q" | ||
| 1945 | "#, | ||
| 1946 | ) | ||
| 1947 | .unwrap_err(); | ||
| 1948 | let msg = err.to_string(); | ||
| 1949 | assert!(msg.contains("invalid strftime"), "{msg}"); | ||
| 1950 | |||
| 1951 | let err = parse_settings_toml( | ||
| 1952 | r#" | ||
| 1953 | [settings] | ||
| 1954 | time_format = "%" | ||
| 1955 | "#, | ||
| 1956 | ) | ||
| 1957 | .unwrap_err(); | ||
| 1958 | let msg = err.to_string(); | ||
| 1959 | assert!(msg.contains("invalid strftime"), "{msg}"); | ||
| 1960 | } | ||
| 1961 | |||
| 1809 | #[test] | 1962 | #[test] |
| 1810 | fn test_parse_tunables_username_display() { | 1963 | fn test_parse_tunables_username_display() { |
| 1811 | let res: Tunables = serde_json::from_str("{\"username_display\": \"username\"}").unwrap(); | 1964 | let res: Tunables = serde_json::from_str("{\"username_display\": \"username\"}").unwrap(); |
src/message/mod.rs +91 −29
| @@ -104,26 +104,9 @@ impl Messages { | |||
| 104 | } | 104 | } |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | const fn span_static(s: &'static str) -> Span<'static> { | ||
| 108 | Span { | ||
| 109 | content: Cow::Borrowed(s), | ||
| 110 | style: Style { | ||
| 111 | fg: None, | ||
| 112 | bg: None, | ||
| 113 | add_modifier: StyleModifier::empty(), | ||
| 114 | sub_modifier: StyleModifier::empty(), | ||
| 115 | underline_color: None, | ||
| 116 | }, | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | const TIME_GUTTER: usize = 12; | ||
| 121 | const READ_GUTTER: usize = 5; | 107 | const READ_GUTTER: usize = 5; |
| 122 | const MIN_MSG_LEN: usize = 30; | 108 | const MIN_MSG_LEN: usize = 30; |
| 123 | 109 | ||
| 124 | const TIME_GUTTER_EMPTY: &str = " "; | ||
| 125 | const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY); | ||
| 126 | |||
| 127 | const USIZE_TOO_SMALL: bool = usize::BITS < u64::BITS; | 110 | const USIZE_TOO_SMALL: bool = usize::BITS < u64::BITS; |
| 128 | 111 | ||
| 129 | /// Convert the [u64] hash to [usize] as needed. | 112 | /// Convert the [u64] hash to [usize] as needed. |
| @@ -221,7 +204,10 @@ impl MessageTimeStamp { | |||
| 221 | } | 204 | } |
| 222 | 205 | ||
| 223 | fn show_date(self, settings: &ApplicationSettings) -> Span<'static> { | 206 | fn show_date(self, settings: &ApplicationSettings) -> Span<'static> { |
| 224 | let time = self.as_datetime().format("%A, %B %d %Y").to_string(); | 207 | let time = self |
| 208 | .as_datetime() | ||
| 209 | .format(settings.tunables.date_format.as_str()) | ||
| 210 | .to_string(); | ||
| 225 | 211 | ||
| 226 | Span::styled(time, settings.theme.timeline.date) | 212 | Span::styled(time, settings.theme.timeline.date) |
| 227 | } | 213 | } |
| @@ -232,8 +218,10 @@ impl MessageTimeStamp { | |||
| 232 | } | 218 | } |
| 233 | 219 | ||
| 234 | fn show_time(self, settings: &ApplicationSettings) -> Span<'static> { | 220 | fn show_time(self, settings: &ApplicationSettings) -> Span<'static> { |
| 235 | let time = self.as_datetime().format("%T"); | 221 | let time = self |
| 236 | let time = format!(" [{time}]"); | 222 | .as_datetime() |
| 223 | .format(settings.tunables.time_format.as_str()) | ||
| 224 | .to_string(); | ||
| 237 | 225 | ||
| 238 | Span::styled(time, settings.theme.timeline.time) | 226 | Span::styled(time, settings.theme.timeline.time) |
| 239 | } | 227 | } |
| @@ -774,7 +762,9 @@ impl<'a> MessageFormatter<'a> { | |||
| 774 | match self.cols { | 762 | match self.cols { |
| 775 | MessageColumns::Four => { | 763 | MessageColumns::Four => { |
| 776 | let settings = self.settings; | 764 | let settings = self.settings; |
| 777 | let time = self.time.take().unwrap_or(TIME_GUTTER_EMPTY_SPAN); | 765 | let time = self.time.take().unwrap_or_else(|| { |
| 766 | space_span(settings.tunables.time_gutter_width, Style::default()) | ||
| 767 | }); | ||
| 778 | 768 | ||
| 779 | let mut line = vec![user_gutter]; | 769 | let mut line = vec![user_gutter]; |
| 780 | line.extend(prev_line.spans); | 770 | line.extend(prev_line.spans); |
| @@ -1129,13 +1119,14 @@ impl Message { | |||
| 1129 | ) -> usize { | 1119 | ) -> usize { |
| 1130 | let width = viewctx.get_width(); | 1120 | let width = viewctx.get_width(); |
| 1131 | let user_gutter = settings.tunables.user_gutter_width; | 1121 | let user_gutter = settings.tunables.user_gutter_width; |
| 1122 | let time_gutter = settings.tunables.time_gutter_width; | ||
| 1132 | 1123 | ||
| 1133 | if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width && | 1124 | if user_gutter + time_gutter + READ_GUTTER + MIN_MSG_LEN <= width && |
| 1134 | settings.tunables.read_receipt_display | 1125 | settings.tunables.read_receipt_display |
| 1135 | { | 1126 | { |
| 1136 | width - user_gutter - TIME_GUTTER - READ_GUTTER | 1127 | width - user_gutter - time_gutter - READ_GUTTER |
| 1137 | } else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width { | 1128 | } else if user_gutter + time_gutter + MIN_MSG_LEN <= width { |
| 1138 | width - user_gutter - TIME_GUTTER | 1129 | width - user_gutter - time_gutter |
| 1139 | } else if user_gutter + MIN_MSG_LEN <= width { | 1130 | } else if user_gutter + MIN_MSG_LEN <= width { |
| 1140 | width - user_gutter | 1131 | width - user_gutter |
| 1141 | } else { | 1132 | } else { |
| @@ -1154,12 +1145,13 @@ impl Message { | |||
| 1154 | let date = self.show_date(prev).then(|| self.timestamp.show_date(settings)); | 1145 | let date = self.show_date(prev).then(|| self.timestamp.show_date(settings)); |
| 1155 | let trackbar = self.show_trackbar(prev, info, settings); | 1146 | let trackbar = self.show_trackbar(prev, info, settings); |
| 1156 | let user_gutter = settings.tunables.user_gutter_width; | 1147 | let user_gutter = settings.tunables.user_gutter_width; |
| 1148 | let time_gutter = settings.tunables.time_gutter_width; | ||
| 1157 | 1149 | ||
| 1158 | if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width && | 1150 | if user_gutter + time_gutter + READ_GUTTER + MIN_MSG_LEN <= width && |
| 1159 | settings.tunables.read_receipt_display | 1151 | settings.tunables.read_receipt_display |
| 1160 | { | 1152 | { |
| 1161 | let cols = MessageColumns::Four; | 1153 | let cols = MessageColumns::Four; |
| 1162 | let fill = width - user_gutter - TIME_GUTTER - READ_GUTTER; | 1154 | let fill = width - user_gutter - time_gutter - READ_GUTTER; |
| 1163 | let user = self.show_sender(prev, true, info, settings, width); | 1155 | let user = self.show_sender(prev, true, info, settings, width); |
| 1164 | let time = Some(self.timestamp.show_time(settings)); | 1156 | let time = Some(self.timestamp.show_time(settings)); |
| 1165 | 1157 | ||
| @@ -1190,9 +1182,9 @@ impl Message { | |||
| 1190 | read, | 1182 | read, |
| 1191 | info, | 1183 | info, |
| 1192 | } | 1184 | } |
| 1193 | } else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width { | 1185 | } else if user_gutter + time_gutter + MIN_MSG_LEN <= width { |
| 1194 | let cols = MessageColumns::Three; | 1186 | let cols = MessageColumns::Three; |
| 1195 | let fill = width - user_gutter - TIME_GUTTER; | 1187 | let fill = width - user_gutter - time_gutter; |
| 1196 | let user = self.show_sender(prev, true, info, settings, width); | 1188 | let user = self.show_sender(prev, true, info, settings, width); |
| 1197 | let time = Some(self.timestamp.show_time(settings)); | 1189 | let time = Some(self.timestamp.show_time(settings)); |
| 1198 | let read = Vec::new(); | 1190 | let read = Vec::new(); |
| @@ -1647,6 +1639,7 @@ pub mod tests { | |||
| 1647 | }; | 1639 | }; |
| 1648 | 1640 | ||
| 1649 | use crate::base::EventLocation; | 1641 | use crate::base::EventLocation; |
| 1642 | use crate::config::time_gutter_width; | ||
| 1650 | use crate::tests::*; | 1643 | use crate::tests::*; |
| 1651 | 1644 | ||
| 1652 | #[test] | 1645 | #[test] |
| @@ -2060,4 +2053,73 @@ pub mod tests { | |||
| 2060 | settings.tunables.message_formatted_display = false; | 2053 | settings.tunables.message_formatted_display = false; |
| 2061 | assert_eq!(render(&settings), "**hello** <world>"); | 2054 | assert_eq!(render(&settings), "**hello** <world>"); |
| 2062 | } | 2055 | } |
| 2056 | |||
| 2057 | /// `MessageTimeStamp::as_datetime` converts to the local timezone, so | ||
| 2058 | /// expectations are computed with the same conversion rather than a | ||
| 2059 | /// hard-coded wall clock. | ||
| 2060 | fn sample_stamp() -> (MessageTimeStamp, DateTime<LocalTz>) { | ||
| 2061 | let millis = MilliSecondsSinceUnixEpoch(UInt::try_from(1_705_325_130_000u64).unwrap()); | ||
| 2062 | let ts = MessageTimeStamp::from(millis); | ||
| 2063 | (ts, ts.as_datetime()) | ||
| 2064 | } | ||
| 2065 | |||
| 2066 | #[test] | ||
| 2067 | fn test_show_time_and_date_defaults() { | ||
| 2068 | let (ts, dt) = sample_stamp(); | ||
| 2069 | let settings = mock_settings(); | ||
| 2070 | |||
| 2071 | let date = ts.show_date(&settings); | ||
| 2072 | assert_eq!(date.content.as_ref(), dt.format("%A, %B %d %Y").to_string()); | ||
| 2073 | |||
| 2074 | let time = ts.show_time(&settings); | ||
| 2075 | assert_eq!(time.content.as_ref(), format!(" [{}]", dt.format("%T"))); | ||
| 2076 | assert_eq!(UnicodeWidthStr::width(time.content.as_ref()), 12); | ||
| 2077 | assert_eq!(settings.tunables.time_gutter_width, 12); | ||
| 2078 | } | ||
| 2079 | |||
| 2080 | #[test] | ||
| 2081 | fn test_time_gutter_width_follows_format() { | ||
| 2082 | let (ts, dt) = sample_stamp(); | ||
| 2083 | let mut settings = mock_settings(); | ||
| 2084 | let view = ViewportContext { dimensions: (120, 10), ..Default::default() }; | ||
| 2085 | let default_cols = Message::message_column_width(&view, &settings); | ||
| 2086 | |||
| 2087 | // "%H:%M" is five columns and is not padded out to the old gutter. | ||
| 2088 | settings.tunables.time_format = "%H:%M".to_string(); | ||
| 2089 | settings.tunables.time_gutter_width = time_gutter_width(&settings.tunables.time_format); | ||
| 2090 | let time = ts.show_time(&settings); | ||
| 2091 | let rendered = dt.format("%H:%M").to_string(); | ||
| 2092 | assert_eq!(time.content.as_ref(), rendered); | ||
| 2093 | assert_eq!(UnicodeWidthStr::width(rendered.as_str()), 5); | ||
| 2094 | assert_eq!(settings.tunables.time_gutter_width, 5); | ||
| 2095 | assert_eq!(Message::message_column_width(&view, &settings), default_cols + 7); | ||
| 2096 | |||
| 2097 | // An empty format hides the time and reserves no column. | ||
| 2098 | settings.tunables.time_format = String::new(); | ||
| 2099 | settings.tunables.time_gutter_width = time_gutter_width(&settings.tunables.time_format); | ||
| 2100 | let time = ts.show_time(&settings); | ||
| 2101 | assert_eq!(time.content.as_ref(), ""); | ||
| 2102 | assert_eq!(UnicodeWidthStr::width(time.content.as_ref()), 0); | ||
| 2103 | assert_eq!(settings.tunables.time_gutter_width, 0); | ||
| 2104 | assert_eq!(Message::message_column_width(&view, &settings), default_cols + 12); | ||
| 2105 | |||
| 2106 | // Wider than the historical 8-column cap, and a wide character counts as two. | ||
| 2107 | settings.tunables.time_format = "%H時%M分".to_string(); | ||
| 2108 | settings.tunables.time_gutter_width = time_gutter_width(&settings.tunables.time_format); | ||
| 2109 | let time = ts.show_time(&settings); | ||
| 2110 | let rendered = dt.format("%H時%M分").to_string(); | ||
| 2111 | assert_eq!(time.content.as_ref(), rendered); | ||
| 2112 | assert_eq!(UnicodeWidthStr::width(rendered.as_str()), 8); | ||
| 2113 | assert_eq!(settings.tunables.time_gutter_width, 8); | ||
| 2114 | assert_eq!(Message::message_column_width(&view, &settings), default_cols + 4); | ||
| 2115 | } | ||
| 2116 | |||
| 2117 | #[test] | ||
| 2118 | fn test_show_custom_date_format() { | ||
| 2119 | let (ts, dt) = sample_stamp(); | ||
| 2120 | let mut settings = mock_settings(); | ||
| 2121 | settings.tunables.date_format = "%Y-%m-%d".to_string(); | ||
| 2122 | let date = ts.show_date(&settings); | ||
| 2123 | assert_eq!(date.content.as_ref(), dt.format("%Y-%m-%d").to_string()); | ||
| 2124 | } | ||
| 2063 | } | 2125 | } |
src/tests.rs +3
| @@ -201,6 +201,9 @@ pub fn mock_tunables() -> TunableValues { | |||
| 201 | ssl_verify: true, | 201 | ssl_verify: true, |
| 202 | cache_policy: Default::default(), | 202 | cache_policy: Default::default(), |
| 203 | send_on_enter: true, | 203 | send_on_enter: true, |
| 204 | date_format: "%A, %B %d %Y".to_string(), | ||
| 205 | time_format: " [%T]".to_string(), | ||
| 206 | time_gutter_width: 12, | ||
| 204 | } | 207 | } |
| 205 | } | 208 | } |
| 206 | 209 | ||