diff --git a/src/the_way/cli.rs b/src/the_way/cli.rs index 5870303..9e4925a 100644 --- a/src/the_way/cli.rs +++ b/src/the_way/cli.rs @@ -17,8 +17,11 @@ global_settings = & [AppSettings::DeriveDisplayOrder] /// Record, retrieve, search, and categorize code snippets pub struct TheWayCLI { /// Force colorization even when not in TTY mode - #[structopt(short, long, global = true)] + #[structopt(short, long)] pub colorize: bool, + /// Turn off colorization + #[structopt(short, long, conflicts_with = "colorize")] + pub plain: bool, #[structopt(subcommand)] pub cmd: TheWaySubcommand, } diff --git a/src/the_way/mod.rs b/src/the_way/mod.rs index 2bcabe1..4c2c53d 100644 --- a/src/the_way/mod.rs +++ b/src/the_way/mod.rs @@ -42,6 +42,8 @@ pub struct TheWay { highlighter: CodeHighlight, /// colorize output even if terminal is not in tty mode colorize: bool, + /// don't colorize output even if terminal is in tty mode + plain: bool, } // All command-line related functions @@ -65,6 +67,7 @@ impl TheWay { highlighter: CodeHighlight::new(&config.theme, config.themes_dir.clone())?, config, colorize: cli.colorize, + plain: cli.plain, }; the_way.set_merge()?; the_way.run(cli)?; @@ -73,6 +76,7 @@ impl TheWay { fn run(&mut self, cli: TheWayCLI) -> color_eyre::Result<()> { self.colorize = cli.colorize; + self.plain = cli.plain; match cli.cmd { TheWaySubcommand::New => self.the_way(), TheWaySubcommand::Cmd { code } => self.the_way_cmd(code), @@ -165,6 +169,7 @@ impl TheWay { )?, false, self.colorize, + self.plain, )?; Ok(()) } @@ -273,7 +278,7 @@ impl TheWay { )?, ); } - utils::smart_print(&colorized, false, self.colorize)?; + utils::smart_print(&colorized, false, self.colorize, self.plain)?; Ok(()) } @@ -403,6 +408,7 @@ impl TheWay { &[(self.highlighter.main_style, input.to_string())], false, self.colorize, + self.plain, )?; Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index cdd2673..753e833 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,7 +2,7 @@ use std::io::Write; use std::process::{Command, Stdio}; use std::str; -use chrono::{Date, DateTime, Utc, MAX_DATE, MIN_DATE}; +use chrono::{Date, DateTime, Utc}; use chrono_english::{parse_date_string, Dialect}; use color_eyre::Help; use dialoguer::{Confirm, Editor, Input}; @@ -142,7 +142,7 @@ pub fn parse_date(date_string: &str) -> color_eyre::Result> { pub fn date_start(from_date: Option>) -> DateTime { match from_date { Some(from_date) => from_date.and_hms(0, 0, 0), - None => MIN_DATE.and_hms(0, 0, 0), + None => Date::::MIN_UTC.and_hms(0, 0, 0), } } @@ -151,7 +151,7 @@ pub fn date_start(from_date: Option>) -> DateTime { pub fn date_end(to_date: Option>) -> DateTime { match to_date { Some(to_date) => to_date.and_hms(23, 59, 59), - None => MAX_DATE.and_hms(23, 59, 59), + None => Date::::MAX_UTC.and_hms(23, 59, 59), } } @@ -240,11 +240,18 @@ pub fn highlight_strings(inputs: &[(Style, String)], bg: bool) -> String { } /// Print with color if stdout is tty else without -pub fn smart_print(inputs: &[(Style, String)], bg: bool, colorize: bool) -> color_eyre::Result<()> { +/// if colorize, always uses color +/// if plain, doesn't use color +pub fn smart_print( + inputs: &[(Style, String)], + bg: bool, + colorize: bool, + plain: bool, +) -> color_eyre::Result<()> { write!( grep_cli::stdout(termcolor::ColorChoice::Auto), "{}", - if grep_cli::is_tty_stdout() | colorize { + if !plain & (grep_cli::is_tty_stdout() | colorize) { highlight_strings(inputs, bg) } else { inputs