Implemented better ui.

This commit is contained in:
didier amyot committed 2023-04-09 06:38:07 -04:00
1 parent 358fb19076
commit 20b5f32c71
8 files changed
+399 -49

No files matched your search

+66 -29
View File
@@ -1,30 +1,67 @@
use chrono::{DateTime, NaiveDateTime, Utc};
use regex::Regex;
//
// fn is_number(value: String) -> bool{
// true
// }
//
// fn pretty_number(value: String) -> String {
// let re = Regex::new(r"\d+").unwrap();
// let cap = re.captures(&value);
// if cap.is_none() {
// return value;
// }
// let mut ret = "".to_string();
// for (idx, letter) in value.as_bytes().iter().enumerate() {
// ret.push(letter);
// }
// "foo".to_string()
//
// }
//
//
// #[cfg(test)]
// pub mod test_minininja_utils {
// use super::*;
//
// #[test]
// fn test_pretty_number() {
// assert_eq!(pretty_number("foo".to_string()), "foo");
// }
// }
use lazy_static::lazy_static;
use crate::db::external_id::ExternalId;
pub fn add_thousand_separators(value: String) -> String {
lazy_static! {
static ref is_number: Regex = Regex::new(r"\d+").unwrap();
}
let cap = is_number.captures(&value);
if cap.is_none() {
return value;
}
let start = value.len() % 3;
let n_blocks = value.len() / 3;
let mut ret = String::new();
ret += value.get(0..start).unwrap_or("");
for i in 0..n_blocks {
ret += " ";
ret += value.get((start + i * 3)..(start + (i + 1) * 3)).unwrap_or("");
}
ret
}
pub fn utc_date(value: String) -> String {
let timestamp = value.parse::<i64>();
if timestamp.is_err() {
return value;
}
let naive = NaiveDateTime::from_timestamp_opt(timestamp.unwrap(), 0);
if naive.is_none() {
return value;
}
let datetime: DateTime<Utc> = DateTime::from_utc(naive.unwrap(), Utc);
datetime.format("%Y-%m-%d %H:%M:%S").to_string()
}
pub fn micro_id(value: String) -> String {
ExternalId::from_string(&value).map(|id| id.micro_id()).unwrap_or(value)
}
#[cfg(test)]
pub mod test_minininja_utils {
use super::*;
#[test]
fn test_add_thousand_separator() {
assert_eq!(add_thousand_separators("foo".to_string()), "foo");
assert_eq!(add_thousand_separators("💖💖".to_string()), "💖💖");
assert_eq!(add_thousand_separators("10".to_string()), "10");
assert_eq!(add_thousand_separators("1000".to_string()), "1 000");
assert_eq!(add_thousand_separators("1000".to_string()), "1 000");
assert_eq!(add_thousand_separators("12345".to_string()), "12 345");
assert_eq!(add_thousand_separators("12345678901234".to_string()), "12 345 678 901 234");
}
#[test]
fn test_utc_date() {
assert_eq!(utc_date("foo".to_string()), "foo");
assert_eq!(utc_date("💖💖".to_string()), "💖💖");
assert_eq!(utc_date("1524820690".to_string()), "2018-04-27 09:18:10");
}
}
+3
View File
@@ -49,6 +49,9 @@ fn create_auto_reloader(enable_template_autoreload: bool) -> AutoReloader {
notifier.watch_path(&tmpl_path, true);
}
env.set_source(minijinja::Source::from_path(tmpl_path));
env.add_filter("thousand_separator", minininja_utils::add_thousand_separators);
env.add_filter("utc_date", minininja_utils::utc_date);
env.add_filter("micro_id", minininja_utils::micro_id);
Ok(env)
})
}