Skip to content

feat: make ExecuteHostCommand accept closure#18018

Draft
Juhan280 wants to merge 12 commits into
nushell:mainfrom
Juhan280:execute_host_command-closure
Draft

feat: make ExecuteHostCommand accept closure#18018
Juhan280 wants to merge 12 commits into
nushell:mainfrom
Juhan280:execute_host_command-closure

Conversation

@Juhan280

@Juhan280 Juhan280 commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Pull request nushell/reedline#1049 is needed to be merged before we can merge this PR. (merged)

This is currently just a prototype. I am open to ideas how we can improve it.

Release notes summary - What our users need to know

The ExecuteHostCommand event for keybindings now also accepts closure along side strings.

$env.config.keybindings = [{
  name: exec_host_command_with_closure
  modifier: control
  keycode: char_t
  mode: [emacs vi_normal vi_insert]
  event: {
    send: ExecuteHostCommand
    cmd: {
     # this is a closure
     commandline edit $"# cmd: (commandline);"
    }
  }
}]

@Juhan280 Juhan280 force-pushed the execute_host_command-closure branch from 87d6614 to 5dc5a23 Compare April 17, 2026 19:40
@Juhan280

Juhan280 commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

Is there a simpler way of doing this without exposing the internal of the evaluate_source function?

@Juhan280 Juhan280 marked this pull request as ready for review April 17, 2026 19:47
@Juhan280 Juhan280 force-pushed the execute_host_command-closure branch from 5dc5a23 to eb3e1f6 Compare April 17, 2026 19:48
@Juhan280

Copy link
Copy Markdown
Contributor Author

@fdncred let me know if you like direction it's going or I am doing something really stupid.

Comment thread crates/nu-cli/src/repl.rs Outdated
@fdncred

fdncred commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

wow, this is a lot of changes. I worry most about the repl.rs changes. I see a lot of code is just moved around into the run_command function. One thing that seems a little odd to me is storing the index as a string in ExecuteHostCommand and then parsing it back out again later.

@fdncred

fdncred commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

I'd also like to understand the use case and/or some examples of using a closure

@Juhan280

Juhan280 commented Apr 18, 2026

Copy link
Copy Markdown
Contributor Author

wow, this is a lot of changes. I worry most about the repl.rs changes. I see a lot of code is just moved around into the run_command function. One thing that seems a little odd to me is storing the index as a string in ExecuteHostCommand and then parsing it back out again later.

Yeah, most of the change is just moving the stuff inside the Signal::Success arm to a function.

about passing a string, ExecuteHostCommand explicitly wants a string. So, i have no other choice. In an earlier commit, i had the whole commit serialized to a json. But i figured that was really inefficient. So, the index is passed as a string intead. We need something to tell apart which of the closures to run when someone triggers a keybind.

I'd also like to understand the use case and/or some examples of using a closure

I added an example to the pr description. Previously we could only write that inside a string e.g. 'commandline edit $"# cmd: (commandline);"'. But it is very error prone as we don't get any autocomplete or other parsing validation. This also allows us to capture outside variables.

fdncred pushed a commit that referenced this pull request May 13, 2026
…18119)

## Description
Previously, the `pre_prompt` and `env_change` hooks couldn't modify the
commandline. Now they can.

As a consequence, commands executed by keybindings (ExecuteHostCommand)
also reset the commandline. I will probably wait for
#18018 to fix that as that PR
separates the `Signal::HostCommand` from the `Signal::Success` arm, so
we can make the hooks work only if its not an `ExecuteHostCommand`.


## User-facing changes (Release notes)
The `pre_prompt` and `env_change` hooks can now modify the commandline
using the `commandline edit` command.

Example:
```nushell
$env.config.hooks.pre_prompt ++= [{ commandline edit "test" }]
```

## Additional notes
N/A
@Juhan280 Juhan280 marked this pull request as draft May 13, 2026 13:17
@fdncred

fdncred commented May 14, 2026

Copy link
Copy Markdown
Contributor

I'm not a fan of these slotmap changes. It makes it harder to understand what's going on.

@Juhan280

Copy link
Copy Markdown
Contributor Author

I'm not a fan of these slotmap changes. It makes it harder to understand what's going on.

I am still experimenting. This is not final.

@fdncred

fdncred commented May 14, 2026

Copy link
Copy Markdown
Contributor

Good. I'd prefer not to make things more obscure. repl.rs is already complicated enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants