nu-explore/ Fix configuration issues (#7520)

close #7518

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt
2022-12-19 20:34:21 +03:00
committed by GitHub
parent 5f48452e3b
commit b01f50bd70
3 changed files with 107 additions and 58 deletions

View File

@ -1,4 +1,4 @@
use std::io::Result;
use std::{collections::HashMap, io::Result};
use nu_protocol::{
engine::{EngineState, Stack},
@ -7,7 +7,7 @@ use nu_protocol::{
use tui::layout::Rect;
use crate::{
nu_common::try_build_table,
nu_common::{try_build_table, NuSpan},
pager::Frame,
util::map_into_value,
views::{Layout, Preview, View, ViewConfig},
@ -128,10 +128,57 @@ impl ConfigView {
fn create_output_string(&mut self, config: ViewConfig) -> String {
match self.format {
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)
}
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(),
})
}