Skip to content

standard library: implement help commands with $nu#8505

Merged
kubouch merged 46 commits intonushell:mainfrom
amtoine:feature/implement-help-commands-in-standard-library
Apr 6, 2023
Merged

standard library: implement help commands with $nu#8505
kubouch merged 46 commits intonushell:mainfrom
amtoine:feature/implement-help-commands-in-standard-library

Conversation

@amtoine
Copy link
Copy Markdown
Member

@amtoine amtoine commented Mar 17, 2023

Related to one task in #8311.

Description

this PR implements the built-in help commands in the standard library using the $nu variable
as the only source to access information about the commands, aliases and modules.
this can be thought as a proof of concept and a replacement proposition in the long-run, as the
help commands are not performance-critical and might not require taking up compilation time
and space.

Note
this PR has in mind the goal of cloning the builtin help commands.
this is what i've tried as much as possible with the information available in $nu 😌

However, some things appear impossible right now, see the Warning below.

progress

details

the error system

the *-not-found-error are here as convenience wrappers to throw all required errors, i.e. when
a user parameter does not correspond to any commands, alias or module.

the actual help commands

the implemented help commands follow the same structure

  • use print-help-header to print the section names in the help messages
  • define a show-* command that takes the command, alias or module information in the form of a
    record and prints as much as possible in the same format as the built-ins
  • define a help * each having the same signature and behaviour
    • get the whole collections of commands, aliases or modules, sorted by name
    • if --find is used, matches the name against the pattern
      • if the result is alone, expand it
      • if not a single result, show the raw table
    • if --find is not used but a name is still provided, show it or throw a *-not-found-error
    • otherwise show all the commands, aliases or modules

User-Facing Changes

users can now access the help commands via use std.nu and not rely on the builtins anymore.

Tests + Formatting

i do not really know what to write here...
should i take a few know commands and compare the outputs of the help on them?

After Submitting

i'd say probably part of the standard library announcement 👍

amtoine added 6 commits March 17, 2023 18:22
By header i mean the green text that appears as a section name.
e.g.
the "search terms" in "Search terms: load, read, load_file, read_file"
when running
```
help open | lines | get 2
```
- `module-not-found-error` is the error message wrapper
- `show-module` is the function that shows a single module doc
- `help modules` is designed as a clone of the built-in `help modules`
@fdncred fdncred added the A:std-library Defining and improving the standard library written in Nu label Mar 18, 2023
@amtoine amtoine marked this pull request as ready for review March 18, 2023 17:01
@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 18, 2023

there it is finally 🎉

i'd love some feedback on that 😋

my main concerns

  • the two one 🔴 points above
  • the 🟠 point above
  • the flags of externals that are not in $nu
  • the help command => i have a draft for this, but it does not work that well for now: should i keep working on this?

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 29, 2023

help commands is returning too many columns compared to the built-in

oh yeah good catch 👍

fixed in 6f7c7a1 👍

Warning
$nu.scope.commands.command_type does not exist... so this is the best i can do with $nu only

>_ use crates/nu-utils/standard_library/std.nu
>_ std help commands | columns
╭───┬──────────────╮
│ 0 │ name         │
│ 1 │ category     │
│ 2 │ usage        │
│ 3 │ signatures   │
│ 4 │ search_terms │
╰───┴──────────────╯
>_ help commands | columns
╭───┬──────────────╮
│ 0 │ name         │
│ 1 │ category     │
│ 2 │ command_type │
│ 3 │ usage        │
│ 4 │ signatures   │
│ 5 │ search_terms │
╰───┴──────────────╯

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 29, 2023

@fdncred i think i've answered to everything 😌

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Mar 29, 2023

Related to help -f arg and coloring. We could do something like this

$nu.scope.commands | find arg | reject module_name examples is_builtin is_sub is_plugin is_custom is_keyword is_extern creates_scope extra_usage

but i think the signatures would need to be updated. Something weird is going on with signatures here recently.

This
image

versus this
image

Note the signature in the later are like <nothing> | bytes build --> <binary>

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 30, 2023

Related to help -f arg and coloring. We could do something like this

$nu.scope.commands | find arg | reject module_name examples is_builtin is_sub is_plugin is_custom is_keyword is_extern creates_scope extra_usage

with this, we gain the highlight but, as i said, we not only search the name, usage and search terms 🤔
do we want

  • hightlighting with find
  • narrower search to avoid hitting garbase matches
    ?

but i think the signatures would need to be updated. Something weird is going on with signatures here recently.

Note the signature in the later are like <nothing> | bytes build --> <binary>

oh yeah you're right 🤔
that's $nu that needs an update for this, right?

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Mar 30, 2023

help -f

I definitely want highlighting and maybe a flag to turn it off. If we don't do it with find we'll need to do it manually by finding the index-of and str replace. It will probably be slow, but I'd prefer to have identical output of what we have now.

that's $nu that needs an update for this, right?

I'm not sure that's stored anywhere right now just like this. You'd have to get signatures then get syntax_shape | where parameter_type == input and something similar for output in order to construct something like exists now.

If we're trying to exactly match help commands we'll also have to add command_type.

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 30, 2023

I definitely want highlighting and maybe a flag to turn it off. If we don't do it with find we'll need to do it manually by finding the index-of and str replace. It will probably be slow, but I'd prefer to have identical output of what we have now.

okey, i add that back 👍

I'm not sure that's stored anywhere right now just like this. You'd have to get signatures then get syntax_shape | where parameter_type == input and something similar for output in order to construct something like exists now.

mm we do not have this kind of output with the new help commands.
$nu.scope.commands has structured signatures, so the output is never like these lines!

If we're trying to exactly match help commands we'll also have to add command_type.

outside of this PR, right?

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 30, 2023

@fdncred
highlighting added in 3305e8f with find $find 👍

Warning
now, any of the help command searches for the find pattern in ALL the columns of the selected $nu.scope.

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Mar 30, 2023

I think we need to document the differences between the stdlib help and rust help. I'm pushing to have them be identical in functionality, but others may disagree. In my mind, if they're not identical, then we have a regression in functionality.

Here's what I know is different so far.

  1. Speed - We can land with having slight speed differences because we can always optimize that later.
  2. help -f highlights too many columns in script form.
  3. help commands has different columns (no command_type) column and the signatures column is different.

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 31, 2023

@fdncred
there are also the points above that are not yet addressed

💡 LEGEND
🔵 an idea
🟡 -> 🟠 -> 🔴 from low to high issue

  1. 🔵 i currently print the exported environment blocks, see help help modules
  2. 🔵 i thought of adding a --raw option to allow custom data processing by not expanding the help page found, but instead return the raw data
  3. 🟡 with the builtin, help externs open returns the help open even though open is not an external => the new std help externs open does not
  4. 🔴 in the built-in help externs, we have all the registered flags that show up => as they are not available in $nu, i do not know how to do that

@fdncred fdncred marked this pull request as draft March 31, 2023 17:02
@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Mar 31, 2023

ok, let's make this draft to show it's still a work in progress.

@amtoine
Copy link
Copy Markdown
Member Author

amtoine commented Mar 31, 2023

ok, let's make this draft to show it's still a work in progress.

👍

@amtoine amtoine marked this pull request as ready for review April 5, 2023 20:47
let commands_string = (
$module.commands
| each {|command|
$"($command) (char lparen)(ansi cyan_bold)($module.name) ($command)(ansi reset)(char rparen)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not correct. I think it's better to drop the cyan version for now because I don't think there is a way to query it correctly from inside Nushell.

The bold cyan shows the actual name of the exported command inside the current scope which depends on how you import it. use std will have std prefix while use std * won't. Later, we might support renaming of modules which would lead to a completely different prefix. We'd probably need to expose the ID of commands inside $nu in order to be able to link the module's command with an existing symbol in the current scope.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be good in 3c54ca6

print $"This module (ansi cyan)does not export(ansi reset) environment."
} else {
print $"This module (ansi cyan)exports(ansi reset) environment."
view source $module.env_block | nu-highlight
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one currently shows as a huge red blob. Prepending export-env before the block should fix it. Although I'm thinking if the export-env block is too big, it might be quite annoying (see https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu, for example), we can just leave it out as well. You can use view source module-name to view the source code if you want.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm you are rigth

use crates/nu-utils/standard_library/std.nu 'help modules'
help modules std

gives a red blob 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i removed it for now in 1e14b46

@kubouch kubouch closed this Apr 6, 2023
@kubouch kubouch reopened this Apr 6, 2023
@kubouch
Copy link
Copy Markdown
Contributor

kubouch commented Apr 6, 2023

Oops, sorry, I misclicked.

Looks good to me. It might need some touches but we can improve it as we go. For example, I personally wouldn't show the export-env block but that's a detail. Otherwise it looks really impressive! The help aliases are a bit different, but it isn't broken when querying a specific alias, that's good.

@kubouch kubouch merged commit d06ebb1 into nushell:main Apr 6, 2023
@amtoine amtoine deleted the feature/implement-help-commands-in-standard-library branch April 7, 2023 15:50
amtoine added a commit to amtoine/nushell that referenced this pull request Apr 7, 2023
This adds the `help` commands from nushell#8505 and the `assert skip`
from nushell#8748.
amtoine added a commit to amtoine/nushell that referenced this pull request Apr 7, 2023
the `help` command needs to be there... 👀... thanks to nushell#8505
sholderbach pushed a commit that referenced this pull request Apr 7, 2023
Related to #8505.

# Description
as #8505 has been landed, this PR fixes the prelude TODO and uses the
`help` commands from the standard library in the prelude.

# User-Facing Changes
can now access `std help ...` to use the `help` implementations from the
standard library

# Tests + Formatting
```
$nothing
```

# After Submitting
```
$nothing
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:std-library Defining and improving the standard library written in Nu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants