Make cursor_shape optional (#10289)

# Description
There are several cursor shape related issues #7151 #9243 #7271 #8452
#10169, you can't disable the cursor shape feature even if you comment
out the entire `cursor_shape` block in the config.nu, and even worse,
when nushell exits with an error, the cursor shape can't be restored,
that is annoying.

This PR provides an opportunity to disable setting the cursor shape.

# User-Facing Changes
If you use the default config.nu, nothing changes, but if you comment
out `cursor_shape` block or set them to `inherit`, related cursor shape
will not be set.

# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `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.
-->
This commit is contained in:
nibon7 2023-09-10 02:42:36 +08:00 committed by GitHub
parent 8501024546
commit 7907dda8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 43 deletions

View File

@ -235,13 +235,15 @@ pub fn evaluate_repl(
// Find the configured cursor shapes for each mode // Find the configured cursor shapes for each mode
let cursor_config = CursorConfig { let cursor_config = CursorConfig {
vi_insert: Some(map_nucursorshape_to_cursorshape( vi_insert: config
config.cursor_shape_vi_insert, .cursor_shape_vi_insert
)), .map(map_nucursorshape_to_cursorshape),
vi_normal: Some(map_nucursorshape_to_cursorshape( vi_normal: config
config.cursor_shape_vi_normal, .cursor_shape_vi_normal
)), .map(map_nucursorshape_to_cursorshape),
emacs: Some(map_nucursorshape_to_cursorshape(config.cursor_shape_emacs)), emacs: config
.cursor_shape_emacs
.map(map_nucursorshape_to_cursorshape),
}; };
perf( perf(
"get config/cursor config", "get config/cursor config",

View File

@ -108,9 +108,9 @@ pub struct Config {
pub show_clickable_links_in_ls: bool, pub show_clickable_links_in_ls: bool,
pub render_right_prompt_on_last_line: bool, pub render_right_prompt_on_last_line: bool,
pub explore: HashMap<String, Value>, pub explore: HashMap<String, Value>,
pub cursor_shape_vi_insert: NuCursorShape, pub cursor_shape_vi_insert: Option<NuCursorShape>,
pub cursor_shape_vi_normal: NuCursorShape, pub cursor_shape_vi_normal: Option<NuCursorShape>,
pub cursor_shape_emacs: NuCursorShape, pub cursor_shape_emacs: Option<NuCursorShape>,
pub datetime_normal_format: Option<String>, pub datetime_normal_format: Option<String>,
pub datetime_table_format: Option<String>, pub datetime_table_format: Option<String>,
pub error_style: String, pub error_style: String,
@ -156,9 +156,9 @@ impl Default for Config {
filesize_metric: false, filesize_metric: false,
filesize_format: "auto".into(), filesize_format: "auto".into(),
cursor_shape_emacs: NuCursorShape::Line, cursor_shape_emacs: None,
cursor_shape_vi_insert: NuCursorShape::Block, cursor_shape_vi_insert: None,
cursor_shape_vi_normal: NuCursorShape::UnderScore, cursor_shape_vi_normal: None,
color_config: HashMap::new(), color_config: HashMap::new(),
use_grid_icons: true, use_grid_icons: true,
@ -683,12 +683,13 @@ impl Value {
($name:expr, $span:expr) => { ($name:expr, $span:expr) => {
Value::string( Value::string(
match $name { match $name {
NuCursorShape::Line => "line", Some(NuCursorShape::Line) => "line",
NuCursorShape::Block => "block", Some(NuCursorShape::Block) => "block",
NuCursorShape::UnderScore => "underscore", Some(NuCursorShape::UnderScore) => "underscore",
NuCursorShape::BlinkLine => "blink_line", Some(NuCursorShape::BlinkLine) => "blink_line",
NuCursorShape::BlinkBlock => "blink_block", Some(NuCursorShape::BlinkBlock) => "blink_block",
NuCursorShape::BlinkUnderScore => "blink_underscore", Some(NuCursorShape::BlinkUnderScore) => "blink_underscore",
None => "inherit",
}, },
$span, $span,
) )
@ -706,31 +707,34 @@ impl Value {
match val_str.as_ref() { match val_str.as_ref() {
"line" => { "line" => {
config.cursor_shape_vi_insert = config.cursor_shape_vi_insert =
NuCursorShape::Line; Some(NuCursorShape::Line);
} }
"block" => { "block" => {
config.cursor_shape_vi_insert = config.cursor_shape_vi_insert =
NuCursorShape::Block; Some(NuCursorShape::Block);
} }
"underscore" => { "underscore" => {
config.cursor_shape_vi_insert = config.cursor_shape_vi_insert =
NuCursorShape::UnderScore; Some(NuCursorShape::UnderScore);
} }
"blink_line" => { "blink_line" => {
config.cursor_shape_vi_insert = config.cursor_shape_vi_insert =
NuCursorShape::BlinkLine; Some(NuCursorShape::BlinkLine);
} }
"blink_block" => { "blink_block" => {
config.cursor_shape_vi_insert = config.cursor_shape_vi_insert =
NuCursorShape::BlinkBlock; Some(NuCursorShape::BlinkBlock);
} }
"blink_underscore" => { "blink_underscore" => {
config.cursor_shape_vi_insert = config.cursor_shape_vi_insert =
NuCursorShape::BlinkUnderScore; Some(NuCursorShape::BlinkUnderScore);
}
"inherit" => {
config.cursor_shape_vi_insert = None;
} }
_ => { _ => {
invalid!(Some(span), invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', or 'blink_underscore'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape!(
@ -754,31 +758,34 @@ impl Value {
match val_str.as_ref() { match val_str.as_ref() {
"line" => { "line" => {
config.cursor_shape_vi_normal = config.cursor_shape_vi_normal =
NuCursorShape::Line; Some(NuCursorShape::Line);
} }
"block" => { "block" => {
config.cursor_shape_vi_normal = config.cursor_shape_vi_normal =
NuCursorShape::Block; Some(NuCursorShape::Block);
} }
"underscore" => { "underscore" => {
config.cursor_shape_vi_normal = config.cursor_shape_vi_normal =
NuCursorShape::UnderScore; Some(NuCursorShape::UnderScore);
} }
"blink_line" => { "blink_line" => {
config.cursor_shape_vi_normal = config.cursor_shape_vi_normal =
NuCursorShape::BlinkLine; Some(NuCursorShape::BlinkLine);
} }
"blink_block" => { "blink_block" => {
config.cursor_shape_vi_normal = config.cursor_shape_vi_normal =
NuCursorShape::BlinkBlock; Some(NuCursorShape::BlinkBlock);
} }
"blink_underscore" => { "blink_underscore" => {
config.cursor_shape_vi_normal = config.cursor_shape_vi_normal =
NuCursorShape::BlinkUnderScore; Some(NuCursorShape::BlinkUnderScore);
}
"inherit" => {
config.cursor_shape_vi_normal = None;
} }
_ => { _ => {
invalid!(Some(span), invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', or 'blink_underscore'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape!(
@ -801,31 +808,35 @@ impl Value {
let val_str = v.to_lowercase(); let val_str = v.to_lowercase();
match val_str.as_ref() { match val_str.as_ref() {
"line" => { "line" => {
config.cursor_shape_emacs = NuCursorShape::Line; config.cursor_shape_emacs =
Some(NuCursorShape::Line);
} }
"block" => { "block" => {
config.cursor_shape_emacs = config.cursor_shape_emacs =
NuCursorShape::Block; Some(NuCursorShape::Block);
} }
"underscore" => { "underscore" => {
config.cursor_shape_emacs = config.cursor_shape_emacs =
NuCursorShape::UnderScore; Some(NuCursorShape::UnderScore);
} }
"blink_line" => { "blink_line" => {
config.cursor_shape_emacs = config.cursor_shape_emacs =
NuCursorShape::BlinkLine; Some(NuCursorShape::BlinkLine);
} }
"blink_block" => { "blink_block" => {
config.cursor_shape_emacs = config.cursor_shape_emacs =
NuCursorShape::BlinkBlock; Some(NuCursorShape::BlinkBlock);
} }
"blink_underscore" => { "blink_underscore" => {
config.cursor_shape_emacs = config.cursor_shape_emacs =
NuCursorShape::BlinkUnderScore; Some(NuCursorShape::BlinkUnderScore);
}
"inherit" => {
config.cursor_shape_emacs = None;
} }
_ => { _ => {
invalid!(Some(span), invalid!(Some(span),
"unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', or 'blink_underscore'" "unrecognized $env.config.{key}.{key2} '{val_str}'; expected either 'line', 'block', 'underscore', 'blink_line', 'blink_block', 'blink_underscore' or 'inherit'"
); );
// Reconstruct // Reconstruct
vals[index] = reconstruct_cursor_shape!( vals[index] = reconstruct_cursor_shape!(

View File

@ -224,9 +224,9 @@ $env.config = {
} }
cursor_shape: { cursor_shape: {
emacs: line # block, underscore, line, blink_block, blink_underscore, blink_line (line is the default) emacs: line # block, underscore, line, blink_block, blink_underscore, blink_line, inherit to skip setting cursor shape (line is the default)
vi_insert: block # block, underscore, line , blink_block, blink_underscore, blink_line (block is the default) vi_insert: block # block, underscore, line, blink_block, blink_underscore, blink_line, inherit to skip setting cursor shape (block is the default)
vi_normal: underscore # block, underscore, line, blink_block, blink_underscore, blink_line (underscore is the default) vi_normal: underscore # block, underscore, line, blink_block, blink_underscore, blink_line, inherit to skip setting cursor shape (underscore is the default)
} }
color_config: $dark_theme # if you want a more interesting theme, you can replace the empty record with `$dark_theme`, `$light_theme` or another custom record color_config: $dark_theme # if you want a more interesting theme, you can replace the empty record with `$dark_theme`, `$light_theme` or another custom record