diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index 41b3a96632..7a18816132 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -14,6 +14,7 @@ pub struct ToHTML; pub struct ToHTMLArgs { html_color: bool, no_color: bool, + dark_bg: bool, } #[async_trait] @@ -26,6 +27,11 @@ impl WholeStreamCommand for ToHTML { Signature::build("to html") .switch("html_color", "change ansi colors to html colors", Some('t')) .switch("no_color", "remove all ansi colors in output", Some('n')) + .switch( + "dark_bg", + "indicate your background color is a darker color", + Some('d'), + ) } fn usage(&self) -> &str { @@ -51,16 +57,23 @@ async fn to_html( ToHTMLArgs { html_color, no_color, + dark_bg, }, input, ) = args.process(®istry).await?; let input: Vec = input.collect().await; let headers = nu_protocol::merge_descriptors(&input); let mut output_string = "".to_string(); - output_string.push_str(""); - // change the body background color - // output_string.push_str(""); - let mut hm = HashMap::new(); + let mut hm: HashMap = HashMap::new(); + + // 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(""); + } // Add grid lines to html // let mut output_string = ""); if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") { - output_string.push_str(""); + // output_string.push_str("
"); + + // change the color of tables + if dark_bg { + output_string.push_str("
"); + } else { + output_string.push_str("
"); + } output_string.push_str(""); - // change the background of tables - // output_string.push_str(""); for header in &headers { output_string.push_str("
"); @@ -154,7 +172,7 @@ async fn to_html( // Check to see if we want to remove all color or change ansi to html colors if html_color { - setup_html_color_regexes(&mut hm); + setup_html_color_regexes(&mut hm, dark_bg); output_string = run_regexes(&hm, &output_string); } else if no_color { setup_no_color_regexes(&mut hm); @@ -166,14 +184,24 @@ async fn to_html( ))) } -fn setup_html_color_regexes(hash: &mut HashMap) { +// fn setup_html_color_regexes(hash: &mut HashMap, is_dark: bool) { +fn setup_html_color_regexes(hash: &mut HashMap, is_dark: bool) { + let text_color = if is_dark { + "white".to_string() + } else { + "black".to_string() + }; + // All the bold colors hash.insert( 0, ( r"(?P\[0m)(?P[[:alnum:][:space:][:punct:]]*)", - // Since this is a reset, reset to black, normal weight font - r"$word", + // Reset the text color, normal weight font + format!( + r"$word", + text_color + ), ), ); hash.insert( @@ -182,7 +210,10 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;30m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;30m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + format!( + r"$word", + text_color + ), ), ); hash.insert( @@ -191,7 +222,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;31m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P
\[1;31m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -200,7 +231,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;32m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;32m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -209,7 +240,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;33m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;33m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -218,7 +249,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;34m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;34m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -227,7 +258,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;35m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;35m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -236,7 +267,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;36m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;36m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -247,7 +278,10 @@ fn setup_html_color_regexes(hash: &mut HashMap\[1;37m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[1;37m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + format!( + r"$word", + text_color + ), ), ); // All the normal colors @@ -257,7 +291,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[30m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[30m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + format!(r"$word", text_color), ), ); hash.insert( @@ -266,7 +300,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[31m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[31m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -275,7 +309,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[32m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[32m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -284,7 +318,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[33m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[33m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -293,7 +327,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[34m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[34m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -302,7 +336,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[35m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[35m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -311,7 +345,7 @@ fn setup_html_color_regexes(hash: &mut HashMap\[36m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[36m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + r"$word".to_string(), ), ); hash.insert( @@ -322,12 +356,13 @@ fn setup_html_color_regexes(hash: &mut HashMap\[37m)(?P[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)", r"(?P\[37m)(?P[[:alnum:][:space:][:punct:]]*)", - r"$word", + format!(r"$word", text_color), ), ); } -fn setup_no_color_regexes(hash: &mut HashMap) { +// fn setup_no_color_regexes(hash: &mut HashMap, is_dark: bool) { +fn setup_no_color_regexes(hash: &mut HashMap) { // We can just use one regex here because we're just removing ansi sequences // and not replacing them with html colors. // attribution: https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python @@ -335,19 +370,21 @@ fn setup_no_color_regexes(hash: &mut HashMap) 0, ( r"(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])", - r"$name_group_doesnt_exist", + r"$name_group_doesnt_exist".to_string(), ), ); } -fn run_regexes(hash: &HashMap, contents: &str) -> String { +// fn run_regexes(hash: &HashMap, contents: &str) -> String { +fn run_regexes(hash: &HashMap, contents: &str) -> String { let mut working_string = contents.to_owned(); let hash_count: u32 = hash.len() as u32; for n in 0..hash_count { let value = hash.get(&n).expect("error getting hash at index"); //println!("{},{}", value.0, value.1); let re = Regex::new(value.0).expect("problem with color regex"); - let after = re.replace_all(&working_string, value.1).to_string(); + // let replace = value.1.to_owned(); + let after = re.replace_all(&working_string, &value.1[..]).to_string(); working_string = after.clone(); } working_string @@ -366,29 +403,51 @@ mod tests { } #[test] - fn test_html_color_flag() { - let mut hm = 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_html_color_regexes(&mut hm); + fn test_cd_html_color_flag_dark_false() { + 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 = 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::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 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() { - let mut hm = 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(); - setup_html_color_regexes(&mut hm); + 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 06b642c637..c892130aef 100644 --- a/crates/nu-cli/tests/format_conversions/html.rs +++ b/crates/nu-cli/tests/format_conversions/html.rs @@ -9,7 +9,10 @@ fn out_html_simple() { "# )); - assert_eq!(actual.out, "3"); + assert_eq!( + actual.out, + "3" + ); } #[test] @@ -23,6 +26,6 @@ fn out_html_table() { assert_eq!( actual.out, - "
name
jason
" + "
name
jason
" ); }