2024-08-06 20:01:59 +00:00
|
|
|
use std::fmt::Display;
|
|
|
|
use std::io::{stdin, stdout, Write};
|
|
|
|
|
2024-08-14 18:49:36 +00:00
|
|
|
use chrono::{Local, NaiveDateTime, TimeZone};
|
|
|
|
use chrono::LocalResult::Single;
|
2024-08-06 20:01:59 +00:00
|
|
|
use log::{debug, error, info, trace, warn};
|
2024-08-14 18:49:36 +00:00
|
|
|
use nostr_sdk::Timestamp;
|
2024-08-06 20:01:59 +00:00
|
|
|
|
|
|
|
pub fn some_non_empty(str: &str) -> Option<String> {
|
2024-08-08 12:09:39 +00:00
|
|
|
if str.is_empty() { None } else { Some(str.to_string()) }
|
2024-08-06 20:01:59 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 12:59:43 +00:00
|
|
|
// TODO as macro so that log comes from appropriate module
|
2024-08-06 20:01:59 +00:00
|
|
|
pub fn or_print<T, U: Display>(result: Result<T, U>) -> Option<T> {
|
|
|
|
match result {
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
Err(error) => {
|
|
|
|
warn!("{}", error);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prompt(prompt: &str) -> Option<String> {
|
|
|
|
print!("{} ", prompt);
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
match stdin().lines().next() {
|
|
|
|
Some(Ok(line)) => Some(line),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 18:49:36 +00:00
|
|
|
// For use in format strings but not possible, so need global find-replace
|
|
|
|
pub const MAX_TIMESTAMP_WIDTH: u8 = 15;
|
|
|
|
/// Format nostr Timestamp relative to local time with optional day specifier or full date if needed
|
|
|
|
pub fn relative_datetimestamp(stamp: &Timestamp) -> String {
|
|
|
|
match Local.timestamp_opt(stamp.as_u64() as i64, 0) {
|
|
|
|
Single(time) => {
|
|
|
|
let date = time.date_naive();
|
|
|
|
let prefix = match Local::now()
|
|
|
|
.date_naive()
|
|
|
|
.signed_duration_since(date)
|
|
|
|
.num_days()
|
|
|
|
{
|
|
|
|
-1 => "tomorrow ".into(),
|
|
|
|
0 => "".into(),
|
|
|
|
1 => "yesterday ".into(),
|
|
|
|
2..=6 => date.format("last %a ").to_string(),
|
|
|
|
_ => date.format("%y-%m-%d ").to_string(),
|
|
|
|
};
|
|
|
|
format!("{}{}", prefix, time.format("%H:%M"))
|
|
|
|
}
|
|
|
|
_ => stamp.to_human_datetime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn local_datetimestamp(stamp: &Timestamp) -> String {
|
|
|
|
format_stamp(stamp, "%y-%m-%d %a %H:%M")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn format_stamp(stamp: &Timestamp, format: &str) -> String {
|
|
|
|
match Local.timestamp_opt(stamp.as_u64() as i64, 0) {
|
|
|
|
Single(time) => time.format(format).to_string(),
|
|
|
|
_ => stamp.to_human_datetime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|