Files
Martin Kronberger 32d1af6b6a feat(config)!: move the prompt into $env.config.prompt (#18734)
## Description

The prompt was configured through twelve bare environment variables.
Six of them were seeded from Rust at REPL startup, two more by
`default_env.nu`, and nothing kept them in step with the config.
All of it now lives in `$env.config.prompt`, together with
`render_right_prompt_on_last_line`.

The old variables still take precedence when a session sets them, and
nothing warns yet.
Thus every init script that writes them (starship, oh-my-posh, Dorothy,
Couchbase Shell) keeps working unchanged.
The warning can follow once those have moved.

The default left and right prompts are still Nu closures.
A closure needs a parsed block, so they cannot sit in
`Config::default()`.
`nu-cli` evaluates them into `$env.config.prompt` before any config file
runs, which keeps `view source $env.config.prompt.left` working and lets
a config wrap the default.

The indicators cannot move into the prompt closure, since reedline hands
`render_prompt_indicator` the current `PromptEditMode`, which the
closure cannot see.

`input --reedline` reads the same config now and drops its own hardcoded
indicators.

## User-facing changes (Release notes)

### The prompt is configured through `$env.config.prompt`

| key | default | replaces |
| --- | --- | --- |
| `left` | closure showing the directory | `$env.PROMPT_COMMAND` |
| `right` | closure showing time and exit code |
`$env.PROMPT_COMMAND_RIGHT` |
| `indicator` | `"> "` | `$env.PROMPT_INDICATOR` |
| `vi_insert` | `"> "` | `$env.PROMPT_INDICATOR_VI_INSERT` |
| `vi_normal` | `": "` | `$env.PROMPT_INDICATOR_VI_NORMAL` |
| `vi_visual` | `"+ "` | new |
| `multiline` | `"::: "` | `$env.PROMPT_MULTILINE_INDICATOR` |
| `render_right_on_last_line` | `false` |
`$env.config.render_right_prompt_on_last_line` |
| `transient.left` | `null` | `$env.TRANSIENT_PROMPT_COMMAND` |
| `transient.right` | `""` | `$env.TRANSIENT_PROMPT_COMMAND_RIGHT` |
| `transient.indicator` | `null` | `$env.TRANSIENT_PROMPT_INDICATOR` |
| `transient.vi_insert` | `null` |
`$env.TRANSIENT_PROMPT_INDICATOR_VI_INSERT` |
| `transient.vi_normal` | `null` |
`$env.TRANSIENT_PROMPT_INDICATOR_VI_NORMAL` |
| `transient.vi_visual` | `null` | new |
| `transient.multiline` | `""` |
`$env.TRANSIENT_PROMPT_MULTILINE_INDICATOR` |

A transient key set to `null` keeps whatever the live prompt showed.

The twelve environment variables are deprecated.
They still win over the config when you set them, so a config or init
script that assigns them keeps working.

### Breaking changes

- **The variables are no longer seeded.**
`$env.PROMPT_COMMAND`, `$env.PROMPT_INDICATOR` and the rest now exist
only if you set them yourself.
Code that reads one to find the current prompt, like `let old =
$env.PROMPT_COMMAND`, has to read `$env.config.prompt.left` instead.
`hide-env TRANSIENT_PROMPT_COMMAND_RIGHT`, the old way to keep the right
prompt in the transient prompt, now errors as well.
  Set `$env.config.prompt.transient.right = null` instead.
- **`$env.config.render_right_prompt_on_last_line` can no longer be
read.**
  Assigning the old name still sets `prompt.render_right_on_last_line`.
- **The vi indicators follow reedline's defaults.**
Insert mode shows `"> "` (was `": "`), normal mode `": "` (was `"> "`).
- **Vi visual mode has its own indicator**, `"+ "`.
  It no longer follows `$env.PROMPT_INDICATOR_VI_NORMAL`.
  Helix select mode uses it too.
- `input --reedline` follows `$env.config.prompt`, so its vi normal
indicator changed from `"〉"` to `": "` and its vi insert indicator from
`": "` to `"> "`.

`commandline set-prompt` gained `--vi-visual`.

`config default` reports the default prompt closures, so
`$env.config.prompt = (config default).prompt` restores the default
prompt.

Fixed the reverse-search indicator running into the matched entry:
`(reverse-search: ls)ls -la` is now `(reverse-search: ls) ls -la`.

## Additional notes

Checked against the nushell integration of every project in the README's
"Officially Supported By" list.
Only virtualenv reads one of the variables: `activate.nu` wraps
`$env.PROMPT_COMMAND`, and with nothing seeded the venv prompt loses the
default prompt behind its prefix.
uv ships a copy of the same script.
Both need a fallback to `$env.config.prompt.left`, and venvs created
before that fix keep the old script.
The starship and oh-my-posh init scripts were run against this branch
and against `main` on a pty.
They render the same in emacs mode, in vi mode the only difference is
the new default indicator.

OSC 133/633 emission is untouched, the sequences of a session on this
branch and on `main` are identical.
2026-11-14 14:17:59 +01:00

147 lines
5.6 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Integration tests for OSC 133/633 shell integration semantic prompt markers
//
// These tests verify that prompt_update::update_prompt correctly renders prompts
// with ANSI escape sequences and semantic markers in the proper left-to-right order.
use nu_cli::{NushellPrompt, update_prompt};
use nu_test_support::prelude::*;
use reedline::{Prompt, PromptEditMode};
// ────────────────────────────────────────────────────────────────────────────────
// PROMPT RENDERING TESTS WITH ACTUAL update_prompt CALLS
// ────────────────────────────────────────────────────────────────────────────────
// These tests call the real prompt_update::update_prompt function and verify
// that prompts are rendered with proper content.
/// Test that update_prompt with OSC 133 renders prompts correctly
#[test]
fn test_update_prompt_with_osc133() {
let tester = test()
.env("PROMPT_COMMAND", "❯ ")
.env("PROMPT_INDICATOR", "> ")
.env("PROMPT_COMMAND_RIGHT", "~/code")
.env("PROMPT_MULTILINE_INDICATOR", "... ")
.env("PWD", "/test");
let config = tester.engine_state.get_config().clone();
// Call the actual update_prompt function
update_prompt(&config, &tester.engine_state, &tester.stack);
let prompt = NushellPrompt::shared(tester.engine_state.prompt_state.clone());
// Verify prompts were updated correctly
let left = prompt.render_prompt_left();
let indicator = prompt.render_prompt_indicator(PromptEditMode::Default);
let right = prompt.render_prompt_right();
let multiline = prompt.render_prompt_multiline_indicator();
assert_eq!(left.as_ref(), "❯ ");
assert_eq!(indicator.as_ref(), "> ");
assert_eq!(right.as_ref(), "~/code");
assert_eq!(multiline.as_ref(), "... ");
}
/// Test that update_prompt with OSC 633 renders prompts correctly
#[test]
fn test_update_prompt_with_osc633() {
let tester = test().env("PROMPT_COMMAND", "❯ ").env("PWD", "/test");
let config = tester.engine_state.get_config().clone();
// Call the actual update_prompt function
update_prompt(&config, &tester.engine_state, &tester.stack);
let prompt = NushellPrompt::shared(tester.engine_state.prompt_state.clone());
let left = prompt.render_prompt_left();
assert_eq!(left.as_ref(), "❯ ");
}
/// Test that update_prompt correctly handles both left and right prompts
#[test]
fn test_update_prompt_left_and_right() {
let tester = test()
.env("PROMPT_COMMAND", "❯ ")
.env("PROMPT_COMMAND_RIGHT", "~/code")
.env("PWD", "/test");
let config = tester.engine_state.get_config().clone();
update_prompt(&config, &tester.engine_state, &tester.stack);
let prompt = NushellPrompt::shared(tester.engine_state.prompt_state.clone());
let left = prompt.render_prompt_left();
let right = prompt.render_prompt_right();
assert_eq!(left.as_ref(), "❯ ");
assert_eq!(right.as_ref(), "~/code");
}
/// Test that update_prompt correctly sets multiline indicator
#[test]
fn test_update_prompt_multiline() {
let tester = test()
.env("PROMPT_MULTILINE_INDICATOR", "... ")
.env("PWD", "/test");
let config = tester.engine_state.get_config().clone();
update_prompt(&config, &tester.engine_state, &tester.stack);
let prompt = NushellPrompt::shared(tester.engine_state.prompt_state.clone());
let multiline = prompt.render_prompt_multiline_indicator();
assert_eq!(multiline.as_ref(), "... ");
}
/// Test that update_prompt respects empty/missing prompt variables
#[test]
fn test_update_prompt_with_missing_vars() {
let tester = test().env("PWD", "/test");
let config = tester.engine_state.get_config().clone();
// Call update_prompt without setting PROMPT_COMMAND env vars
update_prompt(&config, &tester.engine_state, &tester.stack);
// Should still work, just with default/empty prompts
let prompt = NushellPrompt::shared(tester.engine_state.prompt_state.clone());
let left = prompt.render_prompt_left();
// Default behavior when PROMPT_COMMAND is not set - will have some content
assert!(!left.as_ref().is_empty());
}
// ────────────────────────────────────────────────────────────────────────────────
// CONFIGURATION TESTS
// ────────────────────────────────────────────────────────────────────────────────
/// Test that osc133 can be enabled/disabled via config
#[test]
fn test_osc133_config_toggle() -> Result {
test()
.run("$env.config.shell_integration.osc133")
.expect_value_eq(true)?;
test()
.run(
"
$env.config.shell_integration.osc133 = false
$env.config.shell_integration.osc133
",
)
.expect_value_eq(false)
}
/// Test that osc633 can be enabled/disabled via config
#[test]
fn test_osc633_config_toggle() -> Result {
test()
.run("$env.config.shell_integration.osc633")
.expect_value_eq(true)?;
test()
.run(
"
$env.config.shell_integration.osc633 = false
$env.config.shell_integration.osc633
",
)
.expect_value_eq(false)
}