mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Add --no-newline option to nu
(#12410)
# Description I have `nu` set as my shell in my editor, which allows me to easily pipe selections of text to things like `str pascal-case` or even more complex string operation pipelines, which I find super handy. However, the only annoying thing is that I pretty much always have to add `| print -n` at the end, because `nu` adds a newline when it prints the resulting value. This adds a `--no-newline` option to stop that from happening, and then you don't need to pipe to `print -n` anymore, you can just have your shell command for your editor contain that flag. # User-Facing Changes - Add `--no-newline` command line option # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib`
This commit is contained in:
@ -15,6 +15,7 @@ pub fn evaluate_commands(
|
||||
stack: &mut Stack,
|
||||
input: PipelineData,
|
||||
table_mode: Option<Value>,
|
||||
no_newline: bool,
|
||||
) -> Result<Option<i64>> {
|
||||
// Translate environment variables from Strings to Values
|
||||
if let Some(e) = convert_env_values(engine_state, stack) {
|
||||
@ -60,7 +61,13 @@ pub fn evaluate_commands(
|
||||
if let Some(t_mode) = table_mode {
|
||||
config.table_mode = t_mode.coerce_str()?.parse().unwrap_or_default();
|
||||
}
|
||||
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config)
|
||||
crate::eval_file::print_table_or_error(
|
||||
engine_state,
|
||||
stack,
|
||||
pipeline_data,
|
||||
&mut config,
|
||||
no_newline,
|
||||
)
|
||||
}
|
||||
Err(err) => {
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
|
@ -185,6 +185,7 @@ pub(crate) fn print_table_or_error(
|
||||
stack: &mut Stack,
|
||||
mut pipeline_data: PipelineData,
|
||||
config: &mut Config,
|
||||
no_newline: bool,
|
||||
) -> Option<i64> {
|
||||
let exit_code = match &mut pipeline_data {
|
||||
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
|
||||
@ -203,7 +204,7 @@ pub(crate) fn print_table_or_error(
|
||||
if let Some(decl_id) = engine_state.find_decl("table".as_bytes(), &[]) {
|
||||
let command = engine_state.get_decl(decl_id);
|
||||
if command.get_block_id().is_some() {
|
||||
print_or_exit(pipeline_data, engine_state, config);
|
||||
print_or_exit(pipeline_data, engine_state, config, no_newline);
|
||||
} else {
|
||||
// The final call on table command, it's ok to set redirect_output to false.
|
||||
let call = Call::new(Span::new(0, 0));
|
||||
@ -211,7 +212,7 @@ pub(crate) fn print_table_or_error(
|
||||
|
||||
match table {
|
||||
Ok(table) => {
|
||||
print_or_exit(table, engine_state, config);
|
||||
print_or_exit(table, engine_state, config, no_newline);
|
||||
}
|
||||
Err(error) => {
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
@ -221,7 +222,7 @@ pub(crate) fn print_table_or_error(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print_or_exit(pipeline_data, engine_state, config);
|
||||
print_or_exit(pipeline_data, engine_state, config, no_newline);
|
||||
}
|
||||
|
||||
// Make sure everything has finished
|
||||
@ -238,7 +239,12 @@ pub(crate) fn print_table_or_error(
|
||||
}
|
||||
}
|
||||
|
||||
fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) {
|
||||
fn print_or_exit(
|
||||
pipeline_data: PipelineData,
|
||||
engine_state: &mut EngineState,
|
||||
config: &Config,
|
||||
no_newline: bool,
|
||||
) {
|
||||
for item in pipeline_data {
|
||||
if let Value::Error { error, .. } = item {
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
@ -248,7 +254,10 @@ fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, co
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let out = item.to_expanded_string("\n", config) + "\n";
|
||||
let mut out = item.to_expanded_string("\n", config);
|
||||
if !no_newline {
|
||||
out.push('\n');
|
||||
}
|
||||
let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{err}"));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user