mirror of
https://github.com/nushell/nushell.git
synced 2024-11-07 17:14:23 +01:00
Add config file loading
This commit is contained in:
parent
97d7157773
commit
a760e46c1c
9
crates/nu-path/src/helpers.rs
Normal file
9
crates/nu-path/src/helpers.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn home_dir() -> Option<PathBuf> {
|
||||
dirs_next::home_dir()
|
||||
}
|
||||
|
||||
pub fn config_dir() -> Option<PathBuf> {
|
||||
dirs_next::config_dir()
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
mod dots;
|
||||
mod expansions;
|
||||
mod helpers;
|
||||
mod tilde;
|
||||
mod util;
|
||||
|
||||
pub use expansions::{canonicalize, canonicalize_with, expand_path, expand_path_with};
|
||||
pub use helpers::{config_dir, home_dir};
|
||||
pub use tilde::expand_tilde;
|
||||
pub use util::trim_trailing_slash;
|
||||
|
113
src/main.rs
113
src/main.rs
@ -91,6 +91,23 @@ fn main() -> Result<()> {
|
||||
let mut nu_prompt = NushellPrompt::new();
|
||||
let stack = nu_protocol::engine::Stack::new();
|
||||
|
||||
// Load config startup file
|
||||
if let Some(mut config_path) = nu_path::config_dir() {
|
||||
config_path.push("nushell");
|
||||
config_path.push("config.nu");
|
||||
|
||||
// FIXME: remove this message when we're ready
|
||||
println!("Loading config from: {:?}", config_path);
|
||||
|
||||
if config_path.exists() {
|
||||
let config_filename = config_path.to_string_lossy().to_owned();
|
||||
|
||||
if let Ok(contents) = std::fs::read_to_string(&config_path) {
|
||||
eval_source(engine_state.clone(), &stack, &contents, &config_filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
let prompt = update_prompt(
|
||||
PROMPT_COMMAND,
|
||||
@ -124,45 +141,12 @@ fn main() -> Result<()> {
|
||||
continue;
|
||||
}
|
||||
|
||||
let (block, delta) = {
|
||||
let engine_state = engine_state.borrow();
|
||||
let mut working_set = StateWorkingSet::new(&*engine_state);
|
||||
let (output, err) = parse(
|
||||
&mut working_set,
|
||||
Some(&format!("entry #{}", entry_num)),
|
||||
s.as_bytes(),
|
||||
false,
|
||||
);
|
||||
if let Some(err) = err {
|
||||
report_error(&working_set, &err);
|
||||
continue;
|
||||
}
|
||||
(output, working_set.render())
|
||||
};
|
||||
|
||||
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
|
||||
|
||||
let state = nu_protocol::engine::EvaluationContext {
|
||||
engine_state: engine_state.clone(),
|
||||
stack: stack.clone(),
|
||||
};
|
||||
|
||||
match eval_block(&state, &block, Value::nothing()) {
|
||||
Ok(value) => {
|
||||
if let Err(err) = print_value(value, &state) {
|
||||
let engine_state = engine_state.borrow();
|
||||
let working_set = StateWorkingSet::new(&*engine_state);
|
||||
|
||||
report_error(&working_set, &err);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
let engine_state = engine_state.borrow();
|
||||
let working_set = StateWorkingSet::new(&*engine_state);
|
||||
|
||||
report_error(&working_set, &err);
|
||||
}
|
||||
}
|
||||
eval_source(
|
||||
engine_state.clone(),
|
||||
&stack,
|
||||
&s,
|
||||
&format!("entry #{}", entry_num),
|
||||
);
|
||||
}
|
||||
Ok(Signal::CtrlC) => {
|
||||
println!("Ctrl-c");
|
||||
@ -267,3 +251,54 @@ fn update_prompt<'prompt>(
|
||||
|
||||
nu_prompt as &dyn Prompt
|
||||
}
|
||||
|
||||
fn eval_source(
|
||||
engine_state: Rc<RefCell<EngineState>>,
|
||||
stack: &Stack,
|
||||
source: &str,
|
||||
fname: &str,
|
||||
) -> bool {
|
||||
let (block, delta) = {
|
||||
let engine_state = engine_state.borrow();
|
||||
let mut working_set = StateWorkingSet::new(&*engine_state);
|
||||
let (output, err) = parse(
|
||||
&mut working_set,
|
||||
Some(fname), // format!("entry #{}", entry_num)
|
||||
source.as_bytes(),
|
||||
false,
|
||||
);
|
||||
if let Some(err) = err {
|
||||
report_error(&working_set, &err);
|
||||
return false;
|
||||
}
|
||||
(output, working_set.render())
|
||||
};
|
||||
|
||||
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
|
||||
|
||||
let state = nu_protocol::engine::EvaluationContext {
|
||||
engine_state: engine_state.clone(),
|
||||
stack: stack.clone(),
|
||||
};
|
||||
|
||||
match eval_block(&state, &block, Value::nothing()) {
|
||||
Ok(value) => {
|
||||
if let Err(err) = print_value(value, &state) {
|
||||
let engine_state = engine_state.borrow();
|
||||
let working_set = StateWorkingSet::new(&*engine_state);
|
||||
|
||||
report_error(&working_set, &err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
let engine_state = engine_state.borrow();
|
||||
let working_set = StateWorkingSet::new(&*engine_state);
|
||||
|
||||
report_error(&working_set, &err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user