forked from extern/nushell
Use environment variable for env_conversions (#4566)
* Handle string->value env conv. with env. var. Also adds the environment variable for Path/PATH and removes it from config. * Simplify getting the string->value conversion * Refactor env conversion into its own function * Use env var for to_string conversion; Remove conf * Fix indentation in default config
This commit is contained in:
@ -1,43 +1,9 @@
|
||||
use crate::{BlockId, ShellError, Span, Value};
|
||||
use crate::{ShellError, Span, Value};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
const ANIMATE_PROMPT_DEFAULT: bool = true;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct EnvConversion {
|
||||
pub from_string: Option<(BlockId, Span)>,
|
||||
pub to_string: Option<(BlockId, Span)>,
|
||||
}
|
||||
|
||||
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()?,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let from_string = conv_map.get("from_string").cloned();
|
||||
let to_string = conv_map.get("to_string").cloned();
|
||||
|
||||
Ok(EnvConversion {
|
||||
from_string,
|
||||
to_string,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Definition of a parsed keybinding from the config object
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct ParsedKeybinding {
|
||||
@ -60,7 +26,6 @@ pub struct Config {
|
||||
pub filesize_format: String,
|
||||
pub use_ansi_coloring: bool,
|
||||
pub quick_completions: bool,
|
||||
pub env_conversions: HashMap<String, EnvConversion>,
|
||||
pub edit_mode: String,
|
||||
pub max_history_size: i64,
|
||||
pub log_level: String,
|
||||
@ -84,7 +49,6 @@ impl Default for Config {
|
||||
filesize_format: "auto".into(),
|
||||
use_ansi_coloring: true,
|
||||
quick_completions: false,
|
||||
env_conversions: HashMap::new(), // TODO: Add default conversoins
|
||||
edit_mode: "emacs".into(),
|
||||
max_history_size: 1000,
|
||||
log_level: String::new(),
|
||||
@ -210,24 +174,6 @@ impl Value {
|
||||
eprintln!("$config.filesize_format is not a string")
|
||||
}
|
||||
}
|
||||
"env_conversions" => {
|
||||
if let Ok((env_vars, conversions)) = value.as_record() {
|
||||
let mut env_conversions = HashMap::new();
|
||||
|
||||
for (env_var, record) in env_vars.iter().zip(conversions) {
|
||||
// println!("{}: {:?}", env_var, record);
|
||||
if let Ok(conversion) = EnvConversion::from_record(record) {
|
||||
env_conversions.insert(env_var.into(), conversion);
|
||||
} else {
|
||||
eprintln!("$config.env_conversions has incorrect conversion")
|
||||
}
|
||||
}
|
||||
|
||||
config.env_conversions = env_conversions;
|
||||
} else {
|
||||
eprintln!("$config.env_conversions is not a record")
|
||||
}
|
||||
}
|
||||
"edit_mode" => {
|
||||
if let Ok(v) = value.as_string() {
|
||||
config.edit_mode = v.to_lowercase();
|
||||
|
Reference in New Issue
Block a user