Skip to content

Commit

Permalink
feat(cli): add plain flag to not colorize
Browse files Browse the repository at this point in the history
Closes #138
  • Loading branch information
Ninjani committed Aug 24, 2022
1 parent 9e263b1 commit aeaaaba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/the_way/cli.rs
Expand Up @@ -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,
}
Expand Down
8 changes: 7 additions & 1 deletion src/the_way/mod.rs
Expand Up @@ -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
Expand All @@ -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)?;
Expand All @@ -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),
Expand Down Expand Up @@ -165,6 +169,7 @@ impl TheWay {
)?,
false,
self.colorize,
self.plain,
)?;
Ok(())
}
Expand Down Expand Up @@ -273,7 +278,7 @@ impl TheWay {
)?,
);
}
utils::smart_print(&colorized, false, self.colorize)?;
utils::smart_print(&colorized, false, self.colorize, self.plain)?;
Ok(())
}

Expand Down Expand Up @@ -403,6 +408,7 @@ impl TheWay {
&[(self.highlighter.main_style, input.to_string())],
false,
self.colorize,
self.plain,
)?;
Ok(())
}
Expand Down
17 changes: 12 additions & 5 deletions src/utils.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn parse_date(date_string: &str) -> color_eyre::Result<Date<Utc>> {
pub fn date_start(from_date: Option<Date<Utc>>) -> DateTime<Utc> {
match from_date {
Some(from_date) => from_date.and_hms(0, 0, 0),
None => MIN_DATE.and_hms(0, 0, 0),
None => Date::<Utc>::MIN_UTC.and_hms(0, 0, 0),
}
}

Expand All @@ -151,7 +151,7 @@ pub fn date_start(from_date: Option<Date<Utc>>) -> DateTime<Utc> {
pub fn date_end(to_date: Option<Date<Utc>>) -> DateTime<Utc> {
match to_date {
Some(to_date) => to_date.and_hms(23, 59, 59),
None => MAX_DATE.and_hms(23, 59, 59),
None => Date::<Utc>::MAX_UTC.and_hms(23, 59, 59),
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aeaaaba

Please sign in to comment.