Minor refactor in to html (#12172)

Extract the generation of the theme overview into its own function and
elide an else block with early return
This commit is contained in:
Stefan Holderbach 2024-03-12 23:13:32 +01:00 committed by GitHub
parent b95cdd9705
commit cd71372ea9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,7 +5,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Config, DataSource, Example, IntoPipelineData, PipelineData,
PipelineMetadata, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
PipelineMetadata, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
use nu_utils::IgnoreCaseExt;
use rust_embed::RustEmbed;
@ -255,47 +255,10 @@ fn to_html(
let mut output_string = String::new();
let mut regex_hm: HashMap<u32, (&str, String)> = HashMap::with_capacity(17);
// Being essentially a 'help' option, this can afford to be relatively unoptimised
if list {
// If asset doesn't work, make sure to return the default theme
let html_themes = get_html_themes("228_themes.json").unwrap_or_default();
let result: Vec<Value> = html_themes
.themes
.into_iter()
.map(|n| {
Value::record(
record! {
"name" => Value::string(n.name, head),
"black" => Value::string(n.black, head),
"red" => Value::string(n.red, head),
"green" => Value::string(n.green, head),
"yellow" => Value::string(n.yellow, head),
"blue" => Value::string(n.blue, head),
"purple" => Value::string(n.purple, head),
"cyan" => Value::string(n.cyan, head),
"white" => Value::string(n.white, head),
"brightBlack" => Value::string(n.brightBlack, head),
"brightRed" => Value::string(n.brightRed, head),
"brightGreen" => Value::string(n.brightGreen, head),
"brightYellow" => Value::string(n.brightYellow, head),
"brightBlue" => Value::string(n.brightBlue, head),
"brightPurple" => Value::string(n.brightPurple, head),
"brightCyan" => Value::string(n.brightCyan, head),
"brightWhite" => Value::string(n.brightWhite, head),
"background" => Value::string(n.background, head),
"foreground" => Value::string(n.foreground, head),
},
head,
)
})
.collect();
return Ok(
Value::list(result, head).into_pipeline_data_with_metadata(PipelineMetadata {
data_source: DataSource::HtmlThemes,
}),
);
} else {
// Being essentially a 'help' option, this can afford to be relatively unoptimised
return Ok(theme_demo(head));
}
let theme_span = match &theme {
Some(v) => v.span,
None => head,
@ -373,10 +336,48 @@ fn to_html(
setup_no_color_regexes(&mut regex_hm);
output_string = run_regexes(&regex_hm, &output_string);
}
}
Ok(Value::string(output_string, head).into_pipeline_data())
}
fn theme_demo(span: Span) -> PipelineData {
// If asset doesn't work, make sure to return the default theme
let html_themes = get_html_themes("228_themes.json").unwrap_or_default();
let result: Vec<Value> = html_themes
.themes
.into_iter()
.map(|n| {
Value::record(
record! {
"name" => Value::string(n.name, span),
"black" => Value::string(n.black, span),
"red" => Value::string(n.red, span),
"green" => Value::string(n.green, span),
"yellow" => Value::string(n.yellow, span),
"blue" => Value::string(n.blue, span),
"purple" => Value::string(n.purple, span),
"cyan" => Value::string(n.cyan, span),
"white" => Value::string(n.white, span),
"brightBlack" => Value::string(n.brightBlack, span),
"brightRed" => Value::string(n.brightRed, span),
"brightGreen" => Value::string(n.brightGreen, span),
"brightYellow" => Value::string(n.brightYellow, span),
"brightBlue" => Value::string(n.brightBlue, span),
"brightPurple" => Value::string(n.brightPurple, span),
"brightCyan" => Value::string(n.brightCyan, span),
"brightWhite" => Value::string(n.brightWhite, span),
"background" => Value::string(n.background, span),
"foreground" => Value::string(n.foreground, span),
},
span,
)
})
.collect();
Value::list(result, span).into_pipeline_data_with_metadata(PipelineMetadata {
data_source: DataSource::HtmlThemes,
})
}
fn html_list(list: Vec<Value>, config: &Config) -> String {
let mut output_string = String::new();
output_string.push_str("<ol>");