Skip to content

Commit

Permalink
Support configuration files using confy
Browse files Browse the repository at this point in the history
  • Loading branch information
gbagnoli committed Aug 30, 2022
1 parent ad40ba3 commit 132e740
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 21 deletions.
80 changes: 75 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tzbuddy"
version = "0.2.0"
version = "0.3.0"
authors = ["Giacomo Bagnoli <[email protected]>"]
edition = "2018"
homepage = "https://github.com/gbagnoli/tzbuddy.rs"
Expand All @@ -13,7 +13,10 @@ categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = {version= "3", features=["cargo", "derive"]}
anyhow = "^1"
chrono = "^0.4"
chrono-tz = "^0.6"
clap = {version= "3", features=["cargo", "derive"]}
confy = "^0.4"
prettytable-rs = "^0.9"
serde = { version = "1.0", features = ["derive"] }
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ $ brew install tzbuddy
Usage
=======

See `tzbuddy --help` for all available options. There is no configuration, so
you probably want to create an alias in your shell.
See `tzbuddy --help` for all available options.

Prior to version `0.3`, `tzbuddy` did not support configuration files, and you
had to create an alias on the shell.

Since version `0.3` there is support for configuration. Simply pass `--save` and
`tzbuddy` will remember flags when running without. Eventual options on the
commandline takes precedence. `--no-config` will skip loading the config.
Running `--save` without any other option will save an empty config.

Development
===========
Expand Down
50 changes: 38 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Result;
use chrono::{DateTime, Datelike, Duration, NaiveDateTime, Timelike, Utc};
use chrono_tz::Tz;
use clap::Parser;
use prettytable::{format, Cell, Row, Table};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::HashMap;

Expand All @@ -12,7 +14,7 @@ struct TimezoneHours {
tz: Tz,
}

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Serialize, Deserialize, Default)]
#[clap(name = clap::crate_name!())]
#[clap(author = clap::crate_authors!("\n"))]
#[clap(version=clap::crate_version!())]
Expand All @@ -31,11 +33,17 @@ struct Cli {
#[clap(short = 'H', long = "no-header")]
noheader: bool,
/// How many hours to span
#[clap(short, long, default_value_t = 12)]
span: i32,
#[clap(short, long)]
span: Option<i32>,
/// Use 12h (am/pm) format
#[clap(short, long = "am-pm")]
ampm: bool,
/// Save config with current args
#[clap(long)]
save: bool,
/// Do not load config at startup
#[clap(long = "no-config")]
noconfig: bool,
///Calculate times from a specific date (YYYY-mm-dd HH:mm). If omitted, current time is used
date: Option<String>,
}
Expand Down Expand Up @@ -207,22 +215,40 @@ fn print_table(tz_hours: Vec<TimezoneHours>, date: DateTime<Utc>, no_header: boo
table.printstd();
}

fn main() {
fn main() -> Result<()> {
let cli = Cli::parse();
if cli.save {
println!("Saving config");
confy::store("tzbuddy", &cli)?;
}
let config: Cli = if cli.noconfig {
Cli::default()
} else {
confy::load("tzbuddy")?
};

let date = get_utc_date(cli.date);
let mut sort_order = SortOrder::East;
if cli.noorder {
if cli.noorder || config.noorder {
sort_order = SortOrder::None;
} else if cli.inverseorder {
} else if cli.inverseorder || config.noorder {
sort_order = SortOrder::West;
}
let tzhours = calculate_timezone_hours(
get_timezones(cli.timezones),
let timezones = match cli.timezones.len() {
0 => config.timezones,
_ => cli.timezones,
};
let span = match cli.span {
None => config.span.unwrap_or(12),
Some(span) => span,
};
let ampm = cli.ampm || config.ampm;
let tzhours = calculate_timezone_hours(get_timezones(timezones), date, span, ampm, sort_order);
print_table(
tzhours,
date,
cli.span,
cli.ampm,
sort_order,
cli.noheader || config.noheader,
cli.ampm || config.ampm,
);
print_table(tzhours, date, cli.noheader, cli.ampm);
Ok(())
}

0 comments on commit 132e740

Please sign in to comment.