added ability to supply --dark_bg to to html (#2189)

* added ability to supply --dark_bg to to html

* fmt + fixed tests

* updated other html tests

* fmt
This commit is contained in:
Darren Schroeder 2020-07-16 08:19:29 -05:00 committed by GitHub
parent 72f6513d2a
commit a04dfca63a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 46 deletions

View File

@ -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(&registry).await?;
let input: Vec<Value> = input.collect().await;
let headers = nu_protocol::merge_descriptors(&input);
let mut output_string = "<html>".to_string();
output_string.push_str("<body>");
// change the body background color
// output_string.push_str("<body style=\"background-color:lightgray;\">");
let mut hm = HashMap::new();
let mut hm: HashMap<u32, (&str, String)> = 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("<style>body { background-color:black;color:white; }</style><body>");
} else {
output_string.push_str("<style>body { background-color:white;color:black; }</style><body>");
}
// Add grid lines to html
// let mut output_string = "<html><head><style>".to_string();
@ -68,11 +81,16 @@ async fn to_html(
// output_string.push_str("</style></head><body>");
if !headers.is_empty() && (headers.len() > 1 || headers[0] != "") {
output_string.push_str("<table>");
// output_string.push_str("<table>");
// change the color of tables
if dark_bg {
output_string.push_str("<table style=\"background-color:black;color:white;\">");
} else {
output_string.push_str("<table style=\"background-color:white;color:black;\">");
}
output_string.push_str("<tr>");
// change the background of tables
// output_string.push_str("<tr style=\"background-color:darkgray;color:cyan;\">");
for header in &headers {
output_string.push_str("<th>");
@ -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<u32, (&'static str, &'static str)>) {
// fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)>, is_dark: bool) {
fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, String)>, 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<reset>\[0m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
// Since this is a reset, reset to black, normal weight font
r"<span style='color:black;font-weight:normal;'>$word</span>",
// Reset the text color, normal weight font
format!(
r"<span style='color:{};font-weight:normal;'>$word</span>",
text_color
),
),
);
hash.insert(
@ -182,7 +210,10 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Black
// r"(?P<bb>\[1;30m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<bb>\[1;30m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:black;font-weight:bold;'>$word</span>",
format!(
r"<span style='color:{};font-weight:bold;'>$word</span>",
text_color
),
),
);
hash.insert(
@ -191,7 +222,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Red
// r"(?P<br>\[1;31m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<br>\[1;31m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:red;font-weight:bold;'>$word</span>",
r"<span style='color:red;font-weight:bold;'>$word</span>".to_string(),
),
);
hash.insert(
@ -200,7 +231,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Green
// r"(?P<bg>\[1;32m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<bg>\[1;32m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:green;font-weight:bold;'>$word</span>",
r"<span style='color:green;font-weight:bold;'>$word</span>".to_string(),
),
);
hash.insert(
@ -209,7 +240,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Yellow
// r"(?P<by>\[1;33m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<by>\[1;33m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:yellow;font-weight:bold;'>$word</span>",
r"<span style='color:yellow;font-weight:bold;'>$word</span>".to_string(),
),
);
hash.insert(
@ -218,7 +249,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Blue
// r"(?P<bu>\[1;34m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<bu>\[1;34m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:blue;font-weight:bold;'>$word</span>",
r"<span style='color:blue;font-weight:bold;'>$word</span>".to_string(),
),
);
hash.insert(
@ -227,7 +258,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Magenta
// r"(?P<bm>\[1;35m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<bm>\[1;35m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:magenta;font-weight:bold;'>$word</span>",
r"<span style='color:magenta;font-weight:bold;'>$word</span>".to_string(),
),
);
hash.insert(
@ -236,7 +267,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Bold Cyan
// r"(?P<bc>\[1;36m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<bc>\[1;36m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:cyan;font-weight:bold;'>$word</span>",
r"<span style='color:cyan;font-weight:bold;'>$word</span>".to_string(),
),
);
hash.insert(
@ -247,7 +278,10 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// is white. White on white = no bueno.
// r"(?P<bw>\[1;37m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<bw>\[1;37m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:black;font-weight:bold;'>$word</span>",
format!(
r"<span style='color:{};font-weight:bold;'>$word</span>",
text_color
),
),
);
// All the normal colors
@ -257,7 +291,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Black
// r"(?P<b>\[30m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<b>\[30m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:black;'>$word</span>",
format!(r"<span style='color:{};'>$word</span>", text_color),
),
);
hash.insert(
@ -266,7 +300,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Red
// r"(?P<r>\[31m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<r>\[31m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:red;'>$word</span>",
r"<span style='color:red;'>$word</span>".to_string(),
),
);
hash.insert(
@ -275,7 +309,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Green
// r"(?P<g>\[32m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<g>\[32m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:green;'>$word</span>",
r"<span style='color:green;'>$word</span>".to_string(),
),
);
hash.insert(
@ -284,7 +318,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Yellow
// r"(?P<y>\[33m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<y>\[33m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:yellow;'>$word</span>",
r"<span style='color:yellow;'>$word</span>".to_string(),
),
);
hash.insert(
@ -293,7 +327,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Blue
// r"(?P<u>\[34m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<u>\[34m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:blue;'>$word</span>",
r"<span style='color:blue;'>$word</span>".to_string(),
),
);
hash.insert(
@ -302,7 +336,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Magenta
// r"(?P<m>\[35m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<m>\[35m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:magenta;'>$word</span>",
r"<span style='color:magenta;'>$word</span>".to_string(),
),
);
hash.insert(
@ -311,7 +345,7 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// Cyan
// r"(?P<c>\[36m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<c>\[36m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:cyan;'>$word</span>",
r"<span style='color:cyan;'>$word</span>".to_string(),
),
);
hash.insert(
@ -322,12 +356,13 @@ fn setup_html_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)
// is white. White on white = no bueno.
// r"(?P<w>\[37m)(?P<word>[A-Za-z0-9\-'!/_~ &;|=\+\*\.#%:\]$`\(\)]+)",
r"(?P<w>\[37m)(?P<word>[[:alnum:][:space:][:punct:]]*)",
r"<span style='color:black;'>$word</span>",
format!(r"<span style='color:{};'>$word</span>", text_color),
),
);
}
fn setup_no_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)>) {
// fn setup_no_color_regexes(hash: &mut HashMap<u32, (&'static str, &'static str)>, is_dark: bool) {
fn setup_no_color_regexes(hash: &mut HashMap<u32, (&'static str, String)>) {
// 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<u32, (&'static str, &'static str)>)
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<u32, (&'static str, &'static str)>, contents: &str) -> String {
// fn run_regexes(hash: &HashMap<u32, (&'static str, &'static str)>, contents: &str) -> String {
fn run_regexes(hash: &HashMap<u32, (&'static str, String)>, 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"<html><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".to_string();
let cd_help_expected_result = r"<html><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; <span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:cyan;'>dirname<span style='color:black;font-weight:normal;'><br><br> Change to your home directory<br> &gt; </span><span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'><br><br> Change to your home directory (alternate version)<br> &gt; </span></span><span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:cyan;'>~<span style='color:black;font-weight:normal;'><br><br> Change to the previous directory<br> &gt; </span><span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:cyan;'>-<span style='color:black;font-weight:normal;'><br><br></body></html></span></span></span>".to_string();
setup_html_color_regexes(&mut hm);
fn test_cd_html_color_flag_dark_false() {
let mut hm: HashMap<u32, (&str, String)> = HashMap::new();
let cd_help = r"<html><style>body { background-color:white;color:black; }</style><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".to_string();
let cd_help_expected_result = r"<html><style>body { background-color:white;color:black; }</style><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; <span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:cyan;'>dirname<span style='color:black;font-weight:normal;'><br><br> Change to your home directory<br> &gt; </span><span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'><br><br> Change to your home directory (alternate version)<br> &gt; </span></span><span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:cyan;'>~<span style='color:black;font-weight:normal;'><br><br> Change to the previous directory<br> &gt; </span><span style='color:cyan;font-weight:bold;'>cd<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:cyan;'>-<span style='color:black;font-weight:normal;'><br><br></body></html></span></span></span>".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<u32, (&str, String)> = HashMap::new();
let cd_help = r"<html><style>body { background-color:black;color:white; }</style><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".to_string();
let cd_help_expected_result = r"<html><style>body { background-color:black;color:white; }</style><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; <span style='color:cyan;font-weight:bold;'>cd<span style='color:white;font-weight:normal;'></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:cyan;'>dirname<span style='color:white;font-weight:normal;'><br><br> Change to your home directory<br> &gt; </span><span style='color:cyan;font-weight:bold;'>cd<span style='color:white;font-weight:normal;'><br><br> Change to your home directory (alternate version)<br> &gt; </span></span><span style='color:cyan;font-weight:bold;'>cd<span style='color:white;font-weight:normal;'></span></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:cyan;'>~<span style='color:white;font-weight:normal;'><br><br> Change to the previous directory<br> &gt; </span><span style='color:cyan;font-weight:bold;'>cd<span style='color:white;font-weight:normal;'></span></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:cyan;'>-<span style='color:white;font-weight:normal;'><br><br></body></html></span></span></span>".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"<html><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".to_string();
let cd_help_expected_result = r"<html><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".to_string();
let mut hm: HashMap<u32, (&str, String)> = HashMap::new();
let cd_help = r"<html><style>body { background-color:white;color:black; }</style><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".to_string();
let cd_help_expected_result = r"<html><style>body { background-color:white;color:black; }</style><body>Change to a new path.<br><br>Usage:<br> &gt; cd (directory) {flags} <br><br>Parameters:<br> (directory) the directory to change to<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> Change to a new directory called &#x27;dirname&#x27;<br> &gt; cd dirname<br><br> Change to your home directory<br> &gt; cd<br><br> Change to your home directory (alternate version)<br> &gt; cd ~<br><br> Change to the previous directory<br> &gt; cd -<br><br></body></html>".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"<html><body>Filter table to match the condition.<br><br>Usage:<br> &gt; where &lt;condition&gt; {flags} <br><br>Parameters:<br> &lt;condition&gt; the condition that must match<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> List all files in the current directory with sizes greater than 2kb<br> &gt; ls | where size &gt; 2kb<br><br> List only the files in the current directory<br> &gt; ls | where type == File<br><br> List all files with names that contain &quot;Car&quot;<br> &gt; ls | where name =~ &quot;Car&quot;<br><br> List all files that were modified in the last two months<br> &gt; ls | where modified &lt;= 2M<br><br></body></html>".to_string();
let where_help_exptected_results = r"<html><body>Filter table to match the condition.<br><br>Usage:<br> &gt; where &lt;condition&gt; {flags} <br><br>Parameters:<br> &lt;condition&gt; the condition that must match<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> List all files in the current directory with sizes greater than 2kb<br> &gt; <span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>size<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>&gt;<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:magenta;font-weight:bold;'>2<span style='color:black;font-weight:normal;'></span></span><span style='color:cyan;font-weight:bold;'>kb<span style='color:black;font-weight:normal;'><br><br> List only the files in the current directory<br> &gt; </span></span><span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>type<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>==<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:green;'>File<span style='color:black;font-weight:normal;'><br><br> List all files with names that contain &quot;Car&quot;<br> &gt; </span><span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>name<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>=~<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:green;'>&quot;Car&quot;<span style='color:black;font-weight:normal;'><br><br> List all files that were modified in the last two months<br> &gt; </span><span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>modified<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>&lt;=<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:magenta;font-weight:bold;'>2<span style='color:black;font-weight:normal;'></span></span><span style='color:cyan;font-weight:bold;'>M<span style='color:black;font-weight:normal;'><br><br></body></html></span></span></span>".to_string();
setup_html_color_regexes(&mut hm);
fn test_html_color_where_flag_dark_true() {
let mut hm: HashMap<u32, (&str, String)> = HashMap::new();
let where_help = r"<html><style>body { background-color:black;color:white; }</style><body>Filter table to match the condition.<br><br>Usage:<br> &gt; where &lt;condition&gt; {flags} <br><br>Parameters:<br> &lt;condition&gt; the condition that must match<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> List all files in the current directory with sizes greater than 2kb<br> &gt; ls | where size &gt; 2kb<br><br> List only the files in the current directory<br> &gt; ls | where type == File<br><br> List all files with names that contain &quot;Car&quot;<br> &gt; ls | where name =~ &quot;Car&quot;<br><br> List all files that were modified in the last two months<br> &gt; ls | where modified &lt;= 2M<br><br></body></html>".to_string();
let where_help_exptected_results = r"<html><style>body { background-color:black;color:white; }</style><body>Filter table to match the condition.<br><br>Usage:<br> &gt; where &lt;condition&gt; {flags} <br><br>Parameters:<br> &lt;condition&gt; the condition that must match<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> List all files in the current directory with sizes greater than 2kb<br> &gt; <span style='color:cyan;font-weight:bold;'>ls<span style='color:white;font-weight:normal;'></span></span><span style='color:white;'> | <span style='color:white;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>size<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;'>&gt;<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:magenta;font-weight:bold;'>2<span style='color:white;font-weight:normal;'></span></span><span style='color:cyan;font-weight:bold;'>kb<span style='color:white;font-weight:normal;'><br><br> List only the files in the current directory<br> &gt; </span></span><span style='color:cyan;font-weight:bold;'>ls<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> | <span style='color:white;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>type<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;'>==<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:green;'>File<span style='color:white;font-weight:normal;'><br><br> List all files with names that contain &quot;Car&quot;<br> &gt; </span><span style='color:cyan;font-weight:bold;'>ls<span style='color:white;font-weight:normal;'></span></span></span></span><span style='color:white;'> | <span style='color:white;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>name<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;'>=~<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:green;'>&quot;Car&quot;<span style='color:white;font-weight:normal;'><br><br> List all files that were modified in the last two months<br> &gt; </span><span style='color:cyan;font-weight:bold;'>ls<span style='color:white;font-weight:normal;'></span></span></span></span><span style='color:white;'> | <span style='color:white;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>modified<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:yellow;'>&lt;=<span style='color:white;font-weight:normal;'></span></span></span><span style='color:white;'> <span style='color:white;font-weight:normal;'></span><span style='color:magenta;font-weight:bold;'>2<span style='color:white;font-weight:normal;'></span></span><span style='color:cyan;font-weight:bold;'>M<span style='color:white;font-weight:normal;'><br><br></body></html></span></span></span>".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<u32, (&str, String)> = HashMap::new();
let where_help = r"<html><style>body { background-color:white;color:black; }</style><body>Filter table to match the condition.<br><br>Usage:<br> &gt; where &lt;condition&gt; {flags} <br><br>Parameters:<br> &lt;condition&gt; the condition that must match<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> List all files in the current directory with sizes greater than 2kb<br> &gt; ls | where size &gt; 2kb<br><br> List only the files in the current directory<br> &gt; ls | where type == File<br><br> List all files with names that contain &quot;Car&quot;<br> &gt; ls | where name =~ &quot;Car&quot;<br><br> List all files that were modified in the last two months<br> &gt; ls | where modified &lt;= 2M<br><br></body></html>".to_string();
let where_help_exptected_results = r"<html><style>body { background-color:white;color:black; }</style><body>Filter table to match the condition.<br><br>Usage:<br> &gt; where &lt;condition&gt; {flags} <br><br>Parameters:<br> &lt;condition&gt; the condition that must match<br><br>Flags:<br> -h, --help: Display this help message<br><br>Examples:<br> List all files in the current directory with sizes greater than 2kb<br> &gt; <span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>size<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>&gt;<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:magenta;font-weight:bold;'>2<span style='color:black;font-weight:normal;'></span></span><span style='color:cyan;font-weight:bold;'>kb<span style='color:black;font-weight:normal;'><br><br> List only the files in the current directory<br> &gt; </span></span><span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>type<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>==<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:green;'>File<span style='color:black;font-weight:normal;'><br><br> List all files with names that contain &quot;Car&quot;<br> &gt; </span><span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>name<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>=~<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:green;'>&quot;Car&quot;<span style='color:black;font-weight:normal;'><br><br> List all files that were modified in the last two months<br> &gt; </span><span style='color:cyan;font-weight:bold;'>ls<span style='color:black;font-weight:normal;'></span></span></span></span><span style='color:black;'> | <span style='color:black;font-weight:normal;'></span><span style='color:cyan;font-weight:bold;'>where<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;font-weight:bold;'>modified<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:yellow;'>&lt;=<span style='color:black;font-weight:normal;'></span></span></span><span style='color:black;'> <span style='color:black;font-weight:normal;'></span><span style='color:magenta;font-weight:bold;'>2<span style='color:black;font-weight:normal;'></span></span><span style='color:cyan;font-weight:bold;'>M<span style='color:black;font-weight:normal;'><br><br></body></html></span></span></span>".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));
}
}

View File

@ -9,7 +9,10 @@ fn out_html_simple() {
"#
));
assert_eq!(actual.out, "<html><body>3</body></html>");
assert_eq!(
actual.out,
"<html><style>body { background-color:white;color:black; }</style><body>3</body></html>"
);
}
#[test]
@ -23,6 +26,6 @@ fn out_html_table() {
assert_eq!(
actual.out,
"<html><body><table><tr><th>name</th></tr><tr><td>jason</td></tr></table></body></html>"
"<html><style>body { background-color:white;color:black; }</style><body><table style=\"background-color:white;color:black;\"><tr><th>name</th></tr><tr><td>jason</td></tr></table></body></html>"
);
}