Add is-interactive and is-login to NuVariable and allow running scripts with -i (#8306)

Add two rows in `$nu`, `$nu.is-interactive` and `$nu.is-login`, which
are true when nu is run in interactive and login mode respectively.

The `-i` flag now behaves a bit more like that of bash's, where the any
provided command or file is run without REPL but in "interactive mode".
This should entail sourcing interactive-mode config files, but since we
are planning on overhauling the config system soon, I'm holding off on
that. For now, all `-i` does is set `$nu.is-interactive` to be true.

About testing, I can't seem to find where cli-args get tested, so I
haven't written any new tests for this. Also I don't think there are any
docs that need updating. However if I'm wrong please tell me.
This commit is contained in:
StevenDoesStuffs 2023-03-08 20:59:33 -06:00 committed by GitHub
parent d31a51e3bc
commit 7e949595bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 6 deletions

View File

@ -524,13 +524,15 @@ fn variables_completions() {
// Test completions for $nu
let suggestions = completer.complete("$nu.", 4);
assert_eq!(9, suggestions.len());
assert_eq!(11, suggestions.len());
let expected: Vec<String> = vec![
"config-path".into(),
"env-path".into(),
"history-path".into(),
"home-path".into(),
"is-interactive".into(),
"is-login".into(),
"loginshell-path".into(),
"os-info".into(),
"pid".into(),

View File

@ -37,6 +37,9 @@ impl LazyRecord for NuVariable {
cols.push("pid");
cols.push("os-info");
cols.push("is-interactive");
cols.push("is-login");
cols
}
@ -177,6 +180,14 @@ impl LazyRecord for NuVariable {
Ok(os_record)
}
"is-interactive" => Ok(Value::Bool {
val: self.engine_state.is_interactive,
span: self.span,
}),
"is-login" => Ok(Value::Bool {
val: self.engine_state.is_login,
span: self.span,
}),
_ => err(&format!("Could not find column '{column}'")),
}
}

View File

@ -145,6 +145,8 @@ pub struct EngineState {
// If Nushell was started, e.g., with `nu spam.nu`, the file's parent is stored here
pub currently_parsed_cwd: Option<PathBuf>,
pub regex_cache: Arc<Mutex<LruCache<String, Regex>>>,
pub is_interactive: bool,
pub is_login: bool,
}
// The max number of compiled regexes to keep around in a LRU cache, arbitrarily chosen
@ -195,6 +197,8 @@ impl EngineState {
regex_cache: Arc::new(Mutex::new(LruCache::new(
NonZeroUsize::new(REGEX_CACHE_SIZE).expect("tried to create cache of size zero"),
))),
is_interactive: false,
is_login: false,
}
}

View File

@ -63,6 +63,9 @@ fn main() -> Result<()> {
let parsed_nu_cli_args = parse_commandline_args(&args_to_nushell.join(" "), &mut engine_state)
.unwrap_or_else(|_| std::process::exit(1));
engine_state.is_interactive = parsed_nu_cli_args.interactive_shell.is_some();
engine_state.is_login = parsed_nu_cli_args.login_shell.is_some();
let use_color = engine_state.get_config().use_ansi_coloring;
if let Some(level) = parsed_nu_cli_args
.log_level
@ -122,10 +125,7 @@ fn main() -> Result<()> {
start_time = std::time::Instant::now();
// keep this condition in sync with the branches below
acquire_terminal(
parsed_nu_cli_args.commands.is_none()
&& (script_name.is_empty() || parsed_nu_cli_args.interactive_shell.is_some()),
);
acquire_terminal(parsed_nu_cli_args.commands.is_none() && script_name.is_empty());
perf(
"acquire_terminal",
start_time,
@ -234,7 +234,7 @@ fn main() -> Result<()> {
&commands,
input,
)
} else if !script_name.is_empty() && parsed_nu_cli_args.interactive_shell.is_none() {
} else if !script_name.is_empty() {
run_file(
&mut engine_state,
parsed_nu_cli_args,
@ -244,6 +244,7 @@ fn main() -> Result<()> {
input,
)
} else {
engine_state.is_interactive = true;
run_repl(engine_state, parsed_nu_cli_args, entire_start_time)
}
}

View File

@ -199,3 +199,59 @@ fn run_export_extern() {
assert!(actual.out.contains("Usage"));
})
}
#[test]
fn run_in_login_mode() {
let child_output = std::process::Command::new("sh")
.arg("-c")
.arg(format!(
"{:?} -l -c 'echo $nu.is-login'",
nu_test_support::fs::executable_path()
))
.output()
.expect("true");
assert!(child_output.stderr.is_empty());
}
#[test]
fn run_in_not_login_mode() {
let child_output = std::process::Command::new("sh")
.arg("-c")
.arg(format!(
"{:?} -c 'echo $nu.is-login'",
nu_test_support::fs::executable_path()
))
.output()
.expect("false");
assert!(child_output.stderr.is_empty());
}
#[test]
fn run_in_interactive_mode() {
let child_output = std::process::Command::new("sh")
.arg("-c")
.arg(format!(
"{:?} -i -c 'echo $nu.is-interactive'",
nu_test_support::fs::executable_path()
))
.output()
.expect("true");
assert!(child_output.stderr.is_empty());
}
#[test]
fn run_in_noninteractive_mode() {
let child_output = std::process::Command::new("sh")
.arg("-c")
.arg(format!(
"{:?} -c 'echo $nu.is-interactive'",
nu_test_support::fs::executable_path()
))
.output()
.expect("false");
assert!(child_output.stderr.is_empty());
}