2021-11-14 20:25:57 +01:00
|
|
|
use crate::{ShellError, Value};
|
2021-11-20 14:12:35 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::collections::HashMap;
|
2021-11-14 20:25:57 +01:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
|
|
pub struct Config {
|
|
|
|
pub filesize_metric: bool,
|
|
|
|
pub table_mode: String,
|
2021-11-15 21:09:17 +01:00
|
|
|
pub use_ls_colors: bool,
|
2021-11-20 14:12:35 +01:00
|
|
|
pub color_config: HashMap<String, String>,
|
2021-11-14 20:25:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
filesize_metric: false,
|
|
|
|
table_mode: "rounded".into(),
|
2021-11-15 21:09:17 +01:00
|
|
|
use_ls_colors: true,
|
2021-11-20 14:12:35 +01:00
|
|
|
color_config: HashMap::new(),
|
2021-11-14 20:25:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn into_config(self) -> Result<Config, ShellError> {
|
|
|
|
let v = self.as_record()?;
|
|
|
|
|
|
|
|
let mut config = Config::default();
|
|
|
|
|
|
|
|
for (key, value) in v.0.iter().zip(v.1) {
|
|
|
|
match key.as_str() {
|
|
|
|
"filesize_metric" => {
|
|
|
|
config.filesize_metric = value.as_bool()?;
|
|
|
|
}
|
|
|
|
"table_mode" => {
|
|
|
|
config.table_mode = value.as_string()?;
|
|
|
|
}
|
2021-11-15 21:09:17 +01:00
|
|
|
"use_ls_colors" => {
|
|
|
|
config.use_ls_colors = value.as_bool()?;
|
|
|
|
}
|
2021-11-20 14:12:35 +01:00
|
|
|
"color_config" => {
|
|
|
|
let (cols, vals) = value.as_record()?;
|
|
|
|
let mut hm = HashMap::new();
|
|
|
|
for (k, v) in cols.iter().zip(vals) {
|
|
|
|
hm.insert(k.to_string(), v.as_string().unwrap());
|
|
|
|
}
|
|
|
|
config.color_config = hm;
|
|
|
|
}
|
2021-11-14 20:25:57 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
}
|