Add a CLI app that does the same as the GTK app

This commit is contained in:
Savanni D'Gerinel 2024-02-01 16:41:31 -05:00
parent b022153a68
commit 47d5ec73a7
No known key found for this signature in database
GPG Key ID: 3467D9CC77F953B8
3 changed files with 102 additions and 3 deletions

View File

@ -5,10 +5,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
gui = ["gtk", "adw", "gio"]
[dependencies]
adw = { version = "0.4", package = "libadwaita", features = ["v1_2"] }
gio = "0.18.3"
gtk = { version = "0.6", package = "gtk4" }
adw = { version = "0.4", package = "libadwaita", features = ["v1_2"], optional = true }
gio = { version = "0.18.3", optional = true }
gtk = { version = "0.6", package = "gtk4", optional = true }
icu = { version = "1.4.0", features = ["experimental"] }
icu_provider = "1.4.0"
icu_locid = "1.4.0"

96
src/bin/cli-app.rs Normal file
View File

@ -0,0 +1,96 @@
use chrono::{Datelike, Local, Timelike};
use icu::{
calendar::{
types::{IsoHour, IsoMinute, IsoSecond, NanoSecond, Time},
Date, DateTime,
},
datetime::{
options::length::{self, Bag},
DateTimeFormatter,
},
};
use icu_locid::Locale;
use icu_provider::DataLocale;
use libc::{self, LC_MESSAGES, LC_TIME};
fn main() {
let now = chrono::Utc::now();
let now: icu::calendar::DateTime<icu::calendar::Gregorian> = icu::calendar::DateTime::new(
Date::try_new_gregorian_date(now.year() as i32, now.month() as u8, now.day() as u8)
.unwrap(),
Time::new(
IsoHour::try_from(now.hour() as u8).unwrap(),
IsoMinute::try_from(now.minute() as u8).unwrap(),
IsoSecond::try_from(now.second() as u8).unwrap(),
NanoSecond::try_from(0 as u32).unwrap(),
),
);
let locales = ["en-US", "eo", "de-DE", "fr-FR", "es-ES", "zh-CN", "ja-JA"];
let styles: Vec<(String, Locale, Bag)> = locales
.into_iter()
.map(|locale| {
let lc = locale.parse::<Locale>().unwrap();
vec![
(
format!("{}, full date", locale),
lc.clone(),
length::Bag::from_date_style(length::Date::Full).into(),
),
(
format!("{}, long date", locale),
lc.clone(),
length::Bag::from_date_style(length::Date::Long).into(),
),
(
format!("{}, medium date", locale),
lc.clone(),
length::Bag::from_date_style(length::Date::Medium).into(),
),
(
format!("{}, short date", locale),
lc.clone(),
length::Bag::from_date_style(length::Date::Short).into(),
),
(
format!("{}, full time", locale),
lc.clone(),
length::Bag::from_time_style(length::Time::Full).into(),
),
(
format!("{}, long time", locale),
lc.clone(),
length::Bag::from_time_style(length::Time::Long).into(),
),
(
format!("{}, medium time", locale),
lc.clone(),
length::Bag::from_time_style(length::Time::Medium).into(),
),
(
format!("{}, short time", locale),
lc.clone(),
length::Bag::from_time_style(length::Time::Short).into(),
),
]
})
.flatten()
.collect();
for (descriptor, locale, bag) in styles {
let formatter = DateTimeFormatter::try_new(&DataLocale::from(locale), bag.into());
match formatter {
Ok(formatter) => println!(
"[{:20}] {}",
descriptor,
&formatter.format_to_string(&now.to_any()).unwrap()
),
Err(formatter_err) => println!(
"[{:20}] {}",
&descriptor,
&format!("unhandled formatter: {:?}", formatter_err),
),
}
}
}