Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make $env.config access produce a record describing the full state of EngineState.config #7059

Closed
webbedspace opened this issue Nov 8, 2022 · 1 comment · Fixed by #7309
Labels
enhancement New feature or request
Milestone

Comments

@webbedspace
Copy link
Contributor

webbedspace commented Nov 8, 2022

Related problem

Breakoff of discussion here. $env.config access is currently a brittle way of querying the system configuration state (e.g. to check the state of always_trash for UX purposes), because you can put invalid values on $env.config like $env.config.rm_always_trash = 'foobarbaz' and have it actually stick on the record (even though doing so does not update EngineState for obvious reasons), and moreover you can write things like let-env config = "foobarbaz" that just blow the whole record away outright.

Describe the solution you'd like

My idea is that each $env.config access (that is to say, anything that reads from that value) causes Nushell to instead create and return a new record which always and only has values corresponding to those on the EngineState.config struct.

The struct looks like this:

pub struct Config {
    pub external_completer: Option<usize>,
    pub filesize_metric: bool,
    pub table_mode: String,
    pub use_ls_colors: bool,
    pub color_config: HashMap<String, Value>,
    pub use_grid_icons: bool,
    pub footer_mode: FooterMode,
    pub float_precision: i64,
    pub max_external_completion_results: i64,
    pub filesize_format: String,
    pub use_ansi_coloring: bool,
    pub quick_completions: bool,
    pub partial_completions: bool,
    pub completion_algorithm: String,
    pub edit_mode: String,
    pub max_history_size: i64,
    pub sync_history_on_enter: bool,
    pub history_file_format: HistoryFileFormat,
    pub log_level: String,
    pub keybindings: Vec<ParsedKeybinding>,
    pub menus: Vec<ParsedMenu>,
    pub hooks: Hooks,
    pub rm_always_trash: bool,
    pub shell_integration: bool,
    pub buffer_editor: String,
    pub table_index_mode: TableIndexMode,
    pub cd_with_abbreviations: bool,
    pub case_sensitive_completions: bool,
    pub enable_external_completion: bool,
    pub trim_strategy: TrimStrategy,
    pub show_banner: bool,
    pub show_clickable_links_in_ls: bool,
    pub render_right_prompt_on_last_line: bool,
}

$env.config access would thus produce a record holding external_completer, filesize_metric, table_mode, use_ls_colors, color_config, use_grid_icons, footer_mode, float_precision, max_external_completion_results, filesize_format, use_ansi_coloring, quick_completions, partial_completions, completion_algorithm, edit_mode, max_history_size, sync_history_on_enter, history_file_format, log_level, keybindings, menus, hooks, rm_always_trash, shell_integration, buffer_editor, table_index_mode, cd_with_abbreviations, case_sensitive_completions, enable_external_completion, trim_strategy, show_banner, show_clickable_links_in_ls, render_right_prompt_on_last_line keys.

Now, HistoryFileFormat, FooterMode, TrimStrategy and TableIndexMode can be translated back to Nuon values relatively easily. ParsedKeybinding and ParsedMenu might be a bit trickier (haven't examined them just yet).

Describe alternatives you've considered

No response

Additional context and details

No response

@webbedspace webbedspace added the enhancement New feature or request label Nov 8, 2022
@webbedspace webbedspace changed the title Make $env.config access produce a record containing the full state of EngineState.config Make $env.config access produce a record describing the full state of EngineState.config Nov 8, 2022
@fdncred
Copy link
Collaborator

fdncred commented Nov 8, 2022

Agreed that this is a good conversation to have.

It may also be good to have an official blessed command like config set key value/config get key with better error handling and config value enforcement.

We used to have a config command in version 44, pre engine-q. Here's a screenshot.
image

kubouch pushed a commit that referenced this issue Dec 10, 2022
# Description
Closes #7059. Rather than generate a new Record each time $env.config is
accessed (as described in that issue), instead `$env.config = ` now A)
parses the input record, then B) un-parses it into a clean Record with
only the valid values, and stores that as an env-var. The reasoning for
this is that I believe `config_to_nu_record()` (the method that performs
step B) will be useful in later PRs. (See below)

As a result, this also "fixes" the following "bug":
```
〉$env.config = 'butts'
$env.config is not a record
〉$env.config
butts
```
~~Instead, `$env.config = 'butts'` now turns `$env.config` into the
default (not the default config.nu, but `Config::default()`, which
notably has empty keybindings, color_config, menus and hooks vecs).~~

This doesn't attempt to fix #7110. cc @Kangaxx-0

# Example of new behaviour

OLD:
```
〉$env.config = ($env.config | merge { foo: 1 })
$env.config.foo is an unknown config setting
〉$env.config.foo
1
```
NEW:
```
〉$env.config = ($env.config | merge { foo: 1 })
Error:
  × Config record contains invalid values or unknown settings

Error:
  × Error while applying config changes
   ╭─[entry #1:1:1]
 1 │ $env.config = ($env.config | merge { foo: 1 })
   ·                                           ┬
   ·                                           ╰── $env.config.foo is an unknown config setting
   ╰────
  help: This value has been removed from your $env.config record.

〉$env.config.foo
Error: nu:🐚:column_not_found (link)

  × Cannot find column
   ╭─[entry #1:1:1]
 1 │ $env.config = ($env.config | merge { foo: 1 })
   ·                              ──┬──
   ·                                ╰── value originates here
   ╰────
   ╭─[entry #2:1:1]
 1 │ $env.config.foo
   ·             ─┬─
   ·              ╰── cannot find column 'foo'
   ╰────
```
# Example of new errors

OLD:
```
$env.config.cd.baz is an unknown config setting
$env.config.foo is an unknown config setting
$env.config.bar is an unknown config setting
$env.config.table.qux is an unknown config setting
$env.config.history.qux is an unknown config setting
```
NEW:
```
Error: 
  × Config record contains invalid values or unknown settings

Error:
  × Error while applying config changes
     ╭─[C:\Users\Leon\AppData\Roaming\nushell\config.nu:267:1]
 267 │     abbreviations: true # allows `cd s/o/f` to expand to `cd some/other/folder`
 268 │     baz: 3,
     ·          ┬
     ·          ╰── $env.config.cd.baz is an unknown config setting
 269 │   }
     ╰────
  help: This value has been removed from your $env.config record.

Error:
  × Error while applying config changes
     ╭─[C:\Users\Leon\AppData\Roaming\nushell\config.nu:269:1]
 269 │   }
 270 │   foo: 1,
     ·        ┬
     ·        ╰── $env.config.foo is an unknown config setting
 271 │   bar: 2,
     ╰────
  help: This value has been removed from your $env.config record.

Error:
  × Error while applying config changes
     ╭─[C:\Users\Leon\AppData\Roaming\nushell\config.nu:270:1]
 270 │   foo: 1,
 271 │   bar: 2,
     ·        ┬
     ·        ╰── $env.config.bar is an unknown config setting
     ╰────
  help: This value has been removed from your $env.config record.

Error:
  × Error while applying config changes
     ╭─[C:\Users\Leon\AppData\Roaming\nushell\config.nu:279:1]
 279 │     }
 280 │     qux: 4,
     ·          ┬
     ·          ╰── $env.config.table.qux is an unknown config setting
 281 │   }
     ╰────
  help: This value has been removed from your $env.config record.

Error:
  × Error while applying config changes
     ╭─[C:\Users\Leon\AppData\Roaming\nushell\config.nu:285:1]
 285 │     file_format: "plaintext" # "sqlite" or "plaintext"
 286 │  qux: 2
     ·       ┬
     ·       ╰── $env.config.history.qux is an unknown config setting
 287 │   }
     ╰────
  help: This value has been removed from your $env.config record.


```

# User-Facing Changes

See above.

# 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` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# 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.
@hustcer hustcer added this to the v0.73 milestone Dec 10, 2022
Hofer-Julian pushed a commit to Hofer-Julian/nushell that referenced this issue Jan 27, 2023
* Start 0.72 blog post

* document breaking changes

* mention `seq date` and `build-string`

* Document wrap update (nushell#673)

* document `url parse` command (nushell#677)

* Add note about --ignore-case. (nushell#678)

* Command refinement theme

* Mention dataframe change

* Add overlay use changes

* Update 2022-11-29-nushell-0.72.md

* Add config.nu structure changes (nushell#675)

* Add config.nu structure changes.

Note that the advice for "output your existing options" is liable to change based on whether Nushell issue nushell#7059 is implemented or not. That section is preliminary.

* Minor amendment

* mention rust bump

* Mention new errors on `first`/`last` negative idx

* Mention xor and planned operator deprecation

* Update 2022-11-29-nushell-0.72.md

* Add note about virtualenv

* Update virtualenv description

* Update 2022-11-29-nushell-0.72.md

add a bunch of commands and features

* Update 2022-11-29-nushell-0.72.md

* add full changelog

* Fix typos etc.

* Update 2022-11-29-nushell-0.72.md

Co-authored-by: Nano <[email protected]>
Co-authored-by: raccmonteiro <[email protected]>
Co-authored-by: Leon <[email protected]>
Co-authored-by: Jakub Žádník <[email protected]>
Co-authored-by: JT <[email protected]>
Co-authored-by: Stefan Holderbach <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants