forked from extern/nushell
nu-explore/ Fix configuration issues (#7520)
close #7518 Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
parent
5f48452e3b
commit
b01f50bd70
@ -19,29 +19,61 @@ pub fn lookup_ansi_color_style(s: &str) -> Style {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// These two are used only for Explore's very limited color config
|
|
||||||
fn update_hashmap(key: &str, val: &str, hm: &mut HashMap<String, Style>) {
|
|
||||||
// eprintln!("key: {}, val: {}", &key, &val);
|
|
||||||
let color = lookup_ansi_color_style(val);
|
|
||||||
if let Some(v) = hm.get_mut(key) {
|
|
||||||
*v = color;
|
|
||||||
} else {
|
|
||||||
hm.insert(key.to_string(), color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_color_map(colors: &HashMap<String, Value>) -> HashMap<String, Style> {
|
pub fn get_color_map(colors: &HashMap<String, Value>) -> HashMap<String, Style> {
|
||||||
let mut hm: HashMap<String, Style> = HashMap::new();
|
let mut hm: HashMap<String, Style> = HashMap::new();
|
||||||
|
|
||||||
for (key, value) in colors {
|
for (key, value) in colors {
|
||||||
if let Value::String { val, .. } = value {
|
parse_map_entry(&mut hm, key, value);
|
||||||
update_hashmap(key, val, &mut hm);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hm
|
hm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_map_entry(hm: &mut HashMap<String, Style>, key: &str, value: &Value) {
|
||||||
|
let value = match value {
|
||||||
|
Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
|
||||||
|
Value::Record { cols, vals, .. } => get_style_from_value(cols, vals).map(parse_nustyle),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
if let Some(value) = value {
|
||||||
|
hm.entry(key.to_owned()).or_insert(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_style_from_value(cols: &[String], vals: &[Value]) -> Option<NuStyle> {
|
||||||
|
let mut was_set = false;
|
||||||
|
let mut style = NuStyle::from(Style::default());
|
||||||
|
for (col, val) in cols.iter().zip(vals) {
|
||||||
|
match col.as_str() {
|
||||||
|
"bg" => {
|
||||||
|
if let Value::String { val, .. } = val {
|
||||||
|
style.bg = Some(val.clone());
|
||||||
|
was_set = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"fg" => {
|
||||||
|
if let Value::String { val, .. } = val {
|
||||||
|
style.fg = Some(val.clone());
|
||||||
|
was_set = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"attr" => {
|
||||||
|
if let Value::String { val, .. } = val {
|
||||||
|
style.attr = Some(val.clone());
|
||||||
|
was_set = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if was_set {
|
||||||
|
Some(style)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn color_string_to_nustyle(color_string: String) -> Style {
|
fn color_string_to_nustyle(color_string: String) -> Style {
|
||||||
// eprintln!("color_string: {}", &color_string);
|
// eprintln!("color_string: {}", &color_string);
|
||||||
if color_string.is_empty() {
|
if color_string.is_empty() {
|
||||||
|
@ -72,9 +72,9 @@ impl Command for Explore {
|
|||||||
let style_computer = StyleComputer::from_config(engine_state, stack);
|
let style_computer = StyleComputer::from_config(engine_state, stack);
|
||||||
|
|
||||||
let mut config = nu_config.explore.clone();
|
let mut config = nu_config.explore.clone();
|
||||||
prepare_default_config(&mut config);
|
|
||||||
update_config(&mut config, show_index, show_head);
|
|
||||||
include_nu_config(&mut config, &style_computer);
|
include_nu_config(&mut config, &style_computer);
|
||||||
|
update_config(&mut config, show_index, show_head);
|
||||||
|
prepare_default_config(&mut config);
|
||||||
|
|
||||||
let show_banner = is_need_banner(&config).unwrap_or(true);
|
let show_banner = is_need_banner(&config).unwrap_or(true);
|
||||||
let exit_esc = is_need_esc_exit(&config).unwrap_or(true);
|
let exit_esc = is_need_esc_exit(&config).unwrap_or(true);
|
||||||
@ -293,12 +293,13 @@ fn prepare_default_config(config: &mut HashMap<String, Value>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_hash_map(value: &Value) -> Option<HashMap<String, Value>> {
|
fn parse_hash_map(value: &Value) -> Option<HashMap<String, Value>> {
|
||||||
value
|
value.as_record().ok().map(|(cols, vals)| {
|
||||||
.as_string()
|
cols.iter()
|
||||||
.ok()
|
.take(vals.len())
|
||||||
.and_then(|s| nu_json::from_str::<nu_json::Value>(&s).ok())
|
.zip(vals)
|
||||||
.map(convert_json_value_into_value)
|
.map(|(col, val)| (col.clone(), val.clone()))
|
||||||
.and_then(|v| create_map(&v))
|
.collect::<HashMap<_, _>>()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn color(foreground: Option<Color>, background: Option<Color>) -> Style {
|
const fn color(foreground: Option<Color>, background: Option<Color>) -> Style {
|
||||||
@ -339,39 +340,6 @@ fn insert_bool(map: &mut HashMap<String, Value>, key: &str, value: bool) {
|
|||||||
map.insert(String::from(key), Value::boolean(value, Span::unknown()));
|
map.insert(String::from(key), Value::boolean(value, Span::unknown()));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_json_value_into_value(value: nu_json::Value) -> Value {
|
|
||||||
match value {
|
|
||||||
nu_json::Value::Null => Value::nothing(Span::unknown()),
|
|
||||||
nu_json::Value::Bool(val) => Value::boolean(val, Span::unknown()),
|
|
||||||
nu_json::Value::I64(val) => Value::int(val, Span::unknown()),
|
|
||||||
nu_json::Value::U64(val) => Value::int(val as i64, Span::unknown()),
|
|
||||||
nu_json::Value::F64(val) => Value::float(val, Span::unknown()),
|
|
||||||
nu_json::Value::String(val) => Value::string(val, Span::unknown()),
|
|
||||||
nu_json::Value::Array(val) => {
|
|
||||||
let vals = val
|
|
||||||
.into_iter()
|
|
||||||
.map(convert_json_value_into_value)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
Value::List {
|
|
||||||
vals,
|
|
||||||
span: Span::unknown(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nu_json::Value::Object(val) => {
|
|
||||||
let hm = val
|
|
||||||
.into_iter()
|
|
||||||
.map(|(key, value)| {
|
|
||||||
let val = convert_json_value_into_value(value);
|
|
||||||
(key, val)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
map_into_value(hm)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn include_nu_config(config: &mut HashMap<String, Value>, style_computer: &StyleComputer) {
|
fn include_nu_config(config: &mut HashMap<String, Value>, style_computer: &StyleComputer) {
|
||||||
let line_color = lookup_color(style_computer, "separator");
|
let line_color = lookup_color(style_computer, "separator");
|
||||||
if line_color != nu_ansi_term::Style::default() {
|
if line_color != nu_ansi_term::Style::default() {
|
||||||
@ -398,7 +366,9 @@ fn include_nu_config(config: &mut HashMap<String, Value>, style_computer: &Style
|
|||||||
.get("config")
|
.get("config")
|
||||||
.and_then(parse_hash_map)
|
.and_then(parse_hash_map)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
insert_style(&mut map, "border_color", line_color);
|
insert_style(&mut map, "border_color", line_color);
|
||||||
|
|
||||||
config.insert(String::from("config"), map_into_value(map));
|
config.insert(String::from("config"), map_into_value(map));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::io::Result;
|
use std::{collections::HashMap, io::Result};
|
||||||
|
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, Stack},
|
engine::{EngineState, Stack},
|
||||||
@ -7,7 +7,7 @@ use nu_protocol::{
|
|||||||
use tui::layout::Rect;
|
use tui::layout::Rect;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
nu_common::try_build_table,
|
nu_common::{try_build_table, NuSpan},
|
||||||
pager::Frame,
|
pager::Frame,
|
||||||
util::map_into_value,
|
util::map_into_value,
|
||||||
views::{Layout, Preview, View, ViewConfig},
|
views::{Layout, Preview, View, ViewConfig},
|
||||||
@ -128,10 +128,57 @@ impl ConfigView {
|
|||||||
fn create_output_string(&mut self, config: ViewConfig) -> String {
|
fn create_output_string(&mut self, config: ViewConfig) -> String {
|
||||||
match self.format {
|
match self.format {
|
||||||
ConfigFormat::Table => {
|
ConfigFormat::Table => {
|
||||||
let value = map_into_value(config.config.clone());
|
let mut m = config.config.clone();
|
||||||
|
convert_styles(&mut m);
|
||||||
|
|
||||||
|
let value = map_into_value(m);
|
||||||
try_build_table(None, config.nu_config, config.style_computer, value)
|
try_build_table(None, config.nu_config, config.style_computer, value)
|
||||||
}
|
}
|
||||||
ConfigFormat::Nu => nu_json::to_string(&config.config).unwrap_or_default(),
|
ConfigFormat::Nu => nu_json::to_string(&config.config).unwrap_or_default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn convert_styles(m: &mut HashMap<String, Value>) {
|
||||||
|
for value in m.values_mut() {
|
||||||
|
convert_styles_value(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_styles_value(value: &mut Value) {
|
||||||
|
match value {
|
||||||
|
Value::String { val, .. } => {
|
||||||
|
if let Some(v) = convert_style_from_string(val) {
|
||||||
|
*value = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::List { vals, .. } => {
|
||||||
|
for value in vals {
|
||||||
|
convert_styles_value(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Record { vals, .. } => {
|
||||||
|
for value in vals {
|
||||||
|
convert_styles_value(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_style_from_string(s: &str) -> Option<Value> {
|
||||||
|
let style = nu_json::from_str::<nu_color_config::NuStyle>(s).ok()?;
|
||||||
|
let cols = vec![String::from("bg"), String::from("fg"), String::from("attr")];
|
||||||
|
|
||||||
|
let vals = vec![
|
||||||
|
Value::string(style.bg.unwrap_or_default(), NuSpan::unknown()),
|
||||||
|
Value::string(style.fg.unwrap_or_default(), NuSpan::unknown()),
|
||||||
|
Value::string(style.attr.unwrap_or_default(), NuSpan::unknown()),
|
||||||
|
];
|
||||||
|
|
||||||
|
Some(Value::Record {
|
||||||
|
cols,
|
||||||
|
vals,
|
||||||
|
span: NuSpan::unknown(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user