|
78 | 78 |
|
79 | 79 | The result should be similar as in the previous section. |
80 | 80 |
|
81 | | -> **Note** |
82 | | -> that the `use greetings.nu hello` call here first implicitly creates the `greetings` module, |
83 | | -> then takes `hello` from it. You could also write it as `module greetings.nu`, `use greetings hello`. |
84 | | -> Using `module` can be useful if you're not interested in any definitions from the module but want to, |
85 | | -> e.g., re-export the module (`export module greetings.nu`). |
| 81 | +::: tip Note |
| 82 | +Note that the `use greetings.nu hello` call here first implicitly creates the `greetings` module, |
| 83 | +then takes `hello` from it. You could also write it as `module greetings.nu`, `use greetings hello`. |
| 84 | +Using `module` can be useful if you're not interested in any definitions from the module but want to, |
| 85 | +e.g., re-export the module (`export module greetings.nu`). |
| 86 | +::: |
86 | 87 |
|
87 | 88 | ## Modules from Directories |
88 | 89 |
|
|
112 | 113 |
|
113 | 114 | The name of the module follows the same rule as module created from a file: Stem of the directory name, i.e., the directory name, is used as the module name. Again, you could do this as a two-step action using `module` and `use` separately, as explained in the previous section. |
114 | 115 |
|
115 | | -::: tip |
| 116 | +::: tip Note |
116 | 117 | You can define `main` command inside `mod.nu` to create a command named after the module directory. |
117 | 118 | ::: |
118 | 119 |
|
@@ -428,62 +429,68 @@ A common pattern in traditional shells is dumping and auto-sourcing files from a |
428 | 429 | Here we'll create a simple completion module with a submodule dedicated to some Git completions: |
429 | 430 |
|
430 | 431 | 1. Create the completion directory |
| 432 | + |
431 | 433 | `mkdir ($nu.default-config-dir | path join completions)` |
| 434 | + |
432 | 435 | 2. Create an empty `mod.nu` for it |
| 436 | + |
433 | 437 | `touch ($nu.default-config-dir | path join completions mod.nu)` |
| 438 | + |
434 | 439 | 3. Put the following snippet in `git.nu` under the `completions` directory |
435 | 440 |
|
436 | | -```nu |
437 | | -export extern main [ |
438 | | - --version(-v) |
439 | | - -C: string |
440 | | - # ... etc. |
441 | | -] |
442 | | -
|
443 | | -export extern add [ |
444 | | - --verbose(-v) |
445 | | - --dry-run(-n) |
446 | | - # ... etc. |
447 | | -] |
448 | | -
|
449 | | -export extern checkout [ |
450 | | - branch: string@complete-git-branch |
451 | | -] |
452 | | -
|
453 | | -def complete-git-branch [] { |
454 | | - # ... code to list git branches |
455 | | -} |
456 | | -``` |
| 441 | + ```nu |
| 442 | + export extern main [ |
| 443 | + --version(-v) |
| 444 | + -C: string |
| 445 | + # ... etc. |
| 446 | + ] |
| 447 | +
|
| 448 | + export extern add [ |
| 449 | + --verbose(-v) |
| 450 | + --dry-run(-n) |
| 451 | + # ... etc. |
| 452 | + ] |
| 453 | +
|
| 454 | + export extern checkout [ |
| 455 | + branch: string@complete-git-branch |
| 456 | + ] |
| 457 | +
|
| 458 | + def complete-git-branch [] { |
| 459 | + # ... code to list git branches |
| 460 | + } |
| 461 | + ``` |
457 | 462 |
|
458 | 463 | 4. Add `export module git.nu` to `mod.nu` |
459 | 464 | 5. Add the parent of the `completions` directory to your NU_LIB_DIRS inside `env.nu` |
460 | 465 |
|
461 | | -```nu |
462 | | -$env.NU_LIB_DIRS = [ |
463 | | - ... |
464 | | - $nu.default-config-dir |
465 | | -] |
466 | | -``` |
| 466 | + ```nu |
| 467 | + $env.NU_LIB_DIRS = [ |
| 468 | + ... |
| 469 | + $nu.default-config-dir |
| 470 | + ] |
| 471 | + ``` |
| 472 | + |
| 473 | +6. Import the completions to Nushell in your `config.nu`: |
467 | 474 |
|
468 | | -6. import the completions to Nushell in your `config.nu` |
469 | 475 | `use completions *` |
470 | | - Now you've set up a directory where you can put your completion files and you should have some Git completions the next time you start Nushell |
471 | 476 |
|
472 | | -> **Note** |
473 | | -> This will use the file name (in our example `git` from `git.nu`) as the module name. This means some completions might not work if the definition has the base command in its name. |
474 | | -> For example, if you defined our known externals in our `git.nu` as `export extern 'git push' []`, etc. and followed the rest of the steps, you would get subcommands like `git git push`, etc. |
475 | | -> You would need to call `use completions git *` to get the desired subcommands. For this reason, using `main` as outlined in the step above is the preferred way to define subcommands. |
| 477 | +Now you've set up a directory where you can put your completion files, and you should have some Git completions the next time you start Nushell. |
| 478 | + |
| 479 | +::: tip Note |
| 480 | +This will use the file name (in our example `git` from `git.nu`) as the module name. This means some completions might not work if the definition has the base command in its name. |
| 481 | +For example, if you defined our known externals in our `git.nu` as `export extern 'git push' []`, etc. and followed the rest of the steps, you would get subcommands like `git git push`, etc. |
| 482 | +You would need to call `use completions git *` to get the desired subcommands. For this reason, using `main` as outlined in the step above is the preferred way to define subcommands. |
| 483 | +::: |
476 | 484 |
|
477 | 485 | ### Setting environment + aliases (conda style) |
478 | 486 |
|
479 | 487 | `def --env` commands, `export-env` block and aliases can be used to dynamically manipulate "virtual environments" (a concept well known from Python). |
480 | 488 |
|
481 | | -We use it in our official virtualenv integration https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu |
482 | | - |
483 | | -Another example could be our unofficial Conda module: https://github.com/nushell/nu_scripts/blob/f86a060c10f132407694e9ba0f536bfe3ee51efc/modules/virtual_environments/conda.nu |
| 489 | +We use it in our [official virtualenv integration](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu). Another example is our [unofficial Conda module](https://github.com/nushell/nu_scripts/blob/main/modules/virtual_environments/conda.nu). |
484 | 490 |
|
485 | | -> **Warning** |
486 | | -> Work In Progress |
| 491 | +::: warning |
| 492 | +Work in progress |
| 493 | +::: |
487 | 494 |
|
488 | 495 | ## Hiding |
489 | 496 |
|
@@ -522,4 +529,6 @@ It can be one of the following: |
522 | 529 |
|
523 | 530 | - Hides all the module's exports, without the prefix |
524 | 531 |
|
525 | | -> **Note** > `hide` is not a supported keyword at the root of a module (unlike `def` etc.) |
| 532 | +:::tip Note |
| 533 | +`hide` is not a supported keyword at the root of a module (unlike `def` etc.) |
| 534 | +::: |
0 commit comments