2021-12-17 02:04:54 +01:00
|
|
|
use crate::{BlockId, ShellError, Span, 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
|
|
|
|
2021-12-31 23:41:29 +01:00
|
|
|
const ANIMATE_PROMPT_DEFAULT: bool = true;
|
2021-12-02 08:10:40 +01:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
|
|
pub struct EnvConversion {
|
2021-12-21 23:32:38 +01:00
|
|
|
pub from_string: Option<(BlockId, Span)>,
|
|
|
|
pub to_string: Option<(BlockId, Span)>,
|
2021-12-17 02:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvConversion {
|
|
|
|
pub fn from_record(value: &Value) -> Result<Self, ShellError> {
|
|
|
|
let record = value.as_record()?;
|
|
|
|
|
|
|
|
let mut conv_map = HashMap::new();
|
|
|
|
|
|
|
|
for (k, v) in record.0.iter().zip(record.1) {
|
|
|
|
if (k == "from_string") || (k == "to_string") {
|
|
|
|
conv_map.insert(k.as_str(), (v.as_block()?, v.span()?));
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::UnsupportedConfigValue(
|
|
|
|
"'from_string' and 'to_string' fields".into(),
|
|
|
|
k.into(),
|
|
|
|
value.span()?,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 23:32:38 +01:00
|
|
|
let from_string = conv_map.get("from_string").cloned();
|
|
|
|
let to_string = conv_map.get("to_string").cloned();
|
|
|
|
|
|
|
|
Ok(EnvConversion {
|
|
|
|
from_string,
|
|
|
|
to_string,
|
|
|
|
})
|
2021-12-17 02:04:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-29 21:37:09 +01:00
|
|
|
pub use_grid_icons: bool,
|
2021-12-01 20:20:23 +01:00
|
|
|
pub footer_mode: FooterMode,
|
2021-12-02 08:10:40 +01:00
|
|
|
pub animate_prompt: bool,
|
2021-12-07 21:06:14 +01:00
|
|
|
pub float_precision: i64,
|
2021-12-09 20:19:36 +01:00
|
|
|
pub filesize_format: String,
|
2021-12-09 23:06:26 +01:00
|
|
|
pub use_ansi_coloring: bool,
|
2021-12-17 02:04:54 +01:00
|
|
|
pub env_conversions: HashMap<String, EnvConversion>,
|
2021-12-23 10:31:16 +01:00
|
|
|
pub edit_mode: String,
|
2021-12-23 20:59:00 +01:00
|
|
|
pub max_history_size: i64,
|
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-29 21:37:09 +01:00
|
|
|
use_grid_icons: false,
|
2021-12-01 20:20:23 +01:00
|
|
|
footer_mode: FooterMode::Never,
|
2021-12-02 08:10:40 +01:00
|
|
|
animate_prompt: ANIMATE_PROMPT_DEFAULT,
|
2021-12-07 21:06:14 +01:00
|
|
|
float_precision: 4,
|
2021-12-09 20:19:36 +01:00
|
|
|
filesize_format: "auto".into(),
|
2021-12-09 23:06:26 +01:00
|
|
|
use_ansi_coloring: true,
|
2021-12-17 02:04:54 +01:00
|
|
|
env_conversions: HashMap::new(), // TODO: Add default conversoins
|
2021-12-23 10:31:16 +01:00
|
|
|
edit_mode: "emacs".into(),
|
2021-12-23 20:59:00 +01:00
|
|
|
max_history_size: 1000,
|
2021-11-14 20:25:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 20:20:23 +01:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
|
|
pub enum FooterMode {
|
|
|
|
/// Never show the footer
|
|
|
|
Never,
|
|
|
|
/// Always show the footer
|
|
|
|
Always,
|
|
|
|
/// Only show the footer if there are more than RowCount rows
|
|
|
|
RowCount(u64),
|
|
|
|
/// Calculate the screen height, calculate row count, if display will be bigger than screen, add the footer
|
|
|
|
Auto,
|
|
|
|
}
|
|
|
|
|
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" => {
|
2021-12-14 20:34:39 +01:00
|
|
|
let (cols, inner_vals) = value.as_record()?;
|
2021-11-20 14:12:35 +01:00
|
|
|
let mut hm = HashMap::new();
|
2021-12-14 20:34:39 +01:00
|
|
|
for (k, v) in cols.iter().zip(inner_vals) {
|
|
|
|
match &v {
|
|
|
|
Value::Record {
|
|
|
|
cols: inner_cols,
|
|
|
|
vals: inner_vals,
|
|
|
|
span: _,
|
|
|
|
} => {
|
|
|
|
// make a string from our config.color_config section that
|
|
|
|
// looks like this: { fg: "#rrggbb" bg: "#rrggbb" attr: "abc", }
|
|
|
|
// the real key here was to have quotes around the values but not
|
|
|
|
// require them around the keys.
|
|
|
|
|
|
|
|
// maybe there's a better way to generate this but i'm not sure
|
|
|
|
// what it is.
|
|
|
|
let key = k.to_string();
|
|
|
|
let mut val: String = inner_cols
|
|
|
|
.iter()
|
|
|
|
.zip(inner_vals)
|
|
|
|
.map(|(x, y)| {
|
|
|
|
let clony = y.clone();
|
|
|
|
format!("{}: \"{}\" ", x, clony.into_string(", ", &config))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
// now insert the braces at the front and the back to fake the json string
|
|
|
|
val.insert(0, '{');
|
|
|
|
val.push('}');
|
|
|
|
hm.insert(key, val);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
hm.insert(k.to_string(), v.as_string()?);
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 14:12:35 +01:00
|
|
|
}
|
|
|
|
config.color_config = hm;
|
|
|
|
}
|
2021-11-29 21:37:09 +01:00
|
|
|
"use_grid_icons" => {
|
|
|
|
config.use_grid_icons = value.as_bool()?;
|
|
|
|
}
|
2021-12-01 20:20:23 +01:00
|
|
|
"footer_mode" => {
|
2021-12-09 20:19:36 +01:00
|
|
|
let val_str = value.as_string()?.to_lowercase();
|
2021-12-01 20:20:23 +01:00
|
|
|
config.footer_mode = match val_str.as_ref() {
|
|
|
|
"auto" => FooterMode::Auto,
|
|
|
|
"never" => FooterMode::Never,
|
|
|
|
"always" => FooterMode::Always,
|
|
|
|
_ => match &val_str.parse::<u64>() {
|
|
|
|
Ok(number) => FooterMode::RowCount(*number),
|
|
|
|
_ => FooterMode::Never,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-12-02 08:10:40 +01:00
|
|
|
"animate_prompt" => {
|
2021-12-09 17:00:26 +01:00
|
|
|
config.animate_prompt = value.as_bool()?;
|
2021-12-02 08:10:40 +01:00
|
|
|
}
|
2021-12-07 21:06:14 +01:00
|
|
|
"float_precision" => {
|
2021-12-09 17:00:26 +01:00
|
|
|
config.float_precision = value.as_integer()?;
|
|
|
|
}
|
2021-12-09 23:06:26 +01:00
|
|
|
"use_ansi_coloring" => {
|
|
|
|
config.use_ansi_coloring = value.as_bool()?;
|
2021-12-07 21:06:14 +01:00
|
|
|
}
|
2021-12-09 20:19:36 +01:00
|
|
|
"filesize_format" => {
|
|
|
|
config.filesize_format = value.as_string()?.to_lowercase();
|
|
|
|
}
|
2021-12-17 02:04:54 +01:00
|
|
|
"env_conversions" => {
|
|
|
|
let (env_vars, conversions) = value.as_record()?;
|
|
|
|
let mut env_conversions = HashMap::new();
|
|
|
|
|
|
|
|
for (env_var, record) in env_vars.iter().zip(conversions) {
|
2021-12-21 23:32:38 +01:00
|
|
|
// println!("{}: {:?}", env_var, record);
|
2021-12-17 02:04:54 +01:00
|
|
|
env_conversions.insert(env_var.into(), EnvConversion::from_record(record)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.env_conversions = env_conversions;
|
|
|
|
}
|
2021-12-23 10:31:16 +01:00
|
|
|
"edit_mode" => {
|
|
|
|
config.edit_mode = value.as_string()?;
|
|
|
|
}
|
2021-12-23 20:59:00 +01:00
|
|
|
"max_history_size" => {
|
|
|
|
config.max_history_size = value.as_i64()?;
|
|
|
|
}
|
2021-11-14 20:25:57 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
}
|