Browse Source

*way* too much twiddling with ascii layout of output!

tags/v1.2.0
Jonathan Strong 3 years ago
parent
commit
ab3b2cd410
2 changed files with 79 additions and 21 deletions
  1. +1
    -1
      Cargo.toml
  2. +78
    -20
      src/lib.rs

+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
[package]
name = "tzconvert"
version = "1.1.0"
version = "1.2.0"
edition = "2018"
authors = ["Jonathan Strong <jstrong@mmcxi.com>"]



+ 78
- 20
src/lib.rs View File

@@ -6,6 +6,7 @@ use colored::*;
lazy_static::lazy_static! {
static ref HOUR_AMPM: regex::Regex = regex::Regex::new(r#"(?P<hr>\d{1,2})\s?(?P<ampm>(am|pm|AM|PM))"#).unwrap();
static ref HOUR_MINUTE_AMPM: regex::Regex = regex::Regex::new(r#"(?P<hr>\d{1,2}):(?P<minute>\d{2})\s?(?P<ampm>(am|pm|AM|PM))"#).unwrap();
static ref ZERO_PADDED_OFFSET: regex::Regex = regex::Regex::new(r#"(?P<sign>[+-])0(?P<hr>\d)h"#).unwrap();
}

fn parse_time(time_input: &str) -> NaiveTime {
@@ -84,38 +85,95 @@ pub fn convert(from: &str, to: &str, time: &str, day: Option<String>, date: Opti

let dst = src.with_timezone(&totz);

let fromtz_str = fromtz.to_string();
let totz_str = totz.to_string();
let n_spaces = std::cmp::max(fromtz_str.len(), totz_str.len());

let indent = " ";
println!();
println!("{}{} ... {} ... {}",
indent,
pad_spaces(fromtz_str, n_spaces),
src.format("%a, %b %e"),
src.format("%l:%M%P"),
);
// let indent = " ";
// println!();
// println!("{}{} ... {} ... {}",
// indent,
// pad_spaces(fromtz_str, n_spaces),
// src.format("%a, %b %e"),
// src.format("%l:%M%P"),
// );

// println!();

// let dst_line = format!("{}{} ... {} ... {}",
// indent,
// pad_spaces(totz_str, n_spaces),
// dst.format("%a, %b %e"),
// dst.format("%l:%M%P").to_string(),
// );

// println!("{}", dst_line.bold());
println!("tzconvert v{}", structopt::clap::crate_version!());
println!();
let line = get_output(src, dst);
println!(" {}", line);
println!();
}

let dst_line = format!("{}{} ... {} ... {}",
indent,
pad_spaces(totz_str, n_spaces),
dst.format("%a, %b %e"),
dst.format("%l:%M%P").to_string(),
fn get_output(src: DateTime<Tz>, dst: DateTime<Tz>) -> String {
let mut out = String::with_capacity(128);

let fromtz_str = src.timezone().to_string();
let totz_str = dst.timezone().to_string();
let n_spaces = std::cmp::max(fromtz_str.len(), totz_str.len());

//let mut src_str = format!("{tz} | {offset}h | {dt} | {tm}",
let mut src_str = format!("{tz} --+-- {offset}h --+-- {dt} --+-- {tm}",
tz = pad(fromtz_str, n_spaces, " "),
offset = src.offset().fix(),
dt = src.format("%a, %b %e"),
tm = src.format("%l:%M%P"),
);
src_str = clean(src_str);
out.push_str(&format!("\n {}\n {}\n {}\n", pad("", src_str.len(), " "), src_str, pad("", src_str.len(), " ")));
//out.push_str(" -> ");

let abs_diff_in_hours = (src.offset().fix().local_minus_utc() - dst.offset().fix().local_minus_utc()).abs() / 60 / 60;
out.push_str(&format!(" .\n .\n . {diff}h\n .\n .\n ", diff = abs_diff_in_hours));

//let mut dst_str = format!("{tz} | {offset}h | {dt} | {tm}",
let mut dst_str = format!("{tz} --+-- {offset}h --+-- {dt} --+-- {tm}",
//tz = pad_spaces(dst.timezone(), n_spaces),
tz = pad(totz_str, n_spaces, " "),
offset = dst.offset().fix(),
dt = dst.format("%a, %b %e"),
tm = dst.format("%l:%M%P"),
);
dst_str = clean(dst_str);
out.push_str(&format!(" {}\n {}\n {}\n",
pad("", dst_str.len(), " "),
dst_str.bold(),
pad("", dst_str.len(), " "))
);
//out.push_str(&format!(" {}\n {}\n {}\n\n",
// pad("", dst_str.len(), "-"), dst_str.bold(), pad("", dst_str.len(), "-"))
//);

//out = out.replace(
// " -> ",
// &format!("\n\n .\n . {diff}h\n .\n\n ", diff = abs_diff_in_hours),
//);
out
}

println!("{}", dst_line.bold());
println!();
fn clean(mut out: String) -> String {
out = out.replace(":00", "");
//while out.contains(" ") {
// out = out.replace(" ", " ");
//}

out = ZERO_PADDED_OFFSET.replace_all(&out, "${sign}${hr}h").to_string();
out
}

fn pad_spaces<S: ToString>(s: S, n: usize) -> String {
fn pad<S: ToString>(s: S, n: usize, with: &str) -> String {
let s = s.to_string();
let mut out = String::with_capacity(n);
out.push_str(&s);
while out.len() < n {
out.push_str(" ");
out.push_str(with);
}
out
}


Loading…
Cancel
Save