forked from extern/nushell
# Description This PR fixes some problems I found in scripts by adding some additional input_output_types. Here's a list of nushell scripts that it fixed. Look for `# broke here:` below. This PR fixes 3, 4, 6, 7 by adding additional input_output_types. 1 was fixed by changing the script. 2. just doesn't work anymore because mkdir return type has changed. 5, is a problem with the script, the datatype for `...rest` needed to be removed. ```nushell # 1. def terminal-size [] { let sz = (input (ansi size) --bytes-until 'R') # $sz should look like this # Length: 9 (0x9) bytes | printable whitespace ascii_other non_ascii # 00000000: 1b 5b 33 38 3b 31 35 30 52 •[38;150R let sz_len = ($sz | bytes length) # let's skip the esc[ and R let r = ($sz | bytes at 2..($sz_len - 2) | into string) # $r should look like 38;150 # broke here: because $r needed to be a string for split row let size = ($r | split row ';') # output in record syntax { rows: ($size | get 0) columns: ($size | get 1) } } # 2. # make and cd to a folder def-env mkcd [name: path] { # broke here: but apparently doesn't work anymore # It looks like mkdir returns nothing where it used to return a value cd (mkdir $name -v | first) } # 3. # changed 'into datetime' def get-monday [] { (seq date -r --days 7 | # broke here: because into datetime didn't support list input into datetime | where { |e| ($e | date format %u) == "1" }).0 | date format "%Y-%m-%d" } # 4. # Delete all branches that are not in the excepts list # Usage: del-branches [main] def del-branches [ excepts:list # don't delete branch in the list --dry-run(-d) # do a dry-run ] { let branches = (git branch | lines | str trim) # broke here: because str replace didn't support list<string> let remote_branches = (git branch -r | lines | str replace '^.+?/' '' | uniq) if $dry_run { print "Starting Dry-Run" } else { print "Deleting for real" } $branches | each {|it| if ($it not-in $excepts) and ($it not-in $remote_branches) and (not ($it | str starts-with "*")) { # git branch -D $it if $dry_run { print $"git branch -D ($it)" } else { print $"Deleting ($it) for real" #git branch -D $it } } } } # 5. # zoxide script def-env __zoxide_z [...rest] { # `z -` does not work yet, see https://github.com/nushell/nushell/issues/4769 # broke here: 'append doesn't support string input' let arg0 = ($rest | append '~').0 # broke here: 'length doesn't support string input' so change `...rest:string` to `...rest` let path = if (($rest | length) <= 1) and ($arg0 == '-' or ($arg0 | path expand | path type) == dir) { $arg0 } else { (zoxide query --exclude $env.PWD -- $rest | str trim -r -c "\n") } cd $path } # 6. def a [] { let x = (commandline) if ($x | is-empty) { return } # broke here: because commandline was previously only returning Type::Nothing if not ($x | str starts-with "aaa") { print "bbb" } } # 7. # repeat a string x amount of times def repeat [arg: string, dupe: int] { # broke here: 'command does not support range input' 0..<$dupe | reduce -f '' {|i acc| $acc + $arg} } ``` # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
130 lines
4.4 KiB
Rust
130 lines
4.4 KiB
Rust
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::Category;
|
|
use nu_protocol::IntoPipelineData;
|
|
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Type, Value};
|
|
use unicode_segmentation::UnicodeSegmentation;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Commandline;
|
|
|
|
impl Command for Commandline {
|
|
fn name(&self) -> &str {
|
|
"commandline"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("commandline")
|
|
.input_output_types(vec![
|
|
(Type::Nothing, Type::Nothing),
|
|
(Type::String, Type::String),
|
|
])
|
|
.switch(
|
|
"cursor",
|
|
"Set or get the current cursor position",
|
|
Some('c'),
|
|
)
|
|
.switch(
|
|
"append",
|
|
"appends the string to the end of the buffer",
|
|
Some('a'),
|
|
)
|
|
.switch(
|
|
"insert",
|
|
"inserts the string into the buffer at the cursor position",
|
|
Some('i'),
|
|
)
|
|
.switch(
|
|
"replace",
|
|
"replaces the current contents of the buffer (default)",
|
|
Some('r'),
|
|
)
|
|
.optional(
|
|
"cmd",
|
|
SyntaxShape::String,
|
|
"the string to perform the operation with",
|
|
)
|
|
.category(Category::Core)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"View or modify the current command line input buffer."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["repl", "interactive"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
if let Some(cmd) = call.opt::<Value>(engine_state, stack, 0)? {
|
|
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
|
|
|
|
if call.has_flag("cursor") {
|
|
let cmd_str = cmd.as_string()?;
|
|
match cmd_str.parse::<i64>() {
|
|
Ok(n) => {
|
|
repl.cursor_pos = if n <= 0 {
|
|
0usize
|
|
} else {
|
|
repl.buffer
|
|
.grapheme_indices(true)
|
|
.map(|(i, _c)| i)
|
|
.nth(n as usize)
|
|
.unwrap_or(repl.buffer.len())
|
|
}
|
|
}
|
|
Err(_) => {
|
|
return Err(ShellError::CantConvert {
|
|
to_type: "int".to_string(),
|
|
from_type: "string".to_string(),
|
|
span: cmd.span()?,
|
|
help: Some(format!(
|
|
r#"string "{cmd_str}" does not represent a valid integer"#
|
|
)),
|
|
})
|
|
}
|
|
}
|
|
} else if call.has_flag("append") {
|
|
repl.buffer.push_str(&cmd.as_string()?);
|
|
} else if call.has_flag("insert") {
|
|
let cmd_str = cmd.as_string()?;
|
|
let cursor_pos = repl.cursor_pos;
|
|
repl.buffer.insert_str(cursor_pos, &cmd_str);
|
|
repl.cursor_pos += cmd_str.len();
|
|
} else {
|
|
repl.buffer = cmd.as_string()?;
|
|
repl.cursor_pos = repl.buffer.len();
|
|
}
|
|
Ok(Value::Nothing { span: call.head }.into_pipeline_data())
|
|
} else {
|
|
let repl = engine_state.repl_state.lock().expect("repl state mutex");
|
|
if call.has_flag("cursor") {
|
|
let char_pos = repl
|
|
.buffer
|
|
.grapheme_indices(true)
|
|
.chain(std::iter::once((repl.buffer.len(), "")))
|
|
.position(|(i, _c)| i == repl.cursor_pos)
|
|
.expect("Cursor position isn't on a grapheme boundary");
|
|
Ok(Value::String {
|
|
val: char_pos.to_string(),
|
|
span: call.head,
|
|
}
|
|
.into_pipeline_data())
|
|
} else {
|
|
Ok(Value::String {
|
|
val: repl.buffer.to_string(),
|
|
span: call.head,
|
|
}
|
|
.into_pipeline_data())
|
|
}
|
|
}
|
|
}
|
|
}
|