diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index 9f5924935d..a433966c02 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -15,6 +15,7 @@ pub struct ToHTMLArgs { html_color: bool, no_color: bool, dark_bg: bool, + use_campbell: bool, } #[async_trait] @@ -32,6 +33,11 @@ impl WholeStreamCommand for ToHTML { "indicate your background color is a darker color", Some('d'), ) + .switch( + "use_campbell", + "use microsoft's windows terminal color scheme named campell", + Some('c'), + ) } fn usage(&self) -> &str { @@ -47,43 +53,60 @@ 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. - +fn get_campbell_theme(is_dark: bool) -> HashMap<&'static str, String> { // 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", + let mut hm: HashMap<&str, String> = HashMap::new(); - // "cursorColor": "#FFFFFF", - // "selectionBackground": "#FFFFFF", + if is_dark { + hm.insert("bold_black", "#767676".to_string()); + hm.insert("bold_red", "#E74856".to_string()); + hm.insert("bold_green", "#16C60C".to_string()); + hm.insert("bold_yellow", "#F9F1A5".to_string()); + hm.insert("bold_blue", "#3B78FF".to_string()); + hm.insert("bold_magenta", "#B4009E".to_string()); + hm.insert("bold_cyan", "#61D6D6".to_string()); + hm.insert("bold_white", "#F2F2F2".to_string()); - // "background" : "#0C0C0C", - // "foreground" : "#CCCCCC", + hm.insert("black", "#0C0C0C".to_string()); + hm.insert("red", "#C50F1F".to_string()); + hm.insert("green", "#13A10E".to_string()); + hm.insert("yellow", "#C19C00".to_string()); + hm.insert("blue", "#0037DA".to_string()); + hm.insert("magenta", "#881798".to_string()); + hm.insert("cyan", "#3A96DD".to_string()); + hm.insert("white", "#CCCCCC".to_string()); - // "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" - // }, + hm.insert("background", "#0C0C0C".to_string()); + hm.insert("foreground", "#CCCCCC".to_string()); + } else { + hm.insert("bold_black", "#767676".to_string()); + hm.insert("bold_red", "#E74856".to_string()); + hm.insert("bold_green", "#16C60C".to_string()); + hm.insert("bold_yellow", "#F9F1A5".to_string()); + hm.insert("bold_blue", "#3B78FF".to_string()); + hm.insert("bold_magenta", "#B4009E".to_string()); + hm.insert("bold_cyan", "#61D6D6".to_string()); + hm.insert("bold_white", "#F2F2F2".to_string()); + hm.insert("black", "#0C0C0C".to_string()); + hm.insert("red", "#C50F1F".to_string()); + hm.insert("green", "#13A10E".to_string()); + hm.insert("yellow", "#C19C00".to_string()); + hm.insert("blue", "#0037DA".to_string()); + hm.insert("magenta", "#881798".to_string()); + hm.insert("cyan", "#3A96DD".to_string()); + hm.insert("white", "#CCCCCC".to_string()); + + hm.insert("background", "#CCCCCC".to_string()); + hm.insert("foreground", "#0C0C0C".to_string()); + } + + hm +} + +fn get_default_theme(is_dark: bool) -> HashMap<&'static str, String> { let mut hm: HashMap<&str, String> = HashMap::new(); if is_dark { @@ -133,6 +156,20 @@ fn get_colors(is_dark: bool) -> HashMap<&'static str, String> { hm } +fn get_colors(is_dark: bool, use_campbell: 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. + + if use_campbell { + get_campbell_theme(is_dark) + } else { + get_default_theme(is_dark) + } +} + async fn to_html( args: CommandArgs, registry: &CommandRegistry, @@ -144,22 +181,26 @@ async fn to_html( html_color, no_color, dark_bg, + use_campbell, }, input, ) = args.process(®istry).await?; let input: Vec = input.collect().await; let headers = nu_protocol::merge_descriptors(&input); let mut output_string = "".to_string(); - let mut hm: HashMap = HashMap::new(); + let mut regex_hm: HashMap = HashMap::new(); + let color_hm = get_colors(dark_bg, use_campbell); - // if the user wants a dark background, that means the background will be black - // and the foreground will be white, otherwise it's the reverse. I think this - // is the best we can do until we get to color themes. - if dark_bg { - output_string.push_str(""); - } else { - output_string.push_str(""); - } + // change the color of the page + output_string.push_str(&format!( + r"", + color_hm + .get("background") + .expect("Error getting background color"), + color_hm + .get("foreground") + .expect("Error getting foreground color") + )); // Add grid lines to html // let mut output_string = "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 = false; - setup_html_color_regexes(&mut hm, is_dark); - assert_eq!(cd_help_expected_result, run_regexes(&hm, &cd_help)); - } - - #[test] - 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 is_dark = true; - setup_html_color_regexes(&mut hm, is_dark); - assert_eq!(cd_help_expected_result, run_regexes(&hm, &cd_help)); - } - - #[test] - fn test_no_color_flag() { - 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(); - setup_no_color_regexes(&mut hm); - assert_eq!(cd_help_expected_result, run_regexes(&hm, &cd_help)); - } - - #[test] - 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 is_dark = true; - setup_html_color_regexes(&mut hm, is_dark); - assert_eq!(where_help_exptected_results, run_regexes(&hm, &where_help)); - } - - #[test] - fn test_html_color_where_flag_dark_false() { - 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 is_dark = false; - setup_html_color_regexes(&mut hm, is_dark); - assert_eq!(where_help_exptected_results, run_regexes(&hm, &where_help)); - } } diff --git a/crates/nu-cli/tests/format_conversions/html.rs b/crates/nu-cli/tests/format_conversions/html.rs index c892130aef..684e2d68c8 100644 --- a/crates/nu-cli/tests/format_conversions/html.rs +++ b/crates/nu-cli/tests/format_conversions/html.rs @@ -11,7 +11,7 @@ fn out_html_simple() { assert_eq!( actual.out, - "3" + r"3" ); } @@ -20,12 +20,87 @@ fn out_html_table() { let actual = nu!( cwd: ".", pipeline( r#" - echo '{"name": "jason"}' | from json | to html + echo '{"name": "darren"}' | from json | to html "# )); assert_eq!( actual.out, - "
name
jason
" + r"
name
darren
" + ); +} + +#[test] +fn test_cd_html_color_flag_dark_false() { + let actual = nu!( + cwd: ".", pipeline( + r#" + cd --help | to html --html_color + "# + ) + ); + assert_eq!( + actual.out, + 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
-

" + ); +} + +#[test] +fn test_cd_html_color_flag_dark_true() { + let actual = nu!( + cwd: ".", pipeline( + r#" + cd --help | to html --html_color --dark_bg + "# + ) + ); + assert_eq!( + actual.out, + 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
-

" + ); +} + +#[test] +fn test_no_color_flag() { + let actual = nu!( + cwd: ".", pipeline( + r#" + cd --help | to html --no_color + "# + ) + ); + assert_eq!( + actual.out, + 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 -

" + ); +} + +#[test] +fn test_html_color_where_flag_dark_true() { + let actual = nu!( + cwd: ".", pipeline( + r#" + where --help | to html --html_color --dark_bg + "# + ) + ); + assert_eq!( + actual.out, + 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

" + ); +} + +#[test] +fn test_html_color_where_flag_dark_false() { + let actual = nu!( + cwd: ".", pipeline( + r#" + where --help | to html --html_color + "# + ) + ); + assert_eq!( + actual.out, + 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

" ); }