Compare commits

...

75 Commits

Author SHA1 Message Date
c74cff213e remove std clip (#11131)
follow up to
- https://github.com/nushell/nushell/pull/11097

related to
- https://github.com/nushell/nu_scripts/pull/674

> **Important**
> wait for in between Nushell 0.88.0 and 0.89.0

# Description
this PR removes the `std clip` command after it's been deprecated in
https://github.com/nushell/nushell/pull/11097 😋

# User-Facing Changes
`std clip` will no longer be available.

# Tests + Formatting

# After Submitting
2024-01-08 21:46:10 +02:00
2ea5819b02 Bump crate-ci/typos from 1.16.26 to 1.17.0
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.16.26 to 1.17.0.
- [Release notes](https://github.com/crate-ci/typos/releases)
- [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crate-ci/typos/compare/v1.16.26...v1.17.0)

---
updated-dependencies:
- dependency-name: crate-ci/typos
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-01-08 01:17:04 +00:00
1115190a49 Bump shadow-rs from 0.25.0 to 0.26.0
Bumps [shadow-rs](https://github.com/baoyachi/shadow-rs) from 0.25.0 to 0.26.0.
- [Release notes](https://github.com/baoyachi/shadow-rs/releases)
- [Changelog](https://github.com/baoyachi/shadow-rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/baoyachi/shadow-rs/compare/v0.25.0...v0.26.0)

---
updated-dependencies:
- dependency-name: shadow-rs
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-01-08 01:06:59 +00:00
7132c5ad02 add wheres 2024-01-07 19:04:04 -06:00
1920ece759 fix: closure captures can also be constants (#11493)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
When evaluating a closure (in
`EvalRuntime::eval_row_condition_or_closure()`), we try to resolve the
closure's block's captures, but we only check if they're variables on
the stack. We need to also check if they are constants (see the logic in
`Stack::gather_captures()`).

fixes #10701
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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.
-->
2024-01-07 12:51:39 +02:00
6f59abaf43 update reedline version to latest main (#11490)
# Description
Update reedline version to fix
https://github.com/nushell/nushell/issues/11399
2024-01-05 17:19:43 -06:00
5f7425a7b4 Xml errors fix (#11487)
# Description
Fixes #11264
This PR adds checks in `to xml` to output error for malformed xml
entries:
* With columns that are not one of `tag`, `attributes` or `content`
* With no `tag` when entry is not a string
* With `tag` that is not a string
This PR also replaces `attrs` with `attributes` in example and
extra_usage of `to xml` (column was originally named attrs and renamed
to attributes, but this was missed in docs)

# User-Facing Changes
`to xml` will produce error for conditions described above instead of
silently returning nothing

# Tests + Formatting
Added tests for `to xml` to check handling of malformed xml entries
2024-01-05 15:56:13 -06:00
1ab9ec3ebc Bump terminal_size to 0.3 (#11486)
# Description
Simplify the dependencies. There are two different versions of
`terminal_size` that nushell directly depends on.

Related: #8060
2024-01-05 10:19:46 -06:00
f2095ed0cc Fix cross building for target x86_64-pc-windows-gnu on linux (#11485)
# Description

Cross build for target `x86_64-pc-windows-gnu` fails on linux.

```console
nushell on  main [?] is 📦 v0.88.2 via 🦀 v1.77.0-nightly
❯ cargo build --target x86_64-pc-windows-gnu -p nu-system
   Compiling nu-system v0.88.2 (/data/source/nushell/crates/nu-system)
error[E0432]: unresolved import `chrono::Local`
   --> crates/nu-system/src/windows.rs:5:14
    |
5   | use chrono::{Local, NaiveDate};
    |              ^^^^^ no `Local` in the root
    |
note: found an item that was configured out
   --> /path/to/home/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/chrono-0.4.31/src/lib.rs:537:17
    |
537 | pub use offset::Local;
    |                 ^^^^^
    = note: the item is gated behind the `clock` feature

error[E0412]: cannot find type `Local` in crate `chrono`
   --> crates/nu-system/src/windows.rs:68:46
    |
68  |     pub start_time: chrono::DateTime<chrono::Local>,
    |                                              ^^^^^ not found in `chrono`
    |
note: found an item that was configured out
   --> /path/to/home/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/chrono-0.4.31/src/lib.rs:537:17
    |
537 | pub use offset::Local;
    |                 ^^^^^
    = note: the item is gated behind the `clock` feature

Some errors have detailed explanations: E0412, E0432.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `nu-system` (lib) due to 2 previous errors
```
2024-01-05 07:31:38 -06:00
7e26b4fcc2 Bump sysinfo from 0.29 to 0.30 (#11484)
# Description
Bumps `sysinfo` to 0.30.

* Changelog
 https://github.com/GuillaumeGomez/sysinfo/blob/master/CHANGELOG.md#0304

# User-Facing Changes
N/A
2024-01-05 05:31:29 -06:00
ad95e4cc27 Refactor tests (using cococo instead of ^echo) (#11479)
- related PR: #11478 

# Description

Now we can use `nu --testbin cococo` instead of `^echo` to echo messages
to stdout in tests.

But `nu` treats parameters as its own flags when parameter starts with
`-`. So `^echo --foo='bar'` still use `^echo`.

# User-Facing Changes

(none)

# Tests + Formatting

- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library


# After Submitting

(none)
2024-01-05 11:40:56 +08:00
ee5a18167c Replace winapi with windows (#11481)
# Description
`winapi` is not actively maintained, use the Microsoft blessed `windows`
crate instead.

# User-Facing Changes
N/A
2024-01-04 11:17:19 -06:00
75c9e3e5df Try to fix riscv64 building by using unbuntu-latest (#11476)
Try to fix `riscv64gc-unknown-linux-gnu` building by using
`unbuntu-latest`
This PR should close https://github.com/nushell/nushell/issues/11452
The action should run without errors:
https://github.com/nushell/nightly/actions/runs/7394915358
2024-01-04 07:55:10 +08:00
77f10eb270 Fix the test which fails on windows (#11478)
- related PR: #11463

# Description

Currently, `commands::complete::basic` fails on Windows without git
bash.
This pr fixes it.

# User-Facing Changes

(none)

# Tests + Formatting

- [x] (on Windows) `cargo fmt --all -- --check` to check standard code
formatting (`cargo fmt --all` applies these changes)
- [x] (on Windows) `cargo clippy --workspace -- -D warnings -D
clippy::unwrap_used` to check that you're using the standard code style
- [x] (on Windows without git bash, Windows with git bash and Ubuntu)
`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))
- on my Windows with Japanese lang pack: 1 test still fails. (see
#11463)
- [x] (on Windows and Ubuntu) `cargo run -- -c "use std testing; testing
run-tests --path crates/nu-std"` to run the tests for the standard
library

# After Submitting

(none)
2024-01-03 07:22:43 -06:00
42bb42a2e1 Fix rm for symlinks pointing to directory on windows (issue #11461) (#11463)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

- this PR closes #11461

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Using `std::fs::remove_dir` instead of `std::fs::remove_file` when try
remove symlinks pointing to a directory on Windows.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

none

# 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
> ```
-->

- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- I got 2 test fails on my Windows devenv; these fails in main branch
too
- `commands::complete::basic` : passed on Ubuntu, failed on Windows (a
bug?)
- `commands::cp::copy_file_with_read_permission`: failed on Windows with
Japanese environment (This test refers error message, so that fails on
environments using a language except for english.)
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

# 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 fix has no changes to user-facing interface.
2024-01-02 21:27:03 +08:00
f597380112 Bump lsp-types from 0.94.1 to 0.95.0 (#11457)
Bumps [lsp-types](https://github.com/gluon-lang/lsp-types) from 0.94.1
to 0.95.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/gluon-lang/lsp-types/blob/master/CHANGELOG.md">lsp-types's
changelog</a>.</em></p>
<blockquote>
<h2>v0.95.0 (2023-12-12)</h2>
<p><!-- raw HTML omitted --><!-- raw HTML omitted --></p>
<h3>v0.94.2 (2023-12-12)</h3>
<p><!-- raw HTML omitted --><!-- raw HTML omitted --></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="ccf64e2fb6"><code>ccf64e2</code></a>
chore: Release lsp-types version 0.95.0</li>
<li><a
href="bccf50c1c6"><code>bccf50c</code></a>
Update changelog</li>
<li><a
href="75cea03884"><code>75cea03</code></a>
chore: Release lsp-types version 0.94.2</li>
<li><a
href="4084a00cd1"><code>4084a00</code></a>
Update changelog</li>
<li><a
href="b588e166be"><code>b588e16</code></a>
Merge pull request <a
href="https://redirect.github.com/gluon-lang/lsp-types/issues/274">#274</a>
from lapce/inline-completion</li>
<li><a
href="3031a76c44"><code>3031a76</code></a>
Add support for textDocument/inlineCompletion</li>
<li><a
href="038577b0b5"><code>038577b</code></a>
doc: Update readme to request links to the spec for PRs</li>
<li><a
href="f106ccb584"><code>f106ccb</code></a>
Merge pull request <a
href="https://redirect.github.com/gluon-lang/lsp-types/issues/257">#257</a>
from ahlinc/init-work-done-token</li>
<li><a
href="730924021a"><code>7309240</code></a>
Merge pull request <a
href="https://redirect.github.com/gluon-lang/lsp-types/issues/259">#259</a>
from ebkalderon/fix-telemetry-event-params</li>
<li><a
href="a15daede51"><code>a15daed</code></a>
Merge pull request <a
href="https://redirect.github.com/gluon-lang/lsp-types/issues/264">#264</a>
from tage64/master</li>
<li>Additional commits viewable in <a
href="https://github.com/gluon-lang/lsp-types/compare/v0.94.1...v0.95.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=lsp-types&package-manager=cargo&previous-version=0.94.1&new-version=0.95.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-02 07:22:38 +08:00
b92b4120dc Bump crate-ci/typos from 1.16.25 to 1.16.26 (#11458)
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.16.25
to 1.16.26.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/crate-ci/typos/releases">crate-ci/typos's
releases</a>.</em></p>
<blockquote>
<h2>v1.16.26</h2>
<h2>[1.16.26] - 2023-12-27</h2>
<h3>Fixes</h3>
<ul>
<li>Apply <code>extend-ignore-re</code> to file names in addition to
file content</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/crate-ci/typos/blob/master/CHANGELOG.md">crate-ci/typos's
changelog</a>.</em></p>
<blockquote>
<h2>[1.16.26] - 2023-12-27</h2>
<h3>Fixes</h3>
<ul>
<li>Apply <code>extend-ignore-re</code> to file names in addition to
file content</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="45a880d9f8"><code>45a880d</code></a>
chore: Release</li>
<li><a
href="1e55ede61c"><code>1e55ede</code></a>
docs: Update changelog</li>
<li><a
href="e32ec882ea"><code>e32ec88</code></a>
Merge pull request <a
href="https://redirect.github.com/crate-ci/typos/issues/886">#886</a>
from epage/olt</li>
<li><a
href="bf66cbd0b6"><code>bf66cbd</code></a>
fix(config): Apply extend-ignore-re to file names</li>
<li><a
href="a9afeef275"><code>a9afeef</code></a>
test(cli): Show extend-ignore on file names</li>
<li>See full diff in <a
href="https://github.com/crate-ci/typos/compare/v1.16.25...v1.16.26">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=crate-ci/typos&package-manager=github_actions&previous-version=1.16.25&new-version=1.16.26)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-01-02 07:22:19 +08:00
de5ad5de19 Revert "Return external file completions if not empty (#10898)" (#11446)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

This reverts #10898 which breaks external completion.
Not having file completion fallback on empty result is **intentional**
as this indicates that there is nothing to complete at this position.
To have nushell fallback to file completion the external completer can
simply return *nothing*.


`NO RECORDS FOUND`:
```nushell
let external_completer = {|spans|
    []
}
```

Fallback to file completion:
```nushell
let external_completer = {|spans|
}
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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.
-->
2023-12-31 08:45:05 -06:00
64695cd67c bump rust toolchain to 1.73.0 (#11445)
This PR bumps the nushell rust toolchain from 1.72.1 to 1.73.0 since
1.75.0 was released recently.

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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.
-->
2023-12-30 10:41:27 -06:00
21b3eeed99 Allow spreading arguments to commands (#11289)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

Finishes implementing https://github.com/nushell/nushell/issues/10598,
which asks for a spread operator in lists, in records, and when calling
commands.

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

This PR will allow spreading arguments to commands (both internal and
external). It will also deprecate spreading arguments automatically when
passing to external commands.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

- Users will be able to use `...` to spread arguments to custom/builtin
commands that have rest parameters or allow unknown arguments, or to any
external command
- If a custom command doesn't have a rest parameter and it doesn't allow
unknown arguments either, the spread operator will not be allowed
- Passing lists to external commands without `...` will work for now but
will cause a deprecation warning saying that it'll stop working in 0.91
(is 2 versions enough time?)

Here's a function to help with demonstrating some behavior:
```nushell
> def foo [ a, b, c?, d?, ...rest ] { [$a $b $c $d $rest] | to nuon }
```

You can pass a list of arguments to fill in the `rest` parameter using
`...`:
```nushell
> foo 1 2 3 4 ...[5 6]
[1, 2, 3, 4, [5, 6]]
```

If you don't use `...`, the list `[5 6]` will be treated as a single
argument:

```nushell
> foo 1 2 3 4 [5 6] # Note the double [[]]
[1, 2, 3, 4, [[5, 6]]]
```

You can omit optional parameters before the spread arguments:
```nushell
> foo 1 2 3 ...[4 5] # d is omitted here
[1, 2, 3, null, [4, 5]]
```

If you have multiple lists, you can spread them all:
```nushell
> foo 1 2 3 ...[4 5] 6 7 ...[8] ...[]
[1, 2, 3, null, [4, 5, 6, 7, 8]]
```

Here's the kind of error you get when you try to spread arguments to a
command with no rest parameter:

![image](https://github.com/nushell/nushell/assets/45539777/93faceae-00eb-4e59-ac3f-17f98436e6e4)

And this is the warning you get when you pass a list to an external now
(without `...`):


![image](https://github.com/nushell/nushell/assets/45539777/d368f590-201e-49fb-8b20-68476ced415e)


# 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
> ```
-->

Added tests to cover the following cases:
- Spreading arguments to a command that doesn't have a rest parameter
(unexpected spread argument error)
- Spreading arguments to a command that doesn't have a rest parameter
*but* there's also a missing positional argument (missing positional
error)
- Spreading arguments to a command that doesn't have a rest parameter
but does allow unknown arguments, such as `exec` (allowed)
- Spreading a list literal containing arguments of the wrong type (parse
error)
- Spreading a non-list value, both to internal and external commands
- Having named arguments in the middle of rest arguments
- `explain`ing a command call that spreads its arguments

# 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.
-->

# Examples

Suppose you have multiple tables:
```nushell
let people = [[id name age]; [0 alice 100] [1 bob 200] [2 eve 300]]
let evil_twins = [[id name age]; [0 ecila 100] [-1 bob 200] [-2 eve 300]]
```

Maybe you often find yourself needing to merge multiple tables and want
a utility to do that. You could write a function like this:
```nushell
def merge_all [ ...tables ] { $tables | reduce { |it, acc| $acc | merge $it } }
```

Then you can use it like this:
```nushell
> merge_all ...([$people $evil_twins] | each { |$it| $it | select name age })
╭───┬───────┬─────╮
│ # │ name  │ age │
├───┼───────┼─────┤
│ 0 │ ecila │ 100 │
│ 1 │ bob   │ 200 │
│ 2 │ eve   │ 300 │
╰───┴───────┴─────╯
```

Except they had duplicate columns, so now you first want to suffix every
column with a number to tell you which table the column came from. You
can make a command for that:
```nushell
def select_and_merge [ --cols: list<string>, ...tables ] {
  let renamed_tables = $tables
    | enumerate
    | each { |it|
      $it.item | select $cols | rename ...($cols | each { |col| $col + ($it.index | into string) })
    };
  merge_all ...$renamed_tables
}
```
And call it like this:
```nushell
> select_and_merge --cols [name age] $people $evil_twins
╭───┬───────┬──────┬───────┬──────╮
│ # │ name0 │ age0 │ name1 │ age1 │
├───┼───────┼──────┼───────┼──────┤
│ 0 │ alice │  100 │ ecila │  100 │
│ 1 │ bob   │  200 │ bob   │  200 │
│ 2 │ eve   │  300 │ eve   │  300 │
╰───┴───────┴──────┴───────┴──────╯
```

---

Suppose someone's made a command to search for APT packages:

```nushell
# The main command
def search-pkgs [
    --install                   # Whether to install any packages it finds
    log_level: int              # Pretend it's a good idea to make this a required positional parameter
    exclude?: list<string>      # Packages to exclude
    repositories?: list<string> # Which repositories to look in (searches in all if not given)
    ...pkgs                     # Package names to search for
] {
  { install: $install, log_level: $log_level, exclude: ($exclude | to nuon), repositories: ($repositories | to nuon), pkgs: ($pkgs | to nuon) }
}
```

It has a lot of parameters to configure it, so you might make your own
helper commands to wrap around it for specific cases. Here's one
example:
```nushell
# Only look for packages locally
def search-pkgs-local [
    --install              # Whether to install any packages it finds
    log_level: int
    exclude?: list<string> # Packages to exclude
    ...pkgs                # Package names to search for
] {
  # All required and optional positional parameters are given
  search-pkgs --install=$install $log_level [] ["<local URI or something>"] ...$pkgs
}
```
And you can run it like this:
```nushell
> search-pkgs-local --install=false 5 ...["python2.7" "vim"]
╭──────────────┬──────────────────────────────╮
│ install      │ false                        │
│ log_level    │ 5                            │
│ exclude      │ []                           │
│ repositories │ ["<local URI or something>"] │
│ pkgs         │ ["python2.7", vim]           │
╰──────────────┴──────────────────────────────╯
```

One thing I realized when writing this was that if we decide to not
allow passing optional arguments using the spread operator, then you can
(mis?)use the spread operator to skip optional parameters. Here, I
didn't want to give `exclude` explicitly, so I used a spread operator to
pass the packages to install. Without it, I would've needed to do
`search-pkgs-local --install=false 5 [] "python2.7" "vim"` (explicitly
pass `[]` (or `null`, in the general case) to `exclude`). There are
probably more idiomatic ways to do this, but I just thought it was
something interesting.

If you're a virologist of the [xkcd](https://xkcd.com/350/) kind,
another helper command you might make is this:
```nushell
# Install any packages it finds
def live-dangerously [ ...pkgs ] {
  # One optional argument was given (exclude), while another was not (repositories)
  search-pkgs 0 [] ...$pkgs --install # Flags can go after spread arguments
}
```

Running it:
```nushell
> live-dangerously "git" "*vi*" # *vi* because I don't feel like typing out vim and neovim
╭──────────────┬─────────────╮
│ install      │ true        │
│ log_level    │ 0           │
│ exclude      │ []          │
│ repositories │ null        │
│ pkgs         │ [git, *vi*] │
╰──────────────┴─────────────╯
```

Here's an example that uses the spread operator more than once within
the same command call:
```nushell
let extras = [ chrome firefox python java git ]

def search-pkgs-curated [ ...pkgs ] {
  (search-pkgs
      1
      [emacs]
      ["example.com", "foo.com"]
      vim # A must for everyone!
      ...($pkgs | filter { |p| not ($p | str contains "*") }) # Remove packages with globs
      python # Good tool to have
      ...$extras
      --install=false
      python3) # I forget, did I already put Python in extras?
}
```

Running it:
```nushell
> search-pkgs-curated "git" "*vi*"
╭──────────────┬───────────────────────────────────────────────────────────────────╮
│ install      │ false                                                             │
│ log_level    │ 1                                                                 │
│ exclude      │ [emacs]                                                           │
│ repositories │ [example.com, foo.com]                                            │
│ pkgs         │ [vim, git, python, chrome, firefox, python, java, git, "python3"] │
╰──────────────┴───────────────────────────────────────────────────────────────────╯
```
2023-12-28 15:43:20 +08:00
a86a7e6c29 Allow http commands' automatic redirect-following to be disabled (#11329)
Intends to close #8920 

This PR suggests a new flag for the `http` commands, `--redirect-mode`,
which enables users to choose between different redirect handling modes.
The current behaviour of letting ureq silently follow redirects remains
the default, but two new options are introduced here, following the lead
of [JavaScript's `fetch`
API](https://developer.mozilla.org/en-US/docs/Web/API/fetch#redirect):
"manual", where any 3xx response to a request is simply returned as the
command's result, and "error", where any 3xx response causes a network
error like those caused by 4xx and 5xx responses.

This PR is a draft. Tests have not been added or run, the flag is
currently only implemented for the `http get` command, and design tweaks
are likely to be appropriate.

Most notably, it's not obvious to me whether a single flag which can
take one of three values is the nicest solution here.
We might instead consider two binary flags (like
`--no-following-redirects` and `--disallow-redirects`, although I'm bad
at naming things so I need help with that anyway), or completely drop
the "error" option if it's not deemed useful enough. (I personally think
it has some merit, especially since 4xx and 5xx responses are already
treated as errors by default; So this would allow users to treat only
immediate 2xx responses as success)

# User-facing changes
New options for the `http [method]` commands. Behaviour remains
unchanged when the command line flag introduced here is not used.


![image](https://github.com/nushell/nushell/assets/12228688/1eb89f14-7d48-4f41-8a3e-cc0f1bd0a4f8)
2023-12-28 15:26:34 +08:00
15421dc45e Fix the bug for "bytes remove --end" . (#11428)
This PR should close #11426 .

# Description
> ### Describe the bug
> When using the `--end` option of bytes remove, nushell panics if the
provided bytes don't exist. This doesn't seem to affect `bytes remove`
w/o flag or `bytes remove --all`.

# User-Facing Changes
Nushell doesn`t panic anymore.

# Tests + Formatting
Behavior before fixing the bug:
![nu-before
changes](https://github.com/UPB-CS-OpenSourceUpstream/nushell/assets/119429832/f9c26d88-8962-4f38-a373-ba436a26ca7c)
Behavior after fixing the bug:
![nu- after
changes](https://github.com/UPB-CS-OpenSourceUpstream/nushell/assets/119429832/0dd2b487-1696-45a6-9ea2-928cbd3a33a8)
2023-12-28 07:01:55 +08:00
9522052063 More specific errors for missing values in records (#11423)
# Description
Currently, when writing a record, if you don't give the value for a
field, the syntax error highlights the entire record instead of
pinpointing the issue. Here's some examples:

```nushell
> { a: 2, 3 } # Missing colon (and value)
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #2:1:1]
 1 │  { a: 2, 3 }
   ·  ─────┬─────
   ·       ╰── expected record
   ╰────

> { a: 2, 3: } # Missing value
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #3:1:1]
 1 │  { a: 2, 3: }
   ·  ──────┬─────
   ·        ╰── expected record
   ╰────

> { a: 2, 3 4 } # Missing colon
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #4:1:1]
 1 │  { a: 2, 3 4 }
   ·  ──────┬──────
   ·        ╰── expected record
   ╰────
```

In all of them, the entire record is highlighted red because an
`Expr::Garbage` is returned covering that whole span:


![image](https://github.com/nushell/nushell/assets/45539777/36660b50-23be-4353-b180-3f84eff3c220)

This PR is for highlighting only the part inside the record that could
not be parsed. If the record literal is big, an error message pointing
to the start of where the parser thinks things went wrong should help
people fix their code.

# User-Facing Changes
Below are screenshots of the new errors:

If there's a stray record key right before the record ends, it
highlights only that key and tells the user it expected a colon after
it:


![image](https://github.com/nushell/nushell/assets/45539777/94503256-8ea2-47dd-b69a-4b520c66f7b6)

If the record ends before the value for the last field was given, it
highlights the key and colon of that field and tells the user it
expected a value after the colon:


![image](https://github.com/nushell/nushell/assets/45539777/2f3837ec-3b35-4b81-8c57-706f8056ac04)

If there are two consecutive expressions without a colon between them,
it highlights everything from the second expression to the end of the
record and tells the user it expected a colon. I was tempted to add a
help message suggesting adding a colon in between, but that may not
always be the right thing to do.


![image](https://github.com/nushell/nushell/assets/45539777/1abaaaa8-1896-4909-bbb7-9a38cece5250)

# Tests + Formatting

# After Submitting
2023-12-27 10:15:12 +01:00
ba880277bf Remove unnecessary replace_in_variable (#11424)
# Description
`Expression::replace_in_variable` is only called in one place, and it is
called with `new_var_id` = `IN_VARIABLE_ID`. So, it ends up doing
nothing. E.g., adding `debug_assert_eq!(new_var_id, IN_VARIABLE_ID)` in
`replace_in_variable` does not trigger any panic.

# User-Facing Changes
Breaking change for `nu_protocol`.
2023-12-26 18:46:49 +01:00
40241e9be6 Revert "Bump reedline development version" (#11425)
Reverts nushell/nushell#11406

This PR https://github.com/nushell/reedline/pull/688 is causing clear
screens to happen at strange times. Documented here
https://github.com/nushell/nushell/pull/11406#issuecomment-1869591405
2023-12-26 09:08:32 -06:00
34f3da7150 Don't panic when http_client fails (#11422)
# Description

This PR makes `http_client` return `Result<ureq::Agent, ShellError>`, so
errors can be propagated to the caller.
2023-12-25 23:09:37 +08:00
534287ed65 Don't create a thread if stderr_stream is None (#11421)
# Description

There is no need to create a thread if `stderr_stream` is `None`.
2023-12-25 08:10:15 -06:00
913c2b8d1c Bump ical from 0.8.0 to 0.9.0 (#11419)
Bumps [ical](https://github.com/Peltoche/ical-rs) from 0.8.0 to 0.9.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/Peltoche/ical-rs/releases">ical's
releases</a>.</em></p>
<blockquote>
<h2>v0.9.0</h2>
<h2>What's Changed</h2>
<ul>
<li>Add the github actions by <a
href="https://github.com/Peltoche"><code>@​Peltoche</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/35">Peltoche/ical-rs#35</a></li>
<li>Use thiserror instead of failure by <a
href="https://github.com/Peltoche"><code>@​Peltoche</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/36">Peltoche/ical-rs#36</a></li>
<li>Accept lowercase vCards by <a
href="https://github.com/link2xt"><code>@​link2xt</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/37">Peltoche/ical-rs#37</a></li>
<li>Parse multiline attributes with tabs correctly by <a
href="https://github.com/FliegendeWurst"><code>@​FliegendeWurst</code></a>
in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/41">Peltoche/ical-rs#41</a></li>
<li>Ical serde by <a
href="https://github.com/daladim"><code>@​daladim</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/44">Peltoche/ical-rs#44</a></li>
<li>Set the license to Apache 2 by <a
href="https://github.com/Peltoche"><code>@​Peltoche</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/47">Peltoche/ical-rs#47</a></li>
<li>Update README.md by <a
href="https://github.com/Zearin"><code>@​Zearin</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/51">Peltoche/ical-rs#51</a></li>
<li>Adding ical-output support by <a
href="https://github.com/migmedia"><code>@​migmedia</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/49">Peltoche/ical-rs#49</a></li>
<li>Improve documentation of builders. by <a
href="https://github.com/migmedia"><code>@​migmedia</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/52">Peltoche/ical-rs#52</a></li>
<li>Add access functions for properties by <a
href="https://github.com/reedts"><code>@​reedts</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/53">Peltoche/ical-rs#53</a></li>
<li>Fix README by <a
href="https://github.com/westy92"><code>@​westy92</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/55">Peltoche/ical-rs#55</a></li>
<li>Handle the case were a line contains non-utf8 characters by <a
href="https://github.com/Peltoche"><code>@​Peltoche</code></a> in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/57">Peltoche/ical-rs#57</a></li>
</ul>
<h2>New Contributors</h2>
<ul>
<li><a href="https://github.com/link2xt"><code>@​link2xt</code></a> made
their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/37">Peltoche/ical-rs#37</a></li>
<li><a
href="https://github.com/FliegendeWurst"><code>@​FliegendeWurst</code></a>
made their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/41">Peltoche/ical-rs#41</a></li>
<li><a href="https://github.com/daladim"><code>@​daladim</code></a> made
their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/44">Peltoche/ical-rs#44</a></li>
<li><a href="https://github.com/Zearin"><code>@​Zearin</code></a> made
their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/51">Peltoche/ical-rs#51</a></li>
<li><a href="https://github.com/migmedia"><code>@​migmedia</code></a>
made their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/49">Peltoche/ical-rs#49</a></li>
<li><a href="https://github.com/reedts"><code>@​reedts</code></a> made
their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/53">Peltoche/ical-rs#53</a></li>
<li><a href="https://github.com/westy92"><code>@​westy92</code></a> made
their first contribution in <a
href="https://redirect.github.com/Peltoche/ical-rs/pull/55">Peltoche/ical-rs#55</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a
href="https://github.com/Peltoche/ical-rs/compare/0.6.0...v0.9.0">https://github.com/Peltoche/ical-rs/compare/0.6.0...v0.9.0</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li>See full diff in <a
href="https://github.com/Peltoche/ical-rs/commits/v0.9.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=ical&package-manager=cargo&previous-version=0.8.0&new-version=0.9.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-25 09:23:27 +08:00
aeffa188f0 Fix an infinite loop if the input stream and output stream are the same (#11384)
# Description

Fixes #11382 

# User-Facing Changes
* before

```console
nushell/test (109f629) [✘?]
❯ open hello.md
hello
nushell/test (109f629) [✘?]
❯ ls hello.md | get size
╭───┬─────╮
│ 0 │ 6 B │
╰───┴─────╯
nushell/test (109f629) [✘?]
❯ open --raw hello.md | prepend "world" | save --raw --force hello.md
^C
nushell/test (109f629) [✘?]
❯ ls hello.md | get size
╭───┬─────────╮
│ 0 │ 2.8 GiB │
╰───┴─────────╯
```

* after

```console
nushell/test on  fix_save [✘!?⇡]
❯ open hello.md | prepend "hello" | save --force hello.md
nushell/test on  fix_save [✘!?⇡]
❯ open --raw hello.md | prepend "hello" | save --raw --force ../test/hello.md
Error:   × pipeline input and output are same file
   ╭─[entry #4:1:1]
 1 │ open --raw hello.md | prepend "hello" | save --raw --force ../test/hello.md
   ·                                                           ────────┬───────
   ·                                                                   ╰── can't save output to '/data/source/nushell/test/hello.md' while it's being reading
   ╰────
  help: you should change output path


nushell/test on  fix_save [✘!?⇡]
❯ open hello | prepend "hello" | save --force hello
Error:   × pipeline input and output are same file
   ╭─[entry #5:1:1]
 1 │ open hello | prepend "hello" | save --force hello
   ·                                            ──┬──
   ·                                              ╰── can't save output to '/data/source/nushell/test/hello' while it's being reading
   ╰────
  help: you should change output path
```

# Tests + Formatting
Make sure you've run and fixed any issues with these commands:
- [x] add `commands::save::save_same_file_with_extension`
- [x] add `commands::save::save_same_file_without_extension`
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

# After Submitting
2023-12-24 23:29:23 +08:00
543a25599c Simplify the feature gates for stor commands (#11416)
All of them depend on feature `sqlite` so just conditionally `use` the
parent module.
2023-12-24 13:31:46 +01:00
7ad0e5541e Make polars deps optional for cargo test --all (#11415)
As `--workspace/--all` pulls in all crates in the workspace for `cargo
test --workspace` let's make sure that the `polars` family of
dependencies are also feature gated so they only build for `--features
dataframe`. The test modules themselves also depend on the feature.

Should speed up a bare `cargo test --workspace`
2023-12-24 13:12:31 +01:00
c1cc1c82cc Lock out new direct construction of Record (#11414)
# Description
With #11386 we don't have any nushell-internal code directly accessing
the `vals` field of `Record`, so let's make it private so everyone in
the future uses the checked ways guaranteeing matching cols/vals.

The `cols` feel has to remain pub for now as `rename` still directly
mutates this field. See #11020 for challenges for this refactor.

# Plugin-Author-Facing Changes
This is a breaking change for outside plugins that relied on the `pub`
fields.
2023-12-24 13:12:16 +01:00
b0b4c3dffd Check for clean repo after tests (#11409)
Goal: detect problems like adressed by #11407 or missing `Cargo.lock`
update early

Try to fail when there is a non clear `git status` message
2023-12-23 20:28:07 +01:00
f3de373c59 Bump reedline development version (#11406)
Fix for #11399
Provided by nushell/reedline#688
2023-12-23 20:08:37 +01:00
df1fecd2cb Fix sandboxing of redirection tests (#11407)
When running `cargo test --workspace` a file `crates/nu-command/a.txt`
remained which we also saw as an accidential additions in some commits.

Searching for `a.txt` narrowed it down that
`redirection_keep_exit_codes` was not sandboxed in a temporary directory
and created this file.

Went through redirection tests and placed them in a `Playground` to get
sandboxing `dirs` for `nu!(cwd:`.
For those tests where redirection fails and no file should be created
now I added a check that no file is created on accident.


- Sandbox `redirection_keep_exit_codes` test
- Sandbox `no_duplicate_redirection` test
- Check that no redirect file is created on error
- Sandbox `redirection_should_have_a_target` test
2023-12-23 20:01:20 +01:00
9620e27e4f fix: prevent greedy matching of directory names (#11403)
Fixes #11396

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

If a directory name is an exact match, the completer stops greedily
matching directories with the same prefix.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

Completions should work as described in #11396.

# 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.
-->
2023-12-23 00:06:00 -06:00
072ecec8ca Bump unsafe-libyaml from 0.2.9 to 0.2.10 (#11391) 2023-12-21 18:49:12 +00:00
748d82cec1 Bump windows from 0.48.0 to 0.52.0 (#11325)
# Description
Bump `windows` to 0.52.0 and fix `is_admin`

https://github.com/microsoft/windows-rs/pull/2476

# User-Facing Changes
N/A

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-21 18:49:15 +01:00
5e5d1ea81b Bump fancy-regex to single 0.12.0 version (#11389)
Supersedes #11039 that was broken due to dependabot not correctly taking
the workspace into account (this bug has been worked around in #11387)
2023-12-21 17:10:33 +01:00
bb570e42be Bump lscolors from 0.15.0 to 0.16.0 (#11359) 2023-12-21 16:08:19 +00:00
3a050864df Simplify SIGQUIT handling (#11381)
# Description
Simplifies `SIGQUIT` protection to a single `signal` ignore system call.

# User-Facing Changes
`SIGQUIT` is no longer blocked if nushell is in non-interactive mode
(signals should not be blocked in non-interactive mode).
Also a breaking API change for `nu_protocol`.

# Tests + Formatting
Should come after #11178 for testing.
2023-12-21 17:00:38 +01:00
8cfa96b4c0 Construct Records only through checked helpers (#11386)
# Description

Constructing the internals of `Record` without checking the lengths is
bad. (also incompatible with changes to how we store records)

- Use `Record::from_raw_cols_vals` in dataframe code
- Use `record!` macro in dataframe test
- Use `record!` in `nu-color-config` tests
- Stop direct record construction in `nu-command`
- Refactor table construction in `from nuon`

# User-Facing Changes
None

# Tests + Formatting
No new tests, updated tests in equal fashion
2023-12-21 16:48:15 +01:00
6f384da57e Make Call::get_flag_expr return Expression by ref (#11388)
# Description
A small refactor that eliminates some `Expression` cloning.

# User-Facing Changes
Breaking change for `nu_protocol`.
2023-12-21 16:42:07 +01:00
f8e63328d8 Expand the workspace members in Cargo.toml (#11387)
While the `cargo` tools will include crates we depend on in the
directory into the workspace, I observed dependabot not taking all of
our crates into account in its version bump attempts leading to
duplication of outside dependencies.

e.g.
- https://github.com/nushell/nushell/pull/11039
2023-12-21 16:41:31 +01:00
5d98a727ca Deprecate --flag: bool in custom command (#11365)
# Description
While #11057 is merged, it's hard to tell the difference between
`--flag: bool` and `--flag`, and it makes user hard to read custom
commands' signature, and hard to use them correctly.

After discussion, I think we can deprecate `--flag: bool` usage, and
encourage using `--flag` instead.

# User-Facing Changes
The following code will raise warning message, but don't stop from
running.
```nushell
❯ def florb [--dry-run: bool, --another-flag] { "aaa" };  florb
Error:   × Deprecated: --flag: bool
   ╭─[entry #7:1:1]
 1 │ def florb [--dry-run: bool, --another-flag] { "aaa" };  florb
   ·                       ──┬─
   ·                         ╰── `--flag: bool` is deprecated. Please use `--flag` instead, more info: https://www.nushell.sh/book/custom_commands.html
   ╰────

aaa
```

cc @kubouch 

# Tests + Formatting
Done

# After Submitting
- [ ] Add more information under
https://www.nushell.sh/book/custom_commands.html to indicate `--dry-run:
bool` is not allowed,
- [ ] remove `: bool` from custom commands between 0.89 and 0.90

---------

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2023-12-21 10:07:08 +01:00
109f629cb6 📝 Update str trim CLI help doc (#11383)
# Description
Hi! I updated the samples of `str trim` because there were repeated and
clarified the explanations

# User-Facing Changes
Yes! I send the details here:


![image](https://github.com/nushell/nushell/assets/30557287/e30a5612-4214-4365-8b83-7aefbc0ee825)

(`old` is version `88.1` not the latest main)

# Tests + Formatting
~~I ran `toolkit check pr` successfully~~

There was a tiny problem, a test I never touched now it's failing

```nu
(^echo a | complete) == {stdout: "a\n", exit_code: 0}
```
should output `true` but outputs `false`, both in my running `nu`
version and in my PR version
This make the test `nu-command::main commands::complete::basic` fail
located in `crates\nu-command\tests\commands\complete.rs`

# After Submitting
I'm not sure if I need to update nushell.github.io, some of the help is
auto-generated, but maybe not all?
I can file a PR if needed
2023-12-20 14:23:41 -06:00
ff6a67d293 Remove Expr::MatchPattern (#11367)
# Description
Following from #11356, it looks like `Expr::MatchPattern` is no longer
used in any way. This PR removes `Expr::MatchPattern` alongside
`Type::MatchPattern` and `SyntaxShape::MatchPattern`.

# User-Facing Changes
Breaking API change for `nu_protocol`.
2023-12-20 18:52:28 +01:00
03ae01f11e Bump itertools from 0.11.0 to 0.12.0 (#11360)
Bumps [itertools](https://github.com/rust-itertools/itertools) from
0.11.0 to 0.12.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/rust-itertools/itertools/blob/master/CHANGELOG.md">itertools's
changelog</a>.</em></p>
<blockquote>
<h2>0.12.0</h2>
<h3>Breaking</h3>
<ul>
<li>Made <code>take_while_inclusive</code> consume iterator by value (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/709">#709</a>)</li>
<li>Added <code>Clone</code> bound to <code>Unique</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/777">#777</a>)</li>
</ul>
<h3>Added</h3>
<ul>
<li>Added <code>Itertools::try_len</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/723">#723</a>)</li>
<li>Added free function <code>sort_unstable</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/796">#796</a>)</li>
<li>Added <code>GroupMap::fold_with</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/778">#778</a>,
<a
href="https://redirect.github.com/rust-itertools/itertools/issues/785">#785</a>)</li>
<li>Added <code>PeekNth::{peek_mut, peek_nth_mut}</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/716">#716</a>)</li>
<li>Added <code>PeekNth::{next_if, next_if_eq}</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/734">#734</a>)</li>
<li>Added conversion into <code>(Option&lt;A&gt;,Option&lt;B&gt;)</code>
to <code>EitherOrBoth</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/713">#713</a>)</li>
<li>Added conversion from <code>Either&lt;A, B&gt;</code> to
<code>EitherOrBoth&lt;A, B&gt;</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/715">#715</a>)</li>
<li>Implemented <code>ExactSizeIterator</code> for <code>Tuples</code>
(<a
href="https://redirect.github.com/rust-itertools/itertools/issues/761">#761</a>)</li>
<li>Implemented <code>ExactSizeIterator</code> for
<code>(Circular)TupleWindows</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/752">#752</a>)</li>
<li>Made <code>EitherOrBoth&lt;T&gt;</code> a shorthand for
<code>EitherOrBoth&lt;T, T&gt;</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/719">#719</a>)</li>
</ul>
<h3>Changed</h3>
<ul>
<li>Added missing <code>#[must_use]</code> annotations on iterator
adaptors (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/794">#794</a>)</li>
<li>Made <code>Combinations</code> lazy (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/795">#795</a>)</li>
<li>Made <code>Intersperse(With)</code> lazy (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/797">#797</a>)</li>
<li>Made <code>Permutations</code> lazy (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/793">#793</a>)</li>
<li>Made <code>Product</code> lazy (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/800">#800</a>)</li>
<li>Made <code>TupleWindows</code> lazy (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/602">#602</a>)</li>
<li>Specialized <code>Combinations::{count, size_hint}</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/729">#729</a>)</li>
<li>Specialized <code>CombinationsWithReplacement::{count,
size_hint}</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/737">#737</a>)</li>
<li>Specialized <code>Powerset::fold</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/765">#765</a>)</li>
<li>Specialized <code>Powerset::count</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/735">#735</a>)</li>
<li>Specialized <code>TupleCombinations::{count, size_hint}</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/763">#763</a>)</li>
<li>Specialized <code>TupleCombinations::fold</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/775">#775</a>)</li>
<li>Specialized <code>WhileSome::fold</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/780">#780</a>)</li>
<li>Specialized <code>WithPosition::fold</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/772">#772</a>)</li>
<li>Specialized <code>ZipLongest::fold</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/774">#774</a>)</li>
<li>Changed <code>{min, max}_set*</code> operations require
<code>alloc</code> feature, instead of <code>std</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/760">#760</a>)</li>
<li>Improved documentation of <code>tree_fold1</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/787">#787</a>)</li>
<li>Improved documentation of <code>permutations</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/724">#724</a>)</li>
<li>Fixed typo in documentation of <code>multiunzip</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/770">#770</a>)</li>
</ul>
<h3>Notable Internal Changes</h3>
<ul>
<li>Improved specialization tests (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/799">#799</a>,
<a
href="https://redirect.github.com/rust-itertools/itertools/issues/786">#786</a>,
<a
href="https://redirect.github.com/rust-itertools/itertools/issues/782">#782</a>)</li>
<li>Simplified implementation of <code>Permutations</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/739">#739</a>,
<a
href="https://redirect.github.com/rust-itertools/itertools/issues/748">#748</a>,
<a
href="https://redirect.github.com/rust-itertools/itertools/issues/790">#790</a>)</li>
<li>Combined
<code>Merge</code>/<code>MergeBy</code>/<code>MergeJoinBy</code>
implementations (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/736">#736</a>)</li>
<li>Simplified <code>Permutations::size_hint</code> (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/739">#739</a>)</li>
<li>Fix wrapping arithmetic in benchmarks (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/770">#770</a>)</li>
<li>Enforced <code>rustfmt</code> in CI (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/751">#751</a>)</li>
<li>Disallowed compile warnings in CI (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/720">#720</a>)</li>
<li>Used <code>cargo hack</code> to check MSRV (<a
href="https://redirect.github.com/rust-itertools/itertools/issues/754">#754</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="98ecabb47d"><code>98ecabb</code></a>
chore: Release itertools version 0.12.0</li>
<li><a
href="22fc427ac5"><code>22fc427</code></a>
prepare v0.12.0 release</li>
<li><a
href="6d291786a9"><code>6d29178</code></a>
Document the field <code>a_cur</code> of <code>Product</code></li>
<li><a
href="bf2b0129d1"><code>bf2b012</code></a>
Better <code>Product::size_hint</code></li>
<li><a
href="8d07f6b856"><code>8d07f6b</code></a>
Make <code>Product</code> lazy</li>
<li><a
href="d7e6bab9fd"><code>d7e6bab</code></a>
Document the field <code>peek</code> of
<code>IntersperseWith</code></li>
<li><a
href="9b01a11891"><code>9b01a11</code></a>
Make <code>IntersperseWith</code> lazy</li>
<li><a
href="4f22173b93"><code>4f22173</code></a>
Refactor <code>IntersperseWith::next</code></li>
<li><a
href="b76172b412"><code>b76172b</code></a>
chore: adjust docs to reflect discussion in the PR</li>
<li><a
href="955927f6c4"><code>955927f</code></a>
chore: fixup docs of tree_fold1</li>
<li>Additional commits viewable in <a
href="https://github.com/rust-itertools/itertools/compare/v0.11.0...v0.12.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=itertools&package-manager=cargo&previous-version=0.11.0&new-version=0.12.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-20 22:58:07 +08:00
697f3c03f1 enable flag value type checking (#11311)
# Description
Fixes: #11310

# User-Facing Changes
After the change, the following code will go to error:
```nushell
> def a [--x: int = 3] { "aa" }
> let y = "aa"
> a --x=$y
Error: nu::parser::type_mismatch

  × Type mismatch.
   ╭─[entry #32:2:1]
 2 │ let y = "aa"
 3 │ a --x=$y
   ·       ─┬
   ·        ╰── expected int, found string
   ╰────
```
2023-12-20 11:07:19 +01:00
1cecb37628 Bump shadow-rs from 0.24.1 to 0.25.0 (#11282)
Bumps [shadow-rs](https://github.com/baoyachi/shadow-rs) from 0.24.1 to
0.25.0.
<details>
<summary>Commits</summary>
<ul>
<li><a
href="f08828be45"><code>f08828b</code></a>
Update Cargo.toml</li>
<li><a
href="a3cc1a118f"><code>a3cc1a1</code></a>
Update README.md</li>
<li><a
href="96fdafc738"><code>96fdafc</code></a>
Merge pull request <a
href="https://redirect.github.com/baoyachi/shadow-rs/issues/144">#144</a>
from baoyachi/wasm_example</li>
<li><a
href="24d3bd82c0"><code>24d3bd8</code></a>
fix</li>
<li><a
href="aab9bf2d4d"><code>aab9bf2</code></a>
Update check.yml</li>
<li><a
href="1d4e455730"><code>1d4e455</code></a>
add wasm example</li>
<li>See full diff in <a
href="https://github.com/baoyachi/shadow-rs/compare/v0.24.1...v0.25.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=shadow-rs&package-manager=cargo&previous-version=0.24.1&new-version=0.25.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-20 17:49:03 +08:00
89436e978b add special emoji handling for debug --raw (#11368)
# Description

This PR add special handling in `debug -r` for emoji's so that it prints
the code points.

### Before
```nushell
❯ emoji --list | where name =~ farmer | reject utf8_bytes | get 0.emoji | debug -r
String {
    val: "🧑\u{200d}🌾",
    internal_span: Span {
        start: 0,
        end: 0,
    },
}
```

### After
```nushell
❯ emoji --list | where name =~ farmer | reject utf8_bytes | get 0.emoji | debug -r
String {
    val: "\\u{1f9d1}\\u{200d}\\u{1f33e}",
    internal_span: Span {
        start: 0,
        end: 0,
    },
}
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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.
-->
2023-12-19 13:09:31 -06:00
cd0a52cf00 Fix build for BSDs (#11372)
# Description
This PR fixes build for BSD variants (including FreeBSD and NetBSD). 

Currently, `procfs` only support linux, android and l4re, and
0cba269d80 only adds support for NetBSD,
this PR should work on all BSD variants.


b153b782a5/procfs/build.rs (L4-L8)

Fixes #11373 

# User-Facing Changes
* before

```console
nibon7@fbsd /d/s/nushell ((70f7db14))> cargo build
   Compiling tempfile v3.8.1
   Compiling procfs v0.16.0
   Compiling toml_edit v0.21.0
   Compiling native-tls v0.2.11
error: failed to run custom build command for `procfs v0.16.0`

Caused by:
  process didn't exit successfully: `/data/source/nushell/target/debug/build/procfs-d59599f40f32f0d5/build-script-build` (exit status: 1)
  --- stderr
  Building procfs on an for a unsupported platform. Currently only linux and android are supported
  (Your current target_os is freebsd)
warning: build failed, waiting for other jobs to finish...
```

* after

```console
nushell on  bsd [✘!?] is 📦 v0.88.2 via 🦀 v1.74.1
❯ version
╭────────────────────┬───────────────────────────────────────────╮
│ version            │ 0.88.2                                    │
│ branch             │ bsd                                       │
│ commit_hash        │ 151edef186  │
│ build_os           │ freebsd-x86_64                            │
│ build_target       │ x86_64-unknown-freebsd                    │
│ rust_version       │ rustc 1.74.1 (a28077b28 2023-12-04)       │
│ rust_channel       │ stable-x86_64-unknown-freebsd             │
│ cargo_version      │ cargo 1.74.1 (ecb9851af 2023-10-18)       │
│ build_time         │ 2023-12-19 10:12:15 +00:00                │
│ build_rust_channel │ debug                                     │
│ allocator          │ mimalloc                                  │
│ features           │ default, extra, sqlite, trash, which, zip │
│ installed_plugins  │                                           │
╰────────────────────┴───────────────────────────────────────────╯
nushell on  bsd [✘!?] is 📦 v0.88.2 via 🦀 v1.74.1
❯ cargo test --workspace commands::ulimit e>> /dev/null | rg ulimit
test commands::ulimit::limit_set_filesize2 ... ok
test commands::ulimit::limit_set_filesize1 ... ok
test commands::ulimit::limit_set_hard1 ... ok
test commands::ulimit::limit_set_hard2 ... ok
test commands::ulimit::limit_set_invalid1 ... ok
test commands::ulimit::limit_set_invalid3 ... ok
test commands::ulimit::limit_set_invalid4 ... ok
test commands::ulimit::limit_set_invalid5 ... ok
test commands::ulimit::limit_set_soft2 ... ok
test commands::ulimit::limit_set_soft1 ... ok
nushell on  bsd [✘!?] is 📦 v0.88.2 via 🦀 v1.74.1
```


# 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.
-->
2023-12-19 08:58:45 -06:00
c6043eb500 improve completions of use and overlay use (#11330)
# Description
this PR is two-fold
- make `use` and `overlay use` use the same completion algorithm in
48f29b633
- list directory modules in completions of both with 402acde5c

# User-Facing Changes
i currently have the following in my `NU_LIB_DIRS`
<details>
<summary>click to see the script</summary>

```nushell
for dir in $env.NU_LIB_DIRS {
    print $dir
    print (ls $dir --short-names | select name type)
}
```
</details>

```
/home/amtoine/.local/share/nupm/modules
#┬────────name────────┬type
0│nu-git-manager      │dir
1│nu-git-manager-sugar│dir
2│nu-hooks            │dir
3│nu-scripts          │dir
4│nu-themes           │dir
5│nupm                │dir
─┴────────────────────┴────

/home/amtoine/.config/nushell/overlays
#┬──name──┬type
0│ocaml.nu│file
─┴────────┴────
```

> **Note**
> all the samples below are run from the Nushell repo, i.e. a directory
with a `toolkit.nu` module

## before the changes
- `use` would give me `["ocaml.nu", "toolkit.nu"]` 
- `overlay use` would give me `[]` 

## after the changes
both commands give me
```nushell
[
    "nupm/",
    "ocaml.nu",
    "toolkit.nu",
    "nu-scripts/",
    "nu-git-manager/",
    "nu-git-manager-sugar/",
]
```

# Tests + Formatting
- adds a new `directory_completion/mod.nu` to the completion fixtures
- make sure `source-env`, `use` and `overlay-use` are all tested in the
_dotnu_ test
- fix all the other tests that use completions in the fixtures directory
for completions

# After Submitting
2023-12-19 17:14:34 +08:00
729373aba0 fix(cd): on android/termux fails to cd into /sdcard (#10329)
fix on android/termux fails to cd into /sdcard or any directory that
user has access via group

fixes #8095

I am not aware how this works on other platform so feel free to modify
this pr or even close it if it is not correct

# Description
on android or on linux to check if the user belongs to given directory
group, use `libc::getgroups` function

# User-Facing Changes
NA
2023-12-19 16:48:20 +08:00
f59a6990dc Refactor group-by with closure grouper (#11370)
# Description
Refactors `group_closure` in `group_by.rs` as suggested by the TODO
comment.
2023-12-19 15:48:37 +08:00
0f9094ead2 Bump crate-ci/typos from 1.16.24 to 1.16.25 (#11358)
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.16.24
to 1.16.25.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/crate-ci/typos/releases">crate-ci/typos's
releases</a>.</em></p>
<blockquote>
<h2>v1.16.25</h2>
<h2>[1.16.25] - 2023-12-13</h2>
<h3>Fixes</h3>
<ul>
<li>Have correction in <code>extend-words</code> match the original
word's case</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/crate-ci/typos/blob/master/CHANGELOG.md">crate-ci/typos's
changelog</a>.</em></p>
<blockquote>
<h2>[1.16.25] - 2023-12-13</h2>
<h3>Fixes</h3>
<ul>
<li>Have correction in <code>extend-words</code> match the original
word's case</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="5bd389de71"><code>5bd389d</code></a>
chore: Release</li>
<li><a
href="8dbecf3e45"><code>8dbecf3</code></a>
docs: Update changelog</li>
<li><a
href="f8f8c6edf1"><code>f8f8c6e</code></a>
Merge pull request <a
href="https://redirect.github.com/crate-ci/typos/issues/884">#884</a>
from epage/custom</li>
<li><a
href="55d802d0ef"><code>55d802d</code></a>
fix(dict): Mirror original case for custom dict</li>
<li><a
href="86c4c9fb4a"><code>86c4c9f</code></a>
test(cli): Add reproduction case</li>
<li>See full diff in <a
href="https://github.com/crate-ci/typos/compare/v1.16.24...v1.16.25">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=crate-ci/typos&package-manager=github_actions&previous-version=1.16.24&new-version=1.16.25)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-12-19 15:17:10 +08:00
70f7db14d4 Only run $env.PROMPT_COMMAND once per prompt (copy of #10986) (#11366)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

This PR is basically a copy of #10986 by @CAD97, which made
`$env.PROMPT_COMMAND` only run once per prompt, but @fdncred found an
issue where hitting Enter would make the transient prompt appear and be
immediately overwritten by the regular prompt, so it was
[reverted](https://github.com/nushell/nushell/pull/11340).
https://github.com/nushell/nushell/pull/10788 was also made to do the
same thing as #10986 but that ended up having the same issue. For some
reason, this branch doesn't have that problem, although I haven't
figured out why yet.

@CAD97, if you have any inputs or want to make your own PR, let me know.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

When hitting enter, the prompt shouldn't blink in place anymore.

# 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.
-->
2023-12-18 09:33:04 -06:00
pin
0cba269d80 Fix build on NetBSD (#11364)
- this PR should close #11354

# Description
Allow building on NetBSD.

# User-Facing Changes
NA
2023-12-18 06:41:27 -06:00
ec2593efb8 nu-cli repl get_command_finished_marker() does not need to be pub (#11362)
```rust
fn get_command_finished_marker
```
does not need to be public so just doing a little code cleanup
2023-12-17 20:20:31 -08:00
c2283596ac Rename extra's format to format pattern (#11355)
This removes the naming conflict, introduced by `fd77114` (#11334), when
the `extra` feature is enabled.
2023-12-17 17:32:34 -06:00
c9399f5142 Downgrade openssl-src to fix riscv64 build target, close #11345 (#11353) 2023-12-18 05:57:20 +08:00
c9c93f5b4d Remove Value::MatchPattern (#11356)
# Description
`Value::MatchPattern` implies that `MatchPattern`s are first-class
values. This PR removes this case, and commands must now instead use
`Expr::MatchPattern` to extract `MatchPattern`s just like how the
`match` command does using `Expr::MatchBlock`.

# User-Facing Changes
Breaking API change for `nu_protocol` crate.
2023-12-18 07:25:34 +13:00
2264682443 fix shell integration markers (#11352)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->
Fixes #11260 

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Note: my issue description was a bit wrong. Issue one can't be
reproduced without a config (`shell_integration` isn't on by default,
so..), however the issue itself is still valid

For issue 1, the default prompt needs to know about the
`shell_integration` config, and the markers are added around the default
prompt when that's on.

For issue 2, this is actually related to transient prompts. When
rendering, the markers weren't added like for normal prompts.

After the fix the output do now contain the proper markers:

Reproducing the minimum config here for convenience:

```nu
$env.config = {
    show_banner: false
    shell_integration: true
}
# $env.PROMPT_COMMAND = {|| "> " }
```

For issue 1, the output looks like: 
```
[2.3490236,"o","\u001b[?25l\u001b[21;1H\u001b[21;1H\u001b[J\u001b[38;5;10m\u001b]133;A\u001b\\/home/steven\u001b]133;B\u001b\\\u001b[38;5;14m\u001b[38;5;5m\u001b7\u001b[21;84H12/16/2023 03:31:58 PM\u001b8\u001b[0m\u001b[0m\u001b[1;36mecho\u001b[0m\u001b7\u001b8\u001b[?25h"]
[2.5676293,"o","\u001b[6n"]
[2.571353,"o","\u001b[?25l\u001b[21;1H\u001b[21;1H\u001b[J\u001b[38;5;10m\u001b]133;A\u001b\\\u001b]133;A\u001b\\/home/steven\u001b]133;B\u001b\\\u001b]133;B\u001b\\\u001b[38;5;14m\u001b[38;5;5m\u001b7\u001b[21;84H12/16/2023 03:31:59 PM\u001b8\u001b[0m\u001b[0m\u001b[1;36mecho\u001b[0m\u001b7\u001b8\u001b[?25h\u001b[21;1H\r\n\u001b[21;1H"]
[2.571436,"o","\u001b[?2004l"]
[2.5714657,"o","\u001b]133;C\u001b\\"]
```
in line 3, where enter is pressed, `133 A` and `B` are present.

Same for issue 2 (uncomment the `PROMPT_COMMAND` line in the config):
```
[1.9585224,"o","\u001b[?25l\u001b[21;1H\u001b[21;1H\u001b[J\u001b[38;5;10m\u001b]133;A\u001b\\> \u001b]133;B\u001b\\\u001b[38;5;14m\u001b[38;5;5m\u001b7\u001b[21;84H12/16/2023 03:32:15 PM\u001b8\u001b[0m\u001b[0m\u001b[1;36mecho\u001b[0m\u001b7\u001b8\u001b[?25h"]
[2.453972,"o","\u001b[6n"]
[2.4585786,"o","\u001b[?25l\u001b[21;1H\u001b[21;1H\u001b[J\u001b[38;5;10m\u001b]133;A\u001b\\\u001b]133;A\u001b\\> \u001b]133;B\u001b\\\u001b]133;B\u001b\\\u001b[38;5;14m\u001b[38;5;5m\u001b7\u001b[21;84H12/16/2023 03:32:15 PM\u001b8\u001b[0m\u001b[0m\u001b[1;36mecho\u001b[0m\u001b7\u001b8\u001b[?25h\u001b[21;1H\r\n\u001b[21;1H\u001b[?2004l\u001b]133;C\u001b\\\r\n\u001b]133;D;0\u001b\\\u001b]7;file://Aostro-5468/home/steven\u001b\\\u001b]2;~\u0007\u001b[?1l"]
[2.4669976,"o","\u001b[?2004h\u001b[6n"]
[2.4703515,"o","\u001b[6n"]
[2.4736586,"o","\u001b[?25l\u001b[21;1H\u001b[21;1H\u001b[J\u001b[38;5;10m\u001b]133;A\u001b\\> \u001b]133;B\u001b\\\u001b[38;5;14m\u001b[38;5;5m\u001b7\u001b[21;84H12/16/2023 03:32:15 PM\u001b8\u001b[0m\u001b[0m\u001b7\u001b8\u001b[?25h"]
```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

None user facing changes other than that prompt markers are working

# 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.
-->
2023-12-16 20:12:34 -06:00
247c33b6d6 Satisfy clippy lint in benchmark (#11350)
Fixes the two `clippy::let_and_return` lints in `benchmark.rs`.
2023-12-16 21:06:42 +01:00
a6da8ce769 Allow filesize type as a valid limit value (#11349)
# Description
This pr allow us to use `filesize` type as a valid limit value, which is
benefit for some file size based limits.

# User-Facing Changes
```console
/data/source/nushell> ulimit -f                                                                                                   
╭───┬─────────────────────────────────────────────────────┬───────────┬───────────╮
│ # │                     description                     │   soft    │   hard    │
├───┼─────────────────────────────────────────────────────┼───────────┼───────────┤
│ 0 │ Maximum size of files created by the shell (kB, -f) │ unlimited │ unlimited │
╰───┴─────────────────────────────────────────────────────┴───────────┴───────────╯
/data/source/nushell> ulimit -f 10Mib                                                                                           
/data/source/nushell> ulimit -f                                                                                                    
╭───┬─────────────────────────────────────────────────────┬───────┬───────╮
│ # │                     description                     │ soft  │ hard  │
├───┼─────────────────────────────────────────────────────┼───────┼───────┤
│ 0 │ Maximum size of files created by the shell (kB, -f) │ 10240 │ 10240 │
╰───┴─────────────────────────────────────────────────────┴───────┴───────╯
/data/source/nushell> ulimit -n                                                                                                 
╭───┬──────────────────────────────────────────────┬──────┬────────╮
│ # │                 description                  │ soft │  hard  │
├───┼──────────────────────────────────────────────┼──────┼────────┤
│ 0 │ Maximum number of open file descriptors (-n) │ 1024 │ 524288 │
╰───┴──────────────────────────────────────────────┴──────┴────────╯
/data/source/nushell> ulimit -n 10Mib                                                                                            
Error: nu:🐚:type_mismatch

  × Type mismatch.
   ╭─[entry #5:1:1]
 1 │ ulimit -n 10Mib
   ·             ─┬─
   ·              ╰── filesize is not compatible with resource RLIMIT_NOFILE
   ╰────
```

# Tests + Formatting
Make sure you've run and fixed any issues with these commands:

- [x] add `commands::ulimit::limit_set_filesize1`
- [x] add `commands::ulimit::limit_set_filesize2`
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
2023-12-16 09:56:03 -06:00
020e121391 Bubble up errors passed to complete (#11313)
Errors passed in `PipelineData::Value` get thrown in `complete` now.

Also added two simple tests for the command.

Fix #11187
Fix #10204
2023-12-16 09:07:08 -06:00
7d5bd0d6be Allow int type as a valid limit value (#11346)
# Description
This PR allows `int` type as a valid limit value for `ulimit`, so there
is no need to use `into string` to convert limit values in the tests.

# User-Facing Changes
N/A

# Tests + Formatting
Make sure you've run and fixed any issues with these commands:
- [x] add `commands::ulimit::limit_set_invalid3`
- [x] add `commands::ulimit::limit_set_invalid4`
- [x] add `commands::ulimit::limit_set_invalid5`
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
2023-12-16 08:55:44 -06:00
87717b9ddd Don't redraw prompt when transient prompt disabled (#10788)
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

moonlander pointed out in Discord that the transient prompt feature
added in release 0.86 (implemented in #10391) is causing the normal
prompt to be redrawn when the transient prompt variables are unset or
set to null. This PR is for fixing that, although it's more of a bandaid
fix. Maybe the transient prompt feature should be taken out entirely for
now so more thought can be given to its implementation.

Previously, I'd thought that when reedline redraws the prompt after a
command is entered, it's a whole new prompt, but apparently it's
actually the same prompt as the current line (?). So now, `nu_prompt` in
`repl.rs` is an `Arc<RwLock<NushellPrompt>>` (rather than just a
`NushellPrompt`), and this `Arc` is shared with the `TransientPrompt`
object so that if it can't find one of the `TRANSIENT_PROMPT_*`
variables, it uses a segment from `NushellPrompt` rather than
re-evaluate `PROMPT_COMMAND`, `PROMPT_COMMAND_RIGHT`, etc. Using an
`RwLock` means that there's a bunch of `.expect()`s all over the place,
which is not nice. It could perhaps be avoided with some changes on the
reedline side.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

`$env.PROMPT_COMMAND` (and other such variables) should no longer be
executed twice if the corresponding `$env.TRANSIENT_PROMPT_*` variable
is not set.

<!--
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
> ```
-->

# Steps to reproduce

Described by moonlander in Discord
[here](https://discord.com/channels/601130461678272522/614593951969574961/1164928022126792844).

Adding this to `env.nu` will result in `11` being added to
`/tmp/run_count` every time any command is run. The expected behavior is
a single `1` being added to `/tmp/run_count` instead of two. The prompt
command should not be executed again when the prompt is redrawn after a
command is executed.

```nu
$env.PROMPT_COMMAND = {||
  touch /tmp/run_count
  '1' | save /tmp/run_count --append
  '>'
}

# $env.TRANSIENT_PROMPT_COMMAND not set
```

If the following is added to `env.nu`, then `12` will be added to
`/tmp/run_count` every time any command is run, which is expected
behavior because the normal prompt command must be displayed the first
time the prompt is shown, then the transient prompt command is run when
the prompt is redrawn.
```nu
$env.TRANSIENT_PROMPT_COMMAND = {||
  touch /tmp/run_count
  '2' | save /tmp/run_count --append
  '>'
}
```

Here's a screenshot of what adding that first snippet looks like (`cargo
run` in the `main` branch):

![image](https://github.com/nushell/nushell/assets/45539777/b27a5c07-55b4-43c7-8a2c-0deba2d9d53a)


Here's a screenshot of what it looks like with this PR (only one `1` is
added to `/tmp/run_count` each time):

![image](https://github.com/nushell/nushell/assets/45539777/2b5c0a3a-8566-4428-9fda-1ffcc1dd6ae3)
2023-12-15 13:41:44 -06:00
3c6fac059e Properly update to the reedline repo patch (#11342)
Follow up to #11339
2023-12-15 20:16:36 +01:00
9092fc1b12 Revert "Only run $env.PROMPT_COMMAND once per prompt" (#11340)
Reverts nushell/nushell#10986

@CAD97 This isn't working right. I have a 2 line prompt with a transient
prompt. on enter, you see the transient prompt drawn and then the normal
prompt overwrites it.
2023-12-15 11:58:32 -06:00
5b557a888e update reedline to latest + include PR 675 for testing (#11339)
# Description

This PR updates to the latest reedline main branch which includes
reedline 27.1 and reedline PR
https://github.com/nushell/reedline/pull/675 for better resize behavior.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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.
-->
2023-12-15 11:48:54 -06:00
398b756aee Make hover equal to help command (#11320)
# Description
Hi! A few days ago I changed the hover from `--ide-lsp` to match `help`
#11284, now this PR is doing the same but for the new `--lsp` server

I also did some tiny fixes to syntax, with some clippy `pedantic` lints

# User-Facing Changes

![image](https://github.com/nushell/nushell/assets/30557287/0e167dc8-777a-4961-8746-aa29f18eccfa)


# Tests + Formatting
 ran `toolkit check pr`
2023-12-15 11:39:19 -06:00
533c1a89af Only run $env.PROMPT_COMMAND once per prompt (#10986)
# Description

If `$env.TRANSIENT_PROMPT_COMMAND` is not set, use the prompt created by
`$env.PROMPT_COMMAND` instead of running the command a second time. As a
side effect, `$env.TRANSIENT_PROMPT_COMMAND` now runs after the hooks
`pre_prompt` and `env_change`, instead of before.

# User-Facing Changes

- `$env.PROMPT_COMMAND` gets run only once per prompt instead of twice
- `$env.TRANSIENT_PROMPT_COMMAND` now sees any environment set in a
`pre_prompt` or `env_change` hook, like `$env.PROMPT_COMMAND` does
2023-12-15 07:56:29 -06:00
84742275a1 Add ulimit command (#11324)
# Description
Add `ulimit` command to Nushell.

Closes #9563
Closes #3976

Related pr #11246

Reference:
https://github.com/fish-shell/fish-shell/blob/master/fish-rust/src/builtins/ulimit.rs
https://github.com/mirror/busybox/blob/master/shell/shell_common.c#L529

# User-Facing Changes
```
nushell on  ulimit is 📦 v0.88.2 via 🦀 v1.72.1                                                                                                [3/246]
❯ ulimit -a
╭────┬──────────────────────────────────────────────────────────────────────────┬───────────┬───────────╮
│  # │                               description                                │   soft    │   hard    │
├────┼──────────────────────────────────────────────────────────────────────────┼───────────┼───────────┤
│  0 │ Maximum size of core files created                              (kB, -c) │ unlimited │ unlimited │
│  1 │ Maximum size of a process's data segment                        (kB, -d) │ unlimited │ unlimited │
│  2 │ Controls of maximum nice priority                                   (-e) │         0 │         0 │
│  3 │ Maximum size of files created by the shell                      (kB, -f) │ unlimited │ unlimited │
│  4 │ Maximum number of pending signals                                   (-i) │     55273 │     55273 │
│  5 │ Maximum size that may be locked into memory                     (kB, -l) │      8192 │      8192 │
│  6 │ Maximum resident set size                                       (kB, -m) │ unlimited │ unlimited │
│  7 │ Maximum number of open file descriptors                             (-n) │      1024 │    524288 │
│  8 │ Maximum bytes in POSIX message queues                           (kB, -q) │       800 │       800 │
│  9 │ Maximum realtime scheduling priority                                (-r) │         0 │         0 │
│ 10 │ Maximum stack size                                              (kB, -s) │      8192 │ unlimited │
│ 11 │ Maximum amount of CPU time in seconds                      (seconds, -t) │ unlimited │ unlimited │
│ 12 │ Maximum number of processes available to the current user           (-u) │     55273 │     55273 │
│ 13 │ Maximum amount of virtual memory available to each process      (kB, -v) │ unlimited │ unlimited │
│ 14 │ Maximum number of file locks                                        (-x) │ unlimited │ unlimited │
│ 15 │ Maximum contiguous realtime CPU time                                (-y) │ unlimited │ unlimited │
╰────┴──────────────────────────────────────────────────────────────────────────┴───────────┴───────────╯
nushell on  ulimit is 📦 v0.88.2 via 🦀 v1.72.1
❯ ulimit -s
╭───┬─────────────────────────────┬──────┬───────────╮
│ # │         description         │ soft │   hard    │
├───┼─────────────────────────────┼──────┼───────────┤
│ 0 │ Maximum stack size (kB, -s) │ 8192 │ unlimited │
╰───┴─────────────────────────────┴──────┴───────────╯
nushell on  ulimit is 📦 v0.88.2 via 🦀 v1.72.1
❯ ulimit -s 100
nushell on  ulimit is 📦 v0.88.2 via 🦀 v1.72.1
❯ ulimit -s
╭───┬─────────────────────────────┬──────┬──────╮
│ # │         description         │ soft │ hard │
├───┼─────────────────────────────┼──────┼──────┤
│ 0 │ Maximum stack size (kB, -s) │  100 │  100 │
╰───┴─────────────────────────────┴──────┴──────╯
nushell on  ulimit is 📦 v0.88.2 via 🦀 v1.72.1
```

# Tests + Formatting
- [x] add commands::ulimit::limit_set_soft1
- [x] add commands::ulimit::limit_set_soft2
- [x] add commands::ulimit::limit_set_hard1
- [x] add commands::ulimit::limit_set_hard2
- [x] add commands::ulimit::limit_set_invalid1
- [x] add commands::ulimit::limit_set_invalid2
- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
2023-12-15 07:11:17 -06:00
92d968b8c8 ♻️ Match --ide-hover with help command (#11284)
# Description
Hi! @fdncred pointed that the `help` command doesn't give the same
result as hovering a command in the VS Code extension. I digged out that
this trace back from `src/ide.rs`, so I decided to change this: (look at
the window of VS Code when hovering help)

![image](https://github.com/nushell/nushell/assets/30557287/32e24090-9238-423e-88ba-7dd6eb53d885)

into this:

![image](https://github.com/nushell/nushell/assets/30557287/51457cf7-4baf-4220-b901-66a78f7ee817)


# User-Facing Changes
The `--ide-hover` change a lot, by matching the `help` command of the
terminal nushell

The only missing part is the `subcommands` part

# Tests + Formatting
All good!

# After Submitting
2023-12-15 07:00:08 -06:00
147 changed files with 3149 additions and 1704 deletions

1
.github/.typos.toml vendored
View File

@ -12,3 +12,4 @@ IIF = "IIF"
numer = "numer"
ratatui = "ratatui"
doas = "doas"
wheres = "wheres"

View File

@ -92,6 +92,17 @@ jobs:
- name: Tests
run: cargo test --workspace --profile ci --exclude nu_plugin_* ${{ matrix.flags }}
- name: Check for clean repo
shell: bash
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
git status --porcelain
exit 1
else
echo "no changes in working directory";
fi
std-lib-and-python-virtualenv:
strategy:
fail-fast: true
@ -129,6 +140,17 @@ jobs:
run: nu scripts/test_virtualenv.nu
shell: bash
- name: Check for clean repo
shell: bash
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
git status --porcelain
exit 1
else
echo "no changes in working directory";
fi
plugins:
strategy:
fail-fast: true
@ -150,3 +172,14 @@ jobs:
- name: Tests
run: cargo test --profile ci --package nu_plugin_*
- name: Check for clean repo
shell: bash
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
git status --porcelain
exit 1
else
echo "no changes in working directory";
fi

View File

@ -119,7 +119,7 @@ jobs:
os: ubuntu-20.04
target_rustflags: ''
- target: riscv64gc-unknown-linux-gnu
os: ubuntu-20.04
os: ubuntu-latest
target_rustflags: ''
runs-on: ${{matrix.os}}

View File

@ -82,8 +82,8 @@ print $'Start building ($bin)...'; hr-line
# ----------------------------------------------------------------------------
# Build for Ubuntu and macOS
# ----------------------------------------------------------------------------
if $os in [$USE_UBUNTU, 'macos-latest'] {
if $os == $USE_UBUNTU {
if $os in [$USE_UBUNTU, 'macos-latest', 'ubuntu-latest'] {
if $os starts-with ubuntu {
sudo apt update
sudo apt-get install libxcb-composite0-dev -y
}
@ -106,7 +106,7 @@ if $os in [$USE_UBUNTU, 'macos-latest'] {
_ => {
# musl-tools to fix 'Failed to find tool. Is `musl-gcc` installed?'
# Actually just for x86_64-unknown-linux-musl target
if $os == $USE_UBUNTU { sudo apt install musl-tools -y }
if $os starts-with ubuntu { sudo apt install musl-tools -y }
cargo-build-nu $flags
}
}
@ -153,7 +153,7 @@ if ($ver | str trim | is-empty) {
# Create a release archive and send it to output for the following steps
# ----------------------------------------------------------------------------
cd $dist; print $'(char nl)Creating release archive...'; hr-line
if $os in [$USE_UBUNTU, 'macos-latest'] {
if $os in [$USE_UBUNTU, 'macos-latest', 'ubuntu-latest'] {
let files = (ls | get name)
let dest = if $env.RELEASE_TYPE == 'full' { $'($bin)-($version)-($FULL_NAME)' } else { $'($bin)-($version)-($target)' }

View File

@ -66,7 +66,7 @@ jobs:
os: ubuntu-20.04
target_rustflags: ''
- target: riscv64gc-unknown-linux-gnu
os: ubuntu-20.04
os: ubuntu-latest
target_rustflags: ''
runs-on: ${{matrix.os}}

View File

@ -10,6 +10,6 @@ jobs:
uses: actions/checkout@v4
- name: Check spelling
uses: crate-ci/typos@v1.16.24
uses: crate-ci/typos@v1.17.0
with:
config: ./.github/.typos.toml

179
Cargo.lock generated
View File

@ -1252,16 +1252,6 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "fancy-regex"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b95f7c0680e4142284cf8b22c14a476e87d61b004a3a0861872b32ef7ead40a2"
dependencies = [
"bit-set",
"regex",
]
[[package]]
name = "fancy-regex"
version = "0.12.0"
@ -1291,7 +1281,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef033ed5e9bad94e55838ca0ca906db0e043f517adda0c8b79c7a8c66c93c1b5"
dependencies = [
"cfg-if",
"rustix 0.38.28",
"rustix",
"windows-sys 0.48.0",
]
@ -1798,7 +1788,7 @@ dependencies = [
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
"windows-core 0.51.1",
]
[[package]]
@ -1812,9 +1802,9 @@ dependencies = [
[[package]]
name = "ical"
version = "0.8.0"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "356d82bd58997815d55ea6f9081bd4cac149e50ca943f7a4f7c050fec7271c1f"
checksum = "26393c372d4c4d51616084afe36c0b44e4467febaa6f91f11f789094b4863bf9"
dependencies = [
"thiserror",
]
@ -1894,17 +1884,6 @@ version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0508c56cfe9bfd5dfeb0c22ab9a6abfda2f27bdca422132e494266351ed8d83c"
[[package]]
name = "io-lifetimes"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "is-docker"
version = "0.2.0"
@ -1921,7 +1900,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi",
"rustix 0.38.28",
"rustix",
"windows-sys 0.48.0",
]
@ -1974,6 +1953,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.10"
@ -2234,12 +2222,6 @@ dependencies = [
"serde",
]
[[package]]
name = "linux-raw-sys"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
[[package]]
name = "linux-raw-sys"
version = "0.4.12"
@ -2273,9 +2255,9 @@ dependencies = [
[[package]]
name = "lscolors"
version = "0.15.0"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf7015a04103ad78abb77e4b79ed151e767922d1cfde5f62640471c629a2320d"
checksum = "ab0b209ec3976527806024406fe765474b9a1750a0ed4b8f0372364741f50e7b"
dependencies = [
"nu-ansi-term",
]
@ -2294,9 +2276,9 @@ dependencies = [
[[package]]
name = "lsp-types"
version = "0.94.1"
version = "0.95.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c66bfd44a06ae10647fe3f8214762e9369fd4248df1350924b4ef9e770a85ea1"
checksum = "158c1911354ef73e8fe42da6b10c0484cb65c7f1007f28022e847706c1ab6984"
dependencies = [
"bitflags 1.3.2",
"serde",
@ -2677,7 +2659,7 @@ version = "0.88.2"
dependencies = [
"chrono",
"crossterm",
"fancy-regex 0.11.0",
"fancy-regex",
"fuzzy-matcher",
"is_executable",
"log",
@ -2698,7 +2680,7 @@ dependencies = [
"percent-encoding",
"reedline",
"rstest",
"sysinfo",
"sysinfo 0.30.4",
"unicode-segmentation",
"uuid",
"which 5.0.0",
@ -2726,7 +2708,7 @@ version = "0.88.2"
dependencies = [
"chrono",
"chrono-tz",
"fancy-regex 0.12.0",
"fancy-regex",
"indexmap",
"nu-cmd-lang",
"nu-engine",
@ -2748,7 +2730,7 @@ name = "nu-cmd-extra"
version = "0.88.2"
dependencies = [
"ahash",
"fancy-regex 0.11.0",
"fancy-regex",
"heck",
"htmlescape",
"nu-ansi-term",
@ -2772,8 +2754,8 @@ dependencies = [
name = "nu-cmd-lang"
version = "0.88.2"
dependencies = [
"fancy-regex 0.11.0",
"itertools 0.11.0",
"fancy-regex",
"itertools 0.12.0",
"nu-ansi-term",
"nu-engine",
"nu-parser",
@ -2816,7 +2798,7 @@ dependencies = [
"dirs-next",
"dtparse",
"encoding_rs",
"fancy-regex 0.11.0",
"fancy-regex",
"filesize",
"filetime",
"fs_extra",
@ -2824,7 +2806,7 @@ dependencies = [
"human-date-parser",
"indexmap",
"indicatif",
"itertools 0.11.0",
"itertools 0.12.0",
"libc",
"log",
"lscolors",
@ -2877,7 +2859,7 @@ dependencies = [
"serde_urlencoded",
"serde_yaml",
"sha2",
"sysinfo",
"sysinfo 0.30.4",
"tabled",
"terminal_size 0.3.0",
"titlecase",
@ -2894,7 +2876,7 @@ dependencies = [
"uuid",
"wax",
"which 5.0.0",
"windows 0.48.0",
"windows 0.52.0",
"winreg",
]
@ -2925,7 +2907,7 @@ dependencies = [
"nu-utils",
"ratatui",
"strip-ansi-escapes",
"terminal_size 0.2.6",
"terminal_size 0.3.0",
"unicode-width",
]
@ -2973,7 +2955,7 @@ version = "0.88.2"
dependencies = [
"bytesize",
"chrono",
"itertools 0.11.0",
"itertools 0.12.0",
"log",
"nu-engine",
"nu-path",
@ -3020,7 +3002,7 @@ dependencies = [
"byte-unit",
"chrono",
"chrono-humanize",
"fancy-regex 0.11.0",
"fancy-regex",
"indexmap",
"lru",
"miette",
@ -3061,15 +3043,15 @@ dependencies = [
"ntapi",
"once_cell",
"procfs",
"sysinfo",
"winapi",
"sysinfo 0.30.4",
"windows 0.52.0",
]
[[package]]
name = "nu-table"
version = "0.88.2"
dependencies = [
"fancy-regex 0.11.0",
"fancy-regex",
"nu-ansi-term",
"nu-color-config",
"nu-engine",
@ -3414,9 +3396,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-src"
version = "300.2.1+3.2.0"
version = "300.1.6+3.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fe476c29791a5ca0d1273c697e96085bbabbbea2ef7afd5617e78a4b40332d3"
checksum = "439fac53e092cd7442a3660c85dde4643ab3b5bd39040912388dcdabf6b88085"
dependencies = [
"cc",
]
@ -4090,7 +4072,7 @@ dependencies = [
"polars-error",
"rayon",
"smartstring",
"sysinfo",
"sysinfo 0.29.11",
"version_check",
]
@ -4195,7 +4177,7 @@ dependencies = [
"hex",
"lazy_static",
"procfs-core",
"rustix 0.38.28",
"rustix",
]
[[package]]
@ -4392,14 +4374,13 @@ dependencies = [
[[package]]
name = "reedline"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147452ce32c2cba4900b410f1d18b9ca29b67301a8eed76676e42e720d71cc39"
version = "0.27.1"
source = "git+https://github.com/nushell/reedline.git?branch=main#b68ce33c750b700bb4f8adccbc2e160d1b7c8742"
dependencies = [
"chrono",
"crossterm",
"fd-lock",
"itertools 0.10.5",
"itertools 0.12.0",
"nu-ansi-term",
"rusqlite",
"serde",
@ -4646,20 +4627,6 @@ dependencies = [
"semver",
]
[[package]]
name = "rustix"
version = "0.37.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2"
dependencies = [
"bitflags 1.3.2",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys 0.3.8",
"windows-sys 0.48.0",
]
[[package]]
name = "rustix"
version = "0.38.28"
@ -4669,7 +4636,7 @@ dependencies = [
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys 0.4.12",
"linux-raw-sys",
"windows-sys 0.52.0",
]
@ -4901,9 +4868,9 @@ dependencies = [
[[package]]
name = "shadow-rs"
version = "0.24.1"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9198caff1c94f1a5df6664bddbc379896b51b98a55b0b3fedcb23078fe00c77"
checksum = "878cb1e3162d98ee1016b832efbb683ad6302b462a2894c54f488dc0bd96f11c"
dependencies = [
"const_format",
"is_debug",
@ -5282,10 +5249,24 @@ dependencies = [
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]]
name = "sysinfo"
version = "0.30.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "717570a2533606f81f8cfac02a1915a620e725ffb78f6fc5e259769a4d747407"
dependencies = [
"cfg-if",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"windows 0.52.0",
]
[[package]]
name = "tabled"
version = "0.14.0"
@ -5313,7 +5294,7 @@ dependencies = [
"cfg-if",
"fastrand",
"redox_syscall",
"rustix 0.38.28",
"rustix",
"windows-sys 0.48.0",
]
@ -5347,23 +5328,13 @@ dependencies = [
"winapi",
]
[[package]]
name = "terminal_size"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e6bf6f19e9f8ed8d4048dc22981458ebcf406d67e94cd422e5ecd73d63b3237"
dependencies = [
"rustix 0.37.27",
"windows-sys 0.48.0",
]
[[package]]
name = "terminal_size"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
dependencies = [
"rustix 0.38.28",
"rustix",
"windows-sys 0.48.0",
]
@ -5747,9 +5718,9 @@ checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "unsafe-libyaml"
version = "0.2.9"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa"
checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b"
[[package]]
name = "ureq"
@ -6071,7 +6042,7 @@ dependencies = [
"either",
"home",
"once_cell",
"rustix 0.38.28",
"rustix",
]
[[package]]
@ -6083,7 +6054,7 @@ dependencies = [
"either",
"home",
"once_cell",
"rustix 0.38.28",
"rustix",
"windows-sys 0.48.0",
]
@ -6138,11 +6109,12 @@ dependencies = [
[[package]]
name = "windows"
version = "0.48.0"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be"
dependencies = [
"windows-targets 0.48.5",
"windows-core 0.52.0",
"windows-targets 0.52.0",
]
[[package]]
@ -6154,6 +6126,15 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-sys"
version = "0.45.0"
@ -6388,8 +6369,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7dae5072fe1f8db8f8d29059189ac175196e410e40ba42d5d4684ae2f750995"
dependencies = [
"libc",
"linux-raw-sys 0.4.12",
"rustix 0.38.28",
"linux-raw-sys",
"rustix",
]
[[package]]

View File

@ -33,7 +33,11 @@ members = [
"crates/nu-cmd-lang",
"crates/nu-cmd-dataframe",
"crates/nu-command",
"crates/nu-color-config",
"crates/nu-explore",
"crates/nu-json",
"crates/nu-lsp",
"crates/nu-pretty-hex",
"crates/nu-protocol",
"crates/nu-plugin",
"crates/nu_plugin_inc",
@ -43,6 +47,8 @@ members = [
"crates/nu_plugin_custom_values",
"crates/nu_plugin_formats",
"crates/nu-std",
"crates/nu-table",
"crates/nu-term-grid",
"crates/nu-utils",
]
@ -68,6 +74,7 @@ nu-table = { path = "./crates/nu-table", version = "0.88.2" }
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.88.2" }
nu-std = { path = "./crates/nu-std", version = "0.88.2" }
nu-utils = { path = "./crates/nu-utils", version = "0.88.2" }
nu-ansi-term = "0.49.0"
reedline = { version = "0.27.0", features = ["bashisms", "sqlite"] }
@ -166,7 +173,7 @@ bench = false
# To use a development version of a dependency please use a global override here
# changing versions in each sub-crate of the workspace is tedious
[patch.crates-io]
# reedline = { git = "https://github.com/nushell/reedline.git", branch = "main" }
reedline = { git = "https://github.com/nushell/reedline.git", branch = "main" }
# nu-ansi-term = {git = "https://github.com/nushell/nu-ansi-term.git", branch = "main"}
# uu_cp = { git = "https://github.com/uutils/coreutils.git", branch = "main" }

View File

@ -24,13 +24,9 @@ fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
}
fn get_home_path(engine_state: &EngineState) -> PathBuf {
let home_path = if let Some(path) = nu_path::home_dir() {
let canon_home_path = canonicalize_path(engine_state, &path);
canon_home_path
} else {
std::path::PathBuf::new()
};
home_path
nu_path::home_dir()
.map(|path| canonicalize_path(engine_state, &path))
.unwrap_or_default()
}
// FIXME: All benchmarks live in this 1 file to speed up build times when benchmarking.

View File

@ -29,7 +29,7 @@ reedline = { version = "0.27.0", features = ["bashisms", "sqlite"] }
chrono = { default-features = false, features = ["std"], version = "0.4" }
crossterm = "0.27"
fancy-regex = "0.11"
fancy-regex = "0.12"
fuzzy-matcher = "0.3"
is_executable = "1.0"
log = "0.4"
@ -37,7 +37,7 @@ miette = { version = "5.10", features = ["fancy-no-backtrace"] }
once_cell = "1.18"
percent-encoding = "2"
pathdiff = "0.2"
sysinfo = "0.29"
sysinfo = "0.30"
unicode-segmentation = "1.10"
uuid = { version = "1.6.0", features = ["v4"] }
which = "5.0.0"

View File

@ -250,7 +250,9 @@ impl NuCompleter {
working_set.get_span_contents(previous_expr.0).to_vec();
// Completion for .nu files
if prev_expr_str == b"use" || prev_expr_str == b"source-env"
if prev_expr_str == b"use"
|| prev_expr_str == b"overlay use"
|| prev_expr_str == b"source-env"
{
let mut completer =
DotNuCompletion::new(self.engine_state.clone());
@ -352,9 +354,7 @@ impl NuCompleter {
if let Some(external_result) = self.external_completion(
block_id, &spans, offset, new_span,
) {
if !external_result.is_empty() {
return external_result;
}
return external_result;
}
}

View File

@ -22,7 +22,10 @@ fn complete_rec(
Some(base) if matches(base, &entry_name, options) => {
let partial = &partial[1..];
if !partial.is_empty() || isdir {
completions.extend(complete_rec(partial, &path, options, dir, isdir))
completions.extend(complete_rec(partial, &path, options, dir, isdir));
if entry_name.eq(base) {
break;
}
} else {
completions.push(path)
}

View File

@ -5,7 +5,7 @@ use nu_protocol::{
};
use reedline::Suggestion;
use std::{
path::{is_separator, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR},
path::{is_separator, Path, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR},
sync::Arc,
};
@ -91,16 +91,21 @@ impl Completer for DotNuCompletion {
// and transform them into suggestions
let output: Vec<Suggestion> = search_dirs
.into_iter()
.flat_map(|it| {
file_path_completion(span, &partial, &it, options)
.flat_map(|search_dir| {
let completions = file_path_completion(span, &partial, &search_dir, options);
completions
.into_iter()
.filter(|it| {
.filter(move |it| {
// Different base dir, so we list the .nu files or folders
if !is_current_folder {
it.1.ends_with(".nu") || it.1.ends_with(SEP)
} else {
// Lib dirs, so we filter only the .nu files
it.1.ends_with(".nu")
// Lib dirs, so we filter only the .nu files or directory modules
if it.1.ends_with(SEP) {
Path::new(&search_dir).join(&it.1).join("mod.nu").exists()
} else {
it.1.ends_with(".nu")
}
}
})
.map(move |x| Suggestion {

View File

@ -35,6 +35,10 @@ pub fn evaluate_commands(
let mut working_set = StateWorkingSet::new(engine_state);
let output = parse(&mut working_set, None, commands.item.as_bytes(), false);
if let Some(warning) = working_set.parse_warnings.first() {
report_error(&working_set, warning);
}
if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);

View File

@ -1,3 +1,4 @@
use crate::prompt_update::{POST_PROMPT_MARKER, PRE_PROMPT_MARKER};
#[cfg(windows)]
use nu_utils::enable_vt_processing;
use reedline::DefaultPrompt;
@ -11,6 +12,7 @@ use {
/// Nushell prompt definition
#[derive(Clone)]
pub struct NushellPrompt {
shell_integration: bool,
left_prompt_string: Option<String>,
right_prompt_string: Option<String>,
default_prompt_indicator: Option<String>,
@ -20,15 +22,10 @@ pub struct NushellPrompt {
render_right_prompt_on_last_line: bool,
}
impl Default for NushellPrompt {
fn default() -> Self {
NushellPrompt::new()
}
}
impl NushellPrompt {
pub fn new() -> NushellPrompt {
pub fn new(shell_integration: bool) -> NushellPrompt {
NushellPrompt {
shell_integration,
left_prompt_string: None,
right_prompt_string: None,
default_prompt_indicator: None,
@ -111,7 +108,11 @@ impl Prompt for NushellPrompt {
.to_string()
.replace('\n', "\r\n");
prompt.into()
if self.shell_integration {
format!("{PRE_PROMPT_MARKER}{prompt}{POST_PROMPT_MARKER}").into()
} else {
prompt.into()
}
}
}

View File

@ -7,8 +7,6 @@ use nu_protocol::{
Config, PipelineData, Value,
};
use reedline::Prompt;
use std::borrow::Cow;
use std::sync::Arc;
// Name of environment variable where the prompt could be stored
pub(crate) const PROMPT_COMMAND: &str = "PROMPT_COMMAND";
@ -28,8 +26,8 @@ pub(crate) const TRANSIENT_PROMPT_MULTILINE_INDICATOR: &str =
"TRANSIENT_PROMPT_MULTILINE_INDICATOR";
// According to Daniel Imms @Tyriar, we need to do these this way:
// <133 A><prompt><133 B><command><133 C><command output>
const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
pub(crate) const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
pub(crate) const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
fn get_prompt_string(
prompt: &str,
@ -98,12 +96,12 @@ fn get_prompt_string(
})
}
pub(crate) fn update_prompt<'prompt>(
pub(crate) fn update_prompt(
config: &Config,
engine_state: &EngineState,
stack: &Stack,
nu_prompt: &'prompt mut NushellPrompt,
) -> &'prompt dyn Prompt {
nu_prompt: &mut NushellPrompt,
) {
let mut stack = stack.clone();
let left_prompt_string = get_prompt_string(PROMPT_COMMAND, config, engine_state, &mut stack);
@ -146,125 +144,55 @@ pub(crate) fn update_prompt<'prompt>(
(prompt_vi_insert_string, prompt_vi_normal_string),
config.render_right_prompt_on_last_line,
);
let ret_val = nu_prompt as &dyn Prompt;
trace!("update_prompt {}:{}:{}", file!(), line!(), column!());
ret_val
}
struct TransientPrompt {
engine_state: Arc<EngineState>,
stack: Stack,
}
/// Try getting `$env.TRANSIENT_PROMPT_<X>`, and get `$env.PROMPT_<X>` if that fails
fn get_transient_prompt_string(
transient_prompt: &str,
prompt: &str,
/// Construct the transient prompt based on the normal nu_prompt
pub(crate) fn make_transient_prompt(
config: &Config,
engine_state: &EngineState,
stack: &mut Stack,
) -> Option<String> {
get_prompt_string(transient_prompt, config, engine_state, stack)
.or_else(|| get_prompt_string(prompt, config, engine_state, stack))
}
nu_prompt: &NushellPrompt,
) -> Box<dyn Prompt> {
let mut nu_prompt = nu_prompt.clone();
impl Prompt for TransientPrompt {
fn render_prompt_left(&self) -> Cow<str> {
let mut nu_prompt = NushellPrompt::new();
let config = &self.engine_state.get_config().clone();
let mut stack = self.stack.clone();
nu_prompt.update_prompt_left(get_transient_prompt_string(
TRANSIENT_PROMPT_COMMAND,
PROMPT_COMMAND,
config,
&self.engine_state,
&mut stack,
));
nu_prompt.render_prompt_left().to_string().into()
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_COMMAND, config, engine_state, stack) {
nu_prompt.update_prompt_left(Some(s))
}
fn render_prompt_right(&self) -> Cow<str> {
let mut nu_prompt = NushellPrompt::new();
let config = &self.engine_state.get_config().clone();
let mut stack = self.stack.clone();
nu_prompt.update_prompt_right(
get_transient_prompt_string(
TRANSIENT_PROMPT_COMMAND_RIGHT,
PROMPT_COMMAND_RIGHT,
config,
&self.engine_state,
&mut stack,
),
config.render_right_prompt_on_last_line,
);
nu_prompt.render_prompt_right().to_string().into()
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_COMMAND_RIGHT, config, engine_state, stack)
{
nu_prompt.update_prompt_right(Some(s), config.render_right_prompt_on_last_line)
}
fn render_prompt_indicator(&self, prompt_mode: reedline::PromptEditMode) -> Cow<str> {
let mut nu_prompt = NushellPrompt::new();
let config = &self.engine_state.get_config().clone();
let mut stack = self.stack.clone();
nu_prompt.update_prompt_indicator(get_transient_prompt_string(
TRANSIENT_PROMPT_INDICATOR,
PROMPT_INDICATOR,
config,
&self.engine_state,
&mut stack,
));
nu_prompt.update_prompt_vi_insert(get_transient_prompt_string(
TRANSIENT_PROMPT_INDICATOR_VI_INSERT,
PROMPT_INDICATOR_VI_INSERT,
config,
&self.engine_state,
&mut stack,
));
nu_prompt.update_prompt_vi_normal(get_transient_prompt_string(
TRANSIENT_PROMPT_INDICATOR_VI_NORMAL,
PROMPT_INDICATOR_VI_NORMAL,
config,
&self.engine_state,
&mut stack,
));
nu_prompt
.render_prompt_indicator(prompt_mode)
.to_string()
.into()
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_INDICATOR, config, engine_state, stack) {
nu_prompt.update_prompt_indicator(Some(s))
}
fn render_prompt_multiline_indicator(&self) -> Cow<str> {
let mut nu_prompt = NushellPrompt::new();
let config = &self.engine_state.get_config().clone();
let mut stack = self.stack.clone();
nu_prompt.update_prompt_multiline(get_transient_prompt_string(
TRANSIENT_PROMPT_MULTILINE_INDICATOR,
PROMPT_MULTILINE_INDICATOR,
config,
&self.engine_state,
&mut stack,
));
nu_prompt
.render_prompt_multiline_indicator()
.to_string()
.into()
}
fn render_prompt_history_search_indicator(
&self,
history_search: reedline::PromptHistorySearch,
) -> Cow<str> {
NushellPrompt::new()
.render_prompt_history_search_indicator(history_search)
.to_string()
.into()
}
}
/// Construct the transient prompt
pub(crate) fn transient_prompt(engine_state: Arc<EngineState>, stack: &Stack) -> Box<dyn Prompt> {
Box::new(TransientPrompt {
if let Some(s) = get_prompt_string(
TRANSIENT_PROMPT_INDICATOR_VI_INSERT,
config,
engine_state,
stack: stack.clone(),
})
stack,
) {
nu_prompt.update_prompt_vi_insert(Some(s))
}
if let Some(s) = get_prompt_string(
TRANSIENT_PROMPT_INDICATOR_VI_NORMAL,
config,
engine_state,
stack,
) {
nu_prompt.update_prompt_vi_normal(Some(s))
}
if let Some(s) = get_prompt_string(
TRANSIENT_PROMPT_MULTILINE_INDICATOR,
config,
engine_state,
stack,
) {
nu_prompt.update_prompt_multiline(Some(s))
}
Box::new(nu_prompt)
}

View File

@ -32,7 +32,7 @@ use std::{
sync::atomic::Ordering,
time::Instant,
};
use sysinfo::SystemExt;
use sysinfo::System;
// According to Daniel Imms @Tyriar, we need to do these this way:
// <133 A><prompt><133 B><command><133 C><command output>
@ -54,7 +54,8 @@ pub fn evaluate_repl(
) -> Result<()> {
use nu_cmd_base::hook;
use reedline::Signal;
let use_color = engine_state.get_config().use_ansi_coloring;
let config = engine_state.get_config();
let use_color = config.use_ansi_coloring;
// Guard against invocation without a connected terminal.
// reedline / crossterm event polling will fail without a connected tty
@ -68,7 +69,7 @@ pub fn evaluate_repl(
let mut entry_num = 0;
let mut nu_prompt = NushellPrompt::new();
let mut nu_prompt = NushellPrompt::new(config.shell_integration);
let start_time = std::time::Instant::now();
// Translate environment variables from Strings to Values
@ -134,17 +135,6 @@ pub fn evaluate_repl(
use_color,
);
start_time = std::time::Instant::now();
let sys = sysinfo::System::new();
perf(
"get sysinfo",
start_time,
file!(),
line!(),
column!(),
use_color,
);
if let Some(s) = prerun_command {
eval_source(
engine_state,
@ -212,20 +202,6 @@ pub fn evaluate_repl(
use_color,
);
start_time = std::time::Instant::now();
// Reset the SIGQUIT handler
if let Some(sig_quit) = engine_state.get_sig_quit() {
sig_quit.store(false, Ordering::SeqCst);
}
perf(
"reset sig_quit",
start_time,
file!(),
line!(),
column!(),
use_color,
);
start_time = std::time::Instant::now();
let config = engine_state.get_config();
@ -267,11 +243,7 @@ pub fn evaluate_repl(
.with_quick_completions(config.quick_completions)
.with_partial_completions(config.partial_completions)
.with_ansi_colors(config.use_ansi_coloring)
.with_cursor_config(cursor_config)
.with_transient_prompt(prompt_update::transient_prompt(
engine_reference.clone(),
stack,
));
.with_cursor_config(cursor_config);
perf(
"reedline builder",
start_time,
@ -424,7 +396,9 @@ pub fn evaluate_repl(
start_time = std::time::Instant::now();
let config = &engine_state.get_config().clone();
let prompt = prompt_update::update_prompt(config, engine_state, stack, &mut nu_prompt);
prompt_update::update_prompt(config, engine_state, stack, &mut nu_prompt);
let transient_prompt =
prompt_update::make_transient_prompt(config, engine_state, stack, &nu_prompt);
perf(
"update_prompt",
start_time,
@ -437,12 +411,13 @@ pub fn evaluate_repl(
entry_num += 1;
start_time = std::time::Instant::now();
let input = line_editor.read_line(prompt);
line_editor = line_editor.with_transient_prompt(transient_prompt);
let input = line_editor.read_line(&nu_prompt);
let shell_integration = config.shell_integration;
match input {
Ok(Signal::Success(s)) => {
let hostname = sys.host_name();
let hostname = System::host_name();
let history_supports_meta =
matches!(config.history_file_format, HistoryFileFormat::Sqlite);
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
@ -764,7 +739,7 @@ fn map_nucursorshape_to_cursorshape(shape: NuCursorShape) -> Option<SetCursorSty
}
}
pub fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) -> String {
fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) -> String {
let exit_code = stack
.get_env_var(engine_state, "LAST_EXIT_CODE")
.and_then(|e| e.as_i64().ok());

View File

@ -329,7 +329,6 @@ fn find_matching_block_end_in_expr(
Expr::ImportPattern(_) => None,
Expr::Overlay(_) => None,
Expr::Signature(_) => None,
Expr::MatchPattern(_) => None,
Expr::MatchBlock(_) => None,
Expr::Nothing => None,
Expr::Garbage => None,
@ -386,6 +385,7 @@ fn find_matching_block_end_in_expr(
Argument::Named((_, _, opt_expr)) => opt_expr.as_ref(),
Argument::Positional(inner_expr) => Some(inner_expr),
Argument::Unknown(inner_expr) => Some(inner_expr),
Argument::Spread(inner_expr) => Some(inner_expr),
};
if let Some(inner_expr) = opt_expr {

View File

@ -220,6 +220,10 @@ pub fn eval_source(
source,
false,
);
if let Some(warning) = working_set.parse_warnings.first() {
report_error(&working_set, warning);
}
if let Some(err) = working_set.parse_errors.first() {
set_last_exit_code(stack, 1);
report_error(&working_set, err);

View File

@ -91,7 +91,7 @@ fn variables_dollar_sign_with_varialblecompletion() {
let target_dir = "$ ";
let suggestions = completer.complete(target_dir, target_dir.len());
assert_eq!(7, suggestions.len());
assert_eq!(8, suggestions.len());
}
#[rstest]
@ -144,15 +144,34 @@ fn dotnu_completions() {
let completion_str = "source-env ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(1, suggestions.len());
assert_eq!(2, suggestions.len());
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
#[cfg(windows)]
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
#[cfg(not(windows))]
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
// Test use completion
let completion_str = "use ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(1, suggestions.len());
assert_eq!(2, suggestions.len());
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
#[cfg(windows)]
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
#[cfg(not(windows))]
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
// Test overlay use completion
let completion_str = "overlay use ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(2, suggestions.len());
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
#[cfg(windows)]
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
#[cfg(not(windows))]
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
}
#[test]
@ -208,6 +227,7 @@ fn file_completions() {
let expected_paths: Vec<String> = vec![
folder(dir.join("another")),
file(dir.join("custom_completion.nu")),
folder(dir.join("directory_completion")),
file(dir.join("nushell")),
folder(dir.join("test_a")),
folder(dir.join("test_b")),
@ -323,6 +343,7 @@ fn command_ls_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -333,6 +354,7 @@ fn command_ls_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -355,6 +377,7 @@ fn command_open_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -365,6 +388,7 @@ fn command_open_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -388,6 +412,7 @@ fn command_rm_with_globcompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -398,6 +423,7 @@ fn command_rm_with_globcompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -421,6 +447,7 @@ fn command_cp_with_globcompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -431,6 +458,7 @@ fn command_cp_with_globcompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -454,6 +482,7 @@ fn command_save_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -464,6 +493,7 @@ fn command_save_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -487,6 +517,7 @@ fn command_touch_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -497,6 +528,7 @@ fn command_touch_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -520,6 +552,7 @@ fn command_watch_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -530,6 +563,7 @@ fn command_watch_with_filecompletion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -625,6 +659,7 @@ fn folder_with_directorycompletions() {
// Create the expected values
let expected_paths: Vec<String> = vec![
folder(dir.join("another")),
folder(dir.join("directory_completion")),
folder(dir.join("test_a")),
folder(dir.join("test_b")),
folder(dir.join(".hidden_folder")),
@ -839,6 +874,7 @@ fn unknown_command_completion() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -849,6 +885,7 @@ fn unknown_command_completion() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),
@ -899,6 +936,7 @@ fn filecompletions_triggers_after_cursor() {
let expected_paths: Vec<String> = vec![
"another\\".to_string(),
"custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(),
"test_a\\".to_string(),
"test_b\\".to_string(),
@ -909,6 +947,7 @@ fn filecompletions_triggers_after_cursor() {
let expected_paths: Vec<String> = vec![
"another/".to_string(),
"custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(),
"test_a/".to_string(),
"test_b/".to_string(),

View File

@ -26,9 +26,9 @@ num = { version = "0.4", optional = true }
serde = { version = "1.0", features = ["derive"] }
sqlparser = { version = "0.39", optional = true }
polars-io = { version = "0.35", features = ["avro"], optional = true }
polars-arrow = "0.35"
polars-ops = "0.35"
polars-plan = "0.35"
polars-arrow = { version = "0.35", optional = true }
polars-ops = { version = "0.35", optional = true }
polars-plan = { version = "0.35", optional = true }
[dependencies.polars]
features = [
@ -65,7 +65,7 @@ optional = true
version = "0.35"
[features]
dataframe = ["num", "polars", "polars-io", "sqlparser"]
dataframe = ["num", "polars", "polars-io", "polars-arrow", "polars-ops", "polars-plan", "sqlparser"]
default = []
[dev-dependencies]

View File

@ -874,7 +874,7 @@ fn series_to_values(
.iter()
.map(|field| field.name.to_string())
.collect();
let record = Record { cols, vals: vals? };
let record = Record::from_raw_cols_vals(cols, vals?);
Ok(Value::record(record, span))
})
.collect();
@ -982,10 +982,7 @@ fn any_value_to_value(any_value: &AnyValue, span: Span) -> Result<Value, ShellEr
.map(|f| f.name().to_string())
.collect();
Ok(Value::Record {
val: Record {
cols: fields,
vals: values?,
},
val: Record::from_raw_cols_vals(fields, values?),
internal_span: span,
})
}
@ -1059,6 +1056,7 @@ fn time_from_midnight(nanos: i64, span: Span) -> Result<Value, ShellError> {
#[cfg(test)]
mod tests {
use indexmap::indexmap;
use nu_protocol::record;
use polars::export::arrow::array::{BooleanArray, PrimitiveArray};
use polars::prelude::Field;
use polars_io::prelude::StructArray;
@ -1249,13 +1247,10 @@ mod tests {
Field::new(field_name_1, DataType::Boolean),
];
let test_owned_struct = AnyValue::StructOwned(Box::new((values, fields.clone())));
let comparison_owned_record = Value::record(
Record {
cols: vec![field_name_0.to_owned(), field_name_1.to_owned()],
vals: vec![Value::int(1, span), Value::bool(true, span)],
},
span,
);
let comparison_owned_record = Value::test_record(record!(
field_name_0 => Value::int(1, span),
field_name_1 => Value::bool(true, span),
));
assert_eq!(
any_value_to_value(&test_owned_struct, span)?,
comparison_owned_record.clone()

View File

@ -158,7 +158,10 @@ impl NuDataFrame {
.map(|i| format!("{i}"))
.collect::<Vec<String>>();
conversion::insert_record(&mut column_values, Record { cols, vals })?
conversion::insert_record(
&mut column_values,
Record::from_raw_cols_vals(cols, vals),
)?
}
Value::Record { val: record, .. } => {
conversion::insert_record(&mut column_values, record)?

View File

@ -24,7 +24,7 @@ heck = "0.4.1"
num-traits = "0.2"
ahash = "0.8.3"
nu-ansi-term = "0.49.0"
fancy-regex = "0.11.0"
fancy-regex = "0.12.0"
rust-embed = "8.0.0"
serde = "1.0.164"
nu-pretty-hex = { version = "0.88.2", path = "../nu-pretty-hex" }

View File

@ -65,7 +65,7 @@ pub fn add_extra_command_context(mut engine_state: EngineState) -> EngineState {
bind_command!(platform::ansi::Gradient);
bind_command!(
strings::format::Format,
strings::format::FormatPattern,
strings::encode_decode::EncodeHex,
strings::encode_decode::DecodeHex,
strings::str_::case::Str,

View File

@ -10,15 +10,15 @@ use nu_protocol::{
};
#[derive(Clone)]
pub struct Format;
pub struct FormatPattern;
impl Command for Format {
impl Command for FormatPattern {
fn name(&self) -> &str {
"format"
"format pattern"
}
fn signature(&self) -> Signature {
Signature::build("format")
Signature::build("format pattern")
.input_output_types(vec![
(Type::Table(vec![]), Type::List(Box::new(Type::String))),
(Type::Record(vec![]), Type::Any),
@ -80,12 +80,12 @@ impl Command for Format {
vec![
Example {
description: "Print filenames with their sizes",
example: "ls | format '{name}: {size}'",
example: "ls | format pattern '{name}: {size}'",
result: None,
},
Example {
description: "Print elements from some columns of a table",
example: "[[col1, col2]; [v1, v2] [v3, v4]] | format '{col2}'",
example: "[[col1, col2]; [v1, v2] [v3, v4]] | format pattern '{col2}'",
result: Some(Value::list(
vec![Value::test_string("v2"), Value::test_string("v4")],
Span::test_data(),
@ -318,8 +318,8 @@ fn format_record(
mod test {
#[test]
fn test_examples() {
use super::Format;
use super::FormatPattern;
use crate::test_examples;
test_examples(Format {})
test_examples(FormatPattern {})
}
}

View File

@ -1,3 +1,3 @@
mod command;
pub(crate) use command::Format;
pub(crate) use command::FormatPattern;

View File

@ -18,12 +18,12 @@ nu-protocol = { path = "../nu-protocol", version = "0.88.2" }
nu-utils = { path = "../nu-utils", version = "0.88.2" }
nu-ansi-term = "0.49.0"
fancy-regex = "0.11"
itertools = "0.11"
shadow-rs = { version = "0.24", default-features = false }
fancy-regex = "0.12"
itertools = "0.12"
shadow-rs = { version = "0.26", default-features = false }
[build-dependencies]
shadow-rs = { version = "0.24", default-features = false }
shadow-rs = { version = "0.26", default-features = false }
[features]
mimalloc = []

View File

@ -290,7 +290,6 @@ fn describe_value(
| Value::Date { .. }
| Value::Range { .. }
| Value::String { .. }
| Value::MatchPattern { .. }
| Value::Nothing { .. } => Value::record(
record!(
"type" => Value::string(value.get_type().to_string(), head),

View File

@ -278,9 +278,6 @@ impl<'a> std::fmt::Debug for DebuggableValue<'a> {
let rec = val.collect().map_err(|_| std::fmt::Error)?;
write!(f, "LazyRecord({:?})", DebuggableValue(&rec))
}
Value::MatchPattern { val, .. } => {
write!(f, "MatchPattern({:?})", val)
}
}
}
}

View File

@ -92,7 +92,7 @@ fn color_string_to_nustyle(color_string: String) -> Style {
mod tests {
use super::*;
use nu_ansi_term::{Color, Style};
use nu_protocol::{Span, Value};
use nu_protocol::{record, Span, Value};
#[test]
fn test_color_string_to_nustyle_empty_string() {
@ -120,13 +120,10 @@ mod tests {
#[test]
fn test_get_style_from_value() {
// Test case 1: all values are valid
let record = Record {
cols: vec!["bg".to_string(), "fg".to_string(), "attr".to_string()],
vals: vec![
Value::string("red", Span::unknown()),
Value::string("blue", Span::unknown()),
Value::string("bold", Span::unknown()),
],
let record = record! {
"bg" => Value::test_string("red"),
"fg" => Value::test_string("blue"),
"attr" => Value::test_string("bold"),
};
let expected_style = NuStyle {
bg: Some("red".to_string()),
@ -136,19 +133,15 @@ mod tests {
assert_eq!(get_style_from_value(&record), Some(expected_style));
// Test case 2: no values are valid
let record = Record {
cols: vec!["invalid".to_string()],
vals: vec![Value::nothing(Span::unknown())],
let record = record! {
"invalid" => Value::nothing(Span::test_data()),
};
assert_eq!(get_style_from_value(&record), None);
// Test case 3: some values are valid
let record = Record {
cols: vec!["bg".to_string(), "invalid".to_string()],
vals: vec![
Value::string("green", Span::unknown()),
Value::nothing(Span::unknown()),
],
let record = record! {
"bg" => Value::test_string("green"),
"invalid" => Value::nothing(Span::unknown()),
};
let expected_style = NuStyle {
bg: Some("green".to_string()),

View File

@ -131,8 +131,7 @@ impl<'a> StyleComputer<'a> {
Value::Closure { .. }
| Value::CustomValue { .. }
| Value::Error { .. }
| Value::LazyRecord { .. }
| Value::MatchPattern { .. } => TextStyle::basic_left(),
| Value::LazyRecord { .. } => TextStyle::basic_left(),
}
}

View File

@ -42,7 +42,7 @@ dialoguer = { default-features = false, features = ["fuzzy-select"], version = "
digest = { default-features = false, version = "0.10" }
dtparse = "2.0"
encoding_rs = "0.8"
fancy-regex = "0.11"
fancy-regex = "0.12"
filesize = "0.2"
filetime = "0.2"
fs_extra = "1.3"
@ -50,9 +50,9 @@ htmlescape = "0.3"
human-date-parser = "0.1.1"
indexmap = "2.1"
indicatif = "0.17"
itertools = "0.11"
itertools = "0.12"
log = "0.4"
lscolors = { version = "0.15", default-features = false, features = ["nu-ansi-term"] }
lscolors = { version = "0.16", default-features = false, features = ["nu-ansi-term"] }
md5 = { package = "md-5", version = "0.10" }
miette = { version = "5.10", features = ["fancy-no-backtrace"] }
mime = "0.3"
@ -80,7 +80,7 @@ serde_json = "1.0"
serde_urlencoded = "0.7"
serde_yaml = "0.9"
sha2 = "0.10"
sysinfo = "0.29"
sysinfo = "0.30"
tabled = { version = "0.14.0", features = ["color"], default-features = false }
terminal_size = "0.3"
titlecase = "2.0"
@ -104,9 +104,9 @@ winreg = "0.52"
[target.'cfg(unix)'.dependencies]
libc = "0.2"
umask = "2.1"
nix = { version = "0.27", default-features = false, features = ["user"] }
nix = { version = "0.27", default-features = false, features = ["user", "resource"] }
[target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "android"), not(target_os = "ios")))'.dependencies]
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
procfs = "0.16.0"
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies.trash]
@ -122,7 +122,7 @@ features = [
"Win32_Security",
"Win32_System_Threading",
]
version = "0.48"
version = "0.52"
[features]
plugin = ["nu-parser/plugin"]

View File

@ -1,4 +1,4 @@
use nu_engine::eval_expression;
use nu_engine::{eval_expression, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -48,8 +48,7 @@ impl Command for BytesBuild {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let mut output = vec![];
for expr in call.positional_iter() {
let val = eval_expression(engine_state, stack, expr)?;
for val in call.rest_iter_flattened(0, |expr| eval_expression(engine_state, stack, expr))? {
match val {
Value::Binary { mut val, .. } => output.append(&mut val),
// Explicitly propagate errors instead of dropping them.

View File

@ -105,6 +105,13 @@ impl Command for BytesRemove {
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
)),
},
Example {
description: "Remove find binary from end not found",
example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[11]",
result: Some(Value::test_binary (
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA, 0x10],
)),
},
Example {
description: "Remove all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC",
@ -159,8 +166,11 @@ fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
}
// append the remaining thing to result, this can be happening when
// we have something to remove and remove_all is False.
let mut remain = input[..left as usize].iter().copied().rev().collect();
result.append(&mut remain);
// check if the left is positive, if it is not, we don't need to append anything.
if left > 0 {
let mut remain = input[..left as usize].iter().copied().rev().collect();
result.append(&mut remain);
}
result = result.into_iter().rev().collect();
Value::binary(result, span)
} else {

View File

@ -258,16 +258,16 @@ fn histogram_impl(
result.push((
count, // attach count first for easily sorting.
Value::record(
Record {
cols: result_cols.clone(),
vals: vec![
Record::from_raw_cols_vals(
result_cols.clone(),
vec![
val.into_value(),
Value::int(count, span),
Value::float(quantile, span),
Value::string(percentage, span),
Value::string(freq, span),
],
},
),
span,
),
));

View File

@ -260,7 +260,6 @@ fn nu_value_to_string(value: Value, separator: &str) -> String {
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.to_string(),
Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
}
}

View File

@ -499,13 +499,7 @@ pub fn convert_sqlite_row_to_nu_value(row: &Row, span: Span, column_names: Vec<S
vals.push(val);
}
Value::record(
Record {
cols: column_names,
vals,
},
span,
)
Value::record(Record::from_raw_cols_vals(column_names, vals), span)
}
pub fn convert_sqlite_value_to_nu_value(value: ValueRef, span: Span) -> Value {

View File

@ -191,6 +191,24 @@ fn get_arguments(engine_state: &EngineState, stack: &mut Stack, call: Call) -> V
let arg_value_name_span_start = evaled_span.start as i64;
let arg_value_name_span_end = evaled_span.end as i64;
let record = record! {
"arg_type" => Value::string(arg_type, span),
"name" => Value::string(arg_value_name, inner_expr.span),
"type" => Value::string(arg_value_type, span),
"span_start" => Value::int(arg_value_name_span_start, span),
"span_end" => Value::int(arg_value_name_span_end, span),
};
arg_value.push(Value::record(record, inner_expr.span));
}
Argument::Spread(inner_expr) => {
let arg_type = "spread";
let evaluated_expression = get_expression_as_value(engine_state, stack, inner_expr);
let arg_value_name = debug_string_without_formatting(&evaluated_expression);
let arg_value_type = &evaluated_expression.get_type().to_string();
let evaled_span = evaluated_expression.span();
let arg_value_name_span_start = evaled_span.start as i64;
let arg_value_name_span_end = evaled_span.end as i64;
let record = record! {
"arg_type" => Value::string(arg_type, span),
"name" => Value::string(arg_value_name, inner_expr.span),
@ -259,6 +277,5 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.to_string(),
Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
}
}

View File

@ -4,7 +4,7 @@ use nu_protocol::{
record, Category, Example, IntoPipelineData, LazyRecord, PipelineData, Record, ShellError,
Signature, Span, Type, Value,
};
use sysinfo::{Pid, PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt};
use sysinfo::{MemoryRefreshKind, Pid, ProcessRefreshKind, RefreshKind, System};
const ENV_PATH_SEPARATOR_CHAR: char = {
#[cfg(target_family = "windows")]
{
@ -98,8 +98,9 @@ impl LazySystemInfoRecord {
}
"system" => {
// only get information requested
let system_opt =
SystemOpt::from((system_option, || RefreshKind::new().with_memory()));
let system_opt = SystemOpt::from((system_option, || {
RefreshKind::new().with_memory(MemoryRefreshKind::everything())
}));
let system = system_opt.get_system();
@ -135,53 +136,66 @@ impl LazySystemInfoRecord {
"virtual_memory" => Value::filesize(p.virtual_memory() as i64, self.span),
"status" => Value::string(p.status().to_string(), self.span),
"root" => {
if let Some(filename) = p.exe().parent() {
Value::string(filename.to_string_lossy().to_string(), self.span)
if let Some(path) = p.exe().and_then(|p| p.parent()) {
Value::string(path.to_string_lossy().to_string(), self.span)
} else {
Value::nothing(self.span)
}
},
"cwd" => Value::string(p.cwd().to_string_lossy().to_string(), self.span),
"exe_path" => Value::string(p.exe().to_string_lossy().to_string(), self.span),
"cwd" => {
if let Some(path) = p.cwd() {
Value::string(path.to_string_lossy().to_string(), self.span)
}else{
Value::nothing(self.span)
}
},
"exe_path" => {
if let Some(path)= p.exe() {
Value::string(path.to_string_lossy().to_string(), self.span)
}else{
Value::nothing(self.span)
}
},
"command" => Value::string(p.cmd().join(" "), self.span),
"name" => Value::string(p.name().to_string(), self.span),
"environment" => {
let mut env_rec = Record::new();
for val in p.environ() {
if let Some((key, value)) = val.split_once('=') {
let is_env_var_a_list = {
"environment" => {
let mut env_rec = Record::new();
for val in p.environ() {
if let Some((key, value)) = val.split_once('=') {
let is_env_var_a_list = {
{
#[cfg(target_family = "windows")]
{
#[cfg(target_family = "windows")]
{
key == "Path" || key == "PATHEXT" || key == "PSMODULEPATH" || key == "PSModulePath"
}
#[cfg(not(target_family = "windows"))]
{
key == "PATH" || key == "DYLD_FALLBACK_LIBRARY_PATH"
}
key == "Path" || key == "PATHEXT" || key == "PSMODULEPATH" || key == "PSModulePath"
}
#[cfg(not(target_family = "windows"))]
{
key == "PATH" || key == "DYLD_FALLBACK_LIBRARY_PATH"
}
};
if is_env_var_a_list {
let items = value.split(ENV_PATH_SEPARATOR_CHAR).map(|r| Value::string(r.to_string(), self.span)).collect::<Vec<_>>();
env_rec.push(key.to_string(), Value::list(items, self.span));
} else if key == "LS_COLORS" { // LS_COLORS is a special case, it's a colon separated list of key=value pairs
let items = value.split(':').map(|r| Value::string(r.to_string(), self.span)).collect::<Vec<_>>();
env_rec.push(key.to_string(), Value::list(items, self.span));
} else {
env_rec.push(key.to_string(), Value::string(value.to_string(), self.span));
}
};
if is_env_var_a_list {
let items = value.split(ENV_PATH_SEPARATOR_CHAR).map(|r| Value::string(r.to_string(), self.span)).collect::<Vec<_>>();
env_rec.push(key.to_string(), Value::list(items, self.span));
} else if key == "LS_COLORS" { // LS_COLORS is a special case, it's a colon separated list of key=value pairs
let items = value.split(':').map(|r| Value::string(r.to_string(), self.span)).collect::<Vec<_>>();
env_rec.push(key.to_string(), Value::list(items, self.span));
} else {
env_rec.push(key.to_string(), Value::string(value.to_string(), self.span));
}
}
Value::record(env_rec, self.span)
},
}
Value::record(env_rec, self.span)
},
},
self.span,
))
} else {
// If we can't get the process information, just return the system information
// only get information requested
let system_opt =
SystemOpt::from((system_option, || RefreshKind::new().with_memory()));
let system_opt = SystemOpt::from((system_option, || {
RefreshKind::new().with_memory(MemoryRefreshKind::everything())
}));
let system = system_opt.get_system();
Ok(Value::record(
@ -228,7 +242,7 @@ impl<'a> LazyRecord<'a> for LazySystemInfoRecord {
.without_disk_usage()
.without_user(),
)
.with_memory();
.with_memory(MemoryRefreshKind::everything());
// only get information requested
let system = System::new_with_specifics(rk);

View File

@ -86,6 +86,12 @@ impl Command for Metadata {
PipelineMetadata {
data_source: DataSource::HtmlThemes,
} => record.push("source", Value::string("into html --list", head)),
PipelineMetadata {
data_source: DataSource::FilePath(path),
} => record.push(
"source",
Value::string(path.to_string_lossy().to_string(), head),
),
}
}
@ -133,6 +139,12 @@ fn build_metadata_record(arg: &Value, metadata: Option<&PipelineMetadata>, head:
PipelineMetadata {
data_source: DataSource::HtmlThemes,
} => record.push("source", Value::string("into html --list", head)),
PipelineMetadata {
data_source: DataSource::FilePath(path),
} => record.push(
"source",
Value::string(path.to_string_lossy().to_string(), head),
),
}
}

View File

@ -233,6 +233,9 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
Whoami,
};
#[cfg(unix)]
bind_command! { ULimit };
// Date
bind_command! {
Date,

View File

@ -66,16 +66,17 @@ fn is_root_impl() -> bool {
System::Threading::{GetCurrentProcess, OpenProcessToken},
};
let mut handle = HANDLE::default();
let mut elevated = false;
// Checks whether the access token associated with the current process has elevated privileges.
// SAFETY: `elevated` only touched by safe code.
// `handle` lives long enough, initialized, mutated as out param, used, closed with validity check.
// `handle` lives long enough, initialized, mutated, used and closed with validity check.
// `elevation` only read on success and passed with correct `size`.
unsafe {
let mut handle = HANDLE::default();
// Opens the access token associated with the current process.
if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut handle).as_bool() {
if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut handle).is_ok() {
let mut elevation = TOKEN_ELEVATION::default();
let mut size = std::mem::size_of::<TOKEN_ELEVATION>() as u32;
@ -89,7 +90,7 @@ fn is_root_impl() -> bool {
size,
&mut size,
)
.as_bool()
.is_ok()
{
// Whether the token has elevated privileges.
// Safe to read as `GetTokenInformation` will not write outside `elevation` and it succeeded
@ -100,7 +101,7 @@ fn is_root_impl() -> bool {
if !handle.is_invalid() {
// Closes the object handle.
CloseHandle(handle);
let _ = CloseHandle(handle);
}
}

View File

@ -229,7 +229,16 @@ fn have_permission(dir: impl AsRef<Path>) -> PermissionResult<'static> {
}
}
#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn any_group(_current_user_gid: gid_t, owner_group: u32) -> bool {
use crate::filesystem::util::users;
let Some(user_groups) = users::current_user_groups() else {
return false;
};
user_groups.iter().any(|gid| gid.as_raw() == owner_group)
}
#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
fn any_group(current_user_gid: gid_t, owner_group: u32) -> bool {
use crate::filesystem::util::users;

View File

@ -1,10 +1,11 @@
use nu_engine::{current_dir, eval_block, CallExt};
use nu_path::expand_to_real_path;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::util::BufferedReader;
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, RawStream, ShellError,
Signature, Spanned, SyntaxShape, Type, Value,
Category, DataSource, Example, IntoInterruptiblePipelineData, PipelineData, PipelineMetadata,
RawStream, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use std::io::BufReader;
@ -157,6 +158,7 @@ impl Command for Open {
};
let buf_reader = BufReader::new(file);
let real_path = expand_to_real_path(path);
let file_contents = PipelineData::ExternalStream {
stdout: Some(RawStream::new(
@ -168,7 +170,9 @@ impl Command for Open {
stderr: None,
exit_code: None,
span: call_span,
metadata: None,
metadata: Some(PipelineMetadata {
data_source: DataSource::FilePath(real_path),
}),
trim_end_newline: false,
};
let exts_opt: Option<Vec<String>> = if raw {

View File

@ -381,11 +381,23 @@ fn rm(
{
unreachable!()
}
} else if metadata.is_file()
|| is_socket
|| is_fifo
|| metadata.file_type().is_symlink()
{
} else if metadata.is_symlink() {
// In Windows, symlink pointing to a directory can be removed using
// std::fs::remove_dir instead of std::fs::remove_file.
#[cfg(windows)]
{
f.metadata().and_then(|metadata| {
if metadata.is_dir() {
std::fs::remove_dir(&f)
} else {
std::fs::remove_file(&f)
}
})
}
#[cfg(not(windows))]
std::fs::remove_file(&f)
} else if metadata.is_file() || is_socket || is_fifo {
std::fs::remove_file(&f)
} else {
std::fs::remove_dir_all(&f)

View File

@ -4,8 +4,8 @@ use nu_path::expand_path_with;
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, RawStream, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
Category, DataSource, Example, PipelineData, PipelineMetadata, RawStream, ShellError,
Signature, Span, Spanned, SyntaxShape, Type, Value,
};
use std::fs::File;
use std::io::Write;
@ -149,9 +149,42 @@ impl Command for Save {
res
}
}
PipelineData::ListStream(ls, _)
PipelineData::ListStream(ls, pipeline_metadata)
if raw || prepare_path(&path, append, force)?.0.extension().is_none() =>
{
if let Some(PipelineMetadata {
data_source: DataSource::FilePath(input_path),
}) = pipeline_metadata
{
if path.item == input_path {
return Err(ShellError::GenericError {
error: "pipeline input and output are same file".into(),
msg: format!(
"can't save output to '{}' while it's being reading",
path.item.display()
),
span: Some(path.span),
help: Some("you should change output path".into()),
inner: vec![],
});
}
if let Some(ref err_path) = stderr_path {
if err_path.item == input_path {
return Err(ShellError::GenericError {
error: "pipeline input and stderr are same file".into(),
msg: format!(
"can't save stderr to '{}' while it's being reading",
err_path.item.display()
),
span: Some(err_path.span),
help: Some("you should change stderr path".into()),
inner: vec![],
});
}
}
}
let (mut file, _) = get_files(
&path,
stderr_path.as_ref(),
@ -340,10 +373,7 @@ fn prepare_path(
fn open_file(path: &Path, span: Span, append: bool) -> Result<File, ShellError> {
let file = match (append, path.exists()) {
(true, true) => std::fs::OpenOptions::new()
.write(true)
.append(true)
.open(path),
(true, true) => std::fs::OpenOptions::new().append(true).open(path),
_ => std::fs::File::create(path),
};

View File

@ -165,9 +165,8 @@ pub fn is_older(src: &Path, dst: &Path) -> Option<bool> {
#[cfg(unix)]
pub mod users {
use libc::{c_int, gid_t, uid_t};
use libc::{gid_t, uid_t};
use nix::unistd::{Gid, Group, Uid, User};
use std::ffi::CString;
pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
User::from_uid(Uid::from_raw(uid)).ok().flatten()
@ -185,6 +184,7 @@ pub mod users {
Gid::current().as_raw()
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_current_username() -> Option<String> {
User::from_uid(Uid::current())
.ok()
@ -192,6 +192,30 @@ pub mod users {
.map(|user| user.name)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn current_user_groups() -> Option<Vec<Gid>> {
// SAFETY:
// if first arg is 0 then it ignores second argument and returns number of groups present for given user.
let ngroups = unsafe { libc::getgroups(0, core::ptr::null::<gid_t> as *mut _) };
let mut buff: Vec<gid_t> = vec![0; ngroups as usize];
// SAFETY:
// buff is the size of ngroups and getgroups reads max ngroups elements into buff
let found = unsafe { libc::getgroups(ngroups, buff.as_mut_ptr()) };
if found < 0 {
None
} else {
buff.truncate(found as usize);
buff.sort_unstable();
buff.dedup();
buff.into_iter()
.filter_map(|i| get_group_by_gid(i as gid_t))
.map(|group| group.gid)
.collect::<Vec<_>>()
.into()
}
}
/// Returns groups for a provided user name and primary group id.
///
/// # libc functions used
@ -207,7 +231,9 @@ pub mod users {
/// println!("User is a member of group #{group}");
/// }
/// ```
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_user_groups(username: &str, gid: gid_t) -> Option<Vec<Gid>> {
use std::ffi::CString;
// MacOS uses i32 instead of gid_t in getgrouplist for unknown reasons
#[cfg(target_os = "macos")]
let mut buff: Vec<i32> = vec![0; 1024];
@ -218,7 +244,7 @@ pub mod users {
return None;
};
let mut count = buff.len() as c_int;
let mut count = buff.len() as libc::c_int;
// MacOS uses i32 instead of gid_t in getgrouplist for unknown reasons
// SAFETY:

View File

@ -516,7 +516,6 @@ fn value_should_be_printed(
Err(_) => false,
},
Value::Binary { .. } => false,
Value::MatchPattern { .. } => false,
});
if invert {
match_found = !match_found;

View File

@ -174,7 +174,7 @@ pub fn group_by(
Value::CellPath { val, .. } => group_cell_path(val, values)?,
Value::Block { .. } | Value::Closure { .. } => {
let block: Option<Closure> = call.opt(engine_state, stack, 0)?;
group_closure(&values, span, block, stack, engine_state, call)?
group_closure(values, span, block, stack, engine_state, call)?
}
_ => {
@ -231,9 +231,8 @@ pub fn group_no_grouper(values: Vec<Value>) -> Result<IndexMap<String, Vec<Value
Ok(groups)
}
// TODO: refactor this, it's a bit of a mess
fn group_closure(
values: &[Value],
values: Vec<Value>,
span: Span,
block: Option<Closure>,
stack: &mut Stack,
@ -241,13 +240,13 @@ fn group_closure(
call: &Call,
) -> Result<IndexMap<String, Vec<Value>>, ShellError> {
let error_key = "error";
let mut keys: Vec<Result<String, ShellError>> = vec![];
let value_list = Value::list(values.to_vec(), span);
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
for value in values {
if let Some(capture_block) = &block {
if let Some(capture_block) = &block {
let block = engine_state.get_block(capture_block.block_id);
for value in values {
let mut stack = stack.captures_to_stack(capture_block.captures.clone());
let block = engine_state.get_block(capture_block.block_id);
let pipeline = eval_block(
engine_state,
&mut stack,
@ -257,11 +256,16 @@ fn group_closure(
call.redirect_stderr,
);
match pipeline {
let group_key = match pipeline {
Ok(s) => {
let collection: Vec<Value> = s.into_iter().collect();
let mut s = s.into_iter();
if collection.len() > 1 {
let key = match s.next() {
Some(Value::Error { .. }) | None => error_key.into(),
Some(return_value) => return_value.as_string()?,
};
if s.next().is_some() {
return Err(ShellError::GenericError {
error: "expected one value from the block".into(),
msg: "requires a table with one value for grouping".into(),
@ -271,39 +275,14 @@ fn group_closure(
});
}
let value = match collection.first() {
Some(Value::Error { .. }) | None => Value::string(error_key, span),
Some(return_value) => return_value.clone(),
};
key
}
Err(_) => error_key.into(),
};
keys.push(value.as_string());
}
Err(_) => {
keys.push(Ok(error_key.into()));
}
}
groups.entry(group_key).or_default().push(value);
}
}
let map = keys;
let block = Box::new(move |idx: usize, row: &Value| match map.get(idx) {
Some(Ok(key)) => Ok(key.clone()),
Some(Err(reason)) => Err(reason.clone()),
None => row.as_string(),
});
let grouper = &Some(block);
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
for (idx, value) in value_list.into_pipeline_data().into_iter().enumerate() {
let group_key = if let Some(ref grouper) = grouper {
grouper(idx, &value)
} else {
value.as_string()
};
let group = groups.entry(group_key?).or_default();
group.push(value);
}
Ok(groups)
}

View File

@ -55,10 +55,7 @@ fn from_delimited_string_to_value(
.collect::<Vec<Value>>();
rows.push(Value::record(
Record {
cols: headers.clone(),
vals: output_row,
},
Record::from_raw_cols_vals(headers.clone(), output_row),
span,
));
}

View File

@ -242,12 +242,6 @@ fn convert_to_value(
msg: "extra tokens in input file".into(),
span: expr.span,
}),
Expr::MatchPattern(..) => Err(ShellError::OutsideSpannedLabeledError {
src: original_text.to_string(),
error: "Error when loading".into(),
msg: "extra tokens in input file".into(),
span: expr.span,
}),
Expr::GlobPattern(val) => Ok(Value::string(val, span)),
Expr::ImportPattern(..) => Err(ShellError::OutsideSpannedLabeledError {
src: original_text.to_string(),
@ -409,13 +403,7 @@ fn convert_to_value(
}
for row in cells {
let mut vals = vec![];
for cell in row {
vals.push(convert_to_value(cell, span, original_text)?);
}
if cols.len() != vals.len() {
if cols.len() != row.len() {
return Err(ShellError::OutsideSpannedLabeledError {
src: original_text.to_string(),
error: "Error when loading".into(),
@ -423,12 +411,13 @@ fn convert_to_value(
span: expr.span,
});
}
let vals: Vec<Value> = row
.into_iter()
.map(|cell| convert_to_value(cell, span, original_text))
.collect::<Result<_, _>>()?;
output.push(Value::record(
Record {
cols: cols.clone(),
vals,
},
Record::from_raw_cols_vals(cols.clone(), vals),
span,
));
}

View File

@ -127,10 +127,7 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
Value::List { vals, .. } => nu_json::Value::Array(json_list(vals)?),
Value::Error { error, .. } => return Err(*error.clone()),
Value::Closure { .. }
| Value::Block { .. }
| Value::Range { .. }
| Value::MatchPattern { .. } => nu_json::Value::Null,
Value::Closure { .. } | Value::Block { .. } | Value::Range { .. } => nu_json::Value::Null,
Value::Binary { val, .. } => {
nu_json::Value::Array(val.iter().map(|x| nu_json::Value::U64(*x as u64)).collect())
}

View File

@ -241,12 +241,6 @@ pub fn value_to_string(
))
}
}
Value::MatchPattern { .. } => Err(ShellError::UnsupportedInput {
msg: "match patterns are currently not nuon-compatible".to_string(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Nothing { .. } => Ok("null".to_string()),
Value::Range { val, .. } => Ok(format!(
"{}..{}{}",

View File

@ -148,7 +148,6 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
Value::Binary { val, .. } => format!("{val:?}"),
Value::CellPath { val, .. } => val.to_string(),
Value::CustomValue { val, .. } => val.value_string(),
Value::MatchPattern { val, .. } => format!("{:?}", val),
}
}

View File

@ -94,7 +94,6 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
.collect::<Result<Vec<toml::Value>, ShellError>>()?,
),
Value::CustomValue { .. } => toml::Value::String("<Custom Value>".to_string()),
Value::MatchPattern { .. } => toml::Value::String("<Match Pattern>".to_string()),
})
}

View File

@ -34,10 +34,10 @@ impl Command for ToXml {
fn extra_usage(&self) -> &str {
r#"Every XML entry is represented via a record with tag, attribute and content fields.
To represent different types of entries different values must be written to this fields:
1. Tag entry: `{tag: <tag name> attrs: {<attr name>: "<string value>" ...} content: [<entries>]}`
2. Comment entry: `{tag: '!' attrs: null content: "<comment string>"}`
3. Processing instruction (PI): `{tag: '?<pi name>' attrs: null content: "<pi content string>"}`
4. Text: `{tag: null attrs: null content: "<text>"}`. Or as plain `<text>` instead of record.
1. Tag entry: `{tag: <tag name> attributes: {<attr name>: "<string value>" ...} content: [<entries>]}`
2. Comment entry: `{tag: '!' attributes: null content: "<comment string>"}`
3. Processing instruction (PI): `{tag: '?<pi name>' attributes: null content: "<pi content string>"}`
4. Text: `{tag: null attributes: null content: "<text>"}`. Or as plain `<text>` instead of record.
Additionally any field which is: empty record, empty list or null, can be omitted."#
}
@ -46,7 +46,7 @@ Additionally any field which is: empty record, empty list or null, can be omitte
vec![
Example {
description: "Outputs an XML string representing the contents of this table",
example: r#"{tag: note attributes: {} content : [{tag: remember attributes: {} content : [{tag: null attrs: null content : Event}]}]} | to xml"#,
example: r#"{tag: note attributes: {} content : [{tag: remember attributes: {} content : [{tag: null attributes: null content : Event}]}]} | to xml"#,
result: Some(Value::test_string(
"<note><remember>Event</remember></note>",
)),
@ -110,6 +110,17 @@ fn to_xml_entry<W: Write>(
}
if let Value::Record { val: record, .. } = &entry {
if let Some(bad_column) = find_invalid_column(record) {
return Err(ShellError::CantConvert {
to_type: "XML".into(),
from_type: "record".into(),
span: entry_span,
help: Some(format!(
"Invalid column \"{}\" in xml entry. Only \"{}\", \"{}\" and \"{}\" are permitted",
bad_column, COLUMN_TAG_NAME, COLUMN_ATTRS_NAME, COLUMN_CONTENT_NAME
)),
});
}
// If key is not found it is assumed to be nothing. This way
// user can write a tag like {tag: a content: [...]} instead
// of longer {tag: a attributes: {} content: [...]}
@ -144,7 +155,12 @@ fn to_xml_entry<W: Write>(
(Value::String { val: tag_name, .. }, attrs, children) => to_tag_like(
entry_span, tag_name, tag_span, attrs, children, top_level, writer,
),
_ => Ok(()),
_ => Err(ShellError::CantConvert {
to_type: "XML".into(),
from_type: "record".into(),
span: entry_span,
help: Some("Tag missing or is not a string".into()),
}),
}
} else {
Err(ShellError::CantConvert {
@ -156,6 +172,14 @@ fn to_xml_entry<W: Write>(
}
}
fn find_invalid_column(record: &Record) -> Option<&String> {
const VALID_COLS: [&str; 3] = [COLUMN_TAG_NAME, COLUMN_ATTRS_NAME, COLUMN_CONTENT_NAME];
record
.cols
.iter()
.find(|col| !VALID_COLS.contains(&col.as_str()))
}
/// Convert record to tag-like entry: tag, PI, comment.
fn to_tag_like<W: Write>(
entry_span: Span,

View File

@ -97,7 +97,6 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
.collect::<Result<Vec<serde_yaml::Value>, ShellError>>()?,
),
Value::CustomValue { .. } => serde_yaml::Value::Null,
Value::MatchPattern { .. } => serde_yaml::Value::Null,
})
}

View File

@ -23,6 +23,7 @@ mod random;
mod removed;
mod shells;
mod sort_utils;
#[cfg(feature = "sqlite")]
mod stor;
mod strings;
mod system;

View File

@ -5,7 +5,8 @@ use base64::{alphabet, Engine};
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{
record, BufferedReader, IntoPipelineData, PipelineData, RawStream, ShellError, Span, Value,
record, BufferedReader, IntoPipelineData, PipelineData, RawStream, ShellError, Span, Spanned,
Value,
};
use ureq::{Error, ErrorKind, Request, Response};
@ -26,29 +27,45 @@ pub enum BodyType {
Unknown,
}
// Only panics if the user agent is invalid but we define it statically so either
// it always or never fails
#[derive(Clone, Copy, PartialEq)]
pub enum RedirectMode {
Follow,
Error,
Manual,
}
pub fn http_client(
allow_insecure: bool,
redirect_mode: RedirectMode,
engine_state: &EngineState,
stack: &mut Stack,
) -> ureq::Agent {
) -> Result<ureq::Agent, ShellError> {
let tls = native_tls::TlsConnector::builder()
.danger_accept_invalid_certs(allow_insecure)
.build()
.expect("Failed to build network tls");
.map_err(|e| ShellError::GenericError {
error: format!("Failed to build network tls: {}", e),
msg: String::new(),
span: None,
help: None,
inner: vec![],
})?;
let mut agent_builder = ureq::builder()
.user_agent("nushell")
.tls_connector(std::sync::Arc::new(tls));
if let RedirectMode::Manual | RedirectMode::Error = redirect_mode {
agent_builder = agent_builder.redirects(0);
}
if let Some(http_proxy) = retrieve_http_proxy_from_env(engine_state, stack) {
if let Ok(proxy) = ureq::Proxy::new(http_proxy) {
agent_builder = agent_builder.proxy(proxy);
}
};
agent_builder.build()
Ok(agent_builder.build())
}
pub fn http_parse_url(
@ -68,6 +85,18 @@ pub fn http_parse_url(
Ok((requested_url, url))
}
pub fn http_parse_redirect_mode(mode: Option<Spanned<String>>) -> Result<RedirectMode, ShellError> {
mode.map_or(Ok(RedirectMode::Follow), |v| match &v.item[..] {
"follow" | "f" => Ok(RedirectMode::Follow),
"error" | "e" => Ok(RedirectMode::Error),
"manual" | "m" => Ok(RedirectMode::Manual),
_ => Err(ShellError::TypeMismatch {
err_message: "Invalid redirect handling mode".to_string(),
span: v.span,
}),
})
}
pub fn response_to_buffer(
response: Response,
engine_state: &EngineState,
@ -452,6 +481,26 @@ fn transform_response_using_content_type(
};
}
pub fn check_response_redirection(
redirect_mode: RedirectMode,
span: Span,
response: &Result<Response, ShellErrorOrRequestError>,
) -> Result<(), ShellError> {
if let Ok(resp) = response {
if RedirectMode::Error == redirect_mode && (300..400).contains(&resp.status()) {
return Err(ShellError::NetworkFailure {
msg: format!(
"Redirect encountered when redirect handling mode was 'error' ({} {})",
resp.status(),
resp.status_text()
),
span,
});
}
}
Ok(())
}
fn request_handle_response_content(
engine_state: &EngineState,
stack: &mut Stack,

View File

@ -2,12 +2,13 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_timeout, send_request,
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
request_add_authorization_header, request_add_custom_headers, request_handle_response,
request_set_timeout, send_request,
};
use super::client::RequestFlags;
@ -79,6 +80,11 @@ impl Command for SubCommand {
"allow-errors",
"do not fail if the server returns an error code",
Some('e'),
).named(
"redirect-mode",
SyntaxShape::String,
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
Some('R')
)
.filter()
.category(Category::Network)
@ -150,6 +156,7 @@ struct Arguments {
timeout: Option<Value>,
full: bool,
allow_errors: bool,
redirect: Option<Spanned<String>>,
}
fn run_delete(
@ -170,6 +177,7 @@ fn run_delete(
timeout: call.get_flag(engine_state, stack, "max-time")?,
full: call.has_flag("full"),
allow_errors: call.has_flag("allow-errors"),
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
};
helper(engine_state, stack, call, args)
@ -186,8 +194,9 @@ fn helper(
let span = args.url.span();
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
let mut request = client.delete(&requested_url);
request = request_set_timeout(args.timeout, request)?;
@ -202,6 +211,7 @@ fn helper(
allow_errors: args.allow_errors,
};
check_response_redirection(redirect_mode, span, &response)?;
request_handle_response(
engine_state,
stack,

View File

@ -2,16 +2,15 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_timeout, send_request,
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
request_add_authorization_header, request_add_custom_headers, request_handle_response,
request_set_timeout, send_request, RequestFlags,
};
use super::client::RequestFlags;
#[derive(Clone)]
pub struct SubCommand;
@ -73,6 +72,12 @@ impl Command for SubCommand {
"do not fail if the server returns an error code",
Some('e'),
)
.named(
"redirect-mode",
SyntaxShape::String,
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
Some('R')
)
.filter()
.category(Category::Network)
}
@ -137,6 +142,7 @@ struct Arguments {
timeout: Option<Value>,
full: bool,
allow_errors: bool,
redirect: Option<Spanned<String>>,
}
fn run_get(
@ -155,6 +161,7 @@ fn run_get(
timeout: call.get_flag(engine_state, stack, "max-time")?,
full: call.has_flag("full"),
allow_errors: call.has_flag("allow-errors"),
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
};
helper(engine_state, stack, call, args)
}
@ -170,8 +177,9 @@ fn helper(
let span = args.url.span();
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
let mut request = client.get(&requested_url);
request = request_set_timeout(args.timeout, request)?;
@ -186,6 +194,7 @@ fn helper(
allow_errors: args.allow_errors,
};
check_response_redirection(redirect_mode, span, &response)?;
request_handle_response(
engine_state,
stack,

View File

@ -5,12 +5,13 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response_headers, request_set_timeout, send_request,
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
request_add_authorization_header, request_add_custom_headers, request_handle_response_headers,
request_set_timeout, send_request,
};
#[derive(Clone)]
@ -58,6 +59,11 @@ impl Command for SubCommand {
"insecure",
"allow insecure server connections when using SSL",
Some('k'),
).named(
"redirect-mode",
SyntaxShape::String,
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
Some('R')
)
.filter()
.category(Category::Network)
@ -114,6 +120,7 @@ struct Arguments {
user: Option<String>,
password: Option<String>,
timeout: Option<Value>,
redirect: Option<Spanned<String>>,
}
fn run_head(
@ -129,6 +136,7 @@ fn run_head(
user: call.get_flag(engine_state, stack, "user")?,
password: call.get_flag(engine_state, stack, "password")?,
timeout: call.get_flag(engine_state, stack, "max-time")?,
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
};
let ctrl_c = engine_state.ctrlc.clone();
@ -146,8 +154,9 @@ fn helper(
) -> Result<PipelineData, ShellError> {
let span = args.url.span();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
let mut request = client.head(&requested_url);
request = request_set_timeout(args.timeout, request)?;
@ -155,6 +164,7 @@ fn helper(
request = request_add_custom_headers(args.headers, request)?;
let response = send_request(request, None, None, ctrlc);
check_response_redirection(redirect_mode, span, &response)?;
request_handle_response_headers(span, response)
}

View File

@ -10,7 +10,7 @@ use crate::network::http::client::{
request_handle_response, request_set_timeout, send_request,
};
use super::client::RequestFlags;
use super::client::{RedirectMode, RequestFlags};
#[derive(Clone)]
pub struct SubCommand;
@ -160,7 +160,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, RedirectMode::Follow, engine_state, stack)?;
let mut request = client.request("OPTIONS", &requested_url);
request = request_set_timeout(args.timeout, request)?;

View File

@ -2,12 +2,13 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_timeout, send_request,
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
request_add_authorization_header, request_add_custom_headers, request_handle_response,
request_set_timeout, send_request,
};
use super::client::RequestFlags;
@ -75,6 +76,11 @@ impl Command for SubCommand {
"allow-errors",
"do not fail if the server returns an error code",
Some('e'),
).named(
"redirect-mode",
SyntaxShape::String,
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
Some('R')
)
.filter()
.category(Category::Network)
@ -142,6 +148,7 @@ struct Arguments {
timeout: Option<Value>,
full: bool,
allow_errors: bool,
redirect: Option<Spanned<String>>,
}
fn run_patch(
@ -162,6 +169,7 @@ fn run_patch(
timeout: call.get_flag(engine_state, stack, "max-time")?,
full: call.has_flag("full"),
allow_errors: call.has_flag("allow-errors"),
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
};
helper(engine_state, stack, call, args)
@ -178,8 +186,9 @@ fn helper(
let span = args.url.span();
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
let mut request = client.patch(&requested_url);
request = request_set_timeout(args.timeout, request)?;
@ -194,6 +203,7 @@ fn helper(
allow_errors: args.allow_errors,
};
check_response_redirection(redirect_mode, span, &response)?;
request_handle_response(
engine_state,
stack,

View File

@ -2,12 +2,13 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_timeout, send_request,
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
request_add_authorization_header, request_add_custom_headers, request_handle_response,
request_set_timeout, send_request,
};
use super::client::RequestFlags;
@ -75,6 +76,11 @@ impl Command for SubCommand {
"allow-errors",
"do not fail if the server returns an error code",
Some('e'),
).named(
"redirect-mode",
SyntaxShape::String,
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
Some('R')
)
.filter()
.category(Category::Network)
@ -140,6 +146,7 @@ struct Arguments {
timeout: Option<Value>,
full: bool,
allow_errors: bool,
redirect: Option<Spanned<String>>,
}
fn run_post(
@ -160,6 +167,7 @@ fn run_post(
timeout: call.get_flag(engine_state, stack, "max-time")?,
full: call.has_flag("full"),
allow_errors: call.has_flag("allow-errors"),
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
};
helper(engine_state, stack, call, args)
@ -176,8 +184,9 @@ fn helper(
let span = args.url.span();
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
let mut request = client.post(&requested_url);
request = request_set_timeout(args.timeout, request)?;
@ -192,6 +201,7 @@ fn helper(
allow_errors: args.allow_errors,
};
check_response_redirection(redirect_mode, span, &response)?;
request_handle_response(
engine_state,
stack,

View File

@ -2,12 +2,13 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use crate::network::http::client::{
http_client, http_parse_url, request_add_authorization_header, request_add_custom_headers,
request_handle_response, request_set_timeout, send_request,
check_response_redirection, http_client, http_parse_redirect_mode, http_parse_url,
request_add_authorization_header, request_add_custom_headers, request_handle_response,
request_set_timeout, send_request,
};
use super::client::RequestFlags;
@ -75,6 +76,11 @@ impl Command for SubCommand {
"allow-errors",
"do not fail if the server returns an error code",
Some('e'),
).named(
"redirect-mode",
SyntaxShape::String,
"What to do when encountering redirects. Default: 'follow'. Valid options: 'follow' ('f'), 'manual' ('m'), 'error' ('e').",
Some('R')
)
.filter()
.category(Category::Network)
@ -140,6 +146,7 @@ struct Arguments {
timeout: Option<Value>,
full: bool,
allow_errors: bool,
redirect: Option<Spanned<String>>,
}
fn run_put(
@ -160,6 +167,7 @@ fn run_put(
timeout: call.get_flag(engine_state, stack, "max-time")?,
full: call.has_flag("full"),
allow_errors: call.has_flag("allow-errors"),
redirect: call.get_flag(engine_state, stack, "redirect-mode")?,
};
helper(engine_state, stack, call, args)
@ -176,8 +184,9 @@ fn helper(
let span = args.url.span();
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;
let redirect_mode = http_parse_redirect_mode(args.redirect)?;
let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, redirect_mode, engine_state, stack)?;
let mut request = client.put(&requested_url);
request = request_set_timeout(args.timeout, request)?;
@ -192,6 +201,7 @@ fn helper(
allow_errors: args.allow_errors,
};
check_response_redirection(redirect_mode, span, &response)?;
request_handle_response(
engine_state,
stack,

View File

@ -7,6 +7,8 @@ mod is_terminal;
mod kill;
mod sleep;
mod term_size;
#[cfg(unix)]
mod ulimit;
mod whoami;
pub use ansi::{Ansi, AnsiLink, AnsiStrip};
@ -20,4 +22,6 @@ pub use is_terminal::IsTerminal;
pub use kill::Kill;
pub use sleep::Sleep;
pub use term_size::TermSize;
#[cfg(unix)]
pub use ulimit::ULimit;
pub use whoami::Whoami;

View File

@ -0,0 +1,603 @@
use nix::sys::resource::{rlim_t, Resource, RLIM_INFINITY};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use once_cell::sync::Lazy;
/// An object contains resource related parameters
struct ResourceInfo<'a> {
name: &'a str,
desc: &'a str,
flag: char,
multiplier: rlim_t,
resource: Resource,
}
impl<'a> ResourceInfo<'a> {
/// Create a `ResourceInfo` object
fn new(
name: &'a str,
desc: &'a str,
flag: char,
multiplier: rlim_t,
resource: Resource,
) -> Self {
Self {
name,
desc,
flag,
multiplier,
resource,
}
}
/// Get unit
fn get_unit(&self) -> &str {
if self.resource == Resource::RLIMIT_CPU {
"(seconds, "
} else if self.multiplier == 1 {
"("
} else {
"(kB, "
}
}
}
impl<'a> Default for ResourceInfo<'a> {
fn default() -> Self {
Self {
name: "file-size",
desc: "Maximum size of files created by the shell",
flag: 'f',
multiplier: 1024,
resource: Resource::RLIMIT_FSIZE,
}
}
}
static RESOURCE_ARRAY: Lazy<Vec<ResourceInfo>> = Lazy::new(|| {
let resources = [
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
(
"socket-buffers",
"Maximum size of socket buffers",
'b',
1024,
Resource::RLIMIT_SBSIZE,
),
(
"core-size",
"Maximum size of core files created",
'c',
1024,
Resource::RLIMIT_CORE,
),
(
"data-size",
"Maximum size of a process's data segment",
'd',
1024,
Resource::RLIMIT_DATA,
),
#[cfg(any(target_os = "android", target_os = "linux"))]
(
"nice",
"Controls of maximum nice priority",
'e',
1,
Resource::RLIMIT_NICE,
),
(
"file-size",
"Maximum size of files created by the shell",
'f',
1024,
Resource::RLIMIT_FSIZE,
),
#[cfg(any(target_os = "android", target_os = "linux"))]
(
"pending-signals",
"Maximum number of pending signals",
'i',
1,
Resource::RLIMIT_SIGPENDING,
),
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "linux",
target_os = "netbsd"
))]
(
"lock-size",
"Maximum size that may be locked into memory",
'l',
1024,
Resource::RLIMIT_MEMLOCK,
),
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "aix",
))]
(
"resident-set-size",
"Maximum resident set size",
'm',
1024,
Resource::RLIMIT_RSS,
),
(
"file-descriptor-count",
"Maximum number of open file descriptors",
'n',
1,
Resource::RLIMIT_NOFILE,
),
#[cfg(any(target_os = "android", target_os = "linux"))]
(
"queue-size",
"Maximum bytes in POSIX message queues",
'q',
1024,
Resource::RLIMIT_MSGQUEUE,
),
#[cfg(any(target_os = "android", target_os = "linux"))]
(
"realtime-priority",
"Maximum realtime scheduling priority",
'r',
1,
Resource::RLIMIT_RTPRIO,
),
(
"stack-size",
"Maximum stack size",
's',
1024,
Resource::RLIMIT_STACK,
),
(
"cpu-time",
"Maximum amount of CPU time in seconds",
't',
1,
Resource::RLIMIT_CPU,
),
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "aix",
))]
(
"process-count",
"Maximum number of processes available to the current user",
'u',
1,
Resource::RLIMIT_NPROC,
),
#[cfg(not(any(target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")))]
(
"virtual-memory-size",
"Maximum amount of virtual memory available to each process",
'v',
1024,
Resource::RLIMIT_AS,
),
#[cfg(target_os = "freebsd")]
(
"swap-size",
"Maximum swap space",
'w',
1024,
Resource::RLIMIT_SWAP,
),
#[cfg(any(target_os = "android", target_os = "linux"))]
(
"file-locks",
"Maximum number of file locks",
'x',
1,
Resource::RLIMIT_LOCKS,
),
#[cfg(target_os = "linux")]
(
"realtime-maxtime",
"Maximum contiguous realtime CPU time",
'y',
1,
Resource::RLIMIT_RTTIME,
),
#[cfg(target_os = "freebsd")]
(
"kernel-queues",
"Maximum number of kqueues",
'K',
1,
Resource::RLIMIT_KQUEUES,
),
#[cfg(target_os = "freebsd")]
(
"ptys",
"Maximum number of pseudo-terminals",
'P',
1,
Resource::RLIMIT_NPTS,
),
];
let mut resource_array = Vec::new();
for (name, desc, flag, multiplier, res) in resources {
resource_array.push(ResourceInfo::new(name, desc, flag, multiplier, res));
}
resource_array
});
/// Convert `rlim_t` to `Value` representation
fn limit_to_value(limit: rlim_t, multiplier: rlim_t, span: Span) -> Result<Value, ShellError> {
if limit == RLIM_INFINITY {
return Ok(Value::string("unlimited", span));
}
let val = match i64::try_from(limit / multiplier) {
Ok(v) => v,
Err(e) => {
return Err(ShellError::CantConvert {
to_type: "i64".into(),
from_type: "rlim_t".into(),
span,
help: Some(e.to_string()),
});
}
};
Ok(Value::int(val, span))
}
/// Get maximum length of all flag descriptions
fn max_desc_len(call: &Call, print_all: bool) -> usize {
let mut desc_len = 0;
let mut unit_len = 0;
for res in RESOURCE_ARRAY.iter() {
if !print_all && !call.has_flag(res.name) {
continue;
}
desc_len = res.desc.len().max(desc_len);
unit_len = res.get_unit().len().max(unit_len);
}
// Use `RLIMIT_FSIZE` limit if no resource flag provided.
if desc_len == 0 {
let res = ResourceInfo::default();
desc_len = res.desc.len().max(desc_len);
unit_len = res.get_unit().len().max(unit_len);
}
// desc.len() + unit.len() + '-X)'.len()
desc_len + unit_len + 3
}
/// Fill `ResourceInfo` to the record entry
fn fill_record(
res: &ResourceInfo,
max_len: usize,
soft: bool,
hard: bool,
span: Span,
) -> Result<Record, ShellError> {
let mut record = Record::new();
let mut desc = String::new();
desc.push_str(res.desc);
debug_assert!(res.desc.len() + res.get_unit().len() + 3 <= max_len);
let width = max_len - res.desc.len() - res.get_unit().len() - 3;
if width == 0 {
desc.push_str(format!(" {}-{})", res.get_unit(), res.flag).as_str());
} else {
desc.push_str(format!("{:>width$} {}-{})", ' ', res.get_unit(), res.flag).as_str());
}
record.push("description", Value::string(desc, span));
let (soft_limit, hard_limit) = getrlimit(res.resource)?;
if soft {
let soft_limit = limit_to_value(soft_limit, res.multiplier, span)?;
record.push("soft", soft_limit);
}
if hard {
let hard_limit = limit_to_value(hard_limit, res.multiplier, span)?;
record.push("hard", hard_limit);
}
Ok(record)
}
/// Set limits
fn set_limits(
limit_value: &Value,
res: &ResourceInfo,
soft: bool,
hard: bool,
call_span: Span,
) -> Result<(), ShellError> {
let (mut soft_limit, mut hard_limit) = getrlimit(res.resource)?;
let new_limit = parse_limit(limit_value, res, soft, soft_limit, hard_limit, call_span)?;
if hard {
hard_limit = new_limit;
}
if soft {
soft_limit = new_limit;
// Do not attempt to set the soft limit higher than the hard limit.
if (new_limit > hard_limit || new_limit == RLIM_INFINITY) && hard_limit != RLIM_INFINITY {
soft_limit = hard_limit;
}
}
setrlimit(res.resource, soft_limit, hard_limit)
}
/// Print limits
fn print_limits(
call: &Call,
print_all: bool,
soft: bool,
hard: bool,
) -> Result<PipelineData, ShellError> {
let mut output = Vec::new();
let mut print_default_limit = true;
let max_len = max_desc_len(call, print_all);
for res in RESOURCE_ARRAY.iter() {
if !print_all {
// Print specified limit.
if !call.has_flag(res.name) {
continue;
}
}
let record = fill_record(res, max_len, soft, hard, call.head)?;
output.push(Value::record(record, call.head));
if print_default_limit {
print_default_limit = false;
}
}
// Print `RLIMIT_FSIZE` limit if no resource flag provided.
if print_default_limit {
let res = ResourceInfo::default();
let record = fill_record(&res, max_len, soft, hard, call.head)?;
output.push(Value::record(record, call.head));
}
Ok(Value::list(output, call.head).into_pipeline_data())
}
/// Wrap `nix::sys::resource::getrlimit`
fn setrlimit(res: Resource, soft_limit: rlim_t, hard_limit: rlim_t) -> Result<(), ShellError> {
nix::sys::resource::setrlimit(res, soft_limit, hard_limit).map_err(|e| {
ShellError::GenericError {
error: e.to_string(),
msg: String::new(),
span: None,
help: None,
inner: vec![],
}
})
}
/// Wrap `nix::sys::resource::setrlimit`
fn getrlimit(res: Resource) -> Result<(rlim_t, rlim_t), ShellError> {
nix::sys::resource::getrlimit(res).map_err(|e| ShellError::GenericError {
error: e.to_string(),
msg: String::new(),
span: None,
help: None,
inner: vec![],
})
}
/// Parse user input
fn parse_limit(
limit_value: &Value,
res: &ResourceInfo,
soft: bool,
soft_limit: rlim_t,
hard_limit: rlim_t,
call_span: Span,
) -> Result<rlim_t, ShellError> {
match limit_value {
Value::Int { val, internal_span } => {
let value = rlim_t::try_from(*val).map_err(|e| ShellError::CantConvert {
to_type: "rlim_t".into(),
from_type: "i64".into(),
span: *internal_span,
help: Some(e.to_string()),
})?;
let (limit, overflow) = value.overflowing_mul(res.multiplier);
if overflow {
Ok(RLIM_INFINITY)
} else {
Ok(limit)
}
}
Value::Filesize { val, internal_span } => {
if res.multiplier != 1024 {
return Err(ShellError::TypeMismatch {
err_message: format!(
"filesize is not compatible with resource {:?}",
res.resource
),
span: *internal_span,
});
}
rlim_t::try_from(*val).map_err(|e| ShellError::CantConvert {
to_type: "rlim_t".into(),
from_type: "i64".into(),
span: *internal_span,
help: Some(e.to_string()),
})
}
Value::String { val, internal_span } => {
if val == "unlimited" {
Ok(RLIM_INFINITY)
} else if val == "soft" {
if soft {
Ok(hard_limit)
} else {
Ok(soft_limit)
}
} else if val == "hard" {
Ok(hard_limit)
} else {
return Err(ShellError::IncorrectValue {
msg: "Only unlimited, soft and hard are supported for strings".into(),
val_span: *internal_span,
call_span,
});
}
}
_ => Err(ShellError::TypeMismatch {
err_message: format!(
"string, int or filesize required, you provide {}",
limit_value.get_type()
),
span: limit_value.span(),
}),
}
}
#[derive(Clone)]
pub struct ULimit;
impl Command for ULimit {
fn name(&self) -> &str {
"ulimit"
}
fn usage(&self) -> &str {
"Set or get resource usage limits."
}
fn signature(&self) -> Signature {
let mut sig = Signature::build("ulimit")
.input_output_types(vec![
(Type::Nothing, Type::Table(vec![])),
(Type::Nothing, Type::Nothing),
])
.switch("soft", "Sets soft resource limit", Some('S'))
.switch("hard", "Sets hard resource limit", Some('H'))
.switch("all", "Prints all current limits", Some('a'))
.optional("limit", SyntaxShape::Any, "Limit value.")
.category(Category::Platform);
for res in RESOURCE_ARRAY.iter() {
sig = sig.switch(res.name, res.desc, Some(res.flag));
}
sig
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let mut soft = call.has_flag("soft");
let mut hard = call.has_flag("hard");
let all = call.has_flag("all");
if !hard && !soft {
// Set both hard and soft limits if neither was specified.
hard = true;
soft = true;
}
if let Some(limit_value) = call.opt::<Value>(engine_state, stack, 0)? {
let mut set_default_limit = true;
for res in RESOURCE_ARRAY.iter() {
if call.has_flag(res.name) {
set_limits(&limit_value, res, soft, hard, call.head)?;
if set_default_limit {
set_default_limit = false;
}
}
}
// Set `RLIMIT_FSIZE` limit if no resource flag provided.
if set_default_limit {
let res = ResourceInfo::default();
set_limits(&limit_value, &res, hard, soft, call.head)?;
}
Ok(PipelineData::Empty)
} else {
print_limits(call, all, soft, hard)
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Print all current limits",
example: "ulimit -a",
result: None,
},
Example {
description: "Print specified limits",
example: "ulimit --core-size --data-size --file-size",
result: None,
},
Example {
description: "Set limit",
example: "ulimit --core-size 102400",
result: None,
},
Example {
description: "Set stack size soft limit",
example: "ulimit -s -S 10240",
result: None,
},
Example {
description: "Set virtual memory size hard limit",
example: "ulimit -v -H 10240",
result: None,
},
Example {
description: "Set core size limit to unlimited",
example: "ulimit -c unlimited",
result: None,
},
]
}
fn search_terms(&self) -> Vec<&str> {
vec!["resource", "limits"]
}
}

View File

@ -1,35 +1,19 @@
#[cfg(feature = "sqlite")]
mod create;
#[cfg(feature = "sqlite")]
mod delete;
#[cfg(feature = "sqlite")]
mod export;
#[cfg(feature = "sqlite")]
mod import;
#[cfg(feature = "sqlite")]
mod insert;
#[cfg(feature = "sqlite")]
mod open;
#[cfg(feature = "sqlite")]
mod reset;
mod stor_;
#[cfg(feature = "sqlite")]
mod update;
#[cfg(feature = "sqlite")]
pub use create::StorCreate;
#[cfg(feature = "sqlite")]
pub use delete::StorDelete;
#[cfg(feature = "sqlite")]
pub use export::StorExport;
#[cfg(feature = "sqlite")]
pub use import::StorImport;
#[cfg(feature = "sqlite")]
pub use insert::StorInsert;
#[cfg(feature = "sqlite")]
pub use open::StorOpen;
#[cfg(feature = "sqlite")]
pub use reset::StorReset;
pub use stor_::Stor;
#[cfg(feature = "sqlite")]
pub use update::StorUpdate;

View File

@ -199,7 +199,10 @@ fn detect_columns(
};
if !(l_idx <= r_idx && (r_idx >= 0 || l_idx < (cols.len() as isize))) {
return Value::record(Record { cols, vals }, name_span);
return Value::record(
Record::from_raw_cols_vals(cols, vals),
name_span,
);
}
(l_idx.max(0) as usize, (r_idx as usize + 1).min(cols.len()))
@ -210,7 +213,7 @@ fn detect_columns(
}
}
} else {
return Value::record(Record { cols, vals }, name_span);
return Value::record(Record::from_raw_cols_vals(cols, vals), name_span);
};
// Merge Columns
@ -232,7 +235,7 @@ fn detect_columns(
vals.push(binding);
last_seg.into_iter().for_each(|v| vals.push(v));
Value::record(Record { cols, vals }, name_span)
Value::record(Record::from_raw_cols_vals(cols, vals), name_span)
})
.into_pipeline_data(ctrlc))
} else {

View File

@ -133,27 +133,22 @@ impl Command for SubCommand {
result: Some(Value::test_string("Nu shell")),
},
Example {
description: "Trim a specific character",
example: "'=== Nu shell ===' | str trim --char '=' | str trim",
result: Some(Value::test_string("Nu shell")),
description: "Trim a specific character (not the whitespace)",
example: "'=== Nu shell ===' | str trim --char '='",
result: Some(Value::test_string(" Nu shell ")),
},
Example {
description: "Trim whitespace from the beginning of string",
example: "' Nu shell ' | str trim --left",
result: Some(Value::test_string("Nu shell ")),
},
Example {
description: "Trim a specific character",
example: "'=== Nu shell ===' | str trim --char '='",
result: Some(Value::test_string(" Nu shell ")),
},
Example {
description: "Trim whitespace from the end of string",
example: "' Nu shell ' | str trim --right",
result: Some(Value::test_string(" Nu shell")),
},
Example {
description: "Trim a specific character",
description: "Trim a specific character only from the end of the string",
example: "'=== Nu shell ===' | str trim --right --char '='",
result: Some(Value::test_string("=== Nu shell ")),
},

View File

@ -101,6 +101,8 @@ impl Command for Complete {
Ok(Value::record(record, call.head).into_pipeline_data())
}
// bubble up errors from the previous command
PipelineData::Value(Value::Error { error, .. }, _) => Err(*error),
_ => Err(ShellError::GenericError {
error: "Complete only works with external streams".into(),
msg: "complete only works on external streams".into(),

View File

@ -5,7 +5,6 @@ use itertools::Itertools;
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "ios")
))]
use nu_protocol::Span;
use nu_protocol::{
@ -19,7 +18,6 @@ use nu_protocol::{
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "ios")
))]
use procfs::WithCurrentSystemInfo;
@ -123,7 +121,6 @@ fn run_ps(engine_state: &EngineState, call: &Call) -> Result<PipelineData, Shell
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
not(target_os = "ios")
))]
{
let proc_stat = proc

View File

@ -1,8 +1,9 @@
use nu_cmd_base::hook::eval_hook;
use nu_engine::env_to_strings;
use nu_engine::eval_expression;
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, Expr, Expression},
ast::{Call, Expr},
did_you_mean,
engine::{Command, EngineState, Stack},
Category, Example, ListStream, PipelineData, RawStream, ShellError, Signature, Span, Spanned,
@ -113,7 +114,6 @@ pub fn create_external_command(
trim_end_newline: bool,
) -> Result<ExternalCommand, ShellError> {
let name: Spanned<String> = call.req(engine_state, stack, 0)?;
let args: Vec<Value> = call.rest(engine_state, stack, 1)?;
// Translate environment variables from Values to Strings
let env_vars_str = env_to_strings(engine_state, stack)?;
@ -132,11 +132,24 @@ pub fn create_external_command(
}
let mut spanned_args = vec![];
let args_expr: Vec<Expression> = call.positional_iter().skip(1).cloned().collect();
let mut arg_keep_raw = vec![];
for (one_arg, one_arg_expr) in args.into_iter().zip(args_expr) {
match one_arg {
for (arg, spread) in call.rest_iter(1) {
// TODO: Disallow automatic spreading entirely later. This match block will
// have to be refactored, and lists will have to be disallowed in the parser too
match eval_expression(engine_state, stack, arg)? {
Value::List { vals, .. } => {
if !spread {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "Automatically spreading lists is deprecated".into(),
msg: "Spreading lists automatically when calling external commands is deprecated and will be removed in 0.91.".into(),
span: Some(arg.span),
help: Some("Use the spread operator (put a '...' before the argument)".into()),
inner: vec![],
},
);
}
// turn all the strings in the array into params.
// Example: one_arg may be something like ["ls" "-a"]
// convert it to "ls" "-a"
@ -147,15 +160,20 @@ pub fn create_external_command(
}
}
val => {
spanned_args.push(value_as_spanned(val)?);
match one_arg_expr.expr {
// refer to `parse_dollar_expr` function
// the expression type of $variable_name, $"($variable_name)"
// will be Expr::StringInterpolation, Expr::FullCellPath
Expr::StringInterpolation(_) | Expr::FullCellPath(_) => arg_keep_raw.push(true),
_ => arg_keep_raw.push(false),
if spread {
return Err(ShellError::CannotSpreadAsList { span: arg.span });
} else {
spanned_args.push(value_as_spanned(val)?);
match arg.expr {
// refer to `parse_dollar_expr` function
// the expression type of $variable_name, $"($variable_name)"
// will be Expr::StringInterpolation, Expr::FullCellPath
Expr::StringInterpolation(_) | Expr::FullCellPath(_) => {
arg_keep_raw.push(true)
}
_ => arg_keep_raw.push(false),
}
}
{}
}
}
}

View File

@ -8,7 +8,7 @@ use nu_protocol::{
};
use std::time::{Duration, UNIX_EPOCH};
use sysinfo::{
ComponentExt, CpuExt, CpuRefreshKind, DiskExt, NetworkExt, System, SystemExt, UserExt,
Components, CpuRefreshKind, Disks, Networks, System, Users, MINIMUM_CPU_UPDATE_INTERVAL,
};
#[derive(Clone)]
@ -106,14 +106,12 @@ pub fn trim_cstyle_null(s: String) -> String {
}
pub fn disks(span: Span) -> Value {
let mut sys = System::new();
sys.refresh_disks();
sys.refresh_disks_list();
let disks = Disks::new_with_refreshed_list();
let mut output = vec![];
for disk in sys.disks() {
for disk in disks.list() {
let device = trim_cstyle_null(disk.name().to_string_lossy().to_string());
let typ = trim_cstyle_null(String::from_utf8_lossy(disk.file_system()).to_string());
let typ = trim_cstyle_null(disk.file_system().to_string_lossy().to_string());
let record = record! {
"device" => Value::string(device, span),
@ -131,12 +129,10 @@ pub fn disks(span: Span) -> Value {
}
pub fn net(span: Span) -> Value {
let mut sys = System::new();
sys.refresh_networks();
sys.refresh_networks_list();
let networks = Networks::new_with_refreshed_list();
let mut output = vec![];
for (iface, data) in sys.networks() {
for (iface, data) in networks.list() {
let record = record! {
"name" => Value::string(trim_cstyle_null(iface.to_string()), span),
"sent" => Value::filesize(data.total_transmitted() as i64, span),
@ -154,7 +150,7 @@ pub fn cpu(span: Span) -> Value {
// We must refresh the CPU twice a while apart to get valid usage data.
// In theory we could just sleep MINIMUM_CPU_UPDATE_INTERVAL, but I've noticed that
// that gives poor results (error of ~5%). Decided to wait 2x that long, somewhat arbitrarily
std::thread::sleep(System::MINIMUM_CPU_UPDATE_INTERVAL * 2);
std::thread::sleep(MINIMUM_CPU_UPDATE_INTERVAL * 2);
sys.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage());
let mut output = vec![];
@ -163,7 +159,7 @@ pub fn cpu(span: Span) -> Value {
// Round to 1DP (chosen somewhat arbitrarily) so people aren't misled by high-precision floats.
let rounded_usage = (cpu.cpu_usage() * 10.0).round() / 10.0;
let load_avg = sys.load_average();
let load_avg = System::load_average();
let load_avg = trim_cstyle_null(format!(
"{:.2}, {:.2}, {:.2}",
load_avg.one, load_avg.five, load_avg.fifteen
@ -211,42 +207,39 @@ pub fn mem(span: Span) -> Value {
}
pub fn host(span: Span) -> Value {
let mut sys = System::new();
sys.refresh_users_list();
let mut record = Record::new();
if let Some(name) = sys.name() {
if let Some(name) = System::name() {
record.push("name", Value::string(trim_cstyle_null(name), span));
}
if let Some(version) = sys.os_version() {
if let Some(version) = System::os_version() {
record.push("os_version", Value::string(trim_cstyle_null(version), span));
}
if let Some(long_version) = sys.long_os_version() {
if let Some(long_version) = System::long_os_version() {
record.push(
"long_os_version",
Value::string(trim_cstyle_null(long_version), span),
);
}
if let Some(version) = sys.kernel_version() {
if let Some(version) = System::kernel_version() {
record.push(
"kernel_version",
Value::string(trim_cstyle_null(version), span),
);
}
if let Some(hostname) = sys.host_name() {
if let Some(hostname) = System::host_name() {
record.push("hostname", Value::string(trim_cstyle_null(hostname), span));
}
record.push(
"uptime",
Value::duration(1000000000 * sys.uptime() as i64, span),
Value::duration(1000000000 * System::uptime() as i64, span),
);
// Creates a new SystemTime from the specified number of whole seconds
let d = UNIX_EPOCH + Duration::from_secs(sys.boot_time());
let d = UNIX_EPOCH + Duration::from_secs(System::boot_time());
// Create DateTime from SystemTime
let datetime = DateTime::<Local>::from(d);
// Convert to local time and then rfc3339
@ -254,11 +247,16 @@ pub fn host(span: Span) -> Value {
record.push("boot_time", Value::string(timestamp_str, span));
let mut users = vec![];
for user in sys.users() {
let users = Users::new_with_refreshed_list();
let mut users_list = vec![];
for user in users.list() {
let mut groups = vec![];
for group in user.groups() {
groups.push(Value::string(trim_cstyle_null(group.to_string()), span));
groups.push(Value::string(
trim_cstyle_null(group.name().to_string()),
span,
));
}
let record = record! {
@ -266,24 +264,22 @@ pub fn host(span: Span) -> Value {
"groups" => Value::list(groups, span),
};
users.push(Value::record(record, span));
users_list.push(Value::record(record, span));
}
if !users.is_empty() {
record.push("sessions", Value::list(users, span));
record.push("sessions", Value::list(users_list, span));
}
Value::record(record, span)
}
pub fn temp(span: Span) -> Value {
let mut sys = System::new();
sys.refresh_components();
sys.refresh_components_list();
let components = Components::new_with_refreshed_list();
let mut output = vec![];
for component in sys.components() {
for component in components.list() {
let mut record = record! {
"unit" => Value::string(component.label(), span),
"temp" => Value::float(component.temperature() as f64, span),

View File

@ -0,0 +1,29 @@
use nu_test_support::nu;
#[test]
fn basic_stdout() {
let without_complete = nu!(r#"
nu --testbin cococo test
"#);
let with_complete = nu!(r#"
(nu --testbin cococo test | complete).stdout
"#);
assert_eq!(with_complete.out, without_complete.out);
}
#[test]
fn basic_exit_code() {
let with_complete = nu!(r#"
(nu --testbin cococo test | complete).exit_code
"#);
assert_eq!(with_complete.out, "0");
}
#[test]
fn error() {
let actual = nu!("do { not-found } | complete");
assert!(actual.err.contains("executable was not found"));
}

View File

@ -197,7 +197,7 @@ fn def_wrapped_with_block() {
#[test]
fn def_wrapped_from_module() {
let actual = nu!(r#"module spam {
export def --wrapped my-echo [...rest] { ^echo $rest }
export def --wrapped my-echo [...rest] { nu --testbin cococo ...$rest }
}
use spam

View File

@ -49,7 +49,7 @@ fn exec_misc_values() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
nu -c 'let x = "abc"; exec nu --testbin cococo $x [ a b c ]'
nu -c 'let x = "abc"; exec nu --testbin cococo $x ...[ a b c ]'
"#
));

View File

@ -7,6 +7,7 @@ mod break_;
mod cal;
mod cd;
mod compact;
mod complete;
mod config_env_default;
mod config_nu_default;
mod continue_;
@ -106,6 +107,8 @@ mod touch;
mod transpose;
mod try_;
mod ucp;
#[cfg(unix)]
mod ulimit;
mod umkdir;
mod uniq;
mod uniq_by;

View File

@ -38,3 +38,68 @@ fn http_delete_failed_due_to_server_error() {
assert!(actual.err.contains("Bad request (400)"))
}
#[test]
fn http_delete_follows_redirect() {
let mut server = Server::new();
let _mock = server.mock("GET", "/bar").with_body("bar").create();
let _mock = server
.mock("DELETE", "/foo")
.with_status(301)
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!("http delete {url}/foo", url = server.url()).as_str()
));
assert_eq!(&actual.out, "bar");
}
#[test]
fn http_delete_redirect_mode_manual() {
let mut server = Server::new();
let _mock = server
.mock("DELETE", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http delete --redirect-mode manual {url}/foo",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "foo");
}
#[test]
fn http_delete_redirect_mode_error() {
let mut server = Server::new();
let _mock = server
.mock("DELETE", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http delete --redirect-mode error {url}/foo",
url = server.url()
)
.as_str()
));
assert!(&actual.err.contains("nu::shell::network_failure"));
assert!(&actual.err.contains(
"Redirect encountered when redirect handling mode was 'error' (301 Moved Permanently)"
));
}

View File

@ -176,6 +176,71 @@ fn http_get_full_response() {
assert_eq!(header["value"], "close");
}
#[test]
fn http_get_follows_redirect() {
let mut server = Server::new();
let _mock = server.mock("GET", "/bar").with_body("bar").create();
let _mock = server
.mock("GET", "/foo")
.with_status(301)
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!("http get {url}/foo", url = server.url()).as_str()
));
assert_eq!(&actual.out, "bar");
}
#[test]
fn http_get_redirect_mode_manual() {
let mut server = Server::new();
let _mock = server
.mock("GET", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http get --redirect-mode manual {url}/foo",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "foo");
}
#[test]
fn http_get_redirect_mode_error() {
let mut server = Server::new();
let _mock = server
.mock("GET", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http get --redirect-mode error {url}/foo",
url = server.url()
)
.as_str()
));
assert!(&actual.err.contains("nu::shell::network_failure"));
assert!(&actual.err.contains(
"Redirect encountered when redirect handling mode was 'error' (301 Moved Permanently)"
));
}
// These tests require network access; they use badssl.com which is a Google-affiliated site for testing various SSL errors.
// Revisit this if these tests prove to be flaky or unstable.

View File

@ -39,3 +39,75 @@ fn http_head_failed_due_to_server_error() {
assert!(actual.err.contains("Bad request (400)"))
}
#[test]
fn http_head_follows_redirect() {
let mut server = Server::new();
let _mock = server
.mock("HEAD", "/bar")
.with_header("bar", "bar")
.create();
let _mock = server
.mock("HEAD", "/foo")
.with_status(301)
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http head {url}/foo | (where name == bar).0.value",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "bar");
}
#[test]
fn http_head_redirect_mode_manual() {
let mut server = Server::new();
let _mock = server
.mock("HEAD", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http head --redirect-mode manual {url}/foo | (where name == location).0.value",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "/bar");
}
#[test]
fn http_head_redirect_mode_error() {
let mut server = Server::new();
let _mock = server
.mock("HEAD", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http head --redirect-mode error {url}/foo",
url = server.url()
)
.as_str()
));
assert!(&actual.err.contains("nu::shell::network_failure"));
assert!(&actual.err.contains(
"Redirect encountered when redirect handling mode was 'error' (301 Moved Permanently)"
));
}

View File

@ -76,3 +76,68 @@ fn http_patch_failed_due_to_unexpected_body() {
assert!(actual.err.contains("Cannot make request"))
}
#[test]
fn http_patch_follows_redirect() {
let mut server = Server::new();
let _mock = server.mock("GET", "/bar").with_body("bar").create();
let _mock = server
.mock("PATCH", "/foo")
.with_status(301)
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!("http patch {url}/foo patchbody", url = server.url()).as_str()
));
assert_eq!(&actual.out, "bar");
}
#[test]
fn http_patch_redirect_mode_manual() {
let mut server = Server::new();
let _mock = server
.mock("PATCH", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http patch --redirect-mode manual {url}/foo patchbody",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "foo");
}
#[test]
fn http_patch_redirect_mode_error() {
let mut server = Server::new();
let _mock = server
.mock("PATCH", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http patch --redirect-mode error {url}/foo patchbody",
url = server.url()
)
.as_str()
));
assert!(&actual.err.contains("nu::shell::network_failure"));
assert!(&actual.err.contains(
"Redirect encountered when redirect handling mode was 'error' (301 Moved Permanently)"
));
}

View File

@ -112,3 +112,68 @@ fn http_post_json_list_is_success() {
mock.assert();
assert!(actual.out.is_empty())
}
#[test]
fn http_post_follows_redirect() {
let mut server = Server::new();
let _mock = server.mock("GET", "/bar").with_body("bar").create();
let _mock = server
.mock("POST", "/foo")
.with_status(301)
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!("http post {url}/foo postbody", url = server.url()).as_str()
));
assert_eq!(&actual.out, "bar");
}
#[test]
fn http_post_redirect_mode_manual() {
let mut server = Server::new();
let _mock = server
.mock("POST", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http post --redirect-mode manual {url}/foo postbody",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "foo");
}
#[test]
fn http_post_redirect_mode_error() {
let mut server = Server::new();
let _mock = server
.mock("POST", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http post --redirect-mode error {url}/foo postbody",
url = server.url()
)
.as_str()
));
assert!(&actual.err.contains("nu::shell::network_failure"));
assert!(&actual.err.contains(
"Redirect encountered when redirect handling mode was 'error' (301 Moved Permanently)"
));
}

View File

@ -76,3 +76,68 @@ fn http_put_failed_due_to_unexpected_body() {
assert!(actual.err.contains("Cannot make request"))
}
#[test]
fn http_put_follows_redirect() {
let mut server = Server::new();
let _mock = server.mock("GET", "/bar").with_body("bar").create();
let _mock = server
.mock("PUT", "/foo")
.with_status(301)
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!("http put {url}/foo putbody", url = server.url()).as_str()
));
assert_eq!(&actual.out, "bar");
}
#[test]
fn http_put_redirect_mode_manual() {
let mut server = Server::new();
let _mock = server
.mock("PUT", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http put --redirect-mode manual {url}/foo putbody",
url = server.url()
)
.as_str()
));
assert_eq!(&actual.out, "foo");
}
#[test]
fn http_put_redirect_mode_error() {
let mut server = Server::new();
let _mock = server
.mock("PUT", "/foo")
.with_status(301)
.with_body("foo")
.with_header("Location", "/bar")
.create();
let actual = nu!(pipeline(
format!(
"http put --redirect-mode error {url}/foo putbody",
url = server.url()
)
.as_str()
));
assert!(&actual.err.contains("nu::shell::network_failure"));
assert!(&actual.err.contains(
"Redirect encountered when redirect handling mode was 'error' (301 Moved Permanently)"
));
}

View File

@ -161,9 +161,14 @@ fn same_target_redirection_with_too_much_stderr_not_hang_nushell() {
#[test]
fn redirection_keep_exit_codes() {
let out = nu!("do -i { nu --testbin fail e> a.txt } | complete | get exit_code");
// needs to use contains "1", because it complete will output `Some(RawStream)`.
assert!(out.out.contains('1'));
Playground::setup("redirection preserves exit code", |dirs, _| {
let out = nu!(
cwd: dirs.test(),
"do -i { nu --testbin fail e> a.txt } | complete | get exit_code"
);
// needs to use contains "1", because it complete will output `Some(RawStream)`.
assert!(out.out.contains('1'));
});
}
#[test]
@ -302,24 +307,29 @@ fn separate_redirection_support_variable() {
#[test]
fn redirection_should_have_a_target() {
let scripts = [
"echo asdf o+e>",
"echo asdf o>",
"echo asdf e>",
"echo asdf o> e>",
"echo asdf o> tmp.txt e>",
"echo asdf o> e> tmp.txt",
"echo asdf o> | ignore",
"echo asdf o>; echo asdf",
];
for code in scripts {
let actual = nu!(code);
assert!(
actual.err.contains("expected redirection target",),
"should be error, code: {}",
code
);
}
Playground::setup("redirection_should_have_a_target", |dirs, _| {
let scripts = [
"echo asdf o+e>",
"echo asdf o>",
"echo asdf e>",
"echo asdf o> e>",
"echo asdf o> tmp.txt e>",
"echo asdf o> e> tmp.txt",
"echo asdf o> | ignore",
"echo asdf o>; echo asdf",
];
for code in scripts {
let actual = nu!(cwd: dirs.test(), code);
assert!(
actual.err.contains("expected redirection target",),
"should be error, code: {code}",
);
assert!(
!dirs.test().join("tmp.txt").exists(),
"No file should be created on error: {code}",
);
}
});
}
#[test]
@ -352,8 +362,24 @@ fn redirection_with_pipe() {
#[test]
fn no_duplicate_redirection() {
let actual = nu!("echo 3 o> a.txt o> a.txt");
assert!(actual.err.contains("Redirection can be set only once"));
let actual = nu!("echo 3 e> a.txt e> a.txt");
assert!(actual.err.contains("Redirection can be set only once"));
Playground::setup("redirection does not accept duplicate", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"echo 3 o> a.txt o> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
let actual = nu!(
cwd: dirs.test(),
"echo 3 e> a.txt e> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
});
}

View File

@ -375,6 +375,19 @@ fn removes_symlink() {
});
}
#[test]
fn removes_symlink_pointing_to_directory() {
Playground::setup("rm_symlink_to_directory", |dirs, sandbox| {
sandbox.mkdir("test").symlink("test", "test_link");
nu!(cwd: sandbox.cwd(), "rm test_link");
assert!(!dirs.test().join("test_link").exists());
// The pointed directory should not be deleted.
assert!(dirs.test().join("test").exists());
});
}
#[test]
fn removes_file_after_cd() {
Playground::setup("rm_after_cd", |dirs, sandbox| {

View File

@ -139,14 +139,13 @@ fn failed_command_with_semicolon_will_not_execute_following_cmds() {
})
}
#[cfg(not(windows))]
#[test]
fn external_args_with_quoted() {
Playground::setup("external failed command with semicolon", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
^echo "foo=bar 'hi'"
nu --testbin cococo "foo=bar 'hi'"
"#
));
@ -187,14 +186,13 @@ fn external_arg_with_variable_name() {
})
}
#[cfg(not(windows))]
#[test]
fn external_command_escape_args() {
Playground::setup("external failed command with semicolon", |dirs, _| {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
^echo "\"abcd"
nu --testbin cococo "\"abcd"
"#
));
@ -308,13 +306,12 @@ fn can_run_batch_files_without_bat_extension() {
);
}
#[cfg(windows)]
#[test]
fn quotes_trimmed_when_shelling_out() {
// regression test for a bug where we weren't trimming quotes around string args before shelling out to cmd.exe
let actual = nu!(pipeline(
r#"
^echo "foo"
nu --testbin cococo "foo"
"#
));
@ -328,7 +325,7 @@ fn redirect_combine() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
run-external --redirect-combine sh [-c 'echo Foo; echo >&2 Bar']
run-external --redirect-combine sh ...[-c 'echo Foo; echo >&2 Bar']
"#
));

View File

@ -1,6 +1,6 @@
use nu_test_support::fs::{file_contents, Stub};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
use std::io::Write;
#[test]
@ -325,3 +325,45 @@ fn save_file_correct_relative_path() {
assert_eq!(actual, "foo!");
})
}
#[test]
fn save_same_file_with_extension() {
Playground::setup("save_test_16", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
echo 'world'
| save --raw hello.md;
open --raw hello.md
| prepend 'hello'
| save --raw --force hello.md
"
)
);
assert!(actual
.err
.contains("pipeline input and output are same file"));
})
}
#[test]
fn save_same_file_without_extension() {
Playground::setup("save_test_17", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
echo 'world'
| save hello;
open hello
| prepend 'hello'
| save --force hello
"
)
);
assert!(actual
.err
.contains("pipeline input and output are same file"));
})
}

View File

@ -0,0 +1,218 @@
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn limit_set_soft1() {
Playground::setup("limit_set_soft1", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let soft = (ulimit -s | first | get soft);
ulimit -s -H $soft;
let hard = (ulimit -s | first | get hard);
$soft == $hard
"
));
assert!(actual.out.contains("true"));
});
}
#[test]
fn limit_set_soft2() {
Playground::setup("limit_set_soft2", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let soft = (ulimit -s | first | get soft);
ulimit -s -H soft;
let hard = (ulimit -s | first | get hard);
$soft == $hard
"
));
assert!(actual.out.contains("true"));
});
}
#[test]
fn limit_set_hard1() {
Playground::setup("limit_set_hard1", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let hard = (ulimit -s | first | get hard);
ulimit -s $hard;
let soft = (ulimit -s | first | get soft);
$soft == $hard
"
));
assert!(actual.out.contains("true"));
});
}
#[test]
fn limit_set_hard2() {
Playground::setup("limit_set_hard2", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let hard = (ulimit -s | first | get hard);
ulimit -s hard;
let soft = (ulimit -s | first | get soft);
$soft == $hard
"
));
assert!(actual.out.contains("true"));
});
}
#[test]
fn limit_set_invalid1() {
Playground::setup("limit_set_invalid1", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let hard = (ulimit -s | first | get hard);
match $hard {
\"unlimited\" => { echo \"unlimited\" },
$x => {
let new = $x + 1;
ulimit -s $new
}
}
"
));
assert!(
actual.out.contains("unlimited")
|| actual.err.contains("EPERM: Operation not permitted")
);
});
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[test]
fn limit_set_invalid2() {
Playground::setup("limit_set_invalid2", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(),
"
let val = -100;
ulimit -c $val
"
);
assert!(actual.err.contains("can't convert i64 to rlim_t"));
});
}
#[test]
fn limit_set_invalid3() {
Playground::setup("limit_set_invalid3", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(),
"
ulimit -c abcd
"
);
assert!(actual
.err
.contains("Only unlimited, soft and hard are supported for strings"));
});
}
#[test]
fn limit_set_invalid4() {
Playground::setup("limit_set_invalid4", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(),
"
ulimit -c 100.0
"
);
assert!(actual.err.contains("string, int or filesize required"));
});
}
#[test]
fn limit_set_invalid5() {
use nix::sys::resource::rlim_t;
let max = (rlim_t::MAX / 1024) + 1;
Playground::setup("limit_set_invalid5", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
format!(
"
let hard = (ulimit -c | first | get hard);
match $hard {{
\"unlimited\" => {{
ulimit -c -S 0;
ulimit -c {max};
ulimit -c
| first
| get soft
}},
_ => {{
echo \"unlimited\"
}}
}}
").as_str()
));
assert!(actual.out.eq("unlimited"));
});
}
#[test]
fn limit_set_filesize1() {
Playground::setup("limit_set_filesize1", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(), pipeline(
"
let hard = (ulimit -c | first | get hard);
match $hard {
\"unlimited\" => {
ulimit -c 1Mib;
ulimit -c
| first
| get soft
},
$x if $x >= 1024 * 1024 => {
ulimit -c 1Mib;
ulimit -c
| first
| get soft
}
_ => {
echo \"hard limit too small\"
}
}
"
));
assert!(actual.out.eq("1024") || actual.out.eq("hard limit too small"));
});
}
#[test]
fn limit_set_filesize2() {
Playground::setup("limit_set_filesize2", |dirs, _sandbox| {
let actual = nu!(
cwd: dirs.test(),
"
ulimit -n 10Kib
"
);
assert!(actual
.err
.contains("filesize is not compatible with resource RLIMIT_NOFILE"));
});
}

View File

@ -22,3 +22,39 @@ fn table_to_xml_text_and_from_xml_text_back_into_table() {
assert_eq!(actual.out, "true");
}
#[test]
fn to_xml_error_unknown_column() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
{tag: a bad_column: b} | to xml
"#
));
assert!(actual.err.contains("Invalid column \"bad_column\""));
}
#[test]
fn to_xml_error_no_tag() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
{attributes: {a: b c: d}} | to xml
"#
));
assert!(actual.err.contains("Tag missing"));
}
#[test]
fn to_xml_error_tag_not_string() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
{tag: 1 attributes: {a: b c: d}} | to xml
"#
));
assert!(actual.err.contains("not a string"));
}

View File

@ -1,8 +1,8 @@
use nu_protocol::{
ast::Call,
ast::{Call, Expression},
engine::{EngineState, Stack, StateWorkingSet},
eval_const::eval_constant,
FromValue, ShellError,
FromValue, ShellError, Value,
};
use crate::eval_expression;
@ -34,6 +34,10 @@ pub trait CallExt {
starting_pos: usize,
) -> Result<Vec<T>, ShellError>;
fn rest_iter_flattened<F>(&self, start: usize, eval: F) -> Result<Vec<Value>, ShellError>
where
F: FnMut(&Expression) -> Result<Value, ShellError>;
fn opt<T: FromValue>(
&self,
engine_state: &EngineState,
@ -70,7 +74,7 @@ impl CallExt for Call {
name: &str,
) -> Result<Option<T>, ShellError> {
if let Some(expr) = self.get_flag_expr(name) {
let result = eval_expression(engine_state, stack, &expr)?;
let result = eval_expression(engine_state, stack, expr)?;
FromValue::from_value(result).map(Some)
} else {
Ok(None)
@ -83,7 +87,7 @@ impl CallExt for Call {
name: &str,
) -> Result<Option<T>, ShellError> {
if let Some(expr) = self.get_flag_expr(name) {
let result = eval_constant(working_set, &expr)?;
let result = eval_constant(working_set, expr)?;
FromValue::from_value(result).map(Some)
} else {
Ok(None)
@ -98,8 +102,9 @@ impl CallExt for Call {
) -> Result<Vec<T>, ShellError> {
let mut output = vec![];
for expr in self.positional_iter().skip(starting_pos) {
let result = eval_expression(engine_state, stack, expr)?;
for result in self.rest_iter_flattened(starting_pos, |expr| {
eval_expression(engine_state, stack, expr)
})? {
output.push(FromValue::from_value(result)?);
}
@ -113,14 +118,36 @@ impl CallExt for Call {
) -> Result<Vec<T>, ShellError> {
let mut output = vec![];
for expr in self.positional_iter().skip(starting_pos) {
let result = eval_constant(working_set, expr)?;
for result in
self.rest_iter_flattened(starting_pos, |expr| eval_constant(working_set, expr))?
{
output.push(FromValue::from_value(result)?);
}
Ok(output)
}
fn rest_iter_flattened<F>(&self, start: usize, mut eval: F) -> Result<Vec<Value>, ShellError>
where
F: FnMut(&Expression) -> Result<Value, ShellError>,
{
let mut output = Vec::new();
for (expr, spread) in self.rest_iter(start) {
let result = eval(expr)?;
if spread {
match result {
Value::List { mut vals, .. } => output.append(&mut vals),
_ => return Err(ShellError::CannotSpreadAsList { span: expr.span }),
}
} else {
output.push(result);
}
}
Ok(output)
}
fn opt<T: FromValue>(
&self,
engine_state: &EngineState,

View File

@ -1,9 +1,9 @@
use crate::{current_dir_str, get_full_help};
use crate::{call_ext::CallExt, current_dir_str, get_full_help};
use nu_path::expand_path_with;
use nu_protocol::{
ast::{
Argument, Assignment, Block, Call, Expr, Expression, PathMember, PipelineElement,
Redirection,
Argument, Assignment, Block, Call, Expr, Expression, ExternalArgument, PathMember,
PipelineElement, Redirection,
},
engine::{Closure, EngineState, Stack},
eval_base::Eval,
@ -66,11 +66,11 @@ pub fn eval_call(
if let Some(rest_positional) = decl.signature().rest_positional {
let mut rest_items = vec![];
for arg in call.positional_iter().skip(
for result in call.rest_iter_flattened(
decl.signature().required_positional.len()
+ decl.signature().optional_positional.len(),
) {
let result = eval_expression(engine_state, caller_stack, arg)?;
|expr| eval_expression(engine_state, caller_stack, expr),
)? {
rest_items.push(result);
}
@ -182,7 +182,7 @@ fn eval_external(
engine_state: &EngineState,
stack: &mut Stack,
head: &Expression,
args: &[Expression],
args: &[ExternalArgument],
input: PipelineData,
redirect_target: RedirectTarget,
is_subexpression: bool,
@ -198,7 +198,10 @@ fn eval_external(
call.add_positional(head.clone());
for arg in args {
call.add_positional(arg.clone())
match arg {
ExternalArgument::Regular(expr) => call.add_positional(expr.clone()),
ExternalArgument::Spread(expr) => call.add_spread(expr.clone()),
}
}
match redirect_target {
@ -947,7 +950,7 @@ impl Eval for EvalRuntime {
engine_state: &EngineState,
stack: &mut Stack,
head: &Expression,
args: &[Expression],
args: &[ExternalArgument],
is_subexpression: bool,
_: Span,
) -> Result<Value, ShellError> {
@ -1099,7 +1102,18 @@ impl Eval for EvalRuntime {
.get_block(block_id)
.captures
.iter()
.map(|&id| stack.get_var(id, span).map(|var| (id, var)))
.map(|&id| {
stack
.get_var(id, span)
.or_else(|_| {
engine_state
.get_var(id)
.const_val
.clone()
.ok_or(ShellError::VariableNotFoundAtRuntime { span })
})
.map(|var| (id, var))
})
.collect::<Result<_, _>>()?;
Ok(Value::closure(Closure { block_id, captures }, span))

View File

@ -20,10 +20,10 @@ nu-table = { path = "../nu-table", version = "0.88.2" }
nu-json = { path = "../nu-json", version = "0.88.2" }
nu-utils = { path = "../nu-utils", version = "0.88.2" }
terminal_size = "0.2"
terminal_size = "0.3"
strip-ansi-escapes = "0.2.0"
crossterm = "0.27"
ratatui = "0.23"
ansi-str = "0.8"
unicode-width = "0.1"
lscolors = { version = "0.15", default-features = false, features = ["nu-ansi-term"] }
lscolors = { version = "0.16", default-features = false, features = ["nu-ansi-term"] }

View File

@ -15,7 +15,7 @@ nu-protocol = { path = "../nu-protocol", version = "0.88.2" }
reedline = { version = "0.27" }
crossbeam-channel = "0.5.8"
lsp-types = "0.94.1"
lsp-types = "0.95.0"
lsp-server = "0.7.5"
miette = "5.10"
ropey = "1.6.1"

View File

@ -312,7 +312,7 @@ impl LanguageServer {
}
}
}
_ => {}
Id::Value(_) => {}
}
None
}
@ -347,11 +347,22 @@ impl LanguageServer {
Id::Declaration(decl_id) => {
let decl = working_set.get_decl(decl_id);
let mut description = "\n### Signature\n```\n".to_string();
let mut description = String::new();
// First description
description.push_str(&format!("{}\n", decl.usage().replace('\r', "")));
// Additional description
if !decl.extra_usage().is_empty() {
description.push_str(&format!("\n{}\n", decl.extra_usage()));
}
// Usage
description.push_str("### Usage \n```\n");
let signature = decl.signature();
description.push_str(&format!(" {}", signature.name));
if !signature.named.is_empty() {
description.push_str(" {flags}")
description.push_str(" {flags}");
}
for required_arg in &signature.required_positional {
description.push_str(&format!(" <{}>", required_arg.name));
@ -363,6 +374,39 @@ impl LanguageServer {
description.push_str(&format!(" <...{}>", arg.name));
}
description.push_str("\n```\n");
// Flags
if !signature.named.is_empty() {
description.push_str("\n### Flags\n\n");
let mut first = true;
for named in &signature.named {
if first {
first = false;
} else {
description.push('\n');
}
description.push_str(" ");
if let Some(short_flag) = &named.short {
description.push_str(&format!("`-{short_flag}`"));
}
if !named.long.is_empty() {
if named.short.is_some() {
description.push_str(", ");
}
description.push_str(&format!("`--{}`", named.long));
}
if let Some(arg) = &named.arg {
description.push_str(&format!(" `<{}>`", arg.to_type()));
}
if !named.desc.is_empty() {
description.push_str(&format!(" - {}", named.desc));
}
description.push('\n');
}
description.push('\n');
}
// Parameters
if !signature.required_positional.is_empty()
|| !signature.optional_positional.is_empty()
|| signature.rest_positional.is_some()
@ -370,10 +414,10 @@ impl LanguageServer {
description.push_str("\n### Parameters\n\n");
let mut first = true;
for required_arg in &signature.required_positional {
if !first {
description.push('\n');
} else {
if first {
first = false;
} else {
description.push('\n');
}
description.push_str(&format!(
" `{}: {}`",
@ -386,10 +430,10 @@ impl LanguageServer {
description.push('\n');
}
for optional_arg in &signature.optional_positional {
if !first {
description.push('\n');
} else {
if first {
first = false;
} else {
description.push('\n');
}
description.push_str(&format!(
" `{}: {}`",
@ -417,36 +461,10 @@ impl LanguageServer {
}
description.push('\n');
}
if !signature.named.is_empty() {
description.push_str("\n### Flags\n\n");
let mut first = true;
for named in &signature.named {
if !first {
description.push('\n');
} else {
first = false;
}
description.push_str(" ");
if let Some(short_flag) = &named.short {
description.push_str(&format!("`-{}`", short_flag));
}
if !named.long.is_empty() {
if named.short.is_some() {
description.push_str(", ")
}
description.push_str(&format!("`--{}`", named.long));
}
if let Some(arg) = &named.arg {
description.push_str(&format!(" `<{}>`", arg.to_type()))
}
if !named.desc.is_empty() {
description.push_str(&format!(" - {}", named.desc));
}
}
description.push('\n');
}
// Input/output types
if !signature.input_output_types.is_empty() {
description.push_str("\n### Input/output\n");
description.push_str("\n### Input/output types\n");
description.push_str("\n```\n");
for input_output in &signature.input_output_types {
description
@ -454,14 +472,8 @@ impl LanguageServer {
}
description.push_str("\n```\n");
}
description.push_str(&format!(
"### Usage\n {}\n",
decl.usage().replace('\r', "")
));
if !decl.extra_usage().is_empty() {
description
.push_str(&format!("\n### Extra usage:\n {}\n", decl.extra_usage()));
}
// Examples
if !decl.examples().is_empty() {
description.push_str("### Example(s)\n");
for example in decl.examples() {
@ -578,8 +590,9 @@ mod tests {
},
request::{Completion, GotoDefinition, HoverRequest, Initialize, Request, Shutdown},
CompletionParams, DidChangeTextDocumentParams, DidOpenTextDocumentParams,
GotoDefinitionParams, InitializeParams, InitializedParams, TextDocumentContentChangeEvent,
TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url,
GotoDefinitionParams, InitializeParams, InitializedParams, PartialResultParams,
TextDocumentContentChangeEvent, TextDocumentIdentifier, TextDocumentItem,
TextDocumentPositionParams, Url, WorkDoneProgressParams,
};
use nu_test_support::fs::{fixtures, root};
use std::sync::mpsc::Receiver;
@ -671,8 +684,8 @@ mod tests {
character: 0,
},
},
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: PartialResultParams::default(),
})
.unwrap(),
}))
@ -777,8 +790,8 @@ mod tests {
text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character },
},
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: PartialResultParams::default(),
})
.unwrap(),
}))
@ -894,7 +907,7 @@ mod tests {
text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character },
},
work_done_progress_params: Default::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
})
.unwrap(),
}))
@ -957,7 +970,7 @@ mod tests {
serde_json::json!({
"contents": {
"kind": "markdown",
"value": "\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some greeting message\n"
"value": "Renders some greeting message\n### Usage \n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n"
}
})
);
@ -974,8 +987,8 @@ mod tests {
text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character },
},
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: PartialResultParams::default(),
context: None,
})
.unwrap(),

View File

@ -121,7 +121,7 @@ mod tests {
serde_json::json!({
"contents": {
"kind": "markdown",
"value": "\n### Signature\n```\n let {flags} <var_name> <initial_value>\n```\n\n### Parameters\n\n `var_name: any` - Variable name.\n\n `initial_value: any` - Equals sign followed by value.\n\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n### Input/output\n\n```\n any | nothing\n\n```\n### Usage\n Create a variable and give it a value.\n\n### Extra usage:\n This command is a parser keyword. For details, check:\n https://www.nushell.sh/book/thinking_in_nu.html\n### Example(s)\n Set a variable to a value\n```\n let x = 10\n```\n Set a variable to the result of an expression\n```\n let x = 10 + 100\n```\n Set a variable based on the condition\n```\n let x = if false { -1 } else { 1 }\n```\n"
"value": "Create a variable and give it a value.\n\nThis command is a parser keyword. For details, check:\n https://www.nushell.sh/book/thinking_in_nu.html\n### Usage \n```\n let {flags} <var_name> <initial_value>\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n\n### Parameters\n\n `var_name: any` - Variable name.\n\n `initial_value: any` - Equals sign followed by value.\n\n\n### Input/output types\n\n```\n any | nothing\n\n```\n### Example(s)\n Set a variable to a value\n```\n let x = 10\n```\n Set a variable to the result of an expression\n```\n let x = 10 + 100\n```\n Set a variable based on the condition\n```\n let x = if false { -1 } else { 1 }\n```\n"
}
})
);
@ -162,7 +162,7 @@ hello"#,
serde_json::json!({
"contents": {
"kind": "markdown",
"value": "\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some updated greeting message\n"
"value": "Renders some updated greeting message\n### Usage \n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n"
}
})
);
@ -207,7 +207,7 @@ hello"#,
serde_json::json!({
"contents": {
"kind": "markdown",
"value": "\n### Signature\n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n### Usage\n Renders some updated greeting message\n"
"value": "Renders some updated greeting message\n### Usage \n```\n hello {flags}\n```\n\n### Flags\n\n `-h`, `--help` - Display the help message for this command\n\n"
}
})
);

View File

@ -19,7 +19,7 @@ nu-protocol = { path = "../nu-protocol", version = "0.88.2" }
bytesize = "1.3"
chrono = { default-features = false, features = ['std'], version = "0.4" }
itertools = "0.11"
itertools = "0.12"
log = "0.4"
serde_json = "1.0"

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::{
Block, Expr, Expression, ImportPatternMember, MatchPattern, PathMember, Pattern, Pipeline,
PipelineElement, RecordItem,
Argument, Block, Expr, Expression, ExternalArgument, ImportPatternMember, MatchPattern,
PathMember, Pattern, Pipeline, PipelineElement, RecordItem,
};
use nu_protocol::{engine::StateWorkingSet, Span};
use nu_protocol::{DeclId, VarId};
@ -193,17 +193,28 @@ pub fn flatten_expression(
}
let mut args = vec![];
for positional in call.positional_iter() {
let flattened = flatten_expression(working_set, positional);
args.extend(flattened);
}
for named in call.named_iter() {
if named.0.span.end != 0 {
// Ignore synthetic flags
args.push((named.0.span, FlatShape::Flag));
}
if let Some(expr) = &named.2 {
args.extend(flatten_expression(working_set, expr));
for arg in &call.arguments {
match arg {
Argument::Positional(positional) | Argument::Unknown(positional) => {
let flattened = flatten_expression(working_set, positional);
args.extend(flattened);
}
Argument::Named(named) => {
if named.0.span.end != 0 {
// Ignore synthetic flags
args.push((named.0.span, FlatShape::Flag));
}
if let Some(expr) = &named.2 {
args.extend(flatten_expression(working_set, expr));
}
}
Argument::Spread(expr) => {
args.push((
Span::new(expr.span.start - 3, expr.span.start),
FlatShape::Operator,
));
args.extend(flatten_expression(working_set, expr));
}
}
}
// sort these since flags and positional args can be intermixed
@ -231,15 +242,24 @@ pub fn flatten_expression(
for arg in args {
//output.push((*arg, FlatShape::ExternalArg));
match arg {
Expression {
expr: Expr::String(..),
span,
..
} => {
output.push((*span, FlatShape::ExternalArg));
}
_ => {
output.extend(flatten_expression(working_set, arg));
ExternalArgument::Regular(expr) => match expr {
Expression {
expr: Expr::String(..),
span,
..
} => {
output.push((*span, FlatShape::ExternalArg));
}
_ => {
output.extend(flatten_expression(working_set, expr));
}
},
ExternalArgument::Spread(expr) => {
output.push((
Span::new(expr.span.start - 3, expr.span.start),
FlatShape::Operator,
));
output.extend(flatten_expression(working_set, expr));
}
}
}
@ -264,10 +284,6 @@ pub fn flatten_expression(
Expr::Float(_) => {
vec![(expr.span, FlatShape::Float)]
}
Expr::MatchPattern(pattern) => {
// FIXME: do nicer flattening later
flatten_pattern(pattern)
}
Expr::MatchBlock(matches) => {
let mut output = vec![];

Some files were not shown because too many files have changed in this diff Show More