Repository navigation
feat: commandline edit --accept to instantly execute command - #16193
Conversation
2d0e749 to
9d37488
Compare
|
UPDATED
Trialing with
|
9d37488 to
6bb343e
Compare
|
I have updated both PRs to the latest |
|
When I run @@ -79,6 +79,9 @@
$env.config = ($env.config | default [] keybindings)
+# unset previously added default keybinds
+$env.config.keybindings = $env.config.keybindings | where name != atuin
+
$env.config = (
$env.config | upsert keybindings (
$env.config.keybindings |
The existing initialisation script is expected to work fine, but won't auto-accept until it is updated. If this PR is accepted, I will open a PR to Atuin to resolve that. |
This is an important point – you can confirm it is working when you run it explicitly. What version of Atuin are you running? It may be that you are not running a version compatible with the script I provided. An example might be that it isn't returning the atuin --version
The script has code explicitly to ignore that: # Magic token to make sure we don't record commands run by keybindings
let ATUIN_KEYBINDING_TOKEN = $"# (random uuid)"
... |
| #[test] | ||
| fn commandline_test_accept() -> TestResult { | ||
| // Explanation: run_test captures a command's output. | ||
| // But `--accept` executes a new command that then outputs "accepted". | ||
| // So `commandline edit -A` doesn't have any output. | ||
| // Therefore the additional test that pipes to `commandline` | ||
| run_test("commandline edit --accept \"print accepted\"", "") | ||
| } |
There was a problem hiding this comment.
I'm not really sure that this is a useful test
There was a problem hiding this comment.
I will remove that
| // If the engine is configured to immediately | ||
| line_editor = if engine_state | ||
| .immediately_accept | ||
| .compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed) |
There was a problem hiding this comment.
this compare_exchange is clever!
|
Thanks for the initial review @132ikl! Now that the PR for reedline has been merged, I will update this PR |
e92befe to
9803370
Compare
sholderbach
left a comment
There was a problem hiding this comment.
My memory ordering knowledge get's rusty quickly so I am not quite sure if it should be either Release and Acquire or could suffice with a Relaxed on both ends.
Trying to assume the worst case, I think in a multithreaded environment commandline as implemented currently can tear between the value set when passing --accept and further edits set by commandline from different threads. Not sure if we want to accept that.
| engine_state | ||
| .immediately_accept | ||
| .store(true, Ordering::Relaxed); | ||
| } |
There was a problem hiding this comment.
Not sure how much it matters in practice, but shouldn't this be Ordering::Release which would imply a happens-before relationship for the writes in the critical section of engine_state.repl_state?
In nu-cli/repl.rs the compare_exchange which you set to Acquire is ahead of the read of the repl_state mutex which happens right before the call into Reedline::read_line.
I guess for singular uses of commandline --accept the mutex should ensure ordering in itself, as the MutexGuard::drop happens after the store.
There was a problem hiding this comment.
Good questions! I agree that the mutex guarantees overall ordering.
As for the use of Relaxed, for this specific flag, we don't care about other memory operations, because the flag itself is used in isolation and consumers of the flag are not dependent on other operations happening before this.
Same for the compare_exchange, which is used in isolation to determine whether we need to call the line_editor.with_immediately_accept API, which is also configured in isolation.
This is exactly my atuin version.
Maybe there is something off with my general nushell config, I'll see if I can test this on another system or in a VM to rule this out. |
|
I finally found the issue: Now working like a charm, thanks!! |
Great news! |
9803370 to
142f00f
Compare
|
@sholderbach are you satisfied here? if yes, let's get this merged |
sholderbach
left a comment
There was a problem hiding this comment.
For it to be considered background-job-safe we need to consider repl_state and immediately_accept together.
|
@sholderbach thanks – I wondered that too, given these commands can be executed on multiple threads and manipulate shared state, but it didn't seem obvious how to address it. You mentioned a queue, which makes the most sense, but definitely beyond the scope of this PR.
Are you saying this is enough to have the PR accepted? That is, I'll update the |
Example use case: atuin
142f00f to
261f5f6
Compare
|
@sholderbach / @132ikl I've updated the logic:
I have run it many times and cannot get it to produce "no" in the following: 0..10 | each {job spawn {commandline edit --replace "print no"}}; commandline edit --accept --replace "print hello"
|
sholderbach
left a comment
There was a problem hiding this comment.
Thanks for iterating on this.
|
|
||
| start_time = std::time::Instant::now(); | ||
| line_editor = line_editor.with_transient_prompt(transient_prompt); | ||
| line_editor = if engine_state.immediately_accept { |
There was a problem hiding this comment.
There shouldn't be a need for a separate immediately_accept if it is part of the repl_state, right?
There was a problem hiding this comment.
The reason I kept these separate is because the engine_state.repl_state is parsed (and therefore set) in the parse_operation function:
nushell/crates/nu-cli/src/repl.rs
Line 606 in 261f5f6
When executing commandline edit --accept, after this call, ReplState will be updated with changes to the buffer and the accept flag set to true. When flush_engine_state_repl_buffer is called, it sends any changes to the buffer, from the commandline edit command:
nushell/crates/nu-cli/src/repl.rs
Lines 1140 to 1147 in 261f5f6
and then clears its state:
nushell/crates/nu-cli/src/repl.rs
Lines 1150 to 1151 in 261f5f6
You mentioned:
we need to consider repl_state and immediately_accept together
which I agree is more correct. That means that accept should be used with the next call to line_editor.read_line. In that case, I'm taking the value of accept and storing it for the next iteration (and read_line call) and clearing it here. This ensures that from when the lock of repl_state is released and when we call loop_iteration the next time, we take and clear the immediately_accept value from the last flush.
An alternative is we could mutate the line_editor that is passed in, if repl_state.accept is true. Something like:
*line_editor = line_editor.with_immediately_accept(repl.accept);or return the line_editor from flush_engine_state_repl_buffer, so that it is assigned at the call site:
line_editor = flush_engine_state_repl_buffer(engine_state, &mut line_editor);In either case, we always clear the accept flag after calling line_editor.read_line:
line_editor = line_editor
// CLEAR STACK-REFERENCE 1
.with_highlighter(Box::<NoOpHighlighter>::default())
// CLEAR STACK-REFERENCE 2
.with_completer(Box::<DefaultCompleter>::default())
// Ensure immediately accept is always cleared
.with_immediately_accept(false);| engine_state.immediately_accept = repl.accept; | ||
| repl.accept = false; |
There was a problem hiding this comment.
If flush_engine_state_repl_buffer is the only place we alter the buffer, I think it would make sense to place the .with_immediately_accept there as well, reading directly from repl_state.accept. Then it would be guaranteed that any alterations being observed through the whole chain of hooks etc. would be consistent, and no bugs could be introduced by someone in the future unintentionally modifying engine_state.immediately_accept
There was a problem hiding this comment.
Sounds good – I'll make that change 👍🏻
There was a problem hiding this comment.
@sholderbach done! Can confirm the test script is still consistent:
0..10 | each {job spawn {commandline edit --replace "print no"}}; commandline edit --accept --replace "print hello"print hello
Instead of storing the immediately_accept flag in EngineState, handle it directly when flushing the REPL buffer by returning a modified Reedline instance. This simplifies the state management and removes the need for a persistent flag in EngineState, making the code more maintainable and reducing coupling between components.
sholderbach
left a comment
There was a problem hiding this comment.
Awesome, looks good. Thanks for iterating with us
Did it also fixed showing the history item for atuin search? This is happening to me as well and I have enter_accept set to true |
|
@VuiMuich ^^ |
|
@wjarka I am not sure I understand correctly what you mean. |
|
@VuiMuich yes.
This is what I am getting |
|
Alright, for anyone having this issue - it turned out I had the original init.nu created when I set up atuin still active. So it was the original file adding that item, not the one proposed here. Fixed. |
|
📣 Heads up that atuin 18.9.0 changed the shell variable from I've updated the posted script: |
# Summary Adjust the Nushell script to check the version and enable accept support when Nushell 0.106.0 or greater is found. See: nushell/nushell#16193 <!-- Thank you for making a PR! Bug fixes are always welcome, but if you're adding a new feature or changing an existing one, we'd really appreciate if you open an issue, post on the forum, or drop in on Discord --> ## Checks - [x] I am happy for maintainers to push small adjustments to this PR, to speed up the review cycle - [x] I have checked that there are no existing pull requests for the same thing
How did you fix it? |
When I started using nushell my atuin was older (I think v18.08, while the current version is v18.12.1) and pressing enter wasn't executing the command for me. It works now for me and I:
PS. Don't recall where I got the save command originally, I got it from my history today |
|
You can put |



Description
Fixes
Revised
Depends on
Adds the
--acceptflag to thecommandlinecommand, to immediately accept the input.Example use case: atuin
User-facing changes
Release notes summary - What our users need to know
The
commandline editcommand has a new flag--accept(or-A) which immediately executes the resulting commandline.Tests + Formatting
I added two test cases that execute
commandline edit -A.There is also some documentation about their unintuitive expectations output.
After Submitting
The docs can be updated to the new flag:
--accept, -A: immediately execute the command (no additional return required)
Note
This PR will be revised if / when nushell/reedline#933 is accepted
Demo