diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index 00273b88e0..525e2162e0 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -47,6 +47,92 @@ impl WholeStreamCommand for ToHTML { } } +fn get_colors(is_dark: bool) -> HashMap<&'static str, String> { + // Currently now using bold_white and bold_black. + // This is not theming but it is kind of a start. The intent here is to use the + // regular terminal colors which appear on black for most people and are very + // high contrast. But when there's a light background, use something that works + // better for it. + + // for reference here is Microsoft's Campbell Theme + // taken from here + // https://docs.microsoft.com/en-us/windows/terminal/customize-settings/color-schemes + // { + // "name" : "Campbell", + + // "cursorColor": "#FFFFFF", + // "selectionBackground": "#FFFFFF", + + // "background" : "#0C0C0C", + // "foreground" : "#CCCCCC", + + // "black" : "#0C0C0C", + // "blue" : "#0037DA", + // "cyan" : "#3A96DD", + // "green" : "#13A10E", + // "purple" : "#881798", + // "red" : "#C50F1F", + // "white" : "#CCCCCC", + // "yellow" : "#C19C00", + // "brightBlack" : "#767676", + // "brightBlue" : "#3B78FF", + // "brightCyan" : "#61D6D6", + // "brightGreen" : "#16C60C", + // "brightPurple" : "#B4009E", + // "brightRed" : "#E74856", + // "brightWhite" : "#F2F2F2", + // "brightYellow" : "#F9F1A5" + // }, + + let mut hm: HashMap<&str, String> = HashMap::new(); + + if is_dark { + hm.insert("bold_black", "black".to_string()); + hm.insert("bold_red", "red".to_string()); + hm.insert("bold_green", "green".to_string()); + hm.insert("bold_yellow", "yellow".to_string()); + hm.insert("bold_blue", "blue".to_string()); + hm.insert("bold_magenta", "magenta".to_string()); + hm.insert("bold_cyan", "cyan".to_string()); + hm.insert("bold_white", "white".to_string()); + + hm.insert("black", "black".to_string()); + hm.insert("red", "red".to_string()); + hm.insert("green", "green".to_string()); + hm.insert("yellow", "yellow".to_string()); + hm.insert("blue", "blue".to_string()); + hm.insert("magenta", "magenta".to_string()); + hm.insert("cyan", "cyan".to_string()); + hm.insert("white", "white".to_string()); + + hm.insert("background", "black".to_string()); + hm.insert("foreground", "white".to_string()); + } else { + hm.insert("bold_black", "black".to_string()); + hm.insert("bold_red", "red".to_string()); + hm.insert("bold_green", "green".to_string()); + hm.insert("bold_yellow", "#717100".to_string()); + hm.insert("bold_blue", "blue".to_string()); + hm.insert("bold_magenta", "#c800c8".to_string()); + hm.insert("bold_cyan", "#037979".to_string()); + hm.insert("bold_white", "white".to_string()); + + hm.insert("black", "black".to_string()); + hm.insert("red", "red".to_string()); + hm.insert("green", "green".to_string()); + hm.insert("yellow", "#717100".to_string()); + hm.insert("blue", "blue".to_string()); + hm.insert("magenta", "#c800c8".to_string()); + hm.insert("cyan", "#037979".to_string()); + hm.insert("white", "white".to_string()); + + hm.insert("background", "white".to_string()); + hm.insert("foreground", "black".to_string()); + } + + hm +} + async fn to_html( args: CommandArgs, registry: &CommandRegistry, @@ -185,11 +271,7 @@ async fn to_html( } fn setup_html_color_regexes(hash: &mut HashMap, is_dark: bool) { - let text_color = if is_dark { - "white".to_string() - } else { - "black".to_string() - }; + let color_hm = get_colors(is_dark); // All the bold colors hash.insert( @@ -199,7 +281,9 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ // Reset the text color, normal weight font format!( r"$word", - text_color + color_hm + .get("foreground") + .expect("Error getting reset text color") ), ), ); @@ -210,7 +294,9 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ r"(?P\[1;30m)(?P[[:alnum:][:space:][:punct:]]*)", format!( r"$word", - text_color + color_hm + .get("foreground") + .expect("Error getting bold black text color") ), ), ); @@ -219,7 +305,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Bold Red r"(?P
\[1;31m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("bold_red") + .expect("Error getting bold red text color"), + ), ), ); hash.insert( @@ -227,7 +318,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Bold Green r"(?P\[1;32m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("bold_green") + .expect("Error getting bold green text color"), + ), ), ); hash.insert( @@ -235,7 +331,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Bold Yellow r"(?P\[1;33m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("bold_yellow") + .expect("Error getting bold yellow text color"), + ), ), ); hash.insert( @@ -243,7 +344,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Bold Blue r"(?P\[1;34m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("bold_blue") + .expect("Error getting bold blue text color"), + ), ), ); hash.insert( @@ -251,7 +357,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Bold Magenta r"(?P\[1;35m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("bold_magenta") + .expect("Error getting bold magenta text color"), + ), ), ); hash.insert( @@ -259,7 +370,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Bold Cyan r"(?P\[1;36m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("bold_cyan") + .expect("Error getting bold cyan text color"), + ), ), ); hash.insert( @@ -271,7 +387,9 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ r"(?P\[1;37m)(?P[[:alnum:][:space:][:punct:]]*)", format!( r"$word", - text_color + color_hm + .get("foreground") + .expect("Error getting bold bold white text color"), ), ), ); @@ -281,7 +399,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Black r"(?P\[30m)(?P[[:alnum:][:space:][:punct:]]*)", - format!(r"$word", text_color), + format!( + r"$word", + color_hm + .get("foreground") + .expect("Error getting black text color"), + ), ), ); hash.insert( @@ -289,7 +412,10 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Red r"(?P\[31m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm.get("red").expect("Error getting red text color"), + ), ), ); hash.insert( @@ -297,7 +423,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Green r"(?P\[32m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("green") + .expect("Error getting green text color"), + ), ), ); hash.insert( @@ -305,7 +436,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Yellow r"(?P\[33m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("yellow") + .expect("Error getting yellow text color"), + ), ), ); hash.insert( @@ -313,7 +449,10 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Blue r"(?P\[34m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm.get("blue").expect("Error getting blue text color"), + ), ), ); hash.insert( @@ -321,7 +460,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Magenta r"(?P\[35m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm + .get("magenta") + .expect("Error getting magenta text color"), + ), ), ); hash.insert( @@ -329,7 +473,10 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ ( // Cyan r"(?P\[36m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word".to_string(), + format!( + r"$word", + color_hm.get("cyan").expect("Error getting cyan text color"), + ), ), ); hash.insert( @@ -339,7 +486,12 @@ fn setup_html_color_regexes(hash: &mut HashMap, is_ // Let's change this to black since the html background // is white. White on white = no bueno. r"(?P\[37m)(?P[[:alnum:][:space:][:punct:]]*)", - format!(r"$word", text_color), + format!( + r"$word", + color_hm + .get("foreground") + .expect("Error getting white text color"), + ), ), ); } @@ -396,7 +548,7 @@ mod tests { fn test_cd_html_color_flag_dark_true() { let mut hm: HashMap = HashMap::new(); let cd_help = r"Change to a new path.

Usage:
> cd (directory) {flags}

Parameters:
(directory) the directory to change to

Flags:
-h, --help: Display this help message

Examples:
Change to a new directory called 'dirname'
> cd dirname

Change to your home directory
> cd

Change to your home directory (alternate version)
> cd ~

Change to the previous directory
> cd -

".to_string(); - let cd_help_expected_result = r"Change to a new path.

Usage:
> cd (directory) {flags}

Parameters:
(directory) the directory to change to

Flags:
-h, --help: Display this help message

Examples:
Change to a new directory called 'dirname'
> cd dirname

Change to your home directory
>
cd

Change to your home directory (alternate version)
>
cd
~

Change to the previous directory
>
cd
-

".to_string(); + let cd_help_expected_result = r"Change to a new path.

Usage:
> cd (directory) {flags}

Parameters:
(directory) the directory to change to

Flags:
-h, --help: Display this help message

Examples:
Change to a new directory called 'dirname'
> cd dirname

Change to your home directory
>
cd

Change to your home directory (alternate version)
>
cd
~

Change to the previous directory
>
cd
-

".to_string(); let is_dark = true; setup_html_color_regexes(&mut hm, is_dark); assert_eq!(cd_help_expected_result, run_regexes(&hm, &cd_help)); @@ -415,7 +567,7 @@ mod tests { fn test_html_color_where_flag_dark_true() { let mut hm: HashMap = HashMap::new(); let where_help = r"Filter table to match the condition.

Usage:
> where <condition> {flags}

Parameters:
<condition> the condition that must match

Flags:
-h, --help: Display this help message

Examples:
List all files in the current directory with sizes greater than 2kb
> ls | where size > 2kb

List only the files in the current directory
> ls | where type == File

List all files with names that contain "Car"
> ls | where name =~ "Car"

List all files that were modified in the last two months
> ls | where modified <= 2M

".to_string(); - let where_help_exptected_results = r"Filter table to match the condition.

Usage:
> where <condition> {flags}

Parameters:
<condition> the condition that must match

Flags:
-h, --help: Display this help message

Examples:
List all files in the current directory with sizes greater than 2kb
> ls | where size > 2kb

List only the files in the current directory
>
ls
| where type == File

List all files with names that contain "Car"
>
ls
| where name =~ "Car"

List all files that were modified in the last two months
>
ls
| where modified <= 2M

".to_string(); + let where_help_exptected_results = r"Filter table to match the condition.

Usage:
> where <condition> {flags}

Parameters:
<condition> the condition that must match

Flags:
-h, --help: Display this help message

Examples:
List all files in the current directory with sizes greater than 2kb
> ls | where size > 2kb

List only the files in the current directory
>
ls
| where type == File

List all files with names that contain "Car"
>
ls
| where name =~ "Car"

List all files that were modified in the last two months
>
ls
| where modified <= 2M

".to_string(); let is_dark = true; setup_html_color_regexes(&mut hm, is_dark); assert_eq!(where_help_exptected_results, run_regexes(&hm, &where_help));