refactor html module (#5246)

* refactor around html module

* Update html.rs

fix clippy warning

* minify json
This commit is contained in:
Jaffar Ashoor 2022-04-20 16:50:14 +03:00 committed by GitHub
parent 995d8db1fe
commit 0fb6f8f93c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 24 additions and 105 deletions

24
Cargo.lock generated
View File

@ -384,27 +384,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70"
[[package]]
name = "bzip2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6afcd980b5f3a45017c57e57a2fcccbb351cc43a356ce117ef760ef8052b89b0"
dependencies = [
"bzip2-sys",
"libc",
]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "calamine"
version = "0.18.0"
@ -2392,7 +2371,6 @@ dependencies = [
"uuid",
"wax",
"which",
"zip",
]
[[package]]
@ -5067,11 +5045,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93ab48844d61251bb3835145c521d88aa4031d7139e8485990f60ca911fa0815"
dependencies = [
"byteorder",
"bzip2",
"crc32fast",
"flate2",
"thiserror",
"time",
]
[[package]]

View File

@ -71,14 +71,13 @@ embed-resource = "1"
[features]
plugin = ["nu-plugin", "nu-cli/plugin", "nu-parser/plugin", "nu-command/plugin", "nu-protocol/plugin", "nu-engine/plugin"]
default = ["plugin", "which-support", "zip-support", "trash-support"]
default = ["plugin", "which-support", "trash-support"]
stable = ["default"]
extra = ["default", "dataframe"]
wasi = []
# Stable (Default)
which-support = ["nu-command/which-support"]
zip-support = ["nu-command/zip"]
trash-support = ["nu-command/trash-support"]
# Extra

View File

@ -81,7 +81,6 @@ uuid = { version = "0.8.2", features = ["v4"] }
which = { version = "4.2.2", optional = true }
reedline = { git = "https://github.com/nushell/reedline", branch = "main", features = ["bashisms"]}
wax = { version = "0.4.0", features = ["diagnostics"] }
zip = { version="0.5.9", optional = true }
[target.'cfg(unix)'.dependencies]
umask = "1.0.0"

File diff suppressed because one or more lines are too long

View File

@ -230,10 +230,8 @@ fn features_enabled() -> Vec<String> {
names.push("which".to_string());
}
#[cfg(feature = "zip")]
{
// always include it?
names.push("zip".to_string());
}
#[cfg(feature = "clipboard-cli")]
{

View File

@ -46,7 +46,7 @@ mod upsert;
mod where_;
mod window;
mod wrap;
mod zip_;
mod zip;
pub use all::All;
pub use any::Any;
@ -96,4 +96,4 @@ pub use upsert::Upsert;
pub use where_::Where;
pub use window::Window;
pub use wrap::Wrap;
pub use zip_::Zip;
pub use zip::Zip;

View File

@ -158,83 +158,30 @@ fn get_theme_from_asset_file(
theme: &Option<Spanned<String>>,
) -> Result<HashMap<&'static str, String>, ShellError> {
let theme_name = match theme {
Some(s) => s.item.clone(),
None => "default".to_string(), // There is no theme named "default" so this will be HtmlTheme::default(), which is "nu_default".
Some(s) => &s.item,
None => "default", // There is no theme named "default" so this will be HtmlTheme::default(), which is "nu_default".
};
// 228 themes come from
// https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/windowsterminal
// we should find a hit on any name in there
let asset = get_asset_by_name_as_html_themes("228_themes.zip", "228_themes.json");
// If asset doesn't work, make sure to return the default theme
let asset = match asset {
Ok(a) => a,
_ => HtmlThemes::default(),
};
let asset = get_html_themes("228_themes.json").unwrap_or_default();
// Find the theme by theme name
let th = asset
.themes
.iter()
.find(|&n| n.name.to_lowercase() == theme_name.to_lowercase()); // case insensitive search
.into_iter()
.find(|n| n.name.to_lowercase() == theme_name.to_lowercase()) // case insensitive search
.unwrap_or_default();
// If no theme is found by the name provided, ensure we return the default theme
let default_theme = HtmlTheme::default();
let th = match th {
Some(t) => t,
None => &default_theme,
};
// this just means no theme was passed in
if th.name.to_lowercase().eq(&"nu_default".to_string())
// this means there was a theme passed in
&& theme.is_some()
{
return Err(ShellError::NotFound(
theme.as_ref().expect("this should never trigger").span,
));
}
Ok(convert_html_theme_to_hash_map(is_dark, th))
}
#[allow(unused_variables)]
fn get_asset_by_name_as_html_themes(
zip_name: &str,
json_name: &str,
) -> Result<HtmlThemes, Box<dyn Error>> {
match Assets::get(zip_name) {
Some(content) => {
let asset: Vec<u8> = content.data.into();
let reader = std::io::Cursor::new(asset);
#[cfg(feature = "zip")]
{
use std::io::Read;
let mut archive = zip::ZipArchive::new(reader)?;
let mut zip_file = archive.by_name(json_name)?;
let mut contents = String::new();
zip_file.read_to_string(&mut contents)?;
Ok(nu_json::from_str(&contents)?)
}
#[cfg(not(feature = "zip"))]
{
let th = HtmlThemes::default();
Ok(th)
}
}
None => {
let th = HtmlThemes::default();
Ok(th)
}
}
Ok(convert_html_theme_to_hash_map(is_dark, &th))
}
fn convert_html_theme_to_hash_map(
is_dark: bool,
theme: &HtmlTheme,
) -> HashMap<&'static str, String> {
let mut hm: HashMap<&str, String> = HashMap::new();
let mut hm: HashMap<&str, String> = HashMap::with_capacity(18);
hm.insert("bold_black", theme.brightBlack[..].to_string());
hm.insert("bold_red", theme.brightRed[..].to_string());
@ -268,18 +215,17 @@ fn convert_html_theme_to_hash_map(
hm
}
fn get_html_themes(json_name: &str) -> Result<HtmlThemes, Box<dyn Error>> {
match Assets::get(json_name) {
Some(content) => Ok(nu_json::from_slice(&content.data)?),
None => Ok(HtmlThemes::default()),
}
}
fn get_list_of_theme_names() -> Vec<String> {
let asset = get_asset_by_name_as_html_themes("228_themes.zip", "228_themes.json");
// If asset doesn't work, make sure to return the default theme
let html_themes = match asset {
Ok(a) => a,
_ => HtmlThemes::default(),
};
let theme_names: Vec<String> = html_themes.themes.iter().map(|n| n.name.clone()).collect();
theme_names
let html_themes = get_html_themes("228_themes.json").unwrap_or_default();
html_themes.themes.into_iter().map(|n| n.name).collect()
}
fn to_html(
@ -302,7 +248,7 @@ fn to_html(
let headers = Some(headers)
.filter(|headers| !headers.is_empty() && (headers.len() > 1 || !headers[0].is_empty()));
let mut output_string = String::new();
let mut regex_hm: HashMap<u32, (&str, String)> = HashMap::new();
let mut regex_hm: HashMap<u32, (&str, String)> = HashMap::with_capacity(17);
if list {
// Get the list of theme names