From f757389df99b358623013be6d31dcc3b91bee0a4 Mon Sep 17 00:00:00 2001 From: Lovecraftian Horror Date: Thu, 9 Apr 2020 13:44:45 -0400 Subject: [PATCH] Add flag for specifying color preference --- bash_tealdeer | 4 ++++ fish_tealdeer | 1 + src/main.rs | 16 +++++++++++++--- src/types.rs | 8 ++++++++ zsh_tealdeer | 5 +++++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bash_tealdeer b/bash_tealdeer index ba28806..874356e 100644 --- a/bash_tealdeer +++ b/bash_tealdeer @@ -17,6 +17,10 @@ _tealdeer() COMPREPLY=( $(compgen -W 'linux osx sunos windows' -- "${cur}") ) return ;; + --color) + COMPREPLY=( $(compgen -W 'always auto never' -- "${cur}") ) + return + ;; esac if [[ $cur == -* ]]; then diff --git a/fish_tealdeer b/fish_tealdeer index 880b015..d571739 100644 --- a/fish_tealdeer +++ b/fish_tealdeer @@ -15,6 +15,7 @@ complete -c tldr -s m -l markdown -d 'Display the raw markdown instead of ren complete -c tldr -s q -l quiet -d 'Suppress informational messages.' -f complete -c tldr -l config-path -d 'Show config file path.' -f complete -c tldr -l seed-config -d 'Create a basic config.' -f +complete -c tldr -l color -d 'Controls when to use color.' -xa 'always auto never' function __tealdeer_entries tldr --list | string replace -a -i -r "\,\s" "\n" diff --git a/src/main.rs b/src/main.rs index 5fa518b..0886285 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ use crate::config::{get_config_path, make_default_config, Config, MAX_CACHE_AGE} use crate::error::TealdeerError::{CacheError, ConfigError, UpdateError}; use crate::formatter::print_lines; use crate::tokenizer::Tokenizer; -use crate::types::OsType; +use crate::types::{ColorOptions, OsType}; const NAME: &str = "tealdeer"; const APP_INFO: AppInfo = AppInfo { @@ -69,6 +69,7 @@ Options: -q --quiet Suppress informational messages --config-path Show config file path --seed-config Create a basic config + --color Control when to use color [always, auto, never] [default: auto] Examples: @@ -104,6 +105,7 @@ struct Args { flag_config_path: bool, flag_seed_config: bool, flag_markdown: bool, + flag_color: ColorOptions, } /// Print page by path @@ -294,6 +296,8 @@ fn main() { .and_then(|d| d.deserialize()) .unwrap_or_else(|e| e.exit()); + println!("{:?}", args); + // Show version and exit if args.flag_version { let os = get_os(); @@ -313,9 +317,15 @@ fn main() { // Determine the usage of styles #[cfg(target_os = "windows")] - let enable_styles = ansi_term::enable_ansi_support().is_ok(); + let ansi_support = ansi_term::enable_ansi_support().is_ok(); #[cfg(not(target_os = "windows"))] - let enable_styles = true; + let ansi_support = true; + + let enable_styles = match args.flag_color { + ColorOptions::Always => ansi_support, + ColorOptions::Auto => ansi_support, + ColorOptions::Never => false, + }; // Look up config file, if none is found fall back to default config. let config = match Config::load(enable_styles) { diff --git a/src/types.rs b/src/types.rs index d7e4548..c0dfb27 100644 --- a/src/types.rs +++ b/src/types.rs @@ -27,6 +27,14 @@ impl fmt::Display for OsType { } } +#[derive(Debug, Eq, PartialEq, Copy, Clone, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ColorOptions { + Always, + Auto, + Never, +} + #[derive(Debug, Eq, PartialEq)] pub enum LineType { Empty, diff --git a/zsh_tealdeer b/zsh_tealdeer index 53f2609..4eff79f 100644 --- a/zsh_tealdeer +++ b/zsh_tealdeer @@ -25,6 +25,11 @@ _tealdeer() { "($I -q --quiet)"{-q,--quiet}"[Suppress informational messages]" "($I)--config-path[Show config file path]" "($I)--seed-config[Create a basic config]" + "($I --color)"{--color}'[Controls when to use color]:when:(( + always + auto + never + ))' '(- *)'{-h,--help}'[Display help]' '(- *)'{-v,--version}'[Show version information]' '*:file:_files'