mirror of
https://github.com/nushell/nushell.git
synced 2025-07-05 00:50:11 +02:00
Compare commits
385 Commits
Author | SHA1 | Date | |
---|---|---|---|
fb26109049 | |||
d99905b604 | |||
a8890d5cca | |||
5139054325 | |||
039d0a685a | |||
e0685315b4 | |||
02fc844e40 | |||
b48f50f018 | |||
dc0ac8e917 | |||
f2e8c391a2 | |||
7029d24f42 | |||
4e8289d7bb | |||
bf8763fc11 | |||
11375c19d2 | |||
8f4feeb119 | |||
e26364f885 | |||
fff0c6e2cb | |||
68c2729991 | |||
8127b5dd24 | |||
a9caa61ef9 | |||
99fe866d12 | |||
c0ad659985 | |||
f41c53fef1 | |||
981a000ee8 | |||
cc4da104e0 | |||
c266e6adaf | |||
d94b344342 | |||
6367fb6e9e | |||
5615d21ce9 | |||
e2c4ff8180 | |||
39770d4197 | |||
cfdb4bbf25 | |||
3760910f0b | |||
3c632e96f9 | |||
baf86dfb0e | |||
219b44a04f | |||
05ee7ea9c7 | |||
cc0616b753 | |||
cbf5fa6684 | |||
a7fa6d00c1 | |||
49f377688a | |||
0b96962157 | |||
ebce62629e | |||
7aacad3270 | |||
035b882db1 | |||
0872e9c3ae | |||
1a573d17c0 | |||
4f20c370f9 | |||
e4bb248142 | |||
dff6268d66 | |||
8f9aa1a250 | |||
7d2e8875e0 | |||
3515e3ee28 | |||
cf82814606 | |||
fc29d82614 | |||
75ced3e945 | |||
685dc78739 | |||
9daa5f9177 | |||
69fbfb939f | |||
f0ecaabd7d | |||
c16f49cf19 | |||
9411458689 | |||
8771872d86 | |||
cda9ae1e42 | |||
81d68cd478 | |||
4c9078cccc | |||
f51828d049 | |||
d97562f6e8 | |||
234484b6f8 | |||
3bd45c005b | |||
05b7c1fffa | |||
a332712275 | |||
b2d8bd08f8 | |||
217be24963 | |||
bf457cd4fc | |||
88a8e986eb | |||
5f0567f8df | |||
a980b9d0a6 | |||
08504f6e06 | |||
da66484578 | |||
424efdaafe | |||
a65a7df209 | |||
c63bb81c3e | |||
a70e77ba48 | |||
c8b5909ee8 | |||
3b0ba923e4 | |||
1940b36e07 | |||
dfec687a46 | |||
bcd85b6f3e | |||
c4b919b24c | |||
c560bac13f | |||
88d27fd607 | |||
3d5f853b03 | |||
07a37f9b47 | |||
0172ad8461 | |||
e1f74a6d57 | |||
e17f6d654c | |||
817830940b | |||
dc9e8161d9 | |||
7f61cbbfd6 | |||
acca56f77c | |||
6bc695f251 | |||
91bb566ee6 | |||
5f04bbbb8b | |||
49fb5cb1a8 | |||
6e036ca09a | |||
8d1e36fa3c | |||
bccff3b237 | |||
a13a024ac8 | |||
5e7263cd1a | |||
0aafc29fb5 | |||
bd37473515 | |||
1c18e37a7c | |||
547c436281 | |||
e0c0d39ede | |||
4edce44689 | |||
186c08467f | |||
367fb9b504 | |||
ac75562296 | |||
7a9b14b49d | |||
32196cfe78 | |||
4d3283e235 | |||
dd3a3a2717 | |||
83d8e936ad | |||
58576630db | |||
7c84634e3f | |||
671640b0a9 | |||
5f7082f053 | |||
2a90cb7355 | |||
e63976df7e | |||
d8c2493658 | |||
4ed25b63a6 | |||
b318d588fe | |||
42d2adc3e0 | |||
5d1eb031eb | |||
1e7840c376 | |||
a6e3470c6f | |||
582b5f45e8 | |||
eb0b6c87d6 | |||
b6ce907928 | |||
9cffbdb42a | |||
d69e131450 | |||
6e84ba182e | |||
6773dfce8d | |||
13ce9e4f64 | |||
f63f8cb154 | |||
6e1118681d | |||
e5cec8f4eb | |||
6c36bd822c | |||
029c586717 | |||
ea6493c041 | |||
455d32d9e5 | |||
7bd801a167 | |||
b6e84879b6 | |||
f7832c0e82 | |||
8c1ab7e0a3 | |||
9d0f69ac50 | |||
215ca6c5ca | |||
a04c90e22d | |||
a84d410f11 | |||
636bae2466 | |||
739a7ea730 | |||
3893fbb0b1 | |||
948205c8e6 | |||
6278afde8d | |||
f0cb2dafbb | |||
a3c145432e | |||
e6f55da080 | |||
30f98f7e64 | |||
c9409a2edb | |||
b857064d65 | |||
a541382776 | |||
07ad24ab97 | |||
55db643048 | |||
8f9b198d48 | |||
6c7129cc0c | |||
919d55f3fc | |||
bdf63420d1 | |||
b7af715f6b | |||
b6eda33438 | |||
ab641d9f18 | |||
c7e128eed1 | |||
cc0259bbed | |||
23fba6d2ea | |||
3182adb6a0 | |||
d52ec65f18 | |||
b968376be9 | |||
90bd8c82b7 | |||
0955e8c5b6 | |||
ef55367224 | |||
a60f454154 | |||
7a7df3e635 | |||
62198a29c2 | |||
e87a35104a | |||
1e051e573d | |||
e172a621f3 | |||
9f09930834 | |||
20c2de9eed | |||
22ca5a6b8d | |||
8b19399b13 | |||
d289c773d0 | |||
a935e0720f | |||
1c3ff179bc | |||
ccab3d6b6e | |||
3e39fae6e1 | |||
d575fd1c3a | |||
0a2fb137af | |||
4907575d3d | |||
4200df21d3 | |||
e0bb5a2bd2 | |||
a6c2c685bc | |||
1e2fa68db0 | |||
599f16f15c | |||
91da168251 | |||
e104bccfb9 | |||
74bd0e32cc | |||
03015ed33f | |||
79ea70d4ec | |||
3ec76af96e | |||
b8efd2a347 | |||
9083157baa | |||
6cdc9e3b77 | |||
f8d4adfb7a | |||
719d9aa83c | |||
9ebaa737aa | |||
88b0982dac | |||
8c2e12ad79 | |||
2c31b3db07 | |||
eedf833b6f | |||
69d81cc065 | |||
af9c31152a | |||
abb6fca5e3 | |||
3ec1c40320 | |||
619211c1bf | |||
3a685049da | |||
ae54d05930 | |||
e7c4597ad0 | |||
09c9495015 | |||
e05f387632 | |||
9870c7c9a6 | |||
3f75b6b371 | |||
04fed82e5e | |||
f3a1dfef95 | |||
f738932bbd | |||
4968b6b9d0 | |||
ee97c00818 | |||
1dbd431117 | |||
09ab583f64 | |||
9ad6d13982 | |||
8d4426f2f8 | |||
8c8f795e9e | |||
7f2f67238f | |||
740fe942c1 | |||
7c5dcbb3fc | |||
7e055810b1 | |||
5758993e9f | |||
d7014e671d | |||
b0427ca9ff | |||
3af575cce7 | |||
f787d272e6 | |||
f061c9a30e | |||
8812072f06 | |||
e911ff4d67 | |||
28b6db115a | |||
e735bd475f | |||
299d199150 | |||
5e784d38eb | |||
868029f655 | |||
043d1ed9fb | |||
6230a62e9e | |||
71b49c3374 | |||
2eef42c6b9 | |||
0209992f6c | |||
c9d54f821b | |||
59d6dee3b3 | |||
9d25b2f29a | |||
91ff57faa7 | |||
b99affba4b | |||
639bd4fc2e | |||
a0f38f8845 | |||
a11c9e9d70 | |||
bdbcf82967 | |||
1f47d72e86 | |||
d83781ddec | |||
e32e55938b | |||
de08b68ba8 | |||
0e3a8c552c | |||
389e7d2502 | |||
fce6146576 | |||
02313e6819 | |||
df0a174802 | |||
bcb7ef48b6 | |||
9f714e62cb | |||
a95c2198a6 | |||
2df91e7f92 | |||
44be445b57 | |||
e43632fd95 | |||
69e4abad0f | |||
3bedbd0669 | |||
1d15bbc95b | |||
5002d87af4 | |||
2a3805c164 | |||
52f646d8db | |||
36c1073441 | |||
2979595cc5 | |||
d67120be19 | |||
ad31f1cf26 | |||
99798ace7d | |||
ba4becc61c | |||
397499b106 | |||
55c3fc9141 | |||
2830ec008c | |||
4c8b09eb97 | |||
98e0864be8 | |||
6dc71f5ad0 | |||
d6f4e4c4fe | |||
33ae71f300 | |||
abcca0897e | |||
b1379b2b14 | |||
27ebccce80 | |||
6964968f14 | |||
68377c176d | |||
baadaee016 | |||
199aa2ad3a | |||
29b176b719 | |||
6ce20675eb | |||
1e9967c3bf | |||
e0bc85d0dd | |||
e3fd4d3f81 | |||
00709fc5bd | |||
157494e803 | |||
f03ba6793e | |||
475aa4f1dd | |||
1d6ac16530 | |||
573a7e2c7b | |||
cebbc82322 | |||
cf5b2aeb88 | |||
52eb9c2ef3 | |||
702dcd8581 | |||
9e6ada6411 | |||
a38663ec90 | |||
02804ab537 | |||
b2d0d9cf13 | |||
46589faaca | |||
166d5fa4ff | |||
4bd38847c2 | |||
30a4187be4 | |||
f0c83a4459 | |||
fc61416c79 | |||
8200831b07 | |||
497954d84c | |||
bcaef8959c | |||
5bef81a059 | |||
d68c3ec89a | |||
0c72f881a6 | |||
8195e2d638 | |||
e8c20390e0 | |||
13df0af514 | |||
54e9aa92bc | |||
1afff777a6 | |||
071faae772 | |||
08a241f763 | |||
63f9e273b3 | |||
71d604067a | |||
66d0e18674 | |||
a940a8aa80 | |||
0d30550950 | |||
65bb0ff167 | |||
151767a5e3 | |||
a948ec6c2c | |||
28a7461057 | |||
6f47990a63 | |||
183c2221bb | |||
03ee54a4df | |||
2541a712e4 | |||
ee877607fb | |||
93351b889a | |||
5fa9d76500 | |||
cd0d0364ec | |||
cf5fec63c0 | |||
5c5cf418fb | |||
fb14008f50 | |||
18c8c16c5e | |||
299a218de7 | |||
1a081c09de |
2
.github/workflows/audit.yml
vendored
2
.github/workflows/audit.yml
vendored
@ -20,6 +20,6 @@ jobs:
|
||||
continue-on-error: true
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
- uses: rustsec/audit-check@v1.4.1
|
||||
- uses: rustsec/audit-check@v2.0.0
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
39
.github/workflows/ci.yml
vendored
39
.github/workflows/ci.yml
vendored
@ -36,7 +36,7 @@ jobs:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
|
||||
- name: Setup Rust toolchain and cache
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
|
||||
- name: cargo fmt
|
||||
run: cargo fmt --all -- --check
|
||||
@ -64,7 +64,7 @@ jobs:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
|
||||
- name: Setup Rust toolchain and cache
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
|
||||
- name: Tests
|
||||
run: cargo test --workspace --profile ci --exclude nu_plugin_*
|
||||
@ -93,7 +93,7 @@ jobs:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
|
||||
- name: Setup Rust toolchain and cache
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
|
||||
- name: Install Nushell
|
||||
run: cargo install --path . --locked --no-default-features
|
||||
@ -144,7 +144,7 @@ jobs:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
|
||||
- name: Setup Rust toolchain and cache
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
|
||||
- name: Clippy
|
||||
run: cargo clippy --package nu_plugin_* -- $CLIPPY_OPTIONS
|
||||
@ -162,3 +162,34 @@ jobs:
|
||||
else
|
||||
echo "no changes in working directory";
|
||||
fi
|
||||
|
||||
build-wasm:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
|
||||
- name: Setup Rust toolchain and cache
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
|
||||
- name: Add wasm32-unknown-unknown target
|
||||
run: rustup target add wasm32-unknown-unknown
|
||||
|
||||
- run: cargo build -p nu-cmd-base --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-cmd-extra --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-cmd-lang --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-color-config --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-command --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-derive-value --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-engine --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-glob --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-json --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-parser --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-path --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-pretty-hex --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-protocol --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-std --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-system --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-table --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-term-grid --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nu-utils --no-default-features --target wasm32-unknown-unknown
|
||||
- run: cargo build -p nuon --no-default-features --target wasm32-unknown-unknown
|
||||
|
30
.github/workflows/milestone.yml
vendored
Normal file
30
.github/workflows/milestone.yml
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
# Description:
|
||||
# - Add milestone to a merged PR automatically
|
||||
# - Add milestone to a closed issue that has a merged PR fix (if any)
|
||||
|
||||
name: Milestone Action
|
||||
on:
|
||||
issues:
|
||||
types: [closed]
|
||||
pull_request_target:
|
||||
types: [closed]
|
||||
|
||||
jobs:
|
||||
update-milestone:
|
||||
runs-on: ubuntu-latest
|
||||
name: Milestone Update
|
||||
steps:
|
||||
- name: Set Milestone for PR
|
||||
uses: hustcer/milestone-action@main
|
||||
if: github.event.pull_request.merged == true
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
# Bind milestone to closed issue that has a merged PR fix
|
||||
- name: Set Milestone for Issue
|
||||
uses: hustcer/milestone-action@main
|
||||
if: github.event.issue.state == 'closed'
|
||||
with:
|
||||
action: bind-issue
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
27
.github/workflows/nightly-build.yml
vendored
27
.github/workflows/nightly-build.yml
vendored
@ -27,7 +27,7 @@ jobs:
|
||||
# if: github.repository == 'nushell/nightly'
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4.1.7
|
||||
uses: actions/checkout@v4
|
||||
if: github.repository == 'nushell/nightly'
|
||||
with:
|
||||
ref: main
|
||||
@ -36,10 +36,10 @@ jobs:
|
||||
token: ${{ secrets.WORKFLOW_TOKEN }}
|
||||
|
||||
- name: Setup Nushell
|
||||
uses: hustcer/setup-nu@v3.13
|
||||
uses: hustcer/setup-nu@v3
|
||||
if: github.repository == 'nushell/nightly'
|
||||
with:
|
||||
version: 0.97.1
|
||||
version: 0.98.0
|
||||
|
||||
# Synchronize the main branch of nightly repo with the main branch of Nushell official repo
|
||||
- name: Prepare for Nightly Release
|
||||
@ -65,7 +65,7 @@ jobs:
|
||||
}
|
||||
|
||||
standard:
|
||||
name: Std
|
||||
name: Nu
|
||||
needs: prepare
|
||||
strategy:
|
||||
fail-fast: false
|
||||
@ -82,6 +82,7 @@ jobs:
|
||||
- armv7-unknown-linux-gnueabihf
|
||||
- armv7-unknown-linux-musleabihf
|
||||
- riscv64gc-unknown-linux-gnu
|
||||
- loongarch64-unknown-linux-gnu
|
||||
extra: ['bin']
|
||||
include:
|
||||
- target: aarch64-apple-darwin
|
||||
@ -114,11 +115,13 @@ jobs:
|
||||
os: ubuntu-22.04
|
||||
- target: riscv64gc-unknown-linux-gnu
|
||||
os: ubuntu-latest
|
||||
- target: loongarch64-unknown-linux-gnu
|
||||
os: ubuntu-22.04
|
||||
|
||||
runs-on: ${{matrix.os}}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: main
|
||||
fetch-depth: 0
|
||||
@ -128,15 +131,15 @@ jobs:
|
||||
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
||||
|
||||
- name: Setup Rust toolchain and cache
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
|
||||
with:
|
||||
rustflags: ''
|
||||
|
||||
- name: Setup Nushell
|
||||
uses: hustcer/setup-nu@v3.13
|
||||
uses: hustcer/setup-nu@v3
|
||||
with:
|
||||
version: 0.97.1
|
||||
version: 0.98.0
|
||||
|
||||
- name: Release Nu Binary
|
||||
id: nu
|
||||
@ -167,7 +170,7 @@ jobs:
|
||||
# REF: https://github.com/marketplace/actions/gh-release
|
||||
# Create a release only in nushell/nightly repo
|
||||
- name: Publish Archive
|
||||
uses: softprops/action-gh-release@v2.0.8
|
||||
uses: softprops/action-gh-release@v2.0.9
|
||||
if: ${{ startsWith(github.repository, 'nushell/nightly') }}
|
||||
with:
|
||||
prerelease: true
|
||||
@ -187,14 +190,14 @@ jobs:
|
||||
- name: Waiting for Release
|
||||
run: sleep 1800
|
||||
|
||||
- uses: actions/checkout@v4.1.7
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: main
|
||||
|
||||
- name: Setup Nushell
|
||||
uses: hustcer/setup-nu@v3.13
|
||||
uses: hustcer/setup-nu@v3
|
||||
with:
|
||||
version: 0.97.1
|
||||
version: 0.98.0
|
||||
|
||||
# Keep the last a few releases
|
||||
- name: Delete Older Releases
|
||||
|
7
.github/workflows/release-pkg.nu
vendored
7
.github/workflows/release-pkg.nu
vendored
@ -98,6 +98,13 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
|
||||
$env.CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_LINKER = 'armv7r-linux-musleabihf-gcc'
|
||||
cargo-build-nu
|
||||
}
|
||||
'loongarch64-unknown-linux-gnu' => {
|
||||
aria2c https://github.com/loongson/build-tools/releases/download/2024.08.08/x86_64-cross-tools-loongarch64-binutils_2.43-gcc_14.2.0-glibc_2.40.tar.xz
|
||||
tar xf x86_64-cross-tools-loongarch64-*.tar.xz
|
||||
$env.PATH = ($env.PATH | split row (char esep) | prepend $'($env.PWD)/cross-tools/bin')
|
||||
$env.CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_LINKER = 'loongarch64-unknown-linux-gnu-gcc'
|
||||
cargo-build-nu
|
||||
}
|
||||
_ => {
|
||||
# musl-tools to fix 'Failed to find tool. Is `musl-gcc` installed?'
|
||||
# Actually just for x86_64-unknown-linux-musl target
|
||||
|
43
.github/workflows/release.yml
vendored
43
.github/workflows/release.yml
vendored
@ -14,8 +14,8 @@ defaults:
|
||||
shell: bash
|
||||
|
||||
jobs:
|
||||
standard:
|
||||
name: Std
|
||||
release:
|
||||
name: Nu
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
@ -32,6 +32,7 @@ jobs:
|
||||
- armv7-unknown-linux-gnueabihf
|
||||
- armv7-unknown-linux-musleabihf
|
||||
- riscv64gc-unknown-linux-gnu
|
||||
- loongarch64-unknown-linux-gnu
|
||||
extra: ['bin']
|
||||
include:
|
||||
- target: aarch64-apple-darwin
|
||||
@ -64,27 +65,29 @@ jobs:
|
||||
os: ubuntu-22.04
|
||||
- target: riscv64gc-unknown-linux-gnu
|
||||
os: ubuntu-latest
|
||||
- target: loongarch64-unknown-linux-gnu
|
||||
os: ubuntu-22.04
|
||||
|
||||
runs-on: ${{matrix.os}}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.7
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Update Rust Toolchain Target
|
||||
run: |
|
||||
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
|
||||
|
||||
- name: Setup Rust toolchain
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1.10.1
|
||||
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
|
||||
with:
|
||||
cache: false
|
||||
rustflags: ''
|
||||
|
||||
- name: Setup Nushell
|
||||
uses: hustcer/setup-nu@v3.13
|
||||
uses: hustcer/setup-nu@v3
|
||||
with:
|
||||
version: 0.97.1
|
||||
version: 0.98.0
|
||||
|
||||
- name: Release Nu Binary
|
||||
id: nu
|
||||
@ -95,12 +98,36 @@ jobs:
|
||||
TARGET: ${{ matrix.target }}
|
||||
_EXTRA_: ${{ matrix.extra }}
|
||||
|
||||
# REF: https://github.com/marketplace/actions/gh-release
|
||||
# WARN: Don't upgrade this action due to the release per asset issue.
|
||||
# See: https://github.com/softprops/action-gh-release/issues/445
|
||||
- name: Publish Archive
|
||||
uses: softprops/action-gh-release@v2.0.8
|
||||
uses: softprops/action-gh-release@v2.0.5
|
||||
if: ${{ startsWith(github.ref, 'refs/tags/') }}
|
||||
with:
|
||||
draft: true
|
||||
files: ${{ steps.nu.outputs.archive }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
sha256sum:
|
||||
needs: release
|
||||
name: Create Sha256sum
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Download Release Archives
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: >-
|
||||
gh release download ${{ github.ref_name }}
|
||||
--repo ${{ github.repository }}
|
||||
--pattern '*'
|
||||
--dir release
|
||||
- name: Create Checksums
|
||||
run: cd release && shasum -a 256 * > ../SHA256SUMS
|
||||
- name: Publish Checksums
|
||||
uses: softprops/action-gh-release@v2.0.5
|
||||
with:
|
||||
draft: true
|
||||
files: SHA256SUMS
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
2
.github/workflows/typos.yml
vendored
2
.github/workflows/typos.yml
vendored
@ -10,4 +10,4 @@ jobs:
|
||||
uses: actions/checkout@v4.1.7
|
||||
|
||||
- name: Check spelling
|
||||
uses: crate-ci/typos@v1.24.5
|
||||
uses: crate-ci/typos@v1.28.4
|
||||
|
2945
Cargo.lock
generated
2945
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
132
Cargo.toml
132
Cargo.toml
@ -10,8 +10,8 @@ homepage = "https://www.nushell.sh"
|
||||
license = "MIT"
|
||||
name = "nu"
|
||||
repository = "https://github.com/nushell/nushell"
|
||||
rust-version = "1.79.0"
|
||||
version = "0.98.0"
|
||||
rust-version = "1.81.0"
|
||||
version = "0.101.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -66,16 +66,16 @@ alphanumeric-sort = "1.5"
|
||||
ansi-str = "0.8"
|
||||
anyhow = "1.0.82"
|
||||
base64 = "0.22.1"
|
||||
bracoxide = "0.1.2"
|
||||
brotli = "5.0"
|
||||
bracoxide = "0.1.4"
|
||||
brotli = "6.0"
|
||||
byteorder = "1.5"
|
||||
bytes = "1"
|
||||
bytesize = "1.3"
|
||||
calamine = "0.24.0"
|
||||
calamine = "0.26.1"
|
||||
chardetng = "0.1.17"
|
||||
chrono = { default-features = false, version = "0.4.34" }
|
||||
chrono-humanize = "0.2.3"
|
||||
chrono-tz = "0.8"
|
||||
chrono-tz = "0.10"
|
||||
crossbeam-channel = "0.5.8"
|
||||
crossterm = "0.28.1"
|
||||
csv = "1.3"
|
||||
@ -86,13 +86,13 @@ dirs = "5.0"
|
||||
dirs-sys = "0.4"
|
||||
dtparse = "2.0"
|
||||
encoding_rs = "0.8"
|
||||
fancy-regex = "0.13"
|
||||
fancy-regex = "0.14"
|
||||
filesize = "0.2"
|
||||
filetime = "0.2"
|
||||
fuzzy-matcher = "0.3"
|
||||
heck = "0.5.0"
|
||||
human-date-parser = "0.1.1"
|
||||
indexmap = "2.5"
|
||||
human-date-parser = "0.2.0"
|
||||
indexmap = "2.7"
|
||||
indicatif = "0.17"
|
||||
interprocess = "2.2.0"
|
||||
is_executable = "1.0"
|
||||
@ -103,22 +103,22 @@ log = "0.4"
|
||||
lru = "0.12"
|
||||
lscolors = { version = "0.17", default-features = false }
|
||||
lsp-server = "0.7.5"
|
||||
lsp-types = "0.95.0"
|
||||
lsp-types = { version = "0.95.0", features = ["proposed"] }
|
||||
mach2 = "0.4"
|
||||
md5 = { version = "0.10", package = "md-5" }
|
||||
miette = "7.2"
|
||||
miette = "7.3"
|
||||
mime = "0.3.17"
|
||||
mime_guess = "2.0"
|
||||
mockito = { version = "1.5", default-features = false }
|
||||
multipart-rs = "0.1.11"
|
||||
mockito = { version = "1.6", default-features = false }
|
||||
multipart-rs = "0.1.13"
|
||||
native-tls = "0.2"
|
||||
nix = { version = "0.29", default-features = false }
|
||||
notify-debouncer-full = { version = "0.3", default-features = false }
|
||||
nu-ansi-term = "0.50.1"
|
||||
num-format = "0.4"
|
||||
num-traits = "0.2"
|
||||
oem_cp = "2.0.0"
|
||||
omnipath = "0.1"
|
||||
once_cell = "1.18"
|
||||
open = "5.3"
|
||||
os_pipe = { version = "1.2", features = ["io_safety"] }
|
||||
pathdiff = "0.2"
|
||||
@ -127,25 +127,27 @@ pretty_assertions = "1.4"
|
||||
print-positions = "0.6"
|
||||
proc-macro-error = { version = "1.0", default-features = false }
|
||||
proc-macro2 = "1.0"
|
||||
procfs = "0.16.0"
|
||||
procfs = "0.17.0"
|
||||
pwd = "1.3"
|
||||
quick-xml = "0.32.0"
|
||||
quick-xml = "0.37.0"
|
||||
quickcheck = "1.0"
|
||||
quickcheck_macros = "1.0"
|
||||
quote = "1.0"
|
||||
rand = "0.8"
|
||||
getrandom = "0.2" # pick same version that rand requires
|
||||
rand_chacha = "0.3.1"
|
||||
ratatui = "0.26"
|
||||
rayon = "1.10"
|
||||
reedline = "0.35.0"
|
||||
reedline = "0.38.0"
|
||||
regex = "1.9.5"
|
||||
rmp = "0.8"
|
||||
rmp-serde = "1.3"
|
||||
ropey = "1.6.1"
|
||||
roxmltree = "0.19"
|
||||
rstest = { version = "0.18", default-features = false }
|
||||
roxmltree = "0.20"
|
||||
rstest = { version = "0.23", default-features = false }
|
||||
rusqlite = "0.31"
|
||||
rust-embed = "8.5.0"
|
||||
scopeguard = { version = "1.2.0" }
|
||||
serde = { version = "1.0" }
|
||||
serde_json = "1.0"
|
||||
serde_urlencoded = "0.7.1"
|
||||
@ -153,30 +155,31 @@ serde_yaml = "0.9"
|
||||
sha2 = "0.10"
|
||||
strip-ansi-escapes = "0.2.0"
|
||||
syn = "2.0"
|
||||
sysinfo = "0.30"
|
||||
sysinfo = "0.32"
|
||||
tabled = { version = "0.16.0", default-features = false }
|
||||
tempfile = "3.10"
|
||||
terminal_size = "0.3"
|
||||
titlecase = "2.0"
|
||||
tempfile = "3.14"
|
||||
terminal_size = "0.4"
|
||||
titlecase = "3.0"
|
||||
toml = "0.8"
|
||||
trash = "3.3"
|
||||
trash = "5.2"
|
||||
umask = "2.1"
|
||||
unicode-segmentation = "1.11"
|
||||
unicode-width = "0.1"
|
||||
ureq = { version = "2.10", default-features = false }
|
||||
unicode-segmentation = "1.12"
|
||||
unicode-width = "0.2"
|
||||
ureq = { version = "2.12", default-features = false }
|
||||
url = "2.2"
|
||||
uu_cp = "0.0.27"
|
||||
uu_mkdir = "0.0.27"
|
||||
uu_mktemp = "0.0.27"
|
||||
uu_mv = "0.0.27"
|
||||
uu_whoami = "0.0.27"
|
||||
uu_uname = "0.0.27"
|
||||
uucore = "0.0.27"
|
||||
uuid = "1.10.0"
|
||||
uu_cp = "0.0.28"
|
||||
uu_mkdir = "0.0.28"
|
||||
uu_mktemp = "0.0.28"
|
||||
uu_mv = "0.0.28"
|
||||
uu_touch = "0.0.28"
|
||||
uu_whoami = "0.0.28"
|
||||
uu_uname = "0.0.28"
|
||||
uucore = "0.0.28"
|
||||
uuid = "1.11.0"
|
||||
v_htmlescape = "0.15.0"
|
||||
wax = "0.6"
|
||||
which = "6.0.0"
|
||||
windows = "0.54"
|
||||
which = "7.0.0"
|
||||
windows = "0.56"
|
||||
windows-sys = "0.48"
|
||||
winreg = "0.52"
|
||||
|
||||
@ -189,22 +192,22 @@ unchecked_duration_subtraction = "warn"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-cli = { path = "./crates/nu-cli", version = "0.98.0" }
|
||||
nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.98.0" }
|
||||
nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.98.0" }
|
||||
nu-cmd-plugin = { path = "./crates/nu-cmd-plugin", version = "0.98.0", optional = true }
|
||||
nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.98.0" }
|
||||
nu-command = { path = "./crates/nu-command", version = "0.98.0" }
|
||||
nu-engine = { path = "./crates/nu-engine", version = "0.98.0" }
|
||||
nu-explore = { path = "./crates/nu-explore", version = "0.98.0" }
|
||||
nu-lsp = { path = "./crates/nu-lsp/", version = "0.98.0" }
|
||||
nu-parser = { path = "./crates/nu-parser", version = "0.98.0" }
|
||||
nu-path = { path = "./crates/nu-path", version = "0.98.0" }
|
||||
nu-plugin-engine = { path = "./crates/nu-plugin-engine", optional = true, version = "0.98.0" }
|
||||
nu-protocol = { path = "./crates/nu-protocol", version = "0.98.0" }
|
||||
nu-std = { path = "./crates/nu-std", version = "0.98.0" }
|
||||
nu-system = { path = "./crates/nu-system", version = "0.98.0" }
|
||||
nu-utils = { path = "./crates/nu-utils", version = "0.98.0" }
|
||||
nu-cli = { path = "./crates/nu-cli", version = "0.101.0" }
|
||||
nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.101.0" }
|
||||
nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.101.0" }
|
||||
nu-cmd-plugin = { path = "./crates/nu-cmd-plugin", version = "0.101.0", optional = true }
|
||||
nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.101.0" }
|
||||
nu-command = { path = "./crates/nu-command", version = "0.101.0" }
|
||||
nu-engine = { path = "./crates/nu-engine", version = "0.101.0" }
|
||||
nu-explore = { path = "./crates/nu-explore", version = "0.101.0" }
|
||||
nu-lsp = { path = "./crates/nu-lsp/", version = "0.101.0" }
|
||||
nu-parser = { path = "./crates/nu-parser", version = "0.101.0" }
|
||||
nu-path = { path = "./crates/nu-path", version = "0.101.0" }
|
||||
nu-plugin-engine = { path = "./crates/nu-plugin-engine", optional = true, version = "0.101.0" }
|
||||
nu-protocol = { path = "./crates/nu-protocol", version = "0.101.0" }
|
||||
nu-std = { path = "./crates/nu-std", version = "0.101.0" }
|
||||
nu-system = { path = "./crates/nu-system", version = "0.101.0" }
|
||||
nu-utils = { path = "./crates/nu-utils", version = "0.101.0" }
|
||||
reedline = { workspace = true, features = ["bashisms", "sqlite"] }
|
||||
|
||||
crossterm = { workspace = true }
|
||||
@ -234,27 +237,32 @@ nix = { workspace = true, default-features = false, features = [
|
||||
] }
|
||||
|
||||
[dev-dependencies]
|
||||
nu-test-support = { path = "./crates/nu-test-support", version = "0.98.0" }
|
||||
nu-plugin-protocol = { path = "./crates/nu-plugin-protocol", version = "0.98.0" }
|
||||
nu-plugin-core = { path = "./crates/nu-plugin-core", version = "0.98.0" }
|
||||
nu-test-support = { path = "./crates/nu-test-support", version = "0.101.0" }
|
||||
nu-plugin-protocol = { path = "./crates/nu-plugin-protocol", version = "0.101.0" }
|
||||
nu-plugin-core = { path = "./crates/nu-plugin-core", version = "0.101.0" }
|
||||
assert_cmd = "2.0"
|
||||
dirs = { workspace = true }
|
||||
tango-bench = "0.5"
|
||||
tango-bench = "0.6"
|
||||
pretty_assertions = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
rstest = { workspace = true, default-features = false }
|
||||
serial_test = "3.1"
|
||||
serial_test = "3.2"
|
||||
tempfile = { workspace = true }
|
||||
|
||||
[features]
|
||||
plugin = [
|
||||
"nu-plugin-engine",
|
||||
# crates
|
||||
"nu-cmd-plugin",
|
||||
"nu-plugin-engine",
|
||||
|
||||
# features
|
||||
"nu-cli/plugin",
|
||||
"nu-parser/plugin",
|
||||
"nu-cmd-lang/plugin",
|
||||
"nu-command/plugin",
|
||||
"nu-protocol/plugin",
|
||||
"nu-engine/plugin",
|
||||
"nu-engine/plugin",
|
||||
"nu-parser/plugin",
|
||||
"nu-protocol/plugin",
|
||||
]
|
||||
|
||||
default = [
|
||||
@ -320,4 +328,4 @@ bench = false
|
||||
# Run individual benchmarks like `cargo bench -- <regex>` e.g. `cargo bench -- parse`
|
||||
[[bench]]
|
||||
name = "benchmarks"
|
||||
harness = false
|
||||
harness = false
|
@ -58,7 +58,7 @@ For details about which platforms the Nushell team actively supports, see [our p
|
||||
|
||||
## Configuration
|
||||
|
||||
The default configurations can be found at [sample_config](crates/nu-utils/src/sample_config)
|
||||
The default configurations can be found at [sample_config](crates/nu-utils/src/default_files)
|
||||
which are the configuration files one gets when they startup Nushell for the first time.
|
||||
|
||||
It sets all of the default configuration to run Nushell. From here one can
|
||||
@ -229,7 +229,7 @@ Please submit an issue or PR to be added to this list.
|
||||
See [Contributing](CONTRIBUTING.md) for details. Thanks to all the people who already contributed!
|
||||
|
||||
<a href="https://github.com/nushell/nushell/graphs/contributors">
|
||||
<img src="https://contributors-img.web.app/image?repo=nushell/nushell&max=750" />
|
||||
<img src="https://contributors-img.web.app/image?repo=nushell/nushell&max=750&columns=20" />
|
||||
</a>
|
||||
|
||||
## License
|
||||
|
@ -46,9 +46,6 @@ fn setup_stack_and_engine_from_command(command: &str) -> (Stack, EngineState) {
|
||||
|
||||
let mut stack = Stack::new();
|
||||
|
||||
// Support running benchmarks without IR mode
|
||||
stack.use_ir = std::env::var_os("NU_DISABLE_IR").is_none();
|
||||
|
||||
evaluate_commands(
|
||||
&commands,
|
||||
&mut engine,
|
||||
|
@ -5,27 +5,27 @@ repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cli"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-cli"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
|
||||
[dev-dependencies]
|
||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.98.0" }
|
||||
nu-command = { path = "../nu-command", version = "0.98.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.98.0" }
|
||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.101.0" }
|
||||
nu-command = { path = "../nu-command", version = "0.101.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.101.0" }
|
||||
rstest = { workspace = true, default-features = false }
|
||||
tempfile = { workspace = true }
|
||||
|
||||
[dependencies]
|
||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.98.0" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.98.0" }
|
||||
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.98.0", optional = true }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.98.0" }
|
||||
nu-color-config = { path = "../nu-color-config", version = "0.98.0" }
|
||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.101.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0", features = ["os"] }
|
||||
nu-path = { path = "../nu-path", version = "0.101.0" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.101.0" }
|
||||
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.101.0", optional = true }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", features = ["os"] }
|
||||
nu-utils = { path = "../nu-utils", version = "0.101.0" }
|
||||
nu-color-config = { path = "../nu-color-config", version = "0.101.0" }
|
||||
nu-ansi-term = { workspace = true }
|
||||
reedline = { workspace = true, features = ["bashisms", "sqlite"] }
|
||||
|
||||
@ -37,7 +37,6 @@ is_executable = { workspace = true }
|
||||
log = { workspace = true }
|
||||
miette = { workspace = true, features = ["fancy-no-backtrace"] }
|
||||
lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] }
|
||||
once_cell = { workspace = true }
|
||||
percent-encoding = { workspace = true }
|
||||
sysinfo = { workspace = true }
|
||||
unicode-segmentation = { workspace = true }
|
||||
|
@ -17,6 +17,7 @@ pub fn add_cli_context(mut engine_state: EngineState) -> EngineState {
|
||||
CommandlineGetCursor,
|
||||
CommandlineSetCursor,
|
||||
History,
|
||||
HistoryImport,
|
||||
HistorySession,
|
||||
Keybindings,
|
||||
KeybindingsDefault,
|
||||
|
9
crates/nu-cli/src/commands/history/fields.rs
Normal file
9
crates/nu-cli/src/commands/history/fields.rs
Normal file
@ -0,0 +1,9 @@
|
||||
// Each const is named after a HistoryItem field, and the value is the field name to be displayed to
|
||||
// the user (or accept during import).
|
||||
pub const COMMAND_LINE: &str = "command";
|
||||
pub const START_TIMESTAMP: &str = "start_timestamp";
|
||||
pub const HOSTNAME: &str = "hostname";
|
||||
pub const CWD: &str = "cwd";
|
||||
pub const EXIT_STATUS: &str = "exit_status";
|
||||
pub const DURATION: &str = "duration";
|
||||
pub const SESSION_ID: &str = "session_id";
|
@ -5,6 +5,8 @@ use reedline::{
|
||||
SqliteBackedHistory,
|
||||
};
|
||||
|
||||
use super::fields;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct History;
|
||||
|
||||
@ -42,91 +44,76 @@ impl Command for History {
|
||||
let Some(history) = engine_state.history_config() else {
|
||||
return Ok(PipelineData::empty());
|
||||
};
|
||||
|
||||
// todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history`
|
||||
if let Some(config_path) = nu_path::config_dir() {
|
||||
let clear = call.has_flag(engine_state, stack, "clear")?;
|
||||
let long = call.has_flag(engine_state, stack, "long")?;
|
||||
let signals = engine_state.signals().clone();
|
||||
let Some(history_path) = history.file_path() else {
|
||||
return Err(ShellError::ConfigDirNotFound { span: Some(head) });
|
||||
};
|
||||
|
||||
let mut history_path = config_path;
|
||||
history_path.push("nushell");
|
||||
match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
history_path.push("history.sqlite3");
|
||||
}
|
||||
HistoryFileFormat::Plaintext => {
|
||||
history_path.push("history.txt");
|
||||
}
|
||||
}
|
||||
if call.has_flag(engine_state, stack, "clear")? {
|
||||
let _ = std::fs::remove_file(history_path);
|
||||
// TODO: FIXME also clear the auxiliary files when using sqlite
|
||||
return Ok(PipelineData::empty());
|
||||
}
|
||||
|
||||
if clear {
|
||||
let _ = std::fs::remove_file(history_path);
|
||||
// TODO: FIXME also clear the auxiliary files when using sqlite
|
||||
Ok(PipelineData::empty())
|
||||
} else {
|
||||
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
SqliteBackedHistory::with_file(history_path.clone().into(), None, None)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
|
||||
HistoryFileFormat::Plaintext => FileBackedHistory::with_file(
|
||||
history.max_size as usize,
|
||||
history_path.clone().into(),
|
||||
)
|
||||
let long = call.has_flag(engine_state, stack, "long")?;
|
||||
let signals = engine_state.signals().clone();
|
||||
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
SqliteBackedHistory::with_file(history_path.clone(), None, None)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok(),
|
||||
};
|
||||
|
||||
match history.file_format {
|
||||
HistoryFileFormat::Plaintext => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
Value::record(
|
||||
record! {
|
||||
"command" => Value::string(entry.command_line, head),
|
||||
"index" => Value::int(idx as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
create_history_record(idx, entry, long, head)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
}
|
||||
.ok()
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::ConfigDirNotFound { span: Some(head) })
|
||||
HistoryFileFormat::Plaintext => {
|
||||
FileBackedHistory::with_file(history.max_size as usize, history_path.clone())
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
};
|
||||
match history.file_format {
|
||||
HistoryFileFormat::Plaintext => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
Value::record(
|
||||
record! {
|
||||
fields::COMMAND_LINE => Value::string(entry.command_line, head),
|
||||
// TODO: This name is inconsistent with create_history_record.
|
||||
"index" => Value::int(idx as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(move |(idx, entry)| create_history_record(idx, entry, long, head))
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,13 +179,13 @@ fn create_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span)
|
||||
Value::record(
|
||||
record! {
|
||||
"item_id" => item_id_value,
|
||||
"start_timestamp" => start_timestamp_value,
|
||||
"command" => command_value,
|
||||
"session_id" => session_id_value,
|
||||
"hostname" => hostname_value,
|
||||
"cwd" => cwd_value,
|
||||
"duration" => duration_value,
|
||||
"exit_status" => exit_status_value,
|
||||
fields::START_TIMESTAMP => start_timestamp_value,
|
||||
fields::COMMAND_LINE => command_value,
|
||||
fields::SESSION_ID => session_id_value,
|
||||
fields::HOSTNAME => hostname_value,
|
||||
fields::CWD => cwd_value,
|
||||
fields::DURATION => duration_value,
|
||||
fields::EXIT_STATUS => exit_status_value,
|
||||
"idx" => index_value,
|
||||
},
|
||||
head,
|
||||
@ -206,11 +193,11 @@ fn create_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span)
|
||||
} else {
|
||||
Value::record(
|
||||
record! {
|
||||
"start_timestamp" => start_timestamp_value,
|
||||
"command" => command_value,
|
||||
"cwd" => cwd_value,
|
||||
"duration" => duration_value,
|
||||
"exit_status" => exit_status_value,
|
||||
fields::START_TIMESTAMP => start_timestamp_value,
|
||||
fields::COMMAND_LINE => command_value,
|
||||
fields::CWD => cwd_value,
|
||||
fields::DURATION => duration_value,
|
||||
fields::EXIT_STATUS => exit_status_value,
|
||||
},
|
||||
head,
|
||||
)
|
||||
|
415
crates/nu-cli/src/commands/history/history_import.rs
Normal file
415
crates/nu-cli/src/commands/history/history_import.rs
Normal file
@ -0,0 +1,415 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::HistoryFileFormat;
|
||||
|
||||
use reedline::{
|
||||
FileBackedHistory, History, HistoryItem, ReedlineError, SearchQuery, SqliteBackedHistory,
|
||||
};
|
||||
|
||||
use super::fields;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HistoryImport;
|
||||
|
||||
impl Command for HistoryImport {
|
||||
fn name(&self) -> &str {
|
||||
"history import"
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Import command line history"
|
||||
}
|
||||
|
||||
fn extra_description(&self) -> &str {
|
||||
r#"Can import history from input, either successive command lines or more detailed records. If providing records, available fields are:
|
||||
command_line, id, start_timestamp, hostname, cwd, duration, exit_status.
|
||||
|
||||
If no input is provided, will import all history items from existing history in the other format: if current history is stored in sqlite, it will store it in plain text and vice versa.
|
||||
|
||||
Note that history item IDs are ignored when importing from file."#
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("history import")
|
||||
.category(Category::History)
|
||||
.input_output_types(vec![
|
||||
(Type::Nothing, Type::Nothing),
|
||||
(Type::List(Box::new(Type::String)), Type::Nothing),
|
||||
(Type::table(), Type::Nothing),
|
||||
])
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
example: "history import",
|
||||
description:
|
||||
"Append all items from history in the other format to the current history",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
example: "echo foo | history import",
|
||||
description: "Append `foo` to the current history",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
example: "[[ command_line cwd ]; [ foo /home ]] | history import",
|
||||
description: "Append `foo` ran from `/home` to the current history",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let ok = Ok(Value::nothing(call.head).into_pipeline_data());
|
||||
|
||||
let Some(history) = engine_state.history_config() else {
|
||||
return ok;
|
||||
};
|
||||
let Some(current_history_path) = history.file_path() else {
|
||||
return Err(ShellError::ConfigDirNotFound {
|
||||
span: Some(call.head),
|
||||
});
|
||||
};
|
||||
if let Some(bak_path) = backup(¤t_history_path)? {
|
||||
println!("Backed history to {}", bak_path.display());
|
||||
}
|
||||
match input {
|
||||
PipelineData::Empty => {
|
||||
let other_format = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => HistoryFileFormat::Plaintext,
|
||||
HistoryFileFormat::Plaintext => HistoryFileFormat::Sqlite,
|
||||
};
|
||||
let src = new_backend(other_format, None)?;
|
||||
let mut dst = new_backend(history.file_format, Some(current_history_path))?;
|
||||
let items = src
|
||||
.search(SearchQuery::everything(
|
||||
reedline::SearchDirection::Forward,
|
||||
None,
|
||||
))
|
||||
.map_err(error_from_reedline)?
|
||||
.into_iter()
|
||||
.map(Ok);
|
||||
import(dst.as_mut(), items)
|
||||
}
|
||||
_ => {
|
||||
let input = input.into_iter().map(item_from_value);
|
||||
import(
|
||||
new_backend(history.file_format, Some(current_history_path))?.as_mut(),
|
||||
input,
|
||||
)
|
||||
}
|
||||
}?;
|
||||
|
||||
ok
|
||||
}
|
||||
}
|
||||
|
||||
fn new_backend(
|
||||
format: HistoryFileFormat,
|
||||
path: Option<PathBuf>,
|
||||
) -> Result<Box<dyn History>, ShellError> {
|
||||
let path = match path {
|
||||
Some(path) => path,
|
||||
None => {
|
||||
let Some(mut path) = nu_path::nu_config_dir() else {
|
||||
return Err(ShellError::ConfigDirNotFound { span: None });
|
||||
};
|
||||
path.push(format.default_file_name());
|
||||
path.into_std_path_buf()
|
||||
}
|
||||
};
|
||||
|
||||
fn map(
|
||||
result: Result<impl History + 'static, ReedlineError>,
|
||||
) -> Result<Box<dyn History>, ShellError> {
|
||||
result
|
||||
.map(|x| Box::new(x) as Box<dyn History>)
|
||||
.map_err(error_from_reedline)
|
||||
}
|
||||
match format {
|
||||
// Use a reasonably large value for maximum capacity.
|
||||
HistoryFileFormat::Plaintext => map(FileBackedHistory::with_file(0xfffffff, path)),
|
||||
HistoryFileFormat::Sqlite => map(SqliteBackedHistory::with_file(path, None, None)),
|
||||
}
|
||||
}
|
||||
|
||||
fn import(
|
||||
dst: &mut dyn History,
|
||||
src: impl Iterator<Item = Result<HistoryItem, ShellError>>,
|
||||
) -> Result<(), ShellError> {
|
||||
for item in src {
|
||||
let mut item = item?;
|
||||
item.id = None;
|
||||
dst.save(item).map_err(error_from_reedline)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn error_from_reedline(e: ReedlineError) -> ShellError {
|
||||
// TODO: Should we add a new ShellError variant?
|
||||
ShellError::GenericError {
|
||||
error: "Reedline error".to_owned(),
|
||||
msg: format!("{e}"),
|
||||
span: None,
|
||||
help: None,
|
||||
inner: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn item_from_value(v: Value) -> Result<HistoryItem, ShellError> {
|
||||
let span = v.span();
|
||||
match v {
|
||||
Value::Record { val, .. } => item_from_record(val.into_owned(), span),
|
||||
Value::String { val, .. } => Ok(HistoryItem {
|
||||
command_line: val,
|
||||
id: None,
|
||||
start_timestamp: None,
|
||||
session_id: None,
|
||||
hostname: None,
|
||||
cwd: None,
|
||||
duration: None,
|
||||
exit_status: None,
|
||||
more_info: None,
|
||||
}),
|
||||
_ => Err(ShellError::UnsupportedInput {
|
||||
msg: "Only list and record inputs are supported".to_owned(),
|
||||
input: v.get_type().to_string(),
|
||||
msg_span: span,
|
||||
input_span: span,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn item_from_record(mut rec: Record, span: Span) -> Result<HistoryItem, ShellError> {
|
||||
let cmd = match rec.remove(fields::COMMAND_LINE) {
|
||||
Some(v) => v.as_str()?.to_owned(),
|
||||
None => {
|
||||
return Err(ShellError::TypeMismatch {
|
||||
err_message: format!("missing column: {}", fields::COMMAND_LINE),
|
||||
span,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
fn get<T>(
|
||||
rec: &mut Record,
|
||||
field: &'static str,
|
||||
f: impl FnOnce(Value) -> Result<T, ShellError>,
|
||||
) -> Result<Option<T>, ShellError> {
|
||||
rec.remove(field).map(f).transpose()
|
||||
}
|
||||
|
||||
let rec = &mut rec;
|
||||
let item = HistoryItem {
|
||||
command_line: cmd,
|
||||
id: None,
|
||||
start_timestamp: get(rec, fields::START_TIMESTAMP, |v| Ok(v.as_date()?.to_utc()))?,
|
||||
hostname: get(rec, fields::HOSTNAME, |v| Ok(v.as_str()?.to_owned()))?,
|
||||
cwd: get(rec, fields::CWD, |v| Ok(v.as_str()?.to_owned()))?,
|
||||
exit_status: get(rec, fields::EXIT_STATUS, |v| v.as_int())?,
|
||||
duration: get(rec, fields::DURATION, duration_from_value)?,
|
||||
more_info: None,
|
||||
// TODO: Currently reedline doesn't let you create session IDs.
|
||||
session_id: None,
|
||||
};
|
||||
|
||||
if !rec.is_empty() {
|
||||
let cols = rec.columns().map(|s| s.as_str()).collect::<Vec<_>>();
|
||||
return Err(ShellError::TypeMismatch {
|
||||
err_message: format!("unsupported column names: {}", cols.join(", ")),
|
||||
span,
|
||||
});
|
||||
}
|
||||
Ok(item)
|
||||
}
|
||||
|
||||
fn duration_from_value(v: Value) -> Result<std::time::Duration, ShellError> {
|
||||
chrono::Duration::nanoseconds(v.as_duration()?)
|
||||
.to_std()
|
||||
.map_err(|_| ShellError::IOError {
|
||||
msg: "negative duration not supported".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn find_backup_path(path: &Path) -> Result<PathBuf, ShellError> {
|
||||
let Ok(mut bak_path) = path.to_path_buf().into_os_string().into_string() else {
|
||||
// This isn't fundamentally problem, but trying to work with OsString is a nightmare.
|
||||
return Err(ShellError::IOError {
|
||||
msg: "History path mush be representable as UTF-8".to_string(),
|
||||
});
|
||||
};
|
||||
bak_path.push_str(".bak");
|
||||
if !Path::new(&bak_path).exists() {
|
||||
return Ok(bak_path.into());
|
||||
}
|
||||
let base_len = bak_path.len();
|
||||
for i in 1..100 {
|
||||
use std::fmt::Write;
|
||||
bak_path.truncate(base_len);
|
||||
write!(&mut bak_path, ".{i}").unwrap();
|
||||
if !Path::new(&bak_path).exists() {
|
||||
return Ok(PathBuf::from(bak_path));
|
||||
}
|
||||
}
|
||||
Err(ShellError::IOError {
|
||||
msg: "Too many existing backup files".to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
fn backup(path: &Path) -> Result<Option<PathBuf>, ShellError> {
|
||||
match path.metadata() {
|
||||
Ok(md) if md.is_file() => (),
|
||||
Ok(_) => {
|
||||
return Err(ShellError::IOError {
|
||||
msg: "history path exists but is not a file".to_string(),
|
||||
})
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
let bak_path = find_backup_path(path)?;
|
||||
std::fs::copy(path, &bak_path)?;
|
||||
Ok(Some(bak_path))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::DateTime;
|
||||
use rstest::rstest;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_item_from_value_string() -> Result<(), ShellError> {
|
||||
let item = item_from_value(Value::string("foo", Span::unknown()))?;
|
||||
assert_eq!(
|
||||
item,
|
||||
HistoryItem {
|
||||
command_line: "foo".to_string(),
|
||||
id: None,
|
||||
start_timestamp: None,
|
||||
session_id: None,
|
||||
hostname: None,
|
||||
cwd: None,
|
||||
duration: None,
|
||||
exit_status: None,
|
||||
more_info: None
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_from_value_record() {
|
||||
let span = Span::unknown();
|
||||
let rec = new_record(&[
|
||||
("command", Value::string("foo", span)),
|
||||
(
|
||||
"start_timestamp",
|
||||
Value::date(
|
||||
DateTime::parse_from_rfc3339("1996-12-19T16:39:57-08:00").unwrap(),
|
||||
span,
|
||||
),
|
||||
),
|
||||
("hostname", Value::string("localhost", span)),
|
||||
("cwd", Value::string("/home/test", span)),
|
||||
("duration", Value::duration(100_000_000, span)),
|
||||
("exit_status", Value::int(42, span)),
|
||||
]);
|
||||
let item = item_from_value(rec).unwrap();
|
||||
assert_eq!(
|
||||
item,
|
||||
HistoryItem {
|
||||
command_line: "foo".to_string(),
|
||||
id: None,
|
||||
start_timestamp: Some(
|
||||
DateTime::parse_from_rfc3339("1996-12-19T16:39:57-08:00")
|
||||
.unwrap()
|
||||
.to_utc()
|
||||
),
|
||||
hostname: Some("localhost".to_string()),
|
||||
cwd: Some("/home/test".to_string()),
|
||||
duration: Some(std::time::Duration::from_nanos(100_000_000)),
|
||||
exit_status: Some(42),
|
||||
|
||||
session_id: None,
|
||||
more_info: None
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_from_value_record_extra_field() {
|
||||
let span = Span::unknown();
|
||||
let rec = new_record(&[
|
||||
("command_line", Value::string("foo", span)),
|
||||
("id_nonexistent", Value::int(1, span)),
|
||||
]);
|
||||
assert!(item_from_value(rec).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_from_value_record_bad_type() {
|
||||
let span = Span::unknown();
|
||||
let rec = new_record(&[
|
||||
("command_line", Value::string("foo", span)),
|
||||
("id", Value::string("one".to_string(), span)),
|
||||
]);
|
||||
assert!(item_from_value(rec).is_err());
|
||||
}
|
||||
|
||||
fn new_record(rec: &[(&'static str, Value)]) -> Value {
|
||||
let span = Span::unknown();
|
||||
let rec = Record::from_raw_cols_vals(
|
||||
rec.iter().map(|(col, _)| col.to_string()).collect(),
|
||||
rec.iter().map(|(_, val)| val.clone()).collect(),
|
||||
span,
|
||||
span,
|
||||
)
|
||||
.unwrap();
|
||||
Value::record(rec, span)
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::no_backup(&["history.dat"], "history.dat.bak")]
|
||||
#[case::backup_exists(&["history.dat", "history.dat.bak"], "history.dat.bak.1")]
|
||||
#[case::multiple_backups_exists( &["history.dat", "history.dat.bak", "history.dat.bak.1"], "history.dat.bak.2")]
|
||||
fn test_find_backup_path(#[case] existing: &[&str], #[case] want: &str) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for name in existing {
|
||||
std::fs::File::create_new(dir.path().join(name)).unwrap();
|
||||
}
|
||||
let got = find_backup_path(&dir.path().join("history.dat")).unwrap();
|
||||
assert_eq!(got, dir.path().join(want))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_backup() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut history = std::fs::File::create_new(dir.path().join("history.dat")).unwrap();
|
||||
use std::io::Write;
|
||||
write!(&mut history, "123").unwrap();
|
||||
let want_bak_path = dir.path().join("history.dat.bak");
|
||||
assert_eq!(
|
||||
backup(&dir.path().join("history.dat")),
|
||||
Ok(Some(want_bak_path.clone()))
|
||||
);
|
||||
let got_data = String::from_utf8(std::fs::read(want_bak_path).unwrap()).unwrap();
|
||||
assert_eq!(got_data, "123");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_backup_no_file() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let bak_path = backup(&dir.path().join("history.dat")).unwrap();
|
||||
assert!(bak_path.is_none());
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
mod fields;
|
||||
mod history_;
|
||||
mod history_import;
|
||||
mod history_session;
|
||||
|
||||
pub use history_::History;
|
||||
pub use history_import::HistoryImport;
|
||||
pub use history_session::HistorySession;
|
||||
|
@ -7,7 +7,7 @@ mod keybindings_list;
|
||||
mod keybindings_listen;
|
||||
|
||||
pub use commandline::{Commandline, CommandlineEdit, CommandlineGetCursor, CommandlineSetCursor};
|
||||
pub use history::{History, HistorySession};
|
||||
pub use history::{History, HistoryImport, HistorySession};
|
||||
pub use keybindings::Keybindings;
|
||||
pub use keybindings_default::KeybindingsDefault;
|
||||
pub use keybindings_list::KeybindingsList;
|
||||
|
@ -12,7 +12,7 @@ pub trait Completer {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
pos: usize,
|
||||
|
@ -1,5 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
completions::{Completer, CompletionOptions, MatchAlgorithm},
|
||||
completions::{Completer, CompletionOptions},
|
||||
SuggestionKind,
|
||||
};
|
||||
use nu_parser::FlatShape;
|
||||
@ -9,7 +11,7 @@ use nu_protocol::{
|
||||
};
|
||||
use reedline::Suggestion;
|
||||
|
||||
use super::{completion_common::sort_suggestions, SemanticSuggestion};
|
||||
use super::{completion_options::NuMatcher, SemanticSuggestion};
|
||||
|
||||
pub struct CommandCompletion {
|
||||
flattened: Vec<(Span, FlatShape)>,
|
||||
@ -33,13 +35,13 @@ impl CommandCompletion {
|
||||
fn external_command_completion(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
prefix: &str,
|
||||
match_algorithm: MatchAlgorithm,
|
||||
) -> Vec<String> {
|
||||
let mut executables = vec![];
|
||||
sugg_span: reedline::Span,
|
||||
matched_internal: impl Fn(&str) -> bool,
|
||||
matcher: &mut NuMatcher<String>,
|
||||
) -> HashMap<String, SemanticSuggestion> {
|
||||
let mut suggs = HashMap::new();
|
||||
|
||||
// os agnostic way to get the PATH env var
|
||||
let paths = working_set.permanent_state.get_path_env_var();
|
||||
let paths = working_set.permanent_state.get_env_var_insensitive("path");
|
||||
|
||||
if let Some(paths) = paths {
|
||||
if let Ok(paths) = paths.as_list() {
|
||||
@ -54,24 +56,38 @@ impl CommandCompletion {
|
||||
.completions
|
||||
.external
|
||||
.max_results
|
||||
> executables.len() as i64
|
||||
&& !executables.contains(
|
||||
&item
|
||||
.path()
|
||||
.file_name()
|
||||
.map(|x| x.to_string_lossy().to_string())
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
&& matches!(
|
||||
item.path().file_name().map(|x| match_algorithm
|
||||
.matches_str(&x.to_string_lossy(), prefix)),
|
||||
Some(true)
|
||||
)
|
||||
&& is_executable::is_executable(item.path())
|
||||
<= suggs.len() as i64
|
||||
{
|
||||
if let Ok(name) = item.file_name().into_string() {
|
||||
executables.push(name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
let Ok(name) = item.file_name().into_string() else {
|
||||
continue;
|
||||
};
|
||||
let value = if matched_internal(&name) {
|
||||
format!("^{}", name)
|
||||
} else {
|
||||
name.clone()
|
||||
};
|
||||
if suggs.contains_key(&value) {
|
||||
continue;
|
||||
}
|
||||
if matcher.matches(&name) && is_executable::is_executable(item.path()) {
|
||||
// If there's an internal command with the same name, adds ^cmd to the
|
||||
// matcher so that both the internal and external command are included
|
||||
matcher.add(&name, value.clone());
|
||||
suggs.insert(
|
||||
value.clone(),
|
||||
SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value,
|
||||
span: sugg_span,
|
||||
append_whitespace: true,
|
||||
..Default::default()
|
||||
},
|
||||
// TODO: is there a way to create a test?
|
||||
kind: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -79,7 +95,7 @@ impl CommandCompletion {
|
||||
}
|
||||
}
|
||||
|
||||
executables
|
||||
suggs
|
||||
}
|
||||
|
||||
fn complete_commands(
|
||||
@ -88,68 +104,59 @@ impl CommandCompletion {
|
||||
span: Span,
|
||||
offset: usize,
|
||||
find_externals: bool,
|
||||
match_algorithm: MatchAlgorithm,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
let partial = working_set.get_span_contents(span);
|
||||
let mut matcher = NuMatcher::new(String::from_utf8_lossy(partial), options.clone());
|
||||
|
||||
let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial);
|
||||
let sugg_span = reedline::Span::new(span.start - offset, span.end - offset);
|
||||
|
||||
let mut results = working_set
|
||||
.find_commands_by_predicate(filter_predicate, true)
|
||||
.into_iter()
|
||||
.map(move |x| SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(&x.0).to_string(),
|
||||
description: x.1,
|
||||
span: reedline::Span::new(span.start - offset, span.end - offset),
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: Some(SuggestionKind::Command(x.2)),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let partial = working_set.get_span_contents(span);
|
||||
let partial = String::from_utf8_lossy(partial).to_string();
|
||||
|
||||
if find_externals {
|
||||
let results_external = self
|
||||
.external_command_completion(working_set, &partial, match_algorithm)
|
||||
.into_iter()
|
||||
.map(move |x| SemanticSuggestion {
|
||||
let mut internal_suggs = HashMap::new();
|
||||
let filtered_commands = working_set.find_commands_by_predicate(
|
||||
|name| {
|
||||
let name = String::from_utf8_lossy(name);
|
||||
matcher.add(&name, name.to_string())
|
||||
},
|
||||
true,
|
||||
);
|
||||
for (name, description, typ) in filtered_commands {
|
||||
let name = String::from_utf8_lossy(&name);
|
||||
internal_suggs.insert(
|
||||
name.to_string(),
|
||||
SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: x,
|
||||
span: reedline::Span::new(span.start - offset, span.end - offset),
|
||||
value: name.to_string(),
|
||||
description,
|
||||
span: sugg_span,
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO: is there a way to create a test?
|
||||
kind: None,
|
||||
});
|
||||
|
||||
let results_strings: Vec<String> =
|
||||
results.iter().map(|x| x.suggestion.value.clone()).collect();
|
||||
|
||||
for external in results_external {
|
||||
if results_strings.contains(&external.suggestion.value) {
|
||||
results.push(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: format!("^{}", external.suggestion.value),
|
||||
span: external.suggestion.span,
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: external.kind,
|
||||
})
|
||||
} else {
|
||||
results.push(external)
|
||||
}
|
||||
}
|
||||
|
||||
results
|
||||
} else {
|
||||
results
|
||||
kind: Some(SuggestionKind::Command(typ)),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let mut external_suggs = if find_externals {
|
||||
self.external_command_completion(
|
||||
working_set,
|
||||
sugg_span,
|
||||
|name| internal_suggs.contains_key(name),
|
||||
&mut matcher,
|
||||
)
|
||||
} else {
|
||||
HashMap::new()
|
||||
};
|
||||
|
||||
let mut res = Vec::new();
|
||||
for cmd_name in matcher.results() {
|
||||
if let Some(sugg) = internal_suggs
|
||||
.remove(&cmd_name)
|
||||
.or_else(|| external_suggs.remove(&cmd_name))
|
||||
{
|
||||
res.push(sugg);
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
@ -158,7 +165,7 @@ impl Completer for CommandCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
_stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
_prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
pos: usize,
|
||||
@ -188,18 +195,18 @@ impl Completer for CommandCompletion {
|
||||
Span::new(last.0.start, pos),
|
||||
offset,
|
||||
false,
|
||||
options.match_algorithm,
|
||||
options,
|
||||
)
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
if !subcommands.is_empty() {
|
||||
return sort_suggestions(&String::from_utf8_lossy(&prefix), subcommands, options);
|
||||
return subcommands;
|
||||
}
|
||||
|
||||
let config = working_set.get_config();
|
||||
let commands = if matches!(self.flat_shape, nu_parser::FlatShape::External)
|
||||
if matches!(self.flat_shape, nu_parser::FlatShape::External)
|
||||
|| matches!(self.flat_shape, nu_parser::FlatShape::InternalCall(_))
|
||||
|| ((span.end - span.start) == 0)
|
||||
|| is_passthrough_command(working_set.delta.get_file_contents())
|
||||
@ -214,13 +221,11 @@ impl Completer for CommandCompletion {
|
||||
span,
|
||||
offset,
|
||||
config.completions.external.enable,
|
||||
options.match_algorithm,
|
||||
options,
|
||||
)
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
sort_suggestions(&String::from_utf8_lossy(&prefix), commands, options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::completions::{
|
||||
CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion,
|
||||
DotNuCompletion, FileCompletion, FlagCompletion, VariableCompletion,
|
||||
DotNuCompletion, FileCompletion, FlagCompletion, OperatorCompletion, VariableCompletion,
|
||||
};
|
||||
use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style};
|
||||
use nu_engine::eval_block;
|
||||
@ -25,7 +25,7 @@ impl NuCompleter {
|
||||
pub fn new(engine_state: Arc<EngineState>, stack: Arc<Stack>) -> Self {
|
||||
Self {
|
||||
engine_state,
|
||||
stack: Stack::with_parent(stack).reset_out_dest().capture(),
|
||||
stack: Stack::with_parent(stack).reset_out_dest().collect_value(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ impl NuCompleter {
|
||||
&self,
|
||||
completer: &mut T,
|
||||
working_set: &StateWorkingSet,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
new_span: Span,
|
||||
offset: usize,
|
||||
pos: usize,
|
||||
@ -55,7 +55,7 @@ impl NuCompleter {
|
||||
completer.fetch(
|
||||
working_set,
|
||||
&self.stack,
|
||||
prefix.clone(),
|
||||
prefix,
|
||||
new_span,
|
||||
offset,
|
||||
pos,
|
||||
@ -170,23 +170,38 @@ impl NuCompleter {
|
||||
let new_span = Span::new(flat.0.start, flat.0.end - 1);
|
||||
|
||||
// Parses the prefix. Completion should look up to the cursor position, not after.
|
||||
let mut prefix = working_set.get_span_contents(flat.0).to_vec();
|
||||
let mut prefix = working_set.get_span_contents(flat.0);
|
||||
let index = pos - flat.0.start;
|
||||
prefix.drain(index..);
|
||||
prefix = &prefix[..index];
|
||||
|
||||
// Variables completion
|
||||
if prefix.starts_with(b"$") || most_left_var.is_some() {
|
||||
let mut completer =
|
||||
let mut variable_names_completer =
|
||||
VariableCompletion::new(most_left_var.unwrap_or((vec![], vec![])));
|
||||
|
||||
return self.process_completion(
|
||||
&mut completer,
|
||||
let mut variable_completions = self.process_completion(
|
||||
&mut variable_names_completer,
|
||||
&working_set,
|
||||
prefix,
|
||||
new_span,
|
||||
fake_offset,
|
||||
pos,
|
||||
);
|
||||
|
||||
let mut variable_operations_completer =
|
||||
OperatorCompletion::new(pipeline_element.expr.clone());
|
||||
|
||||
let mut variable_operations_completions = self.process_completion(
|
||||
&mut variable_operations_completer,
|
||||
&working_set,
|
||||
prefix,
|
||||
new_span,
|
||||
fake_offset,
|
||||
pos,
|
||||
);
|
||||
|
||||
variable_completions.append(&mut variable_operations_completions);
|
||||
return variable_completions;
|
||||
}
|
||||
|
||||
// Flags completion
|
||||
@ -196,7 +211,7 @@ impl NuCompleter {
|
||||
let result = self.process_completion(
|
||||
&mut completer,
|
||||
&working_set,
|
||||
prefix.clone(),
|
||||
prefix,
|
||||
new_span,
|
||||
fake_offset,
|
||||
pos,
|
||||
@ -270,6 +285,29 @@ impl NuCompleter {
|
||||
fake_offset,
|
||||
pos,
|
||||
);
|
||||
} else if matches!(
|
||||
previous_expr.1,
|
||||
FlatShape::Float
|
||||
| FlatShape::Int
|
||||
| FlatShape::String
|
||||
| FlatShape::List
|
||||
| FlatShape::Bool
|
||||
| FlatShape::Variable(_)
|
||||
) {
|
||||
let mut completer =
|
||||
OperatorCompletion::new(pipeline_element.expr.clone());
|
||||
|
||||
let operator_suggestion = self.process_completion(
|
||||
&mut completer,
|
||||
&working_set,
|
||||
prefix,
|
||||
new_span,
|
||||
fake_offset,
|
||||
pos,
|
||||
);
|
||||
if !operator_suggestion.is_empty() {
|
||||
return operator_suggestion;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -327,7 +365,7 @@ impl NuCompleter {
|
||||
let mut out: Vec<_> = self.process_completion(
|
||||
&mut completer,
|
||||
&working_set,
|
||||
prefix.clone(),
|
||||
prefix,
|
||||
new_span,
|
||||
fake_offset,
|
||||
pos,
|
||||
@ -533,6 +571,11 @@ mod completer_tests {
|
||||
|
||||
let mut completer = NuCompleter::new(engine_state.into(), Arc::new(Stack::new()));
|
||||
let dataset = [
|
||||
("1 bit-sh", true, "b", vec!["bit-shl", "bit-shr"]),
|
||||
("1.0 bit-sh", false, "b", vec![]),
|
||||
("1 m", true, "m", vec!["mod"]),
|
||||
("1.0 m", true, "m", vec!["mod"]),
|
||||
("\"a\" s", true, "s", vec!["starts-with"]),
|
||||
("sudo", false, "", Vec::new()),
|
||||
("sudo l", true, "l", vec!["ls", "let", "lines", "loop"]),
|
||||
(" sudo", false, "", Vec::new()),
|
||||
|
@ -1,22 +1,20 @@
|
||||
use super::MatchAlgorithm;
|
||||
use crate::{
|
||||
completions::{matches, CompletionOptions},
|
||||
SemanticSuggestion,
|
||||
};
|
||||
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
|
||||
use super::{completion_options::NuMatcher, MatchAlgorithm};
|
||||
use crate::completions::CompletionOptions;
|
||||
use nu_ansi_term::Style;
|
||||
use nu_engine::env_to_string;
|
||||
use nu_path::dots::expand_ndots;
|
||||
use nu_path::{expand_to_real_path, home_dir};
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
CompletionSort, Span,
|
||||
Span,
|
||||
};
|
||||
use nu_utils::get_ls_colors;
|
||||
use nu_utils::IgnoreCaseExt;
|
||||
use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct PathBuiltFromString {
|
||||
cwd: PathBuf,
|
||||
parts: Vec<String>,
|
||||
isdir: bool,
|
||||
}
|
||||
@ -30,76 +28,84 @@ pub struct PathBuiltFromString {
|
||||
/// want_directory: Whether we want only directories as completion matches.
|
||||
/// Some commands like `cd` can only be run on directories whereas others
|
||||
/// like `ls` can be run on regular files as well.
|
||||
pub fn complete_rec(
|
||||
fn complete_rec(
|
||||
partial: &[&str],
|
||||
built: &PathBuiltFromString,
|
||||
cwd: &Path,
|
||||
built_paths: &[PathBuiltFromString],
|
||||
options: &CompletionOptions,
|
||||
want_directory: bool,
|
||||
isdir: bool,
|
||||
) -> Vec<PathBuiltFromString> {
|
||||
let mut completions = vec![];
|
||||
|
||||
if let Some((&base, rest)) = partial.split_first() {
|
||||
if base.chars().all(|c| c == '.') && (isdir || !rest.is_empty()) {
|
||||
let mut built = built.clone();
|
||||
built.parts.push(base.to_string());
|
||||
built.isdir = true;
|
||||
return complete_rec(rest, &built, cwd, options, want_directory, isdir);
|
||||
}
|
||||
}
|
||||
|
||||
let mut built_path = cwd.to_path_buf();
|
||||
for part in &built.parts {
|
||||
built_path.push(part);
|
||||
}
|
||||
|
||||
let Ok(result) = built_path.read_dir() else {
|
||||
return completions;
|
||||
};
|
||||
|
||||
let mut entries = Vec::new();
|
||||
for entry in result.filter_map(|e| e.ok()) {
|
||||
let entry_name = entry.file_name().to_string_lossy().into_owned();
|
||||
let entry_isdir = entry.path().is_dir();
|
||||
let mut built = built.clone();
|
||||
built.parts.push(entry_name.clone());
|
||||
built.isdir = entry_isdir;
|
||||
|
||||
if !want_directory || entry_isdir {
|
||||
entries.push((entry_name, built));
|
||||
let built_paths: Vec<_> = built_paths
|
||||
.iter()
|
||||
.map(|built| {
|
||||
let mut built = built.clone();
|
||||
built.parts.push(base.to_string());
|
||||
built.isdir = true;
|
||||
built
|
||||
})
|
||||
.collect();
|
||||
return complete_rec(rest, &built_paths, options, want_directory, isdir);
|
||||
}
|
||||
}
|
||||
|
||||
let prefix = partial.first().unwrap_or(&"");
|
||||
let sorted_entries = sort_completions(prefix, entries, options, |(entry, _)| entry);
|
||||
let mut matcher = NuMatcher::new(prefix, options.clone());
|
||||
|
||||
for (entry_name, built) in sorted_entries {
|
||||
for built in built_paths {
|
||||
let mut path = built.cwd.clone();
|
||||
for part in &built.parts {
|
||||
path.push(part);
|
||||
}
|
||||
|
||||
let Ok(result) = path.read_dir() else {
|
||||
continue;
|
||||
};
|
||||
|
||||
for entry in result.filter_map(|e| e.ok()) {
|
||||
let entry_name = entry.file_name().to_string_lossy().into_owned();
|
||||
let entry_isdir = entry.path().is_dir();
|
||||
let mut built = built.clone();
|
||||
built.parts.push(entry_name.clone());
|
||||
built.isdir = entry_isdir;
|
||||
|
||||
if !want_directory || entry_isdir {
|
||||
matcher.add(entry_name.clone(), (entry_name, built));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut completions = vec![];
|
||||
for (entry_name, built) in matcher.results() {
|
||||
match partial.split_first() {
|
||||
Some((base, rest)) => {
|
||||
if matches(base, &entry_name, options) {
|
||||
// We use `isdir` to confirm that the current component has
|
||||
// at least one next component or a slash.
|
||||
// Serves as confirmation to ignore longer completions for
|
||||
// components in between.
|
||||
if !rest.is_empty() || isdir {
|
||||
completions.extend(complete_rec(
|
||||
rest,
|
||||
&built,
|
||||
cwd,
|
||||
options,
|
||||
want_directory,
|
||||
isdir,
|
||||
));
|
||||
} else {
|
||||
completions.push(built);
|
||||
}
|
||||
// We use `isdir` to confirm that the current component has
|
||||
// at least one next component or a slash.
|
||||
// Serves as confirmation to ignore longer completions for
|
||||
// components in between.
|
||||
if !rest.is_empty() || isdir {
|
||||
completions.extend(complete_rec(
|
||||
rest,
|
||||
&[built],
|
||||
options,
|
||||
want_directory,
|
||||
isdir,
|
||||
));
|
||||
} else {
|
||||
completions.push(built);
|
||||
}
|
||||
if entry_name.eq(base)
|
||||
&& matches!(options.match_algorithm, MatchAlgorithm::Prefix)
|
||||
&& isdir
|
||||
{
|
||||
break;
|
||||
|
||||
// For https://github.com/nushell/nushell/issues/13204
|
||||
if isdir && options.match_algorithm == MatchAlgorithm::Prefix {
|
||||
let exact_match = if options.case_sensitive {
|
||||
entry_name.eq(base)
|
||||
} else {
|
||||
entry_name.to_folded_case().eq(&base.to_folded_case())
|
||||
};
|
||||
if exact_match {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
@ -147,15 +153,25 @@ fn surround_remove(partial: &str) -> String {
|
||||
partial.to_string()
|
||||
}
|
||||
|
||||
pub struct FileSuggestion {
|
||||
pub span: nu_protocol::Span,
|
||||
pub path: String,
|
||||
pub style: Option<Style>,
|
||||
pub cwd: PathBuf,
|
||||
}
|
||||
|
||||
/// # Parameters
|
||||
/// * `cwds` - A list of directories in which to search. The only reason this isn't a single string
|
||||
/// is because dotnu_completions searches in multiple directories at once
|
||||
pub fn complete_item(
|
||||
want_directory: bool,
|
||||
span: nu_protocol::Span,
|
||||
partial: &str,
|
||||
cwd: &str,
|
||||
cwds: &[impl AsRef<str>],
|
||||
options: &CompletionOptions,
|
||||
engine_state: &EngineState,
|
||||
stack: &Stack,
|
||||
) -> Vec<(nu_protocol::Span, String, Option<Style>)> {
|
||||
) -> Vec<FileSuggestion> {
|
||||
let cleaned_partial = surround_remove(partial);
|
||||
let isdir = cleaned_partial.ends_with(is_separator);
|
||||
let expanded_partial = expand_ndots(Path::new(&cleaned_partial));
|
||||
@ -175,18 +191,21 @@ pub fn complete_item(
|
||||
partial.push_str(&format!("{path_separator}."));
|
||||
}
|
||||
|
||||
let cwd_pathbuf = Path::new(cwd).to_path_buf();
|
||||
let cwd_pathbufs: Vec<_> = cwds
|
||||
.iter()
|
||||
.map(|cwd| Path::new(cwd.as_ref()).to_path_buf())
|
||||
.collect();
|
||||
let ls_colors = (engine_state.config.completions.use_ls_colors
|
||||
&& engine_state.config.use_ansi_coloring)
|
||||
.then(|| {
|
||||
let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
|
||||
Some(v) => env_to_string("LS_COLORS", &v, engine_state, stack).ok(),
|
||||
Some(v) => env_to_string("LS_COLORS", v, engine_state, stack).ok(),
|
||||
None => None,
|
||||
};
|
||||
get_ls_colors(ls_colors_env_str)
|
||||
});
|
||||
|
||||
let mut cwd = cwd_pathbuf.clone();
|
||||
let mut cwds = cwd_pathbufs.clone();
|
||||
let mut prefix_len = 0;
|
||||
let mut original_cwd = OriginalCwd::None;
|
||||
|
||||
@ -194,19 +213,21 @@ pub fn complete_item(
|
||||
match components.peek().cloned() {
|
||||
Some(c @ Component::Prefix(..)) => {
|
||||
// windows only by definition
|
||||
cwd = [c, Component::RootDir].iter().collect();
|
||||
cwds = vec![[c, Component::RootDir].iter().collect()];
|
||||
prefix_len = c.as_os_str().len();
|
||||
original_cwd = OriginalCwd::Prefix(c.as_os_str().to_string_lossy().into_owned());
|
||||
}
|
||||
Some(c @ Component::RootDir) => {
|
||||
// This is kind of a hack. When joining an empty string with the rest,
|
||||
// we add the slash automagically
|
||||
cwd = PathBuf::from(c.as_os_str());
|
||||
cwds = vec![PathBuf::from(c.as_os_str())];
|
||||
prefix_len = 1;
|
||||
original_cwd = OriginalCwd::Prefix(String::new());
|
||||
}
|
||||
Some(Component::Normal(home)) if home.to_string_lossy() == "~" => {
|
||||
cwd = home_dir().map(Into::into).unwrap_or(cwd_pathbuf);
|
||||
cwds = home_dir()
|
||||
.map(|dir| vec![dir.into()])
|
||||
.unwrap_or(cwd_pathbufs);
|
||||
prefix_len = 1;
|
||||
original_cwd = OriginalCwd::Home;
|
||||
}
|
||||
@ -223,8 +244,14 @@ pub fn complete_item(
|
||||
|
||||
complete_rec(
|
||||
partial.as_slice(),
|
||||
&PathBuiltFromString::default(),
|
||||
&cwd,
|
||||
&cwds
|
||||
.into_iter()
|
||||
.map(|cwd| PathBuiltFromString {
|
||||
cwd,
|
||||
parts: Vec::new(),
|
||||
isdir: false,
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
options,
|
||||
want_directory,
|
||||
isdir,
|
||||
@ -234,6 +261,7 @@ pub fn complete_item(
|
||||
if should_collapse_dots {
|
||||
p = collapse_ndots(p);
|
||||
}
|
||||
let cwd = p.cwd.clone();
|
||||
let path = original_cwd.apply(p, path_separator);
|
||||
let style = ls_colors.as_ref().map(|lsc| {
|
||||
lsc.style_for_path_with_metadata(
|
||||
@ -245,7 +273,12 @@ pub fn complete_item(
|
||||
.map(lscolors::Style::to_nu_ansi_term_style)
|
||||
.unwrap_or_default()
|
||||
});
|
||||
(span, escape_path(path, want_directory), style)
|
||||
FileSuggestion {
|
||||
span,
|
||||
path: escape_path(path, want_directory),
|
||||
style,
|
||||
cwd,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@ -267,8 +300,10 @@ pub fn escape_path(path: String, dir: bool) -> String {
|
||||
let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']);
|
||||
let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']);
|
||||
let maybe_flag = path.starts_with('-');
|
||||
let maybe_variable = path.starts_with('$');
|
||||
let maybe_number = path.parse::<f64>().is_ok();
|
||||
if filename_contaminated || dirname_contaminated || maybe_flag || maybe_number {
|
||||
if filename_contaminated || dirname_contaminated || maybe_flag || maybe_variable || maybe_number
|
||||
{
|
||||
format!("`{path}`")
|
||||
} else {
|
||||
path
|
||||
@ -308,45 +343,6 @@ pub fn adjust_if_intermediate(
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience function to sort suggestions using [`sort_completions`]
|
||||
pub fn sort_suggestions(
|
||||
prefix: &str,
|
||||
items: Vec<SemanticSuggestion>,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
sort_completions(prefix, items, options, |it| &it.suggestion.value)
|
||||
}
|
||||
|
||||
/// # Arguments
|
||||
/// * `prefix` - What the user's typed, for sorting by fuzzy matcher score
|
||||
pub fn sort_completions<T>(
|
||||
prefix: &str,
|
||||
mut items: Vec<T>,
|
||||
options: &CompletionOptions,
|
||||
get_value: fn(&T) -> &str,
|
||||
) -> Vec<T> {
|
||||
// Sort items
|
||||
if options.sort == CompletionSort::Smart && options.match_algorithm == MatchAlgorithm::Fuzzy {
|
||||
let mut matcher = SkimMatcherV2::default();
|
||||
if options.case_sensitive {
|
||||
matcher = matcher.respect_case();
|
||||
} else {
|
||||
matcher = matcher.ignore_case();
|
||||
};
|
||||
items.sort_by(|a, b| {
|
||||
let a_str = get_value(a);
|
||||
let b_str = get_value(b);
|
||||
let a_score = matcher.fuzzy_match(a_str, prefix).unwrap_or_default();
|
||||
let b_score = matcher.fuzzy_match(b_str, prefix).unwrap_or_default();
|
||||
b_score.cmp(&a_score).then(a_str.cmp(b_str))
|
||||
});
|
||||
} else {
|
||||
items.sort_by(|a, b| get_value(a).cmp(get_value(b)));
|
||||
}
|
||||
|
||||
items
|
||||
}
|
||||
|
||||
/// Collapse multiple ".." components into n-dots.
|
||||
///
|
||||
/// It performs the reverse operation of `expand_ndots`, collapsing sequences of ".." into n-dots,
|
||||
@ -357,6 +353,7 @@ fn collapse_ndots(path: PathBuiltFromString) -> PathBuiltFromString {
|
||||
let mut result = PathBuiltFromString {
|
||||
parts: Vec::with_capacity(path.parts.len()),
|
||||
isdir: path.isdir,
|
||||
cwd: path.cwd,
|
||||
};
|
||||
|
||||
let mut dot_count = 0;
|
||||
|
@ -1,7 +1,10 @@
|
||||
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
|
||||
use nu_parser::trim_quotes_str;
|
||||
use nu_protocol::{CompletionAlgorithm, CompletionSort};
|
||||
use std::fmt::Display;
|
||||
use nu_utils::IgnoreCaseExt;
|
||||
use std::{borrow::Cow, fmt::Display};
|
||||
|
||||
use super::SemanticSuggestion;
|
||||
|
||||
/// Describes how suggestions should be matched.
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
@ -19,33 +22,154 @@ pub enum MatchAlgorithm {
|
||||
Fuzzy,
|
||||
}
|
||||
|
||||
impl MatchAlgorithm {
|
||||
/// Returns whether the `needle` search text matches the given `haystack`.
|
||||
pub fn matches_str(&self, haystack: &str, needle: &str) -> bool {
|
||||
let haystack = trim_quotes_str(haystack);
|
||||
let needle = trim_quotes_str(needle);
|
||||
match *self {
|
||||
MatchAlgorithm::Prefix => haystack.starts_with(needle),
|
||||
pub struct NuMatcher<T> {
|
||||
options: CompletionOptions,
|
||||
needle: String,
|
||||
state: State<T>,
|
||||
}
|
||||
|
||||
enum State<T> {
|
||||
Prefix {
|
||||
/// Holds (haystack, item)
|
||||
items: Vec<(String, T)>,
|
||||
},
|
||||
Fuzzy {
|
||||
matcher: Box<SkimMatcherV2>,
|
||||
/// Holds (haystack, item, score)
|
||||
items: Vec<(String, T, i64)>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Filters and sorts suggestions
|
||||
impl<T> NuMatcher<T> {
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `needle` - The text to search for
|
||||
pub fn new(needle: impl AsRef<str>, options: CompletionOptions) -> NuMatcher<T> {
|
||||
let orig_needle = trim_quotes_str(needle.as_ref());
|
||||
let lowercase_needle = if options.case_sensitive {
|
||||
orig_needle.to_owned()
|
||||
} else {
|
||||
orig_needle.to_folded_case()
|
||||
};
|
||||
match options.match_algorithm {
|
||||
MatchAlgorithm::Prefix => NuMatcher {
|
||||
options,
|
||||
needle: lowercase_needle,
|
||||
state: State::Prefix { items: Vec::new() },
|
||||
},
|
||||
MatchAlgorithm::Fuzzy => {
|
||||
let matcher = SkimMatcherV2::default();
|
||||
matcher.fuzzy_match(haystack, needle).is_some()
|
||||
let mut matcher = SkimMatcherV2::default();
|
||||
if options.case_sensitive {
|
||||
matcher = matcher.respect_case();
|
||||
} else {
|
||||
matcher = matcher.ignore_case();
|
||||
};
|
||||
NuMatcher {
|
||||
options,
|
||||
needle: orig_needle.to_owned(),
|
||||
state: State::Fuzzy {
|
||||
matcher: Box::new(matcher),
|
||||
items: Vec::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether the `needle` search text matches the given `haystack`.
|
||||
pub fn matches_u8(&self, haystack: &[u8], needle: &[u8]) -> bool {
|
||||
match *self {
|
||||
MatchAlgorithm::Prefix => haystack.starts_with(needle),
|
||||
MatchAlgorithm::Fuzzy => {
|
||||
let haystack_str = String::from_utf8_lossy(haystack);
|
||||
let needle_str = String::from_utf8_lossy(needle);
|
||||
|
||||
let matcher = SkimMatcherV2::default();
|
||||
matcher.fuzzy_match(&haystack_str, &needle_str).is_some()
|
||||
/// Returns whether or not the haystack matches the needle. If it does, `item` is added
|
||||
/// to the list of matches (if given).
|
||||
///
|
||||
/// Helper to avoid code duplication between [NuMatcher::add] and [NuMatcher::matches].
|
||||
fn matches_aux(&mut self, haystack: &str, item: Option<T>) -> bool {
|
||||
let haystack = trim_quotes_str(haystack);
|
||||
match &mut self.state {
|
||||
State::Prefix { items } => {
|
||||
let haystack_folded = if self.options.case_sensitive {
|
||||
Cow::Borrowed(haystack)
|
||||
} else {
|
||||
Cow::Owned(haystack.to_folded_case())
|
||||
};
|
||||
let matches = if self.options.positional {
|
||||
haystack_folded.starts_with(self.needle.as_str())
|
||||
} else {
|
||||
haystack_folded.contains(self.needle.as_str())
|
||||
};
|
||||
if matches {
|
||||
if let Some(item) = item {
|
||||
items.push((haystack.to_string(), item));
|
||||
}
|
||||
}
|
||||
matches
|
||||
}
|
||||
State::Fuzzy { items, matcher } => {
|
||||
let Some(score) = matcher.fuzzy_match(haystack, &self.needle) else {
|
||||
return false;
|
||||
};
|
||||
if let Some(item) = item {
|
||||
items.push((haystack.to_string(), item, score));
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Add the given item if the given haystack matches the needle.
|
||||
///
|
||||
/// Returns whether the item was added.
|
||||
pub fn add(&mut self, haystack: impl AsRef<str>, item: T) -> bool {
|
||||
self.matches_aux(haystack.as_ref(), Some(item))
|
||||
}
|
||||
|
||||
/// Returns whether the haystack matches the needle.
|
||||
pub fn matches(&mut self, haystack: &str) -> bool {
|
||||
self.matches_aux(haystack, None)
|
||||
}
|
||||
|
||||
/// Get all the items that matched (sorted)
|
||||
pub fn results(self) -> Vec<T> {
|
||||
match self.state {
|
||||
State::Prefix { mut items, .. } => {
|
||||
items.sort_by(|(haystack1, _), (haystack2, _)| {
|
||||
let cmp_sensitive = haystack1.cmp(haystack2);
|
||||
if self.options.case_sensitive {
|
||||
cmp_sensitive
|
||||
} else {
|
||||
haystack1
|
||||
.to_folded_case()
|
||||
.cmp(&haystack2.to_folded_case())
|
||||
.then(cmp_sensitive)
|
||||
}
|
||||
});
|
||||
items.into_iter().map(|(_, item)| item).collect::<Vec<_>>()
|
||||
}
|
||||
State::Fuzzy { mut items, .. } => {
|
||||
match self.options.sort {
|
||||
CompletionSort::Alphabetical => {
|
||||
items.sort_by(|(haystack1, _, _), (haystack2, _, _)| {
|
||||
haystack1.cmp(haystack2)
|
||||
});
|
||||
}
|
||||
CompletionSort::Smart => {
|
||||
items.sort_by(|(haystack1, _, score1), (haystack2, _, score2)| {
|
||||
score2.cmp(score1).then(haystack1.cmp(haystack2))
|
||||
});
|
||||
}
|
||||
}
|
||||
items
|
||||
.into_iter()
|
||||
.map(|(_, item, _)| item)
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NuMatcher<SemanticSuggestion> {
|
||||
pub fn add_semantic_suggestion(&mut self, sugg: SemanticSuggestion) -> bool {
|
||||
let value = sugg.suggestion.value.to_string();
|
||||
self.add(value, sugg)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CompletionAlgorithm> for MatchAlgorithm {
|
||||
@ -105,35 +229,49 @@ impl Default for CompletionOptions {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::MatchAlgorithm;
|
||||
use rstest::rstest;
|
||||
|
||||
#[test]
|
||||
fn match_algorithm_prefix() {
|
||||
let algorithm = MatchAlgorithm::Prefix;
|
||||
use super::{CompletionOptions, MatchAlgorithm, NuMatcher};
|
||||
|
||||
assert!(algorithm.matches_str("example text", ""));
|
||||
assert!(algorithm.matches_str("example text", "examp"));
|
||||
assert!(!algorithm.matches_str("example text", "text"));
|
||||
|
||||
assert!(algorithm.matches_u8(&[1, 2, 3], &[]));
|
||||
assert!(algorithm.matches_u8(&[1, 2, 3], &[1, 2]));
|
||||
assert!(!algorithm.matches_u8(&[1, 2, 3], &[2, 3]));
|
||||
#[rstest]
|
||||
#[case(MatchAlgorithm::Prefix, "example text", "", true)]
|
||||
#[case(MatchAlgorithm::Prefix, "example text", "examp", true)]
|
||||
#[case(MatchAlgorithm::Prefix, "example text", "text", false)]
|
||||
#[case(MatchAlgorithm::Fuzzy, "example text", "", true)]
|
||||
#[case(MatchAlgorithm::Fuzzy, "example text", "examp", true)]
|
||||
#[case(MatchAlgorithm::Fuzzy, "example text", "ext", true)]
|
||||
#[case(MatchAlgorithm::Fuzzy, "example text", "mplxt", true)]
|
||||
#[case(MatchAlgorithm::Fuzzy, "example text", "mpp", false)]
|
||||
fn match_algorithm_simple(
|
||||
#[case] match_algorithm: MatchAlgorithm,
|
||||
#[case] haystack: &str,
|
||||
#[case] needle: &str,
|
||||
#[case] should_match: bool,
|
||||
) {
|
||||
let options = CompletionOptions {
|
||||
match_algorithm,
|
||||
..Default::default()
|
||||
};
|
||||
let mut matcher = NuMatcher::new(needle, options);
|
||||
matcher.add(haystack, haystack);
|
||||
if should_match {
|
||||
assert_eq!(vec![haystack], matcher.results());
|
||||
} else {
|
||||
assert_ne!(vec![haystack], matcher.results());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_algorithm_fuzzy() {
|
||||
let algorithm = MatchAlgorithm::Fuzzy;
|
||||
|
||||
assert!(algorithm.matches_str("example text", ""));
|
||||
assert!(algorithm.matches_str("example text", "examp"));
|
||||
assert!(algorithm.matches_str("example text", "ext"));
|
||||
assert!(algorithm.matches_str("example text", "mplxt"));
|
||||
assert!(!algorithm.matches_str("example text", "mpp"));
|
||||
|
||||
assert!(algorithm.matches_u8(&[1, 2, 3], &[]));
|
||||
assert!(algorithm.matches_u8(&[1, 2, 3], &[1, 2]));
|
||||
assert!(algorithm.matches_u8(&[1, 2, 3], &[2, 3]));
|
||||
assert!(algorithm.matches_u8(&[1, 2, 3], &[1, 3]));
|
||||
assert!(!algorithm.matches_u8(&[1, 2, 3], &[2, 2]));
|
||||
fn match_algorithm_fuzzy_sort_score() {
|
||||
let options = CompletionOptions {
|
||||
match_algorithm: MatchAlgorithm::Fuzzy,
|
||||
..Default::default()
|
||||
};
|
||||
let mut matcher = NuMatcher::new("fob", options);
|
||||
for item in ["foo/bar", "fob", "foo bar"] {
|
||||
matcher.add(item, item);
|
||||
}
|
||||
// Sort by score, then in alphabetical order
|
||||
assert_eq!(vec!["fob", "foo bar", "foo/bar"], matcher.results());
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +1,25 @@
|
||||
use crate::completions::{
|
||||
completer::map_value_completions, Completer, CompletionOptions, MatchAlgorithm,
|
||||
SemanticSuggestion,
|
||||
completer::map_value_completions, Completer, CompletionOptions, SemanticSuggestion,
|
||||
};
|
||||
use nu_engine::eval_call;
|
||||
use nu_protocol::{
|
||||
ast::{Argument, Call, Expr, Expression},
|
||||
debugger::WithoutDebug,
|
||||
engine::{Stack, StateWorkingSet},
|
||||
CompletionSort, PipelineData, Span, Type, Value,
|
||||
DeclId, PipelineData, Span, Type, Value,
|
||||
};
|
||||
use nu_utils::IgnoreCaseExt;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::completion_common::sort_suggestions;
|
||||
use super::completion_options::NuMatcher;
|
||||
|
||||
pub struct CustomCompletion {
|
||||
stack: Stack,
|
||||
decl_id: usize,
|
||||
decl_id: DeclId,
|
||||
line: String,
|
||||
}
|
||||
|
||||
impl CustomCompletion {
|
||||
pub fn new(stack: Stack, decl_id: usize, line: String) -> Self {
|
||||
pub fn new(stack: Stack, decl_id: DeclId, line: String) -> Self {
|
||||
Self {
|
||||
stack,
|
||||
decl_id,
|
||||
@ -35,7 +33,7 @@ impl Completer for CustomCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
_stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
pos: usize,
|
||||
@ -69,6 +67,7 @@ impl Completer for CustomCompletion {
|
||||
);
|
||||
|
||||
let mut custom_completion_options = None;
|
||||
let mut should_sort = true;
|
||||
|
||||
// Parse result
|
||||
let suggestions = result
|
||||
@ -86,10 +85,9 @@ impl Completer for CustomCompletion {
|
||||
let options = val.get("options");
|
||||
|
||||
if let Some(Value::Record { val: options, .. }) = &options {
|
||||
let should_sort = options
|
||||
.get("sort")
|
||||
.and_then(|val| val.as_bool().ok())
|
||||
.unwrap_or(false);
|
||||
if let Some(sort) = options.get("sort").and_then(|val| val.as_bool().ok()) {
|
||||
should_sort = sort;
|
||||
}
|
||||
|
||||
custom_completion_options = Some(CompletionOptions {
|
||||
case_sensitive: options
|
||||
@ -99,20 +97,16 @@ impl Completer for CustomCompletion {
|
||||
positional: options
|
||||
.get("positional")
|
||||
.and_then(|val| val.as_bool().ok())
|
||||
.unwrap_or(true),
|
||||
.unwrap_or(completion_options.positional),
|
||||
match_algorithm: match options.get("completion_algorithm") {
|
||||
Some(option) => option
|
||||
.coerce_string()
|
||||
.ok()
|
||||
.and_then(|option| option.try_into().ok())
|
||||
.unwrap_or(MatchAlgorithm::Prefix),
|
||||
.unwrap_or(completion_options.match_algorithm),
|
||||
None => completion_options.match_algorithm,
|
||||
},
|
||||
sort: if should_sort {
|
||||
CompletionSort::Alphabetical
|
||||
} else {
|
||||
CompletionSort::Smart
|
||||
},
|
||||
sort: completion_options.sort,
|
||||
});
|
||||
}
|
||||
|
||||
@ -123,41 +117,19 @@ impl Completer for CustomCompletion {
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let options = custom_completion_options
|
||||
.as_ref()
|
||||
.unwrap_or(completion_options);
|
||||
let suggestions = filter(&prefix, suggestions, options);
|
||||
sort_suggestions(&String::from_utf8_lossy(&prefix), suggestions, options)
|
||||
let options = custom_completion_options.unwrap_or(completion_options.clone());
|
||||
let mut matcher = NuMatcher::new(String::from_utf8_lossy(prefix), options);
|
||||
|
||||
if should_sort {
|
||||
for sugg in suggestions {
|
||||
matcher.add_semantic_suggestion(sugg);
|
||||
}
|
||||
matcher.results()
|
||||
} else {
|
||||
suggestions
|
||||
.into_iter()
|
||||
.filter(|sugg| matcher.matches(&sugg.suggestion.value))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn filter(
|
||||
prefix: &[u8],
|
||||
items: Vec<SemanticSuggestion>,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
items
|
||||
.into_iter()
|
||||
.filter(|it| match options.match_algorithm {
|
||||
MatchAlgorithm::Prefix => match (options.case_sensitive, options.positional) {
|
||||
(true, true) => it.suggestion.value.as_bytes().starts_with(prefix),
|
||||
(true, false) => it
|
||||
.suggestion
|
||||
.value
|
||||
.contains(std::str::from_utf8(prefix).unwrap_or("")),
|
||||
(false, positional) => {
|
||||
let value = it.suggestion.value.to_folded_case();
|
||||
let prefix = std::str::from_utf8(prefix).unwrap_or("").to_folded_case();
|
||||
if positional {
|
||||
value.starts_with(&prefix)
|
||||
} else {
|
||||
value.contains(&prefix)
|
||||
}
|
||||
}
|
||||
},
|
||||
MatchAlgorithm::Fuzzy => options
|
||||
.match_algorithm
|
||||
.matches_u8(it.suggestion.value.as_bytes(), prefix),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ use crate::completions::{
|
||||
completion_common::{adjust_if_intermediate, complete_item, AdjustView},
|
||||
Completer, CompletionOptions,
|
||||
};
|
||||
use nu_ansi_term::Style;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
Span,
|
||||
@ -10,7 +9,7 @@ use nu_protocol::{
|
||||
use reedline::Suggestion;
|
||||
use std::path::Path;
|
||||
|
||||
use super::SemanticSuggestion;
|
||||
use super::{completion_common::FileSuggestion, SemanticSuggestion};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct DirectoryCompletion {}
|
||||
@ -26,13 +25,13 @@ impl Completer for DirectoryCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
_pos: usize,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
let AdjustView { prefix, span, .. } = adjust_if_intermediate(&prefix, working_set, span);
|
||||
let AdjustView { prefix, span, .. } = adjust_if_intermediate(prefix, working_set, span);
|
||||
|
||||
// Filter only the folders
|
||||
#[allow(deprecated)]
|
||||
@ -47,11 +46,11 @@ impl Completer for DirectoryCompletion {
|
||||
.into_iter()
|
||||
.map(move |x| SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: x.1,
|
||||
style: x.2,
|
||||
value: x.path,
|
||||
style: x.style,
|
||||
span: reedline::Span {
|
||||
start: x.0.start - offset,
|
||||
end: x.0.end - offset,
|
||||
start: x.span.start - offset,
|
||||
end: x.span.end - offset,
|
||||
},
|
||||
..Suggestion::default()
|
||||
},
|
||||
@ -92,6 +91,6 @@ pub fn directory_completion(
|
||||
options: &CompletionOptions,
|
||||
engine_state: &EngineState,
|
||||
stack: &Stack,
|
||||
) -> Vec<(nu_protocol::Span, String, Option<Style>)> {
|
||||
complete_item(true, span, partial, cwd, options, engine_state, stack)
|
||||
) -> Vec<FileSuggestion> {
|
||||
complete_item(true, span, partial, &[cwd], options, engine_state, stack)
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use nu_protocol::{
|
||||
use reedline::Suggestion;
|
||||
use std::path::{is_separator, Path, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR};
|
||||
|
||||
use super::{completion_common::sort_suggestions, SemanticSuggestion};
|
||||
use super::SemanticSuggestion;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct DotNuCompletion {}
|
||||
@ -22,13 +22,13 @@ impl Completer for DotNuCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
_pos: usize,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
let prefix_str = String::from_utf8_lossy(&prefix).replace('`', "");
|
||||
let prefix_str = String::from_utf8_lossy(prefix).replace('`', "");
|
||||
let mut search_dirs: Vec<String> = vec![];
|
||||
|
||||
// If prefix_str is only a word we want to search in the current dir
|
||||
@ -87,49 +87,44 @@ impl Completer for DotNuCompletion {
|
||||
|
||||
// Fetch the files filtering the ones that ends with .nu
|
||||
// and transform them into suggestions
|
||||
let output: Vec<SemanticSuggestion> = search_dirs
|
||||
.into_iter()
|
||||
.flat_map(|search_dir| {
|
||||
let completions = file_path_completion(
|
||||
span,
|
||||
&partial,
|
||||
&search_dir,
|
||||
options,
|
||||
working_set.permanent_state,
|
||||
stack,
|
||||
);
|
||||
completions
|
||||
.into_iter()
|
||||
.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 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| SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: x.1,
|
||||
style: x.2,
|
||||
span: reedline::Span {
|
||||
start: x.0.start - offset,
|
||||
end: x.0.end - offset,
|
||||
},
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO????
|
||||
kind: None,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
sort_suggestions(&prefix_str, output, options)
|
||||
let completions = file_path_completion(
|
||||
span,
|
||||
&partial,
|
||||
&search_dirs.iter().map(|d| d.as_str()).collect::<Vec<_>>(),
|
||||
options,
|
||||
working_set.permanent_state,
|
||||
stack,
|
||||
);
|
||||
completions
|
||||
.into_iter()
|
||||
.filter(move |it| {
|
||||
// Different base dir, so we list the .nu files or folders
|
||||
if !is_current_folder {
|
||||
it.path.ends_with(".nu") || it.path.ends_with(SEP)
|
||||
} else {
|
||||
// Lib dirs, so we filter only the .nu files or directory modules
|
||||
if it.path.ends_with(SEP) {
|
||||
Path::new(&it.cwd).join(&it.path).join("mod.nu").exists()
|
||||
} else {
|
||||
it.path.ends_with(".nu")
|
||||
}
|
||||
}
|
||||
})
|
||||
.map(move |x| SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: x.path,
|
||||
style: x.style,
|
||||
span: reedline::Span {
|
||||
start: x.span.start - offset,
|
||||
end: x.span.end - offset,
|
||||
},
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO????
|
||||
kind: None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
@ -2,16 +2,14 @@ use crate::completions::{
|
||||
completion_common::{adjust_if_intermediate, complete_item, AdjustView},
|
||||
Completer, CompletionOptions,
|
||||
};
|
||||
use nu_ansi_term::Style;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
Span,
|
||||
};
|
||||
use nu_utils::IgnoreCaseExt;
|
||||
use reedline::Suggestion;
|
||||
use std::path::Path;
|
||||
|
||||
use super::SemanticSuggestion;
|
||||
use super::{completion_common::FileSuggestion, SemanticSuggestion};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct FileCompletion {}
|
||||
@ -27,7 +25,7 @@ impl Completer for FileCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
_pos: usize,
|
||||
@ -37,14 +35,14 @@ impl Completer for FileCompletion {
|
||||
prefix,
|
||||
span,
|
||||
readjusted,
|
||||
} = adjust_if_intermediate(&prefix, working_set, span);
|
||||
} = adjust_if_intermediate(prefix, working_set, span);
|
||||
|
||||
#[allow(deprecated)]
|
||||
let items: Vec<_> = complete_item(
|
||||
readjusted,
|
||||
span,
|
||||
&prefix,
|
||||
&working_set.permanent_state.current_work_dir(),
|
||||
&[&working_set.permanent_state.current_work_dir()],
|
||||
options,
|
||||
working_set.permanent_state,
|
||||
stack,
|
||||
@ -52,11 +50,11 @@ impl Completer for FileCompletion {
|
||||
.into_iter()
|
||||
.map(move |x| SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: x.1,
|
||||
style: x.2,
|
||||
value: x.path,
|
||||
style: x.style,
|
||||
span: reedline::Span {
|
||||
start: x.0.start - offset,
|
||||
end: x.0.end - offset,
|
||||
start: x.span.start - offset,
|
||||
end: x.span.end - offset,
|
||||
},
|
||||
..Suggestion::default()
|
||||
},
|
||||
@ -95,21 +93,10 @@ impl Completer for FileCompletion {
|
||||
pub fn file_path_completion(
|
||||
span: nu_protocol::Span,
|
||||
partial: &str,
|
||||
cwd: &str,
|
||||
cwds: &[impl AsRef<str>],
|
||||
options: &CompletionOptions,
|
||||
engine_state: &EngineState,
|
||||
stack: &Stack,
|
||||
) -> Vec<(nu_protocol::Span, String, Option<Style>)> {
|
||||
complete_item(false, span, partial, cwd, options, engine_state, stack)
|
||||
}
|
||||
|
||||
pub fn matches(partial: &str, from: &str, options: &CompletionOptions) -> bool {
|
||||
// Check for case sensitive
|
||||
if !options.case_sensitive {
|
||||
return options
|
||||
.match_algorithm
|
||||
.matches_str(&from.to_folded_case(), &partial.to_folded_case());
|
||||
}
|
||||
|
||||
options.match_algorithm.matches_str(from, partial)
|
||||
) -> Vec<FileSuggestion> {
|
||||
complete_item(false, span, partial, cwds, options, engine_state, stack)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::completions::{completion_common::sort_suggestions, Completer, CompletionOptions};
|
||||
use crate::completions::{completion_options::NuMatcher, Completer, CompletionOptions};
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression},
|
||||
engine::{Stack, StateWorkingSet},
|
||||
@ -24,7 +24,7 @@ impl Completer for FlagCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
_stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
_pos: usize,
|
||||
@ -35,7 +35,7 @@ impl Completer for FlagCompletion {
|
||||
let decl = working_set.get_decl(call.decl_id);
|
||||
let sig = decl.signature();
|
||||
|
||||
let mut output = vec![];
|
||||
let mut matcher = NuMatcher::new(String::from_utf8_lossy(prefix), options.clone());
|
||||
|
||||
for named in &sig.named {
|
||||
let flag_desc = &named.desc;
|
||||
@ -44,34 +44,7 @@ impl Completer for FlagCompletion {
|
||||
short.encode_utf8(&mut named);
|
||||
named.insert(0, b'-');
|
||||
|
||||
if options.match_algorithm.matches_u8(&named, &prefix) {
|
||||
output.push(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(&named).to_string(),
|
||||
description: Some(flag_desc.to_string()),
|
||||
span: reedline::Span {
|
||||
start: span.start - offset,
|
||||
end: span.end - offset,
|
||||
},
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO????
|
||||
kind: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if named.long.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut named = named.long.as_bytes().to_vec();
|
||||
named.insert(0, b'-');
|
||||
named.insert(0, b'-');
|
||||
|
||||
if options.match_algorithm.matches_u8(&named, &prefix) {
|
||||
output.push(SemanticSuggestion {
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(&named).to_string(),
|
||||
description: Some(flag_desc.to_string()),
|
||||
@ -86,9 +59,32 @@ impl Completer for FlagCompletion {
|
||||
kind: None,
|
||||
});
|
||||
}
|
||||
|
||||
if named.long.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut named = named.long.as_bytes().to_vec();
|
||||
named.insert(0, b'-');
|
||||
named.insert(0, b'-');
|
||||
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(&named).to_string(),
|
||||
description: Some(flag_desc.to_string()),
|
||||
span: reedline::Span {
|
||||
start: span.start - offset,
|
||||
end: span.end - offset,
|
||||
},
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO????
|
||||
kind: None,
|
||||
});
|
||||
}
|
||||
|
||||
return sort_suggestions(&String::from_utf8_lossy(&prefix), output, options);
|
||||
return matcher.results();
|
||||
}
|
||||
|
||||
vec![]
|
||||
|
@ -8,6 +8,7 @@ mod directory_completions;
|
||||
mod dotnu_completions;
|
||||
mod file_completions;
|
||||
mod flag_completions;
|
||||
mod operator_completions;
|
||||
mod variable_completions;
|
||||
|
||||
pub use base::{Completer, SemanticSuggestion, SuggestionKind};
|
||||
@ -17,6 +18,7 @@ pub use completion_options::{CompletionOptions, MatchAlgorithm};
|
||||
pub use custom_completions::CustomCompletion;
|
||||
pub use directory_completions::DirectoryCompletion;
|
||||
pub use dotnu_completions::DotNuCompletion;
|
||||
pub use file_completions::{file_path_completion, matches, FileCompletion};
|
||||
pub use file_completions::{file_path_completion, FileCompletion};
|
||||
pub use flag_completions::FlagCompletion;
|
||||
pub use operator_completions::OperatorCompletion;
|
||||
pub use variable_completions::VariableCompletion;
|
||||
|
166
crates/nu-cli/src/completions/operator_completions.rs
Normal file
166
crates/nu-cli/src/completions/operator_completions.rs
Normal file
@ -0,0 +1,166 @@
|
||||
use crate::completions::{
|
||||
completion_options::NuMatcher, Completer, CompletionOptions, SemanticSuggestion, SuggestionKind,
|
||||
};
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression},
|
||||
engine::{Stack, StateWorkingSet},
|
||||
Span, Type,
|
||||
};
|
||||
use reedline::Suggestion;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OperatorCompletion {
|
||||
previous_expr: Expression,
|
||||
}
|
||||
|
||||
impl OperatorCompletion {
|
||||
pub fn new(previous_expr: Expression) -> Self {
|
||||
OperatorCompletion { previous_expr }
|
||||
}
|
||||
}
|
||||
|
||||
impl Completer for OperatorCompletion {
|
||||
fn fetch(
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
_stack: &Stack,
|
||||
_prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
_pos: usize,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
//Check if int, float, or string
|
||||
let partial = std::str::from_utf8(working_set.get_span_contents(span)).unwrap_or("");
|
||||
let op = match &self.previous_expr.expr {
|
||||
Expr::BinaryOp(x, _, _) => &x.expr,
|
||||
_ => {
|
||||
return vec![];
|
||||
}
|
||||
};
|
||||
let possible_operations = match op {
|
||||
Expr::Int(_) => vec![
|
||||
("+", "Add (Plus)"),
|
||||
("-", "Subtract (Minus)"),
|
||||
("*", "Multiply"),
|
||||
("/", "Divide"),
|
||||
("==", "Equal to"),
|
||||
("!=", "Not equal to"),
|
||||
("//", "Floor division"),
|
||||
("<", "Less than"),
|
||||
(">", "Greater than"),
|
||||
("<=", "Less than or equal to"),
|
||||
(">=", "Greater than or equal to"),
|
||||
("mod", "Floor division remainder (Modulo)"),
|
||||
("**", "Power of"),
|
||||
("bit-or", "Bitwise OR"),
|
||||
("bit-xor", "Bitwise exclusive OR"),
|
||||
("bit-and", "Bitwise AND"),
|
||||
("bit-shl", "Bitwise shift left"),
|
||||
("bit-shr", "Bitwise shift right"),
|
||||
("in", "Is a member of (doesn't use regex)"),
|
||||
("not-in", "Is not a member of (doesn't use regex)"),
|
||||
],
|
||||
Expr::String(_) => vec![
|
||||
("=~", "Contains regex match"),
|
||||
("like", "Contains regex match"),
|
||||
("!~", "Does not contain regex match"),
|
||||
("not-like", "Does not contain regex match"),
|
||||
(
|
||||
"++",
|
||||
"Concatenates two lists, two strings, or two binary values",
|
||||
),
|
||||
("in", "Is a member of (doesn't use regex)"),
|
||||
("not-in", "Is not a member of (doesn't use regex)"),
|
||||
("starts-with", "Starts with"),
|
||||
("ends-with", "Ends with"),
|
||||
],
|
||||
Expr::Float(_) => vec![
|
||||
("+", "Add (Plus)"),
|
||||
("-", "Subtract (Minus)"),
|
||||
("*", "Multiply"),
|
||||
("/", "Divide"),
|
||||
("==", "Equal to"),
|
||||
("!=", "Not equal to"),
|
||||
("//", "Floor division"),
|
||||
("<", "Less than"),
|
||||
(">", "Greater than"),
|
||||
("<=", "Less than or equal to"),
|
||||
(">=", "Greater than or equal to"),
|
||||
("mod", "Floor division remainder (Modulo)"),
|
||||
("**", "Power of"),
|
||||
("in", "Is a member of (doesn't use regex)"),
|
||||
("not-in", "Is not a member of (doesn't use regex)"),
|
||||
],
|
||||
Expr::Bool(_) => vec![
|
||||
(
|
||||
"and",
|
||||
"Both values are true (short-circuits when first value is false)",
|
||||
),
|
||||
(
|
||||
"or",
|
||||
"Either value is true (short-circuits when first value is true)",
|
||||
),
|
||||
("xor", "One value is true and the other is false"),
|
||||
("not", "Negates a value or expression"),
|
||||
("in", "Is a member of (doesn't use regex)"),
|
||||
("not-in", "Is not a member of (doesn't use regex)"),
|
||||
],
|
||||
Expr::FullCellPath(path) => match path.head.expr {
|
||||
Expr::List(_) => vec![(
|
||||
"++",
|
||||
"Concatenates two lists, two strings, or two binary values",
|
||||
)],
|
||||
Expr::Var(id) => get_variable_completions(id, working_set),
|
||||
_ => vec![],
|
||||
},
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
let mut matcher = NuMatcher::new(partial, options.clone());
|
||||
for (symbol, desc) in possible_operations.into_iter() {
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: symbol.to_string(),
|
||||
description: Some(desc.to_string()),
|
||||
span: reedline::Span::new(span.start - offset, span.end - offset),
|
||||
append_whitespace: true,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: Some(SuggestionKind::Command(
|
||||
nu_protocol::engine::CommandType::Builtin,
|
||||
)),
|
||||
});
|
||||
}
|
||||
matcher.results()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_variable_completions<'a>(
|
||||
id: nu_protocol::Id<nu_protocol::marker::Var>,
|
||||
working_set: &StateWorkingSet,
|
||||
) -> Vec<(&'a str, &'a str)> {
|
||||
let var = working_set.get_variable(id);
|
||||
if !var.mutable {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
match var.ty {
|
||||
Type::List(_) | Type::String | Type::Binary => vec![
|
||||
(
|
||||
"++=",
|
||||
"Concatenates two lists, two strings, or two binary values",
|
||||
),
|
||||
("=", "Assigns a value to a variable."),
|
||||
],
|
||||
|
||||
Type::Int | Type::Float => vec![
|
||||
("=", "Assigns a value to a variable."),
|
||||
("+=", "Adds a value to a variable."),
|
||||
("-=", "Subtracts a value from a variable."),
|
||||
("*=", "Multiplies a variable by a value"),
|
||||
("/=", "Divides a variable by a value."),
|
||||
],
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
use crate::completions::{
|
||||
Completer, CompletionOptions, MatchAlgorithm, SemanticSuggestion, SuggestionKind,
|
||||
};
|
||||
use crate::completions::{Completer, CompletionOptions, SemanticSuggestion, SuggestionKind};
|
||||
use nu_engine::{column::get_columns, eval_variable};
|
||||
use nu_protocol::{
|
||||
engine::{Stack, StateWorkingSet},
|
||||
@ -9,7 +7,7 @@ use nu_protocol::{
|
||||
use reedline::Suggestion;
|
||||
use std::str;
|
||||
|
||||
use super::completion_common::sort_suggestions;
|
||||
use super::completion_options::NuMatcher;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VariableCompletion {
|
||||
@ -27,13 +25,12 @@ impl Completer for VariableCompletion {
|
||||
&mut self,
|
||||
working_set: &StateWorkingSet,
|
||||
stack: &Stack,
|
||||
prefix: Vec<u8>,
|
||||
prefix: &[u8],
|
||||
span: Span,
|
||||
offset: usize,
|
||||
_pos: usize,
|
||||
options: &CompletionOptions,
|
||||
) -> Vec<SemanticSuggestion> {
|
||||
let mut output = vec![];
|
||||
let builtins = ["$nu", "$in", "$env"];
|
||||
let var_str = std::str::from_utf8(&self.var_context.0).unwrap_or("");
|
||||
let var_id = working_set.find_variable(&self.var_context.0);
|
||||
@ -42,7 +39,8 @@ impl Completer for VariableCompletion {
|
||||
end: span.end - offset,
|
||||
};
|
||||
let sublevels_count = self.var_context.1.len();
|
||||
let prefix_str = String::from_utf8_lossy(&prefix);
|
||||
let prefix_str = String::from_utf8_lossy(prefix);
|
||||
let mut matcher = NuMatcher::new(prefix_str, options.clone());
|
||||
|
||||
// Completions for the given variable
|
||||
if !var_str.is_empty() {
|
||||
@ -63,37 +61,25 @@ impl Completer for VariableCompletion {
|
||||
|
||||
if let Some(val) = env_vars.get(&target_var_str) {
|
||||
for suggestion in nested_suggestions(val, &nested_levels, current_span) {
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
suggestion.suggestion.value.as_bytes(),
|
||||
&prefix,
|
||||
) {
|
||||
output.push(suggestion);
|
||||
}
|
||||
matcher.add_semantic_suggestion(suggestion);
|
||||
}
|
||||
|
||||
return sort_suggestions(&prefix_str, output, options);
|
||||
return matcher.results();
|
||||
}
|
||||
} else {
|
||||
// No nesting provided, return all env vars
|
||||
for env_var in env_vars {
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
env_var.0.as_bytes(),
|
||||
&prefix,
|
||||
) {
|
||||
output.push(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: env_var.0,
|
||||
span: current_span,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: Some(SuggestionKind::Type(env_var.1.get_type())),
|
||||
});
|
||||
}
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: env_var.0,
|
||||
span: current_span,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: Some(SuggestionKind::Type(env_var.1.get_type())),
|
||||
});
|
||||
}
|
||||
|
||||
return sort_suggestions(&prefix_str, output, options);
|
||||
return matcher.results();
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,16 +94,10 @@ impl Completer for VariableCompletion {
|
||||
) {
|
||||
for suggestion in nested_suggestions(&nuval, &self.var_context.1, current_span)
|
||||
{
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
suggestion.suggestion.value.as_bytes(),
|
||||
&prefix,
|
||||
) {
|
||||
output.push(suggestion);
|
||||
}
|
||||
matcher.add_semantic_suggestion(suggestion);
|
||||
}
|
||||
|
||||
return sort_suggestions(&prefix_str, output, options);
|
||||
return matcher.results();
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,37 +110,25 @@ impl Completer for VariableCompletion {
|
||||
if let Ok(value) = var {
|
||||
for suggestion in nested_suggestions(&value, &self.var_context.1, current_span)
|
||||
{
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
suggestion.suggestion.value.as_bytes(),
|
||||
&prefix,
|
||||
) {
|
||||
output.push(suggestion);
|
||||
}
|
||||
matcher.add_semantic_suggestion(suggestion);
|
||||
}
|
||||
|
||||
return sort_suggestions(&prefix_str, output, options);
|
||||
return matcher.results();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Variable completion (e.g: $en<tab> to complete $env)
|
||||
for builtin in builtins {
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
builtin.as_bytes(),
|
||||
&prefix,
|
||||
) {
|
||||
output.push(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: builtin.to_string(),
|
||||
span: current_span,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO is there a way to get the VarId to get the type???
|
||||
kind: None,
|
||||
});
|
||||
}
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: builtin.to_string(),
|
||||
span: current_span,
|
||||
..Suggestion::default()
|
||||
},
|
||||
// TODO is there a way to get the VarId to get the type???
|
||||
kind: None,
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: The following can be refactored (see find_commands_by_predicate() used in
|
||||
@ -170,40 +138,7 @@ impl Completer for VariableCompletion {
|
||||
for scope_frame in working_set.delta.scope.iter().rev() {
|
||||
for overlay_frame in scope_frame.active_overlays(&mut removed_overlays).rev() {
|
||||
for v in &overlay_frame.vars {
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
v.0,
|
||||
&prefix,
|
||||
) {
|
||||
output.push(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(v.0).to_string(),
|
||||
span: current_span,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: Some(SuggestionKind::Type(
|
||||
working_set.get_variable(*v.1).ty.clone(),
|
||||
)),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Permanent state vars
|
||||
// for scope in &self.engine_state.scope {
|
||||
for overlay_frame in working_set
|
||||
.permanent_state
|
||||
.active_overlays(&removed_overlays)
|
||||
.rev()
|
||||
{
|
||||
for v in &overlay_frame.vars {
|
||||
if options.match_algorithm.matches_u8_insensitive(
|
||||
options.case_sensitive,
|
||||
v.0,
|
||||
&prefix,
|
||||
) {
|
||||
output.push(SemanticSuggestion {
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(v.0).to_string(),
|
||||
span: current_span,
|
||||
@ -217,11 +152,28 @@ impl Completer for VariableCompletion {
|
||||
}
|
||||
}
|
||||
|
||||
output = sort_suggestions(&prefix_str, output, options);
|
||||
// Permanent state vars
|
||||
// for scope in &self.engine_state.scope {
|
||||
for overlay_frame in working_set
|
||||
.permanent_state
|
||||
.active_overlays(&removed_overlays)
|
||||
.rev()
|
||||
{
|
||||
for v in &overlay_frame.vars {
|
||||
matcher.add_semantic_suggestion(SemanticSuggestion {
|
||||
suggestion: Suggestion {
|
||||
value: String::from_utf8_lossy(v.0).to_string(),
|
||||
span: current_span,
|
||||
..Suggestion::default()
|
||||
},
|
||||
kind: Some(SuggestionKind::Type(
|
||||
working_set.get_variable(*v.1).ty.clone(),
|
||||
)),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
output.dedup(); // TODO: Removes only consecutive duplicates, is it intended?
|
||||
|
||||
output
|
||||
matcher.results()
|
||||
}
|
||||
}
|
||||
|
||||
@ -302,13 +254,3 @@ fn recursive_value(val: &Value, sublevels: &[Vec<u8>]) -> Result<Value, Span> {
|
||||
Ok(val.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl MatchAlgorithm {
|
||||
pub fn matches_u8_insensitive(&self, sensitive: bool, haystack: &[u8], needle: &[u8]) -> bool {
|
||||
if sensitive {
|
||||
self.matches_u8(haystack, needle)
|
||||
} else {
|
||||
self.matches_u8(&haystack.to_ascii_lowercase(), &needle.to_ascii_lowercase())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use nu_path::canonicalize_with;
|
||||
use nu_protocol::{engine::StateWorkingSet, ParseError, PluginRegistryFile, Spanned};
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack},
|
||||
report_shell_error, HistoryFileFormat, PipelineData,
|
||||
report_shell_error, PipelineData,
|
||||
};
|
||||
#[cfg(feature = "plugin")]
|
||||
use nu_utils::perf;
|
||||
@ -16,15 +16,8 @@ const PLUGIN_FILE: &str = "plugin.msgpackz";
|
||||
#[cfg(feature = "plugin")]
|
||||
const OLD_PLUGIN_FILE: &str = "plugin.nu";
|
||||
|
||||
const HISTORY_FILE_TXT: &str = "history.txt";
|
||||
const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn read_plugin_file(
|
||||
engine_state: &mut EngineState,
|
||||
plugin_file: Option<Spanned<String>>,
|
||||
storage_path: &str,
|
||||
) {
|
||||
pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
|
||||
use nu_protocol::ShellError;
|
||||
use std::path::Path;
|
||||
|
||||
@ -52,7 +45,7 @@ pub fn read_plugin_file(
|
||||
let mut start_time = std::time::Instant::now();
|
||||
// Reading signatures from plugin registry file
|
||||
// The plugin.msgpackz file stores the parsed signature collected from each registered plugin
|
||||
add_plugin_file(engine_state, plugin_file.clone(), storage_path);
|
||||
add_plugin_file(engine_state, plugin_file.clone());
|
||||
perf!(
|
||||
"add plugin file to engine_state",
|
||||
start_time,
|
||||
@ -70,8 +63,7 @@ pub fn read_plugin_file(
|
||||
log::warn!("Plugin file not found: {}", plugin_path.display());
|
||||
|
||||
// Try migration of an old plugin file if this wasn't a custom plugin file
|
||||
if plugin_file.is_none() && migrate_old_plugin_file(engine_state, storage_path)
|
||||
{
|
||||
if plugin_file.is_none() && migrate_old_plugin_file(engine_state) {
|
||||
let Ok(file) = std::fs::File::open(&plugin_path) else {
|
||||
log::warn!("Failed to load newly migrated plugin file");
|
||||
return;
|
||||
@ -159,11 +151,7 @@ pub fn read_plugin_file(
|
||||
}
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn add_plugin_file(
|
||||
engine_state: &mut EngineState,
|
||||
plugin_file: Option<Spanned<String>>,
|
||||
storage_path: &str,
|
||||
) {
|
||||
pub fn add_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
|
||||
use std::path::Path;
|
||||
|
||||
use nu_protocol::report_parse_error;
|
||||
@ -189,9 +177,8 @@ pub fn add_plugin_file(
|
||||
),
|
||||
);
|
||||
}
|
||||
} else if let Some(mut plugin_path) = nu_path::config_dir() {
|
||||
} else if let Some(plugin_path) = nu_path::nu_config_dir() {
|
||||
// Path to store plugins signatures
|
||||
plugin_path.push(storage_path);
|
||||
let mut plugin_path =
|
||||
canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path.into());
|
||||
plugin_path.push(PLUGIN_FILE);
|
||||
@ -228,33 +215,15 @@ pub fn eval_config_contents(
|
||||
engine_state.file = prev_file;
|
||||
|
||||
// Merge the environment in case env vars changed in the config
|
||||
match engine_state.cwd(Some(stack)) {
|
||||
Ok(cwd) => {
|
||||
if let Err(e) = engine_state.merge_env(stack, cwd) {
|
||||
report_shell_error(engine_state, &e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
report_shell_error(engine_state, &e);
|
||||
}
|
||||
if let Err(e) = engine_state.merge_env(stack) {
|
||||
report_shell_error(engine_state, &e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> Option<PathBuf> {
|
||||
nu_path::config_dir().map(|mut history_path| {
|
||||
history_path.push(storage_path);
|
||||
history_path.push(match mode {
|
||||
HistoryFileFormat::Plaintext => HISTORY_FILE_TXT,
|
||||
HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE,
|
||||
});
|
||||
history_path.into()
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "plugin")]
|
||||
pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool {
|
||||
pub fn migrate_old_plugin_file(engine_state: &EngineState) -> bool {
|
||||
use nu_protocol::{
|
||||
PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature,
|
||||
ShellError,
|
||||
@ -267,10 +236,9 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -
|
||||
return false;
|
||||
};
|
||||
|
||||
let Some(config_dir) = nu_path::config_dir().and_then(|mut dir| {
|
||||
dir.push(storage_path);
|
||||
nu_path::canonicalize_with(dir, &cwd).ok()
|
||||
}) else {
|
||||
let Some(config_dir) =
|
||||
nu_path::nu_config_dir().and_then(|dir| nu_path::canonicalize_with(dir, &cwd).ok())
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
|
||||
|
@ -9,6 +9,8 @@ use nu_protocol::{
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::util::print_pipeline;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EvaluateCommandsOpts {
|
||||
pub table_mode: Option<Value>,
|
||||
@ -72,7 +74,7 @@ pub fn evaluate_commands(
|
||||
|
||||
if let Some(err) = working_set.compile_errors.first() {
|
||||
report_compile_error(&working_set, err);
|
||||
// Not a fatal error, for now
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
(output, working_set.render())
|
||||
@ -93,7 +95,7 @@ pub fn evaluate_commands(
|
||||
t_mode.coerce_str()?.parse().unwrap_or_default();
|
||||
}
|
||||
|
||||
pipeline.print(engine_state, stack, no_newline, false)?;
|
||||
print_pipeline(engine_state, stack, pipeline, no_newline)?;
|
||||
|
||||
info!("evaluate {}:{}:{}", file!(), line!(), column!());
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::util::eval_source;
|
||||
use crate::util::{eval_source, print_pipeline};
|
||||
use log::{info, trace};
|
||||
use nu_engine::{convert_env_values, eval_block};
|
||||
use nu_parser::parse;
|
||||
@ -89,7 +89,7 @@ pub fn evaluate_file(
|
||||
|
||||
if let Some(err) = working_set.compile_errors.first() {
|
||||
report_compile_error(&working_set, err);
|
||||
// Not a fatal error, for now
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
// Look for blocks whose name starts with "main" and replace it with the filename.
|
||||
@ -119,7 +119,7 @@ pub fn evaluate_file(
|
||||
};
|
||||
|
||||
// Print the pipeline output of the last command of the file.
|
||||
pipeline.print(engine_state, stack, true, false)?;
|
||||
print_pipeline(engine_state, stack, pipeline, true)?;
|
||||
|
||||
// Invoke the main command with arguments.
|
||||
// Arguments with whitespaces are quoted, thus can be safely concatenated by whitespace.
|
||||
|
@ -21,7 +21,6 @@ pub use config_files::eval_config_contents;
|
||||
pub use eval_cmds::{evaluate_commands, EvaluateCommandsOpts};
|
||||
pub use eval_file::evaluate_file;
|
||||
pub use menus::NuHelpCompleter;
|
||||
pub use nu_cmd_base::util::get_init_cwd;
|
||||
pub use nu_highlight::NuHighlight;
|
||||
pub use print::Print;
|
||||
pub use prompt::NushellPrompt;
|
||||
|
@ -2,7 +2,7 @@ use nu_engine::eval_block;
|
||||
use nu_protocol::{
|
||||
debugger::WithoutDebug,
|
||||
engine::{EngineState, Stack},
|
||||
IntoPipelineData, Span, Value,
|
||||
BlockId, IntoPipelineData, Span, Value,
|
||||
};
|
||||
use reedline::{menu_functions::parse_selection_char, Completer, Suggestion};
|
||||
use std::sync::Arc;
|
||||
@ -10,7 +10,7 @@ use std::sync::Arc;
|
||||
const SELECTION_CHAR: char = '!';
|
||||
|
||||
pub struct NuMenuCompleter {
|
||||
block_id: usize,
|
||||
block_id: BlockId,
|
||||
span: Span,
|
||||
stack: Stack,
|
||||
engine_state: Arc<EngineState>,
|
||||
@ -19,7 +19,7 @@ pub struct NuMenuCompleter {
|
||||
|
||||
impl NuMenuCompleter {
|
||||
pub fn new(
|
||||
block_id: usize,
|
||||
block_id: BlockId,
|
||||
span: Span,
|
||||
stack: Stack,
|
||||
engine_state: Arc<EngineState>,
|
||||
@ -28,7 +28,7 @@ impl NuMenuCompleter {
|
||||
Self {
|
||||
block_id,
|
||||
span,
|
||||
stack: stack.reset_out_dest().capture(),
|
||||
stack: stack.reset_out_dest().collect_value(),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::ByteStreamSource;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Print;
|
||||
@ -50,7 +51,7 @@ Since this command has no output, there is no point in piping it with other comm
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
mut input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
||||
let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
|
||||
@ -64,15 +65,24 @@ Since this command has no output, there is no point in piping it with other comm
|
||||
arg.into_pipeline_data()
|
||||
.print_raw(engine_state, no_newline, to_stderr)?;
|
||||
} else {
|
||||
arg.into_pipeline_data()
|
||||
.print(engine_state, stack, no_newline, to_stderr)?;
|
||||
arg.into_pipeline_data().print_table(
|
||||
engine_state,
|
||||
stack,
|
||||
no_newline,
|
||||
to_stderr,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
} else if !input.is_nothing() {
|
||||
if let PipelineData::ByteStream(stream, _) = &mut input {
|
||||
if let ByteStreamSource::Child(child) = stream.source_mut() {
|
||||
child.ignore_error(true);
|
||||
}
|
||||
}
|
||||
if raw {
|
||||
input.print_raw(engine_state, no_newline, to_stderr)?;
|
||||
} else {
|
||||
input.print(engine_state, stack, no_newline, to_stderr)?;
|
||||
input.print_table(engine_state, stack, no_newline, to_stderr)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,7 @@
|
||||
use crate::prompt_update::{
|
||||
POST_PROMPT_MARKER, PRE_PROMPT_MARKER, VSCODE_POST_PROMPT_MARKER, VSCODE_PRE_PROMPT_MARKER,
|
||||
};
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack},
|
||||
Value,
|
||||
};
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
#[cfg(windows)]
|
||||
use nu_utils::enable_vt_processing;
|
||||
use reedline::{
|
||||
@ -124,8 +121,11 @@ impl Prompt for NushellPrompt {
|
||||
.replace('\n', "\r\n");
|
||||
|
||||
if self.shell_integration_osc633 {
|
||||
if self.stack.get_env_var(&self.engine_state, "TERM_PROGRAM")
|
||||
== Some(Value::test_string("vscode"))
|
||||
if self
|
||||
.stack
|
||||
.get_env_var(&self.engine_state, "TERM_PROGRAM")
|
||||
.and_then(|v| v.as_str().ok())
|
||||
== Some("vscode")
|
||||
{
|
||||
// We're in vscode and we have osc633 enabled
|
||||
format!("{VSCODE_PRE_PROMPT_MARKER}{prompt}{VSCODE_POST_PROMPT_MARKER}").into()
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::NushellPrompt;
|
||||
use log::trace;
|
||||
use log::{trace, warn};
|
||||
use nu_engine::ClosureEvalOnce;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack},
|
||||
@ -30,30 +30,21 @@ pub(crate) const TRANSIENT_PROMPT_MULTILINE_INDICATOR: &str =
|
||||
pub(crate) const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
|
||||
pub(crate) const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
|
||||
pub(crate) const PRE_EXECUTION_MARKER: &str = "\x1b]133;C\x1b\\";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const POST_EXECUTION_MARKER_PREFIX: &str = "\x1b]133;D;";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const POST_EXECUTION_MARKER_SUFFIX: &str = "\x1b\\";
|
||||
|
||||
// OSC633 is the same as OSC133 but specifically for VSCode
|
||||
pub(crate) const VSCODE_PRE_PROMPT_MARKER: &str = "\x1b]633;A\x1b\\";
|
||||
pub(crate) const VSCODE_POST_PROMPT_MARKER: &str = "\x1b]633;B\x1b\\";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const VSCODE_PRE_EXECUTION_MARKER: &str = "\x1b]633;C\x1b\\";
|
||||
#[allow(dead_code)]
|
||||
//"\x1b]633;D;{}\x1b\\"
|
||||
pub(crate) const VSCODE_POST_EXECUTION_MARKER_PREFIX: &str = "\x1b]633;D;";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const VSCODE_POST_EXECUTION_MARKER_SUFFIX: &str = "\x1b\\";
|
||||
#[allow(dead_code)]
|
||||
//"\x1b]633;E;{}\x1b\\"
|
||||
pub(crate) const VSCODE_COMMANDLINE_MARKER_PREFIX: &str = "\x1b]633;E;";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const VSCODE_COMMANDLINE_MARKER_SUFFIX: &str = "\x1b\\";
|
||||
#[allow(dead_code)]
|
||||
// "\x1b]633;P;Cwd={}\x1b\\"
|
||||
pub(crate) const VSCODE_CWD_PROPERTY_MARKER_PREFIX: &str = "\x1b]633;P;Cwd=";
|
||||
#[allow(dead_code)]
|
||||
pub(crate) const VSCODE_CWD_PROPERTY_MARKER_SUFFIX: &str = "\x1b\\";
|
||||
|
||||
pub(crate) const RESET_APPLICATION_MODE: &str = "\x1b[?1l";
|
||||
@ -68,7 +59,7 @@ fn get_prompt_string(
|
||||
.get_env_var(engine_state, prompt)
|
||||
.and_then(|v| match v {
|
||||
Value::Closure { val, .. } => {
|
||||
let result = ClosureEvalOnce::new(engine_state, stack, *val)
|
||||
let result = ClosureEvalOnce::new(engine_state, stack, val.as_ref().clone())
|
||||
.run_with_input(PipelineData::Empty);
|
||||
|
||||
trace!(
|
||||
@ -89,8 +80,13 @@ fn get_prompt_string(
|
||||
})
|
||||
.and_then(|pipeline_data| {
|
||||
let output = pipeline_data.collect_string("", config).ok();
|
||||
let ansi_output = output.map(|mut x| {
|
||||
// Always reset the color at the start of the right prompt
|
||||
// to ensure there is no ansi bleed over
|
||||
if x.is_empty() && prompt == PROMPT_COMMAND_RIGHT {
|
||||
x.insert_str(0, "\x1b[0m")
|
||||
};
|
||||
|
||||
output.map(|mut x| {
|
||||
// Just remove the very last newline.
|
||||
if x.ends_with('\n') {
|
||||
x.pop();
|
||||
@ -100,7 +96,11 @@ fn get_prompt_string(
|
||||
x.pop();
|
||||
}
|
||||
x
|
||||
})
|
||||
});
|
||||
// Let's keep this for debugging purposes with nu --log-level warn
|
||||
warn!("{}:{}:{} {:?}", file!(), line!(), column!(), ansi_output);
|
||||
|
||||
ansi_output
|
||||
})
|
||||
}
|
||||
|
||||
@ -119,7 +119,11 @@ pub(crate) fn update_prompt(
|
||||
// Now that we have the prompt string lets ansify it.
|
||||
// <133 A><prompt><133 B><command><133 C><command output>
|
||||
let left_prompt_string = if config.shell_integration.osc633 {
|
||||
if stack.get_env_var(engine_state, "TERM_PROGRAM") == Some(Value::test_string("vscode")) {
|
||||
if stack
|
||||
.get_env_var(engine_state, "TERM_PROGRAM")
|
||||
.and_then(|v| v.as_str().ok())
|
||||
== Some("vscode")
|
||||
{
|
||||
// We're in vscode and we have osc633 enabled
|
||||
Some(format!(
|
||||
"{VSCODE_PRE_PROMPT_MARKER}{configured_left_prompt_string}{VSCODE_POST_PROMPT_MARKER}"
|
||||
|
@ -5,11 +5,10 @@ use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style};
|
||||
use nu_engine::eval_block;
|
||||
use nu_parser::parse;
|
||||
use nu_protocol::{
|
||||
create_menus,
|
||||
debugger::WithoutDebug,
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
extract_value, Config, EditBindings, ParsedKeybinding, ParsedMenu, PipelineData, Record,
|
||||
ShellError, Span, Value,
|
||||
extract_value, Config, EditBindings, FromValue, ParsedKeybinding, ParsedMenu, PipelineData,
|
||||
Record, ShellError, Span, Type, Value,
|
||||
};
|
||||
use reedline::{
|
||||
default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings,
|
||||
@ -36,6 +35,41 @@ const DEFAULT_COMPLETION_MENU: &str = r#"
|
||||
}
|
||||
}"#;
|
||||
|
||||
const DEFAULT_IDE_COMPLETION_MENU: &str = r#"
|
||||
{
|
||||
name: ide_completion_menu
|
||||
only_buffer_difference: false
|
||||
marker: "| "
|
||||
type: {
|
||||
layout: ide
|
||||
min_completion_width: 0,
|
||||
max_completion_width: 50,
|
||||
max_completion_height: 10, # will be limited by the available lines in the terminal
|
||||
padding: 0,
|
||||
border: true,
|
||||
cursor_offset: 0,
|
||||
description_mode: "prefer_right"
|
||||
min_description_width: 0
|
||||
max_description_width: 50
|
||||
max_description_height: 10
|
||||
description_offset: 1
|
||||
# If true, the cursor pos will be corrected, so the suggestions match up with the typed text
|
||||
#
|
||||
# C:\> str
|
||||
# str join
|
||||
# str trim
|
||||
# str split
|
||||
correct_cursor_pos: false
|
||||
}
|
||||
style: {
|
||||
text: green
|
||||
selected_text: { attr: r }
|
||||
description_text: yellow
|
||||
match_text: { attr: u }
|
||||
selected_match_text: { attr: ur }
|
||||
}
|
||||
}"#;
|
||||
|
||||
const DEFAULT_HISTORY_MENU: &str = r#"
|
||||
{
|
||||
name: history_menu
|
||||
@ -95,6 +129,7 @@ pub(crate) fn add_menus(
|
||||
// Checking if the default menus have been added from the config file
|
||||
let default_menus = [
|
||||
("completion_menu", DEFAULT_COMPLETION_MENU),
|
||||
("ide_completion_menu", DEFAULT_IDE_COMPLETION_MENU),
|
||||
("history_menu", DEFAULT_HISTORY_MENU),
|
||||
("help_menu", DEFAULT_HELP_MENU),
|
||||
];
|
||||
@ -122,7 +157,7 @@ pub(crate) fn add_menus(
|
||||
|
||||
engine_state.merge_delta(delta)?;
|
||||
|
||||
let mut temp_stack = Stack::new().capture();
|
||||
let mut temp_stack = Stack::new().collect_value();
|
||||
let input = PipelineData::Empty;
|
||||
menu_eval_results.push(eval_block::<WithoutDebug>(
|
||||
&engine_state,
|
||||
@ -137,15 +172,13 @@ pub(crate) fn add_menus(
|
||||
|
||||
for res in menu_eval_results.into_iter() {
|
||||
if let PipelineData::Value(value, None) = res {
|
||||
for menu in create_menus(&value)? {
|
||||
line_editor = add_menu(
|
||||
line_editor,
|
||||
&menu,
|
||||
new_engine_state_ref.clone(),
|
||||
stack,
|
||||
config.clone(),
|
||||
)?;
|
||||
}
|
||||
line_editor = add_menu(
|
||||
line_editor,
|
||||
&ParsedMenu::from_value(value)?,
|
||||
new_engine_state_ref.clone(),
|
||||
stack,
|
||||
config.clone(),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -168,22 +201,22 @@ fn add_menu(
|
||||
"list" => add_list_menu(line_editor, menu, engine_state, stack, config),
|
||||
"ide" => add_ide_menu(line_editor, menu, engine_state, stack, config),
|
||||
"description" => add_description_menu(line_editor, menu, engine_state, stack, config),
|
||||
_ => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "columnar, list, ide or description".to_string(),
|
||||
value: menu.r#type.to_abbreviated_string(&config),
|
||||
span: menu.r#type.span(),
|
||||
str => Err(ShellError::InvalidValue {
|
||||
valid: "'columnar', 'list', 'ide', or 'description'".into(),
|
||||
actual: format!("'{str}'"),
|
||||
span,
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "only record type".to_string(),
|
||||
value: menu.r#type.to_abbreviated_string(&config),
|
||||
span: menu.r#type.span(),
|
||||
Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::record(),
|
||||
actual: menu.r#type.get_type(),
|
||||
span,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn get_style(record: &Record, name: &str, span: Span) -> Option<Style> {
|
||||
fn get_style(record: &Record, name: &'static str, span: Span) -> Option<Style> {
|
||||
extract_value(name, record, span)
|
||||
.ok()
|
||||
.map(|text| match text {
|
||||
@ -262,30 +295,23 @@ pub(crate) fn add_columnar_menu(
|
||||
let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
|
||||
columnar_menu = columnar_menu.with_only_buffer_difference(only_buffer_difference);
|
||||
|
||||
let span = menu.source.span();
|
||||
match &menu.source {
|
||||
Value::Nothing { .. } => {
|
||||
Ok(line_editor.with_menu(ReedlineMenu::EngineCompleter(Box::new(columnar_menu))))
|
||||
}
|
||||
Value::Closure { val, .. } => {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
val.block_id,
|
||||
span,
|
||||
stack.captures_to_stack(val.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
Ok(line_editor.with_menu(ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(columnar_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}))
|
||||
}
|
||||
_ => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "block or omitted value".to_string(),
|
||||
value: menu.source.to_abbreviated_string(config),
|
||||
let completer = if let Some(closure) = &menu.source {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
closure.block_id,
|
||||
span,
|
||||
}),
|
||||
}
|
||||
stack.captures_to_stack(closure.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(columnar_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}
|
||||
} else {
|
||||
ReedlineMenu::EngineCompleter(Box::new(columnar_menu))
|
||||
};
|
||||
|
||||
Ok(line_editor.with_menu(completer))
|
||||
}
|
||||
|
||||
// Adds a search menu to the line editor
|
||||
@ -318,30 +344,23 @@ pub(crate) fn add_list_menu(
|
||||
let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
|
||||
list_menu = list_menu.with_only_buffer_difference(only_buffer_difference);
|
||||
|
||||
let span = menu.source.span();
|
||||
match &menu.source {
|
||||
Value::Nothing { .. } => {
|
||||
Ok(line_editor.with_menu(ReedlineMenu::HistoryMenu(Box::new(list_menu))))
|
||||
let completer = if let Some(closure) = &menu.source {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
closure.block_id,
|
||||
span,
|
||||
stack.captures_to_stack(closure.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(list_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}
|
||||
Value::Closure { val, .. } => {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
val.block_id,
|
||||
span,
|
||||
stack.captures_to_stack(val.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
Ok(line_editor.with_menu(ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(list_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}))
|
||||
}
|
||||
_ => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "block or omitted value".to_string(),
|
||||
value: menu.source.to_abbreviated_string(&config),
|
||||
span: menu.source.span(),
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
ReedlineMenu::HistoryMenu(Box::new(list_menu))
|
||||
};
|
||||
|
||||
Ok(line_editor.with_menu(completer))
|
||||
}
|
||||
|
||||
// Adds an IDE menu to the line editor
|
||||
@ -416,9 +435,9 @@ pub(crate) fn add_ide_menu(
|
||||
vertical,
|
||||
)
|
||||
} else {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "bool or record".to_string(),
|
||||
value: border.to_abbreviated_string(&config),
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("bool or record"),
|
||||
actual: border.get_type(),
|
||||
span: border.span(),
|
||||
});
|
||||
}
|
||||
@ -439,10 +458,10 @@ pub(crate) fn add_ide_menu(
|
||||
"left" => ide_menu.with_description_mode(DescriptionMode::Left),
|
||||
"right" => ide_menu.with_description_mode(DescriptionMode::Right),
|
||||
"prefer_right" => ide_menu.with_description_mode(DescriptionMode::PreferRight),
|
||||
_ => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "\"left\", \"right\" or \"prefer_right\"".to_string(),
|
||||
value: description_mode.to_abbreviated_string(&config),
|
||||
str => {
|
||||
return Err(ShellError::InvalidValue {
|
||||
valid: "'left', 'right', or 'prefer_right'".into(),
|
||||
actual: format!("'{str}'"),
|
||||
span: description_mode.span(),
|
||||
});
|
||||
}
|
||||
@ -499,30 +518,23 @@ pub(crate) fn add_ide_menu(
|
||||
let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
|
||||
ide_menu = ide_menu.with_only_buffer_difference(only_buffer_difference);
|
||||
|
||||
let span = menu.source.span();
|
||||
match &menu.source {
|
||||
Value::Nothing { .. } => {
|
||||
Ok(line_editor.with_menu(ReedlineMenu::EngineCompleter(Box::new(ide_menu))))
|
||||
}
|
||||
Value::Closure { val, .. } => {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
val.block_id,
|
||||
span,
|
||||
stack.captures_to_stack(val.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
Ok(line_editor.with_menu(ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(ide_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}))
|
||||
}
|
||||
_ => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "block or omitted value".to_string(),
|
||||
value: menu.source.to_abbreviated_string(&config),
|
||||
let completer = if let Some(closure) = &menu.source {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
closure.block_id,
|
||||
span,
|
||||
}),
|
||||
}
|
||||
stack.captures_to_stack(closure.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(ide_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}
|
||||
} else {
|
||||
ReedlineMenu::EngineCompleter(Box::new(ide_menu))
|
||||
};
|
||||
|
||||
Ok(line_editor.with_menu(completer))
|
||||
}
|
||||
|
||||
// Adds a description menu to the line editor
|
||||
@ -587,34 +599,27 @@ pub(crate) fn add_description_menu(
|
||||
let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
|
||||
description_menu = description_menu.with_only_buffer_difference(only_buffer_difference);
|
||||
|
||||
let span = menu.source.span();
|
||||
match &menu.source {
|
||||
Value::Nothing { .. } => {
|
||||
let completer = Box::new(NuHelpCompleter::new(engine_state, config));
|
||||
Ok(line_editor.with_menu(ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(description_menu),
|
||||
completer,
|
||||
}))
|
||||
let completer = if let Some(closure) = &menu.source {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
closure.block_id,
|
||||
span,
|
||||
stack.captures_to_stack(closure.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(description_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}
|
||||
Value::Closure { val, .. } => {
|
||||
let menu_completer = NuMenuCompleter::new(
|
||||
val.block_id,
|
||||
span,
|
||||
stack.captures_to_stack(val.captures.clone()),
|
||||
engine_state,
|
||||
only_buffer_difference,
|
||||
);
|
||||
Ok(line_editor.with_menu(ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(description_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}))
|
||||
} else {
|
||||
let menu_completer = NuHelpCompleter::new(engine_state, config);
|
||||
ReedlineMenu::WithCompleter {
|
||||
menu: Box::new(description_menu),
|
||||
completer: Box::new(menu_completer),
|
||||
}
|
||||
_ => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "closure or omitted value".to_string(),
|
||||
value: menu.source.to_abbreviated_string(&config),
|
||||
span: menu.source.span(),
|
||||
}),
|
||||
}
|
||||
};
|
||||
|
||||
Ok(line_editor.with_menu(completer))
|
||||
}
|
||||
|
||||
fn add_menu_keybindings(keybindings: &mut Keybindings) {
|
||||
@ -629,6 +634,16 @@ fn add_menu_keybindings(keybindings: &mut Keybindings) {
|
||||
]),
|
||||
);
|
||||
|
||||
keybindings.add_binding(
|
||||
KeyModifiers::CONTROL,
|
||||
KeyCode::Char(' '),
|
||||
ReedlineEvent::UntilFound(vec![
|
||||
ReedlineEvent::Menu("ide_completion_menu".to_string()),
|
||||
ReedlineEvent::MenuNext,
|
||||
ReedlineEvent::Edit(vec![EditCommand::Complete]),
|
||||
]),
|
||||
);
|
||||
|
||||
keybindings.add_binding(
|
||||
KeyModifiers::SHIFT,
|
||||
KeyCode::BackTab,
|
||||
@ -728,9 +743,9 @@ fn add_keybinding(
|
||||
"emacs" => add_parsed_keybinding(emacs_keybindings, keybinding, config),
|
||||
"vi_insert" => add_parsed_keybinding(insert_keybindings, keybinding, config),
|
||||
"vi_normal" => add_parsed_keybinding(normal_keybindings, keybinding, config),
|
||||
m => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "emacs, vi_insert or vi_normal".to_string(),
|
||||
value: m.to_string(),
|
||||
str => Err(ShellError::InvalidValue {
|
||||
valid: "'emacs', 'vi_insert', or 'vi_normal'".into(),
|
||||
actual: format!("'{str}'"),
|
||||
span,
|
||||
}),
|
||||
},
|
||||
@ -748,9 +763,9 @@ fn add_keybinding(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
v => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "string or list of strings".to_string(),
|
||||
value: v.to_abbreviated_string(config),
|
||||
v => Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("string or list<string>"),
|
||||
actual: v.get_type(),
|
||||
span: v.span(),
|
||||
}),
|
||||
}
|
||||
@ -761,91 +776,107 @@ fn add_parsed_keybinding(
|
||||
keybinding: &ParsedKeybinding,
|
||||
config: &Config,
|
||||
) -> Result<(), ShellError> {
|
||||
let modifier = match keybinding
|
||||
.modifier
|
||||
.to_expanded_string("", config)
|
||||
.to_ascii_lowercase()
|
||||
.as_str()
|
||||
{
|
||||
"control" => KeyModifiers::CONTROL,
|
||||
"shift" => KeyModifiers::SHIFT,
|
||||
"alt" => KeyModifiers::ALT,
|
||||
"none" => KeyModifiers::NONE,
|
||||
"shift_alt" | "alt_shift" => KeyModifiers::SHIFT | KeyModifiers::ALT,
|
||||
"control_shift" | "shift_control" => KeyModifiers::CONTROL | KeyModifiers::SHIFT,
|
||||
"control_alt" | "alt_control" => KeyModifiers::CONTROL | KeyModifiers::ALT,
|
||||
"control_alt_shift" | "control_shift_alt" => {
|
||||
KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SHIFT
|
||||
}
|
||||
_ => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "CONTROL, SHIFT, ALT or NONE".to_string(),
|
||||
value: keybinding.modifier.to_abbreviated_string(config),
|
||||
span: keybinding.modifier.span(),
|
||||
})
|
||||
}
|
||||
let Ok(modifier_str) = keybinding.modifier.as_str() else {
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::String,
|
||||
actual: keybinding.modifier.get_type(),
|
||||
span: keybinding.modifier.span(),
|
||||
});
|
||||
};
|
||||
|
||||
let keycode = match keybinding
|
||||
.keycode
|
||||
.to_expanded_string("", config)
|
||||
.to_ascii_lowercase()
|
||||
.as_str()
|
||||
{
|
||||
"backspace" => KeyCode::Backspace,
|
||||
"enter" => KeyCode::Enter,
|
||||
c if c.starts_with("char_") => {
|
||||
let mut char_iter = c.chars().skip(5);
|
||||
let pos1 = char_iter.next();
|
||||
let pos2 = char_iter.next();
|
||||
|
||||
let char = if let (Some(char), None) = (pos1, pos2) {
|
||||
char
|
||||
} else {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "char_<CHAR: unicode codepoint>".to_string(),
|
||||
value: c.to_string(),
|
||||
span: keybinding.keycode.span(),
|
||||
});
|
||||
};
|
||||
|
||||
KeyCode::Char(char)
|
||||
let mut modifier = KeyModifiers::NONE;
|
||||
if !str::eq_ignore_ascii_case(modifier_str, "none") {
|
||||
for part in modifier_str.split('_') {
|
||||
match part.to_ascii_lowercase().as_str() {
|
||||
"control" => modifier |= KeyModifiers::CONTROL,
|
||||
"shift" => modifier |= KeyModifiers::SHIFT,
|
||||
"alt" => modifier |= KeyModifiers::ALT,
|
||||
"super" => modifier |= KeyModifiers::SUPER,
|
||||
"hyper" => modifier |= KeyModifiers::HYPER,
|
||||
"meta" => modifier |= KeyModifiers::META,
|
||||
_ => {
|
||||
return Err(ShellError::InvalidValue {
|
||||
valid: "'control', 'shift', 'alt', 'super', 'hyper', 'meta', or 'none'"
|
||||
.into(),
|
||||
actual: format!("'{part}'"),
|
||||
span: keybinding.modifier.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
"space" => KeyCode::Char(' '),
|
||||
"down" => KeyCode::Down,
|
||||
"up" => KeyCode::Up,
|
||||
"left" => KeyCode::Left,
|
||||
"right" => KeyCode::Right,
|
||||
"home" => KeyCode::Home,
|
||||
"end" => KeyCode::End,
|
||||
"pageup" => KeyCode::PageUp,
|
||||
"pagedown" => KeyCode::PageDown,
|
||||
"tab" => KeyCode::Tab,
|
||||
"backtab" => KeyCode::BackTab,
|
||||
"delete" => KeyCode::Delete,
|
||||
"insert" => KeyCode::Insert,
|
||||
c if c.starts_with('f') => {
|
||||
let fn_num: u8 = c[1..]
|
||||
}
|
||||
|
||||
let Ok(keycode) = keybinding.keycode.as_str() else {
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::String,
|
||||
actual: keybinding.keycode.get_type(),
|
||||
span: keybinding.keycode.span(),
|
||||
});
|
||||
};
|
||||
|
||||
let keycode_lower = keycode.to_ascii_lowercase();
|
||||
|
||||
let keycode = if let Some(rest) = keycode_lower.strip_prefix("char_") {
|
||||
let error = |valid: &str, actual: &str| ShellError::InvalidValue {
|
||||
valid: valid.into(),
|
||||
actual: actual.into(),
|
||||
span: keybinding.keycode.span(),
|
||||
};
|
||||
|
||||
let mut char_iter = rest.chars();
|
||||
let char = match (char_iter.next(), char_iter.next()) {
|
||||
(Some(char), None) => char,
|
||||
(Some('u'), Some(_)) => {
|
||||
// This will never panic as we know there are at least two symbols
|
||||
let Ok(code_point) = u32::from_str_radix(&rest[1..], 16) else {
|
||||
return Err(error("a valid hex code", keycode));
|
||||
};
|
||||
|
||||
char::from_u32(code_point).ok_or(error("a valid Unicode code point", keycode))?
|
||||
}
|
||||
_ => return Err(error("'char_<char>' or 'char_u<hex code>'", keycode)),
|
||||
};
|
||||
|
||||
KeyCode::Char(char)
|
||||
} else {
|
||||
match keycode_lower.as_str() {
|
||||
"backspace" => KeyCode::Backspace,
|
||||
"enter" => KeyCode::Enter,
|
||||
"space" => KeyCode::Char(' '),
|
||||
"down" => KeyCode::Down,
|
||||
"up" => KeyCode::Up,
|
||||
"left" => KeyCode::Left,
|
||||
"right" => KeyCode::Right,
|
||||
"home" => KeyCode::Home,
|
||||
"end" => KeyCode::End,
|
||||
"pageup" => KeyCode::PageUp,
|
||||
"pagedown" => KeyCode::PageDown,
|
||||
"tab" => KeyCode::Tab,
|
||||
"backtab" => KeyCode::BackTab,
|
||||
"delete" => KeyCode::Delete,
|
||||
"insert" => KeyCode::Insert,
|
||||
c if c.starts_with('f') => c[1..]
|
||||
.parse()
|
||||
.ok()
|
||||
.filter(|num| matches!(num, 1..=20))
|
||||
.ok_or(ShellError::UnsupportedConfigValue {
|
||||
expected: "(f1|f2|...|f20)".to_string(),
|
||||
value: format!("unknown function key: {c}"),
|
||||
.filter(|num| (1..=35).contains(num))
|
||||
.map(KeyCode::F)
|
||||
.ok_or(ShellError::InvalidValue {
|
||||
valid: "'f1', 'f2', ..., or 'f35'".into(),
|
||||
actual: format!("'{keycode}'"),
|
||||
span: keybinding.keycode.span(),
|
||||
})?;
|
||||
KeyCode::F(fn_num)
|
||||
}
|
||||
"null" => KeyCode::Null,
|
||||
"esc" | "escape" => KeyCode::Esc,
|
||||
_ => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "crossterm KeyCode".to_string(),
|
||||
value: keybinding.keycode.to_abbreviated_string(config),
|
||||
span: keybinding.keycode.span(),
|
||||
})
|
||||
})?,
|
||||
"null" => KeyCode::Null,
|
||||
"esc" | "escape" => KeyCode::Esc,
|
||||
_ => {
|
||||
return Err(ShellError::InvalidValue {
|
||||
valid: "a crossterm KeyCode".into(),
|
||||
actual: format!("'{keycode}'"),
|
||||
span: keybinding.keycode.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(event) = parse_event(&keybinding.event, config)? {
|
||||
keybindings.add_binding(modifier, keycode, event);
|
||||
} else {
|
||||
@ -867,8 +898,8 @@ impl<'config> EventType<'config> {
|
||||
.map(Self::Send)
|
||||
.or_else(|_| extract_value("edit", record, span).map(Self::Edit))
|
||||
.or_else(|_| extract_value("until", record, span).map(Self::Until))
|
||||
.map_err(|_| ShellError::MissingConfigValue {
|
||||
missing_value: "send, edit or until".to_string(),
|
||||
.map_err(|_| ShellError::MissingRequiredColumn {
|
||||
column: "'send', 'edit', or 'until'",
|
||||
span,
|
||||
})
|
||||
}
|
||||
@ -906,9 +937,9 @@ fn parse_event(value: &Value, config: &Config) -> Result<Option<ReedlineEvent>,
|
||||
.iter()
|
||||
.map(|value| match parse_event(value, config) {
|
||||
Ok(inner) => match inner {
|
||||
None => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "List containing valid events".to_string(),
|
||||
value: "Nothing value (null)".to_string(),
|
||||
None => Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("record or table"),
|
||||
actual: value.get_type(),
|
||||
span: value.span(),
|
||||
}),
|
||||
Some(event) => Ok(event),
|
||||
@ -919,9 +950,9 @@ fn parse_event(value: &Value, config: &Config) -> Result<Option<ReedlineEvent>,
|
||||
|
||||
Ok(Some(ReedlineEvent::UntilFound(events)))
|
||||
}
|
||||
v => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "list of events".to_string(),
|
||||
value: v.to_abbreviated_string(config),
|
||||
v => Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::list(Type::Any),
|
||||
actual: v.get_type(),
|
||||
span: v.span(),
|
||||
}),
|
||||
},
|
||||
@ -931,9 +962,9 @@ fn parse_event(value: &Value, config: &Config) -> Result<Option<ReedlineEvent>,
|
||||
.iter()
|
||||
.map(|value| match parse_event(value, config) {
|
||||
Ok(inner) => match inner {
|
||||
None => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "List containing valid events".to_string(),
|
||||
value: "Nothing value (null)".to_string(),
|
||||
None => Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("record or table"),
|
||||
actual: value.get_type(),
|
||||
span: value.span(),
|
||||
}),
|
||||
Some(event) => Ok(event),
|
||||
@ -945,9 +976,9 @@ fn parse_event(value: &Value, config: &Config) -> Result<Option<ReedlineEvent>,
|
||||
Ok(Some(ReedlineEvent::Multiple(events)))
|
||||
}
|
||||
Value::Nothing { .. } => Ok(None),
|
||||
v => Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "record or list of records, null to unbind key".to_string(),
|
||||
value: v.to_abbreviated_string(config),
|
||||
v => Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("record, table, or nothing"),
|
||||
actual: v.get_type(),
|
||||
span: v.span(),
|
||||
}),
|
||||
}
|
||||
@ -996,12 +1027,12 @@ fn event_from_record(
|
||||
let cmd = extract_value("cmd", record, span)?;
|
||||
ReedlineEvent::ExecuteHostCommand(cmd.to_expanded_string("", config))
|
||||
}
|
||||
v => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "Reedline event".to_string(),
|
||||
value: v.to_string(),
|
||||
str => {
|
||||
return Err(ShellError::InvalidValue {
|
||||
valid: "a reedline event".into(),
|
||||
actual: format!("'{str}'"),
|
||||
span,
|
||||
})
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -1094,7 +1125,7 @@ fn edit_from_record(
|
||||
}
|
||||
"insertchar" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
EditCommand::InsertChar(char)
|
||||
}
|
||||
"insertstring" => {
|
||||
@ -1131,17 +1162,17 @@ fn edit_from_record(
|
||||
"redo" => EditCommand::Redo,
|
||||
"cutrightuntil" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
EditCommand::CutRightUntil(char)
|
||||
}
|
||||
"cutrightbefore" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
EditCommand::CutRightBefore(char)
|
||||
}
|
||||
"moverightuntil" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
let select = extract_value("select", record, span)
|
||||
.and_then(|value| value.as_bool())
|
||||
.unwrap_or(false);
|
||||
@ -1149,7 +1180,7 @@ fn edit_from_record(
|
||||
}
|
||||
"moverightbefore" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
let select = extract_value("select", record, span)
|
||||
.and_then(|value| value.as_bool())
|
||||
.unwrap_or(false);
|
||||
@ -1157,17 +1188,17 @@ fn edit_from_record(
|
||||
}
|
||||
"cutleftuntil" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
EditCommand::CutLeftUntil(char)
|
||||
}
|
||||
"cutleftbefore" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
EditCommand::CutLeftBefore(char)
|
||||
}
|
||||
"moveleftuntil" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
let select = extract_value("select", record, span)
|
||||
.and_then(|value| value.as_bool())
|
||||
.unwrap_or(false);
|
||||
@ -1175,7 +1206,7 @@ fn edit_from_record(
|
||||
}
|
||||
"moveleftbefore" => {
|
||||
let value = extract_value("value", record, span)?;
|
||||
let char = extract_char(value, config)?;
|
||||
let char = extract_char(value)?;
|
||||
let select = extract_value("select", record, span)
|
||||
.and_then(|value| value.as_bool())
|
||||
.unwrap_or(false);
|
||||
@ -1192,28 +1223,36 @@ fn edit_from_record(
|
||||
#[cfg(feature = "system-clipboard")]
|
||||
"pastesystem" => EditCommand::PasteSystem,
|
||||
"selectall" => EditCommand::SelectAll,
|
||||
e => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "reedline EditCommand".to_string(),
|
||||
value: e.to_string(),
|
||||
str => {
|
||||
return Err(ShellError::InvalidValue {
|
||||
valid: "a reedline EditCommand".into(),
|
||||
actual: format!("'{str}'"),
|
||||
span,
|
||||
})
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Ok(edit)
|
||||
}
|
||||
|
||||
fn extract_char(value: &Value, config: &Config) -> Result<char, ShellError> {
|
||||
let span = value.span();
|
||||
value
|
||||
.to_expanded_string("", config)
|
||||
.chars()
|
||||
.next()
|
||||
.ok_or_else(|| ShellError::MissingConfigValue {
|
||||
missing_value: "char to insert".to_string(),
|
||||
span,
|
||||
fn extract_char(value: &Value) -> Result<char, ShellError> {
|
||||
if let Ok(str) = value.as_str() {
|
||||
let mut chars = str.chars();
|
||||
match (chars.next(), chars.next()) {
|
||||
(Some(c), None) => Ok(c),
|
||||
_ => Err(ShellError::InvalidValue {
|
||||
valid: "a single character".into(),
|
||||
actual: format!("'{str}'"),
|
||||
span: value.span(),
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::String,
|
||||
actual: value.get_type(),
|
||||
span: value.span(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -1342,7 +1381,7 @@ mod test {
|
||||
|
||||
let span = Span::test_data();
|
||||
let b = EventType::try_from_record(&event, span);
|
||||
assert!(matches!(b, Err(ShellError::MissingConfigValue { .. })));
|
||||
assert!(matches!(b, Err(ShellError::MissingRequiredColumn { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -16,10 +16,7 @@ use crate::{
|
||||
use crossterm::cursor::SetCursorStyle;
|
||||
use log::{error, trace, warn};
|
||||
use miette::{ErrReport, IntoDiagnostic, Result};
|
||||
use nu_cmd_base::{
|
||||
hook::eval_hook,
|
||||
util::{get_editor, get_guaranteed_cwd},
|
||||
};
|
||||
use nu_cmd_base::util::get_editor;
|
||||
use nu_color_config::StyleComputer;
|
||||
#[allow(deprecated)]
|
||||
use nu_engine::{convert_env_values, current_dir_str, env_to_strings};
|
||||
@ -53,7 +50,6 @@ use sysinfo::System;
|
||||
pub fn evaluate_repl(
|
||||
engine_state: &mut EngineState,
|
||||
stack: Stack,
|
||||
nushell_path: &str,
|
||||
prerun_command: Option<Spanned<String>>,
|
||||
load_std_lib: Option<Spanned<String>>,
|
||||
entire_start_time: Instant,
|
||||
@ -100,7 +96,7 @@ pub fn evaluate_repl(
|
||||
|
||||
unique_stack.set_last_exit_code(0, Span::unknown());
|
||||
|
||||
let mut line_editor = get_line_editor(engine_state, nushell_path, use_color)?;
|
||||
let mut line_editor = get_line_editor(engine_state, use_color)?;
|
||||
let temp_file = temp_dir().join(format!("{}.nu", uuid::Uuid::new_v4()));
|
||||
|
||||
if let Some(s) = prerun_command {
|
||||
@ -112,8 +108,7 @@ pub fn evaluate_repl(
|
||||
PipelineData::empty(),
|
||||
false,
|
||||
);
|
||||
let cwd = get_guaranteed_cwd(engine_state, &unique_stack);
|
||||
engine_state.merge_env(&mut unique_stack, cwd)?;
|
||||
engine_state.merge_env(&mut unique_stack)?;
|
||||
}
|
||||
|
||||
let hostname = System::host_name();
|
||||
@ -136,15 +131,7 @@ pub fn evaluate_repl(
|
||||
// https://code.visualstudio.com/docs/terminal/shell-integration#_vs-code-custom-sequences-osc-633-st
|
||||
let cmd_text = line_editor.current_buffer_contents().to_string();
|
||||
|
||||
let replaced_cmd_text = cmd_text
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
'\n' => '\x0a',
|
||||
'\r' => '\x0d',
|
||||
'\x1b' => '\x1b',
|
||||
_ => c,
|
||||
})
|
||||
.collect();
|
||||
let replaced_cmd_text = escape_special_vscode_bytes(&cmd_text)?;
|
||||
|
||||
run_shell_integration_osc633(
|
||||
engine_state,
|
||||
@ -163,7 +150,7 @@ pub fn evaluate_repl(
|
||||
eval_source(
|
||||
engine_state,
|
||||
&mut unique_stack,
|
||||
r#"use std banner; banner"#.as_bytes(),
|
||||
r#"banner"#.as_bytes(),
|
||||
"show_banner",
|
||||
PipelineData::empty(),
|
||||
false,
|
||||
@ -220,7 +207,7 @@ pub fn evaluate_repl(
|
||||
}
|
||||
Err(_) => {
|
||||
// line_editor is lost in the error case so reconstruct a new one
|
||||
line_editor = get_line_editor(engine_state, nushell_path, use_color)?;
|
||||
line_editor = get_line_editor(engine_state, use_color)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -228,11 +215,44 @@ pub fn evaluate_repl(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_line_editor(
|
||||
engine_state: &mut EngineState,
|
||||
nushell_path: &str,
|
||||
use_color: bool,
|
||||
) -> Result<Reedline> {
|
||||
fn escape_special_vscode_bytes(input: &str) -> Result<String, ShellError> {
|
||||
let bytes = input
|
||||
.chars()
|
||||
.flat_map(|c| {
|
||||
let mut buf = [0; 4]; // Buffer to hold UTF-8 bytes of the character
|
||||
let c_bytes = c.encode_utf8(&mut buf); // Get UTF-8 bytes for the character
|
||||
|
||||
if c_bytes.len() == 1 {
|
||||
let byte = c_bytes.as_bytes()[0];
|
||||
|
||||
match byte {
|
||||
// Escape bytes below 0x20
|
||||
b if b < 0x20 => format!("\\x{:02X}", byte).into_bytes(),
|
||||
// Escape semicolon as \x3B
|
||||
b';' => "\\x3B".to_string().into_bytes(),
|
||||
// Escape backslash as \\
|
||||
b'\\' => "\\\\".to_string().into_bytes(),
|
||||
// Otherwise, return the character unchanged
|
||||
_ => vec![byte],
|
||||
}
|
||||
} else {
|
||||
// pass through multi-byte characters unchanged
|
||||
c_bytes.bytes().collect()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
String::from_utf8(bytes).map_err(|err| ShellError::CantConvert {
|
||||
to_type: "string".to_string(),
|
||||
from_type: "bytes".to_string(),
|
||||
span: Span::unknown(),
|
||||
help: Some(format!(
|
||||
"Error {err}, Unable to convert {input} to escaped bytes"
|
||||
)),
|
||||
})
|
||||
}
|
||||
|
||||
fn get_line_editor(engine_state: &mut EngineState, use_color: bool) -> Result<Reedline> {
|
||||
let mut start_time = std::time::Instant::now();
|
||||
let mut line_editor = Reedline::create();
|
||||
|
||||
@ -243,7 +263,7 @@ fn get_line_editor(
|
||||
if let Some(history) = engine_state.history_config() {
|
||||
start_time = std::time::Instant::now();
|
||||
|
||||
line_editor = setup_history(nushell_path, engine_state, line_editor, history)?;
|
||||
line_editor = setup_history(engine_state, line_editor, history)?;
|
||||
|
||||
perf!("setup history", start_time, use_color);
|
||||
}
|
||||
@ -280,17 +300,12 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
||||
hostname,
|
||||
} = ctx;
|
||||
|
||||
let cwd = get_guaranteed_cwd(engine_state, &stack);
|
||||
|
||||
let mut start_time = std::time::Instant::now();
|
||||
// Before doing anything, merge the environment from the previous REPL iteration into the
|
||||
// permanent state.
|
||||
if let Err(err) = engine_state.merge_env(&mut stack, cwd) {
|
||||
if let Err(err) = engine_state.merge_env(&mut stack) {
|
||||
report_shell_error(engine_state, &err);
|
||||
}
|
||||
// Check whether $env.NU_DISABLE_IR is set, so that the user can change it in the REPL
|
||||
// Temporary while IR eval is optional
|
||||
stack.use_ir = !stack.has_env_var(engine_state, "NU_DISABLE_IR");
|
||||
perf!("merge env", start_time, use_color);
|
||||
|
||||
start_time = std::time::Instant::now();
|
||||
@ -298,20 +313,26 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
||||
perf!("reset signals", start_time, use_color);
|
||||
|
||||
start_time = std::time::Instant::now();
|
||||
// Right before we start our prompt and take input from the user,
|
||||
// fire the "pre_prompt" hook
|
||||
if let Some(hook) = engine_state.get_config().hooks.pre_prompt.clone() {
|
||||
if let Err(err) = eval_hook(engine_state, &mut stack, None, vec![], &hook, "pre_prompt") {
|
||||
report_shell_error(engine_state, &err);
|
||||
}
|
||||
// Right before we start our prompt and take input from the user, fire the "pre_prompt" hook
|
||||
if let Err(err) = hook::eval_hooks(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
vec![],
|
||||
&engine_state.get_config().hooks.pre_prompt.clone(),
|
||||
"pre_prompt",
|
||||
) {
|
||||
report_shell_error(engine_state, &err);
|
||||
}
|
||||
perf!("pre-prompt hook", start_time, use_color);
|
||||
|
||||
start_time = std::time::Instant::now();
|
||||
// Next, check all the environment variables they ask for
|
||||
// fire the "env_change" hook
|
||||
let env_change = engine_state.get_config().hooks.env_change.clone();
|
||||
if let Err(error) = hook::eval_env_change_hook(env_change, engine_state, &mut stack) {
|
||||
if let Err(error) = hook::eval_env_change_hook(
|
||||
&engine_state.get_config().hooks.env_change.clone(),
|
||||
engine_state,
|
||||
&mut stack,
|
||||
) {
|
||||
report_shell_error(engine_state, &error)
|
||||
}
|
||||
perf!("env-change hook", start_time, use_color);
|
||||
@ -363,7 +384,11 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
))
|
||||
.with_cursor_config(cursor_config);
|
||||
.with_cursor_config(cursor_config)
|
||||
.with_visual_selection_style(nu_ansi_term::Style {
|
||||
is_reverse: true,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
perf!("reedline builder", start_time, use_color);
|
||||
|
||||
@ -492,18 +517,17 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
||||
|
||||
// Right before we start running the code the user gave us, fire the `pre_execution`
|
||||
// hook
|
||||
if let Some(hook) = config.hooks.pre_execution.clone() {
|
||||
{
|
||||
// Set the REPL buffer to the current command for the "pre_execution" hook
|
||||
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
|
||||
repl.buffer = repl_cmd_line_text.to_string();
|
||||
drop(repl);
|
||||
|
||||
if let Err(err) = eval_hook(
|
||||
if let Err(err) = hook::eval_hooks(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
None,
|
||||
vec![],
|
||||
&hook,
|
||||
&engine_state.get_config().hooks.pre_execution.clone(),
|
||||
"pre_execution",
|
||||
) {
|
||||
report_shell_error(engine_state, &err);
|
||||
@ -518,8 +542,10 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
||||
drop(repl);
|
||||
|
||||
if shell_integration_osc633 {
|
||||
if stack.get_env_var(engine_state, "TERM_PROGRAM")
|
||||
== Some(Value::test_string("vscode"))
|
||||
if stack
|
||||
.get_env_var(engine_state, "TERM_PROGRAM")
|
||||
.and_then(|v| v.as_str().ok())
|
||||
== Some("vscode")
|
||||
{
|
||||
start_time = Instant::now();
|
||||
|
||||
@ -736,7 +762,7 @@ fn fill_in_result_related_history_metadata(
|
||||
c.duration = Some(cmd_duration);
|
||||
c.exit_status = stack
|
||||
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
||||
.and_then(|e| e.as_i64().ok());
|
||||
.and_then(|e| e.as_int().ok());
|
||||
c
|
||||
})
|
||||
.into_diagnostic()?; // todo: don't stop repl if error here?
|
||||
@ -841,7 +867,7 @@ fn do_auto_cd(
|
||||
|
||||
let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS");
|
||||
let mut shells = if let Some(v) = shells {
|
||||
v.into_list().unwrap_or_else(|_| vec![cwd])
|
||||
v.clone().into_list().unwrap_or_else(|_| vec![cwd])
|
||||
} else {
|
||||
vec![cwd]
|
||||
};
|
||||
@ -1033,7 +1059,11 @@ fn run_shell_integration_osc633(
|
||||
if let Ok(path) = current_dir_str(engine_state, stack) {
|
||||
// Supported escape sequences of Microsoft's Visual Studio Code (vscode)
|
||||
// https://code.visualstudio.com/docs/terminal/shell-integration#_supported-escape-sequences
|
||||
if stack.get_env_var(engine_state, "TERM_PROGRAM") == Some(Value::test_string("vscode")) {
|
||||
if stack
|
||||
.get_env_var(engine_state, "TERM_PROGRAM")
|
||||
.and_then(|v| v.as_str().ok())
|
||||
== Some("vscode")
|
||||
{
|
||||
let start_time = Instant::now();
|
||||
|
||||
// If we're in vscode, run their specific ansi escape sequence.
|
||||
@ -1051,16 +1081,8 @@ fn run_shell_integration_osc633(
|
||||
|
||||
// escape a few things because this says so
|
||||
// https://code.visualstudio.com/docs/terminal/shell-integration#_vs-code-custom-sequences-osc-633-st
|
||||
|
||||
let replaced_cmd_text: String = repl_cmd_line_text
|
||||
.chars()
|
||||
.map(|c| match c {
|
||||
'\n' => '\x0a',
|
||||
'\r' => '\x0d',
|
||||
'\x1b' => '\x1b',
|
||||
_ => c,
|
||||
})
|
||||
.collect();
|
||||
let replaced_cmd_text =
|
||||
escape_special_vscode_bytes(&repl_cmd_line_text).unwrap_or(repl_cmd_line_text);
|
||||
|
||||
//OSC 633 ; E ; <commandline> [; <nonce] ST - Explicitly set the command line with an optional nonce.
|
||||
run_ansi_sequence(&format!(
|
||||
@ -1098,7 +1120,6 @@ fn flush_engine_state_repl_buffer(engine_state: &mut EngineState, line_editor: &
|
||||
/// Setup history management for Reedline
|
||||
///
|
||||
fn setup_history(
|
||||
nushell_path: &str,
|
||||
engine_state: &mut EngineState,
|
||||
line_editor: Reedline,
|
||||
history: HistoryConfig,
|
||||
@ -1110,7 +1131,7 @@ fn setup_history(
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(path) = crate::config_files::get_history_path(nushell_path, history.file_format) {
|
||||
if let Some(path) = history.file_path() {
|
||||
return update_line_editor_history(
|
||||
engine_state,
|
||||
path,
|
||||
@ -1228,10 +1249,14 @@ fn get_command_finished_marker(
|
||||
) -> String {
|
||||
let exit_code = stack
|
||||
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
||||
.and_then(|e| e.as_i64().ok());
|
||||
.and_then(|e| e.as_int().ok());
|
||||
|
||||
if shell_integration_osc633 {
|
||||
if stack.get_env_var(engine_state, "TERM_PROGRAM") == Some(Value::test_string("vscode")) {
|
||||
if stack
|
||||
.get_env_var(engine_state, "TERM_PROGRAM")
|
||||
.and_then(|v| v.as_str().ok())
|
||||
== Some("vscode")
|
||||
{
|
||||
// We're in vscode and we have osc633 enabled
|
||||
format!(
|
||||
"{}{}{}",
|
||||
@ -1280,7 +1305,11 @@ fn run_finaliziation_ansi_sequence(
|
||||
) {
|
||||
if shell_integration_osc633 {
|
||||
// Only run osc633 if we are in vscode
|
||||
if stack.get_env_var(engine_state, "TERM_PROGRAM") == Some(Value::test_string("vscode")) {
|
||||
if stack
|
||||
.get_env_var(engine_state, "TERM_PROGRAM")
|
||||
.and_then(|v| v.as_str().ok())
|
||||
== Some("vscode")
|
||||
{
|
||||
let start_time = Instant::now();
|
||||
|
||||
run_ansi_sequence(&get_command_finished_marker(
|
||||
@ -1331,10 +1360,9 @@ fn run_finaliziation_ansi_sequence(
|
||||
|
||||
// Absolute paths with a drive letter, like 'C:', 'D:\', 'E:\foo'
|
||||
#[cfg(windows)]
|
||||
static DRIVE_PATH_REGEX: once_cell::sync::Lazy<fancy_regex::Regex> =
|
||||
once_cell::sync::Lazy::new(|| {
|
||||
fancy_regex::Regex::new(r"^[a-zA-Z]:[/\\]?").expect("Internal error: regex creation")
|
||||
});
|
||||
static DRIVE_PATH_REGEX: std::sync::LazyLock<fancy_regex::Regex> = std::sync::LazyLock::new(|| {
|
||||
fancy_regex::Regex::new(r"^[a-zA-Z]:[/\\]?").expect("Internal error: regex creation")
|
||||
});
|
||||
|
||||
// A best-effort "does this string look kinda like a path?" function to determine whether to auto-cd
|
||||
fn looks_like_path(orig: &str) -> bool {
|
||||
@ -1378,8 +1406,7 @@ fn trailing_slash_looks_like_path() {
|
||||
fn are_session_ids_in_sync() {
|
||||
let engine_state = &mut EngineState::new();
|
||||
let history = engine_state.history_config().unwrap();
|
||||
let history_path =
|
||||
crate::config_files::get_history_path("nushell", history.file_format).unwrap();
|
||||
let history_path = history.file_path().unwrap();
|
||||
let line_editor = reedline::Reedline::create();
|
||||
let history_session_id = reedline::Reedline::create_history_session_id();
|
||||
let line_editor = update_line_editor_history(
|
||||
@ -1397,7 +1424,7 @@ fn are_session_ids_in_sync() {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_auto_cd {
|
||||
use super::{do_auto_cd, parse_operation, ReplOperation};
|
||||
use super::{do_auto_cd, escape_special_vscode_bytes, parse_operation, ReplOperation};
|
||||
use nu_path::AbsolutePath;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use tempfile::tempdir;
|
||||
@ -1547,4 +1574,43 @@ mod test_auto_cd {
|
||||
let input = if cfg!(windows) { r"foo\" } else { "foo/" };
|
||||
check(tempdir, input, dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_vscode_semicolon_test() {
|
||||
let input = r#"now;is"#;
|
||||
let expected = r#"now\x3Bis"#;
|
||||
let actual = escape_special_vscode_bytes(input).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_vscode_backslash_test() {
|
||||
let input = r#"now\is"#;
|
||||
let expected = r#"now\\is"#;
|
||||
let actual = escape_special_vscode_bytes(input).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_vscode_linefeed_test() {
|
||||
let input = "now\nis";
|
||||
let expected = r#"now\x0Ais"#;
|
||||
let actual = escape_special_vscode_bytes(input).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_vscode_tab_null_cr_test() {
|
||||
let input = "now\t\0\ris";
|
||||
let expected = r#"now\x09\x00\x0Dis"#;
|
||||
let actual = escape_special_vscode_bytes(input).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_vscode_multibyte_ok() {
|
||||
let input = "now🍪is";
|
||||
let actual = escape_special_vscode_bytes(input).unwrap();
|
||||
assert_eq!(input, actual);
|
||||
}
|
||||
}
|
||||
|
@ -144,8 +144,6 @@ impl Highlighter for NuHighlighter {
|
||||
}
|
||||
FlatShape::Flag => add_colored_token(&shape.1, next_token),
|
||||
FlatShape::Pipe => add_colored_token(&shape.1, next_token),
|
||||
FlatShape::And => add_colored_token(&shape.1, next_token),
|
||||
FlatShape::Or => add_colored_token(&shape.1, next_token),
|
||||
FlatShape::Redirection => add_colored_token(&shape.1, next_token),
|
||||
FlatShape::Custom(..) => add_colored_token(&shape.1, next_token),
|
||||
FlatShape::MatchPattern => add_colored_token(&shape.1, next_token),
|
||||
|
@ -1,6 +1,8 @@
|
||||
#![allow(clippy::byte_char_slices)]
|
||||
|
||||
use nu_cmd_base::hook::eval_hook;
|
||||
use nu_engine::{eval_block, eval_block_with_early_return};
|
||||
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
|
||||
use nu_parser::{lex, parse, unescape_unquote_string, Token, TokenContents};
|
||||
use nu_protocol::{
|
||||
cli_error::report_compile_error,
|
||||
debugger::WithoutDebug,
|
||||
@ -10,7 +12,7 @@ use nu_protocol::{
|
||||
};
|
||||
#[cfg(windows)]
|
||||
use nu_utils::enable_vt_processing;
|
||||
use nu_utils::perf;
|
||||
use nu_utils::{escape_quote_string, perf};
|
||||
use std::path::Path;
|
||||
|
||||
// This will collect environment variables from std::env and adds them to a stack.
|
||||
@ -201,6 +203,35 @@ fn gather_env_vars(
|
||||
}
|
||||
}
|
||||
|
||||
/// Print a pipeline with formatting applied based on display_output hook.
|
||||
///
|
||||
/// This function should be preferred when printing values resulting from a completed evaluation.
|
||||
/// For values printed as part of a command's execution, such as values printed by the `print` command,
|
||||
/// the `PipelineData::print_table` function should be preferred instead as it is not config-dependent.
|
||||
///
|
||||
/// `no_newline` controls if we need to attach newline character to output.
|
||||
pub fn print_pipeline(
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
pipeline: PipelineData,
|
||||
no_newline: bool,
|
||||
) -> Result<(), ShellError> {
|
||||
if let Some(hook) = engine_state.get_config().hooks.display_output.clone() {
|
||||
let pipeline = eval_hook(
|
||||
engine_state,
|
||||
stack,
|
||||
Some(pipeline),
|
||||
vec![],
|
||||
&hook,
|
||||
"display_output",
|
||||
)?;
|
||||
pipeline.print_raw(engine_state, no_newline, false)
|
||||
} else {
|
||||
// if display_output isn't set, we should still prefer to print with some formatting
|
||||
pipeline.print_table(engine_state, stack, no_newline, false)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_source(
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
@ -221,7 +252,7 @@ pub fn eval_source(
|
||||
report_shell_error(engine_state, &err);
|
||||
let code = err.exit_code();
|
||||
stack.set_last_error(&err);
|
||||
code
|
||||
code.unwrap_or(0)
|
||||
}
|
||||
};
|
||||
|
||||
@ -267,7 +298,7 @@ fn evaluate_source(
|
||||
|
||||
if let Some(err) = working_set.compile_errors.first() {
|
||||
report_compile_error(&working_set, err);
|
||||
// Not a fatal error, for now
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
(output, working_set.render())
|
||||
@ -281,21 +312,8 @@ fn evaluate_source(
|
||||
eval_block::<WithoutDebug>(engine_state, stack, &block, input)
|
||||
}?;
|
||||
|
||||
if let PipelineData::ByteStream(..) = pipeline {
|
||||
pipeline.print(engine_state, stack, false, false)
|
||||
} else if let Some(hook) = engine_state.get_config().hooks.display_output.clone() {
|
||||
let pipeline = eval_hook(
|
||||
engine_state,
|
||||
stack,
|
||||
Some(pipeline),
|
||||
vec![],
|
||||
&hook,
|
||||
"display_output",
|
||||
)?;
|
||||
pipeline.print(engine_state, stack, false, false)
|
||||
} else {
|
||||
pipeline.print(engine_state, stack, true, false)
|
||||
}?;
|
||||
let no_newline = matches!(&pipeline, &PipelineData::ByteStream(..));
|
||||
print_pipeline(engine_state, stack, pipeline, no_newline)?;
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
296
crates/nu-cli/tests/commands/history_import.rs
Normal file
296
crates/nu-cli/tests/commands/history_import.rs
Normal file
@ -0,0 +1,296 @@
|
||||
use nu_protocol::HistoryFileFormat;
|
||||
use nu_test_support::{nu, Outcome};
|
||||
use reedline::{
|
||||
FileBackedHistory, History, HistoryItem, HistoryItemId, ReedlineError, SearchQuery,
|
||||
SqliteBackedHistory,
|
||||
};
|
||||
use rstest::rstest;
|
||||
use tempfile::TempDir;
|
||||
|
||||
struct Test {
|
||||
cfg_dir: TempDir,
|
||||
}
|
||||
|
||||
impl Test {
|
||||
fn new(history_format: &'static str) -> Self {
|
||||
let cfg_dir = tempfile::Builder::new()
|
||||
.prefix("history_import_test")
|
||||
.tempdir()
|
||||
.unwrap();
|
||||
// Assigning to $env.config.history.file_format seems to work only in startup
|
||||
// configuration.
|
||||
std::fs::write(
|
||||
cfg_dir.path().join("env.nu"),
|
||||
format!("$env.config.history.file_format = {history_format:?}"),
|
||||
)
|
||||
.unwrap();
|
||||
Self { cfg_dir }
|
||||
}
|
||||
|
||||
fn nu(&self, cmd: impl AsRef<str>) -> Outcome {
|
||||
let env = [(
|
||||
"XDG_CONFIG_HOME".to_string(),
|
||||
self.cfg_dir.path().to_str().unwrap().to_string(),
|
||||
)];
|
||||
let env_config = self.cfg_dir.path().join("env.nu");
|
||||
nu!(envs: env, env_config: env_config, cmd.as_ref())
|
||||
}
|
||||
|
||||
fn open_plaintext(&self) -> Result<FileBackedHistory, ReedlineError> {
|
||||
FileBackedHistory::with_file(
|
||||
100,
|
||||
self.cfg_dir
|
||||
.path()
|
||||
.join("nushell")
|
||||
.join(HistoryFileFormat::Plaintext.default_file_name()),
|
||||
)
|
||||
}
|
||||
|
||||
fn open_sqlite(&self) -> Result<SqliteBackedHistory, ReedlineError> {
|
||||
SqliteBackedHistory::with_file(
|
||||
self.cfg_dir
|
||||
.path()
|
||||
.join("nushell")
|
||||
.join(HistoryFileFormat::Sqlite.default_file_name()),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn open_backend(&self, format: HistoryFileFormat) -> Result<Box<dyn History>, ReedlineError> {
|
||||
fn boxed(be: impl History + 'static) -> Box<dyn History> {
|
||||
Box::new(be)
|
||||
}
|
||||
use HistoryFileFormat::*;
|
||||
match format {
|
||||
Plaintext => self.open_plaintext().map(boxed),
|
||||
Sqlite => self.open_sqlite().map(boxed),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum HistorySource {
|
||||
Vec(Vec<HistoryItem>),
|
||||
Command(&'static str),
|
||||
}
|
||||
|
||||
struct TestCase {
|
||||
dst_format: HistoryFileFormat,
|
||||
dst_history: Vec<HistoryItem>,
|
||||
src_history: HistorySource,
|
||||
want_history: Vec<HistoryItem>,
|
||||
}
|
||||
|
||||
const EMPTY_TEST_CASE: TestCase = TestCase {
|
||||
dst_format: HistoryFileFormat::Plaintext,
|
||||
dst_history: Vec::new(),
|
||||
src_history: HistorySource::Vec(Vec::new()),
|
||||
want_history: Vec::new(),
|
||||
};
|
||||
|
||||
impl TestCase {
|
||||
fn run(self) {
|
||||
use HistoryFileFormat::*;
|
||||
let test = Test::new(match self.dst_format {
|
||||
Plaintext => "plaintext",
|
||||
Sqlite => "sqlite",
|
||||
});
|
||||
save_all(
|
||||
&mut *test.open_backend(self.dst_format).unwrap(),
|
||||
self.dst_history,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let outcome = match self.src_history {
|
||||
HistorySource::Vec(src_history) => {
|
||||
let src_format = match self.dst_format {
|
||||
Plaintext => Sqlite,
|
||||
Sqlite => Plaintext,
|
||||
};
|
||||
save_all(&mut *test.open_backend(src_format).unwrap(), src_history).unwrap();
|
||||
test.nu("history import")
|
||||
}
|
||||
HistorySource::Command(cmd) => {
|
||||
let mut cmd = cmd.to_string();
|
||||
cmd.push_str(" | history import");
|
||||
test.nu(cmd)
|
||||
}
|
||||
};
|
||||
assert!(outcome.status.success());
|
||||
let got = query_all(&*test.open_backend(self.dst_format).unwrap()).unwrap();
|
||||
|
||||
// Compare just the commands first, for readability.
|
||||
fn commands_only(items: &[HistoryItem]) -> Vec<&str> {
|
||||
items
|
||||
.iter()
|
||||
.map(|item| item.command_line.as_str())
|
||||
.collect()
|
||||
}
|
||||
assert_eq!(commands_only(&got), commands_only(&self.want_history));
|
||||
// If commands match, compare full items.
|
||||
assert_eq!(got, self.want_history);
|
||||
}
|
||||
}
|
||||
|
||||
fn query_all(history: &dyn History) -> Result<Vec<HistoryItem>, ReedlineError> {
|
||||
history.search(SearchQuery::everything(
|
||||
reedline::SearchDirection::Forward,
|
||||
None,
|
||||
))
|
||||
}
|
||||
|
||||
fn save_all(history: &mut dyn History, items: Vec<HistoryItem>) -> Result<(), ReedlineError> {
|
||||
for item in items {
|
||||
history.save(item)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const EMPTY_ITEM: HistoryItem = HistoryItem {
|
||||
command_line: String::new(),
|
||||
id: None,
|
||||
start_timestamp: None,
|
||||
session_id: None,
|
||||
hostname: None,
|
||||
cwd: None,
|
||||
duration: None,
|
||||
exit_status: None,
|
||||
more_info: None,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn history_import_pipe_string() {
|
||||
TestCase {
|
||||
dst_format: HistoryFileFormat::Plaintext,
|
||||
src_history: HistorySource::Command("echo bar"),
|
||||
want_history: vec![HistoryItem {
|
||||
id: Some(HistoryItemId::new(0)),
|
||||
command_line: "bar".to_string(),
|
||||
..EMPTY_ITEM
|
||||
}],
|
||||
..EMPTY_TEST_CASE
|
||||
}
|
||||
.run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_import_pipe_record() {
|
||||
TestCase {
|
||||
dst_format: HistoryFileFormat::Sqlite,
|
||||
src_history: HistorySource::Command("[[cwd command]; [/tmp some_command]]"),
|
||||
want_history: vec![HistoryItem {
|
||||
id: Some(HistoryItemId::new(1)),
|
||||
command_line: "some_command".to_string(),
|
||||
cwd: Some("/tmp".to_string()),
|
||||
..EMPTY_ITEM
|
||||
}],
|
||||
..EMPTY_TEST_CASE
|
||||
}
|
||||
.run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_empty_plaintext() {
|
||||
TestCase {
|
||||
dst_format: HistoryFileFormat::Plaintext,
|
||||
src_history: HistorySource::Vec(vec![
|
||||
HistoryItem {
|
||||
command_line: "foo".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
command_line: "bar".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
]),
|
||||
want_history: vec![
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(0)),
|
||||
command_line: "foo".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(1)),
|
||||
command_line: "bar".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
],
|
||||
..EMPTY_TEST_CASE
|
||||
}
|
||||
.run()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_empty_sqlite() {
|
||||
TestCase {
|
||||
dst_format: HistoryFileFormat::Sqlite,
|
||||
src_history: HistorySource::Vec(vec![
|
||||
HistoryItem {
|
||||
command_line: "foo".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
command_line: "bar".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
]),
|
||||
want_history: vec![
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(1)),
|
||||
command_line: "foo".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(2)),
|
||||
command_line: "bar".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
],
|
||||
..EMPTY_TEST_CASE
|
||||
}
|
||||
.run()
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::plaintext(HistoryFileFormat::Plaintext)]
|
||||
#[case::sqlite(HistoryFileFormat::Sqlite)]
|
||||
fn to_existing(#[case] dst_format: HistoryFileFormat) {
|
||||
TestCase {
|
||||
dst_format,
|
||||
dst_history: vec![
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(0)),
|
||||
command_line: "original-1".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(1)),
|
||||
command_line: "original-2".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
],
|
||||
src_history: HistorySource::Vec(vec![HistoryItem {
|
||||
id: Some(HistoryItemId::new(1)),
|
||||
command_line: "new".to_string(),
|
||||
..EMPTY_ITEM
|
||||
}]),
|
||||
want_history: vec![
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(0)),
|
||||
command_line: "original-1".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(1)),
|
||||
command_line: "original-2".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
HistoryItem {
|
||||
id: Some(HistoryItemId::new(2)),
|
||||
command_line: "new".to_string(),
|
||||
..EMPTY_ITEM
|
||||
},
|
||||
],
|
||||
}
|
||||
.run()
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
mod history_import;
|
||||
mod keybindings_list;
|
||||
mod nu_highlight;
|
||||
|
@ -18,11 +18,11 @@ use support::{
|
||||
#[fixture]
|
||||
fn completer() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Add record value as example
|
||||
let record = "def tst [--mod -s] {}";
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -31,11 +31,12 @@ fn completer() -> NuCompleter {
|
||||
#[fixture]
|
||||
fn completer_strings() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Add record value as example
|
||||
let record = r#"def animals [] { ["cat", "dog", "eel" ] }
|
||||
def my-command [animal: string@animals] { print $animal }"#;
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -44,7 +45,7 @@ fn completer_strings() -> NuCompleter {
|
||||
#[fixture]
|
||||
fn extern_completer() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Add record value as example
|
||||
let record = r#"
|
||||
@ -55,7 +56,7 @@ fn extern_completer() -> NuCompleter {
|
||||
-b: string@animals
|
||||
]
|
||||
"#;
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -64,7 +65,7 @@ fn extern_completer() -> NuCompleter {
|
||||
#[fixture]
|
||||
fn completer_strings_with_options() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
// Add record value as example
|
||||
let record = r#"
|
||||
# To test that the config setting has no effect on the custom completions
|
||||
@ -81,7 +82,28 @@ fn completer_strings_with_options() -> NuCompleter {
|
||||
}
|
||||
}
|
||||
def my-command [animal: string@animals] { print $animal }"#;
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
fn completer_strings_no_sort() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
let command = r#"
|
||||
def animals [] {
|
||||
{
|
||||
completions: ["zzzfoo", "foo", "not matched", "abcfoo" ],
|
||||
options: {
|
||||
completion_algorithm: "fuzzy",
|
||||
sort: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
def my-command [animal: string@animals] { print $animal }"#;
|
||||
assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -90,7 +112,7 @@ fn completer_strings_with_options() -> NuCompleter {
|
||||
#[fixture]
|
||||
fn custom_completer() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Add record value as example
|
||||
let record = r#"
|
||||
@ -104,7 +126,7 @@ fn custom_completer() -> NuCompleter {
|
||||
completer: $external_completer
|
||||
}
|
||||
"#;
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -113,7 +135,7 @@ fn custom_completer() -> NuCompleter {
|
||||
#[fixture]
|
||||
fn subcommand_completer() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
let commands = r#"
|
||||
$env.config.completions.algorithm = "fuzzy"
|
||||
@ -123,7 +145,7 @@ fn subcommand_completer() -> NuCompleter {
|
||||
def "foo aabcrr" [] {}
|
||||
def food [] {}
|
||||
"#;
|
||||
assert!(support::merge_input(commands.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(commands.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -133,13 +155,13 @@ fn subcommand_completer() -> NuCompleter {
|
||||
#[fixture]
|
||||
fn fuzzy_alpha_sort_completer() -> NuCompleter {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
let config = r#"
|
||||
$env.config.completions.algorithm = "fuzzy"
|
||||
$env.config.completions.sort = "alphabetical"
|
||||
"#;
|
||||
assert!(support::merge_input(config.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(config.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
NuCompleter::new(Arc::new(engine), Arc::new(stack))
|
||||
@ -209,6 +231,13 @@ fn customcompletions_case_insensitive(mut completer_strings_with_options: NuComp
|
||||
match_suggestions(&expected, &suggestions);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn customcompletions_no_sort(mut completer_strings_no_sort: NuCompleter) {
|
||||
let suggestions = completer_strings_no_sort.complete("my-command foo", 14);
|
||||
let expected: Vec<String> = vec!["zzzfoo".into(), "foo".into(), "abcfoo".into()];
|
||||
match_suggestions(&expected, &suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dotnu_completions() {
|
||||
// Create a new engine
|
||||
@ -328,6 +357,39 @@ fn file_completions() {
|
||||
// Match the results
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
|
||||
// Test completions for the current folder even with parts before the autocomplet
|
||||
let target_dir = format!("cp somefile.txt {dir_str}{MAIN_SEPARATOR}");
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
// Create the expected values
|
||||
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")),
|
||||
file(dir.join(".hidden_file")),
|
||||
folder(dir.join(".hidden_folder")),
|
||||
];
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let separator = '/';
|
||||
let target_dir = format!("cp somefile.txt {dir_str}{separator}");
|
||||
let slash_suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
let expected_slash_paths: Vec<String> = expected_paths
|
||||
.iter()
|
||||
.map(|s| s.replace('\\', "/"))
|
||||
.collect();
|
||||
|
||||
match_suggestions(&expected_slash_paths, &slash_suggestions);
|
||||
}
|
||||
|
||||
// Match the results
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
|
||||
// Test completions for a file
|
||||
let target_dir = format!("cp {}", folder(dir.join("another")));
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
@ -362,6 +424,75 @@ fn file_completions() {
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_command_rest_any_args_file_completions() {
|
||||
// Create a new engine
|
||||
let (dir, dir_str, mut engine, mut stack) = new_engine();
|
||||
let command = r#"def list [ ...args: any ] {}"#;
|
||||
assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
// Test completions for the current folder
|
||||
let target_dir = format!("list {dir_str}{MAIN_SEPARATOR}");
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
// Create the expected values
|
||||
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")),
|
||||
file(dir.join(".hidden_file")),
|
||||
folder(dir.join(".hidden_folder")),
|
||||
];
|
||||
|
||||
// Match the results
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
|
||||
// Test completions for the current folder even with parts before the autocomplet
|
||||
let target_dir = format!("list somefile.txt {dir_str}{MAIN_SEPARATOR}");
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
// Create the expected values
|
||||
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")),
|
||||
file(dir.join(".hidden_file")),
|
||||
folder(dir.join(".hidden_folder")),
|
||||
];
|
||||
|
||||
// Match the results
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
|
||||
// Test completions for a file
|
||||
let target_dir = format!("list {}", folder(dir.join("another")));
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
// Create the expected values
|
||||
let expected_paths: Vec<String> = vec![file(dir.join("another").join("newfile"))];
|
||||
|
||||
// Match the results
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
|
||||
// Test completions for hidden files
|
||||
let target_dir = format!("list {}", file(dir.join(".hidden_folder").join(".")));
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
let expected_paths: Vec<String> =
|
||||
vec![file(dir.join(".hidden_folder").join(".hidden_subfile"))];
|
||||
|
||||
// Match the results
|
||||
match_suggestions(&expected_paths, &suggestions);
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn file_completions_with_mixed_separators() {
|
||||
@ -889,8 +1020,8 @@ fn subcommand_completions(mut subcommand_completer: NuCompleter) {
|
||||
match_suggestions(
|
||||
&vec![
|
||||
"foo bar".to_string(),
|
||||
"foo aabcrr".to_string(),
|
||||
"foo abaz".to_string(),
|
||||
"foo aabcrr".to_string(),
|
||||
],
|
||||
&suggestions,
|
||||
);
|
||||
@ -942,7 +1073,7 @@ fn flag_completions() {
|
||||
// Test completions for the 'ls' flags
|
||||
let suggestions = completer.complete("ls -", 4);
|
||||
|
||||
assert_eq!(16, suggestions.len());
|
||||
assert_eq!(18, suggestions.len());
|
||||
|
||||
let expected: Vec<String> = vec![
|
||||
"--all".into(),
|
||||
@ -953,14 +1084,16 @@ fn flag_completions() {
|
||||
"--long".into(),
|
||||
"--mime-type".into(),
|
||||
"--short-names".into(),
|
||||
"-D".into(),
|
||||
"--threads".into(),
|
||||
"-a".into(),
|
||||
"-D".into(),
|
||||
"-d".into(),
|
||||
"-f".into(),
|
||||
"-h".into(),
|
||||
"-l".into(),
|
||||
"-m".into(),
|
||||
"-s".into(),
|
||||
"-t".into(),
|
||||
];
|
||||
|
||||
// Match results
|
||||
@ -1194,11 +1327,11 @@ fn folder_with_directorycompletions_do_not_collapse_dots() {
|
||||
#[test]
|
||||
fn variables_completions() {
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Add record value as example
|
||||
let record = "let actor = { name: 'Tom Hardy', age: 44 }";
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
@ -1284,7 +1417,7 @@ fn variables_completions() {
|
||||
assert_eq!(3, suggestions.len());
|
||||
|
||||
#[cfg(windows)]
|
||||
let expected: Vec<String> = vec!["PWD".into(), "Path".into(), "TEST".into()];
|
||||
let expected: Vec<String> = vec!["Path".into(), "PWD".into(), "TEST".into()];
|
||||
#[cfg(not(windows))]
|
||||
let expected: Vec<String> = vec!["PATH".into(), "PWD".into(), "TEST".into()];
|
||||
|
||||
@ -1309,11 +1442,11 @@ fn variables_completions() {
|
||||
|
||||
#[test]
|
||||
fn alias_of_command_and_flags() {
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Create an alias
|
||||
let alias = r#"alias ll = ls -l"#;
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
@ -1328,11 +1461,11 @@ fn alias_of_command_and_flags() {
|
||||
|
||||
#[test]
|
||||
fn alias_of_basic_command() {
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Create an alias
|
||||
let alias = r#"alias ll = ls "#;
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
@ -1347,14 +1480,14 @@ fn alias_of_basic_command() {
|
||||
|
||||
#[test]
|
||||
fn alias_of_another_alias() {
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Create an alias
|
||||
let alias = r#"alias ll = ls -la"#;
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir.clone()).is_ok());
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
// Create the second alias
|
||||
let alias = r#"alias lf = ll -f"#;
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
@ -1371,7 +1504,7 @@ fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> {
|
||||
let completer = format!("$env.config.completions.external.completer = {completer}");
|
||||
|
||||
// Create a new engine
|
||||
let (dir, _, mut engine_state, mut stack) = new_engine();
|
||||
let (_, _, mut engine_state, mut stack) = new_engine();
|
||||
let (block, delta) = {
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
let block = parse(&mut working_set, None, completer.as_bytes(), false);
|
||||
@ -1387,7 +1520,7 @@ fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> {
|
||||
);
|
||||
|
||||
// Merge environment into the permanent state
|
||||
assert!(engine_state.merge_env(&mut stack, &dir).is_ok());
|
||||
assert!(engine_state.merge_env(&mut stack).is_ok());
|
||||
|
||||
// Instantiate a new completer
|
||||
let mut completer = NuCompleter::new(Arc::new(engine_state), Arc::new(stack));
|
||||
@ -1573,14 +1706,31 @@ fn sort_fuzzy_completions_in_alphabetical_order(mut fuzzy_alpha_sort_completer:
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_match() {
|
||||
let (dir, _, engine, stack) = new_partial_engine();
|
||||
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
let target_dir = format!("open {}", folder(dir.join("pArTiAL")));
|
||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||
|
||||
// Since it's an exact match, only 'partial' should be suggested, not
|
||||
// 'partial-a' and stuff. Implemented in #13302
|
||||
match_suggestions(
|
||||
&vec![file(dir.join("partial").join("hello.txt"))],
|
||||
&suggestions,
|
||||
);
|
||||
}
|
||||
|
||||
#[ignore = "was reverted, still needs fixing"]
|
||||
#[rstest]
|
||||
fn alias_offset_bug_7648() {
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Create an alias
|
||||
let alias = r#"alias ea = ^$env.EDITOR /tmp/test.s"#;
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
@ -1595,11 +1745,11 @@ fn alias_offset_bug_7648() {
|
||||
#[ignore = "was reverted, still needs fixing"]
|
||||
#[rstest]
|
||||
fn alias_offset_bug_7754() {
|
||||
let (dir, _, mut engine, mut stack) = new_engine();
|
||||
let (_, _, mut engine, mut stack) = new_engine();
|
||||
|
||||
// Create an alias
|
||||
let alias = r#"alias ll = ls -l"#;
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
|
||||
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack).is_ok());
|
||||
|
||||
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
|
||||
|
||||
@ -1609,13 +1759,3 @@ fn alias_offset_bug_7754() {
|
||||
// This crashes before PR #7756
|
||||
let _suggestions = completer.complete("ll -a | c", 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_path_env_var_8003() {
|
||||
// Create a new engine
|
||||
let (_, _, engine, _) = new_engine();
|
||||
// Get the path env var in a platform agnostic way
|
||||
let the_path = engine.get_path_env_var();
|
||||
// Make sure it's not empty
|
||||
assert!(the_path.is_some());
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ pub fn new_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
|
||||
);
|
||||
|
||||
// Merge environment into the permanent state
|
||||
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
||||
let merge_result = engine_state.merge_env(&mut stack);
|
||||
assert!(merge_result.is_ok());
|
||||
|
||||
(dir, dir_str, engine_state, stack)
|
||||
@ -109,7 +109,7 @@ pub fn new_dotnu_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
|
||||
);
|
||||
|
||||
// Merge environment into the permanent state
|
||||
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
||||
let merge_result = engine_state.merge_env(&mut stack);
|
||||
assert!(merge_result.is_ok());
|
||||
|
||||
(dir, dir_str, engine_state, stack)
|
||||
@ -144,7 +144,7 @@ pub fn new_quote_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
|
||||
);
|
||||
|
||||
// Merge environment into the permanent state
|
||||
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
||||
let merge_result = engine_state.merge_env(&mut stack);
|
||||
assert!(merge_result.is_ok());
|
||||
|
||||
(dir, dir_str, engine_state, stack)
|
||||
@ -179,7 +179,7 @@ pub fn new_partial_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
|
||||
);
|
||||
|
||||
// Merge environment into the permanent state
|
||||
let merge_result = engine_state.merge_env(&mut stack, &dir);
|
||||
let merge_result = engine_state.merge_env(&mut stack);
|
||||
assert!(merge_result.is_ok());
|
||||
|
||||
(dir, dir_str, engine_state, stack)
|
||||
@ -223,7 +223,6 @@ pub fn merge_input(
|
||||
input: &[u8],
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
dir: AbsolutePathBuf,
|
||||
) -> Result<(), ShellError> {
|
||||
let (block, delta) = {
|
||||
let mut working_set = StateWorkingSet::new(engine_state);
|
||||
@ -246,5 +245,5 @@ pub fn merge_input(
|
||||
.is_ok());
|
||||
|
||||
// Merge environment into the permanent state
|
||||
engine_state.merge_env(stack, &dir)
|
||||
engine_state.merge_env(stack)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-cmd-base"
|
||||
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-base"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -13,10 +13,10 @@ version = "0.98.0"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.98.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.98.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0", default-features = false }
|
||||
nu-parser = { path = "../nu-parser", version = "0.101.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.101.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", default-features = false }
|
||||
|
||||
indexmap = { workspace = true }
|
||||
miette = { workspace = true }
|
||||
|
@ -1,4 +1,3 @@
|
||||
use crate::util::get_guaranteed_cwd;
|
||||
use miette::Result;
|
||||
use nu_engine::{eval_block, eval_block_with_early_return};
|
||||
use nu_parser::parse;
|
||||
@ -8,54 +7,55 @@ use nu_protocol::{
|
||||
engine::{Closure, EngineState, Stack, StateWorkingSet},
|
||||
PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
pub fn eval_env_change_hook(
|
||||
env_change_hook: Option<Value>,
|
||||
env_change_hook: &HashMap<String, Vec<Value>>,
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
) -> Result<(), ShellError> {
|
||||
if let Some(hook) = env_change_hook {
|
||||
match hook {
|
||||
Value::Record { val, .. } => {
|
||||
for (env_name, hook_value) in &*val {
|
||||
let before = engine_state
|
||||
.previous_env_vars
|
||||
.get(env_name)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
for (env, hooks) in env_change_hook {
|
||||
let before = engine_state.previous_env_vars.get(env);
|
||||
let after = stack.get_env_var(engine_state, env);
|
||||
if before != after {
|
||||
let before = before.cloned().unwrap_or_default();
|
||||
let after = after.cloned().unwrap_or_default();
|
||||
|
||||
let after = stack
|
||||
.get_env_var(engine_state, env_name)
|
||||
.unwrap_or_default();
|
||||
eval_hooks(
|
||||
engine_state,
|
||||
stack,
|
||||
vec![("$before".into(), before), ("$after".into(), after.clone())],
|
||||
hooks,
|
||||
"env_change",
|
||||
)?;
|
||||
|
||||
if before != after {
|
||||
eval_hook(
|
||||
engine_state,
|
||||
stack,
|
||||
None,
|
||||
vec![("$before".into(), before), ("$after".into(), after.clone())],
|
||||
hook_value,
|
||||
"env_change",
|
||||
)?;
|
||||
|
||||
Arc::make_mut(&mut engine_state.previous_env_vars)
|
||||
.insert(env_name.to_string(), after);
|
||||
}
|
||||
}
|
||||
}
|
||||
x => {
|
||||
return Err(ShellError::TypeMismatch {
|
||||
err_message: "record for the 'env_change' hook".to_string(),
|
||||
span: x.span(),
|
||||
});
|
||||
}
|
||||
Arc::make_mut(&mut engine_state.previous_env_vars).insert(env.clone(), after);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn eval_hooks(
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
arguments: Vec<(String, Value)>,
|
||||
hooks: &[Value],
|
||||
hook_name: &str,
|
||||
) -> Result<(), ShellError> {
|
||||
for hook in hooks {
|
||||
eval_hook(
|
||||
engine_state,
|
||||
stack,
|
||||
None,
|
||||
arguments.clone(),
|
||||
hook,
|
||||
&format!("{hook_name} list, recursive"),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn eval_hook(
|
||||
engine_state: &mut EngineState,
|
||||
stack: &mut Stack,
|
||||
@ -92,11 +92,12 @@ pub fn eval_hook(
|
||||
);
|
||||
if let Some(err) = working_set.parse_errors.first() {
|
||||
report_parse_error(&working_set, err);
|
||||
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "valid source code".into(),
|
||||
value: "source code with syntax errors".into(),
|
||||
span,
|
||||
return Err(ShellError::GenericError {
|
||||
error: format!("Failed to run {hook_name} hook"),
|
||||
msg: "source code has errors".into(),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: Vec::new(),
|
||||
});
|
||||
}
|
||||
|
||||
@ -132,16 +133,7 @@ pub fn eval_hook(
|
||||
}
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
for val in vals {
|
||||
eval_hook(
|
||||
engine_state,
|
||||
stack,
|
||||
None,
|
||||
arguments.clone(),
|
||||
val,
|
||||
&format!("{hook_name} list, recursive"),
|
||||
)?;
|
||||
}
|
||||
eval_hooks(engine_state, stack, arguments, vals, hook_name)?;
|
||||
}
|
||||
Value::Record { val, .. } => {
|
||||
// Hooks can optionally be a record in this form:
|
||||
@ -167,10 +159,10 @@ pub fn eval_hook(
|
||||
{
|
||||
val
|
||||
} else {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "boolean output".to_string(),
|
||||
value: "other PipelineData variant".to_string(),
|
||||
span: other_span,
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::Bool,
|
||||
actual: pipeline_data.get_type(),
|
||||
span: pipeline_data.span().unwrap_or(other_span),
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -179,9 +171,9 @@ pub fn eval_hook(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "block".to_string(),
|
||||
value: format!("{}", condition.get_type()),
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::Closure,
|
||||
actual: condition.get_type(),
|
||||
span: other_span,
|
||||
});
|
||||
}
|
||||
@ -224,11 +216,12 @@ pub fn eval_hook(
|
||||
);
|
||||
if let Some(err) = working_set.parse_errors.first() {
|
||||
report_parse_error(&working_set, err);
|
||||
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "valid source code".into(),
|
||||
value: "source code with syntax errors".into(),
|
||||
span: source_span,
|
||||
return Err(ShellError::GenericError {
|
||||
error: format!("Failed to run {hook_name} hook"),
|
||||
msg: "source code has errors".into(),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: Vec::new(),
|
||||
});
|
||||
}
|
||||
|
||||
@ -263,9 +256,9 @@ pub fn eval_hook(
|
||||
run_hook(engine_state, stack, val, input, arguments, source_span)?;
|
||||
}
|
||||
other => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "block or string".to_string(),
|
||||
value: format!("{}", other.get_type()),
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("string or closure"),
|
||||
actual: other.get_type(),
|
||||
span: source_span,
|
||||
});
|
||||
}
|
||||
@ -276,16 +269,15 @@ pub fn eval_hook(
|
||||
output = run_hook(engine_state, stack, val, input, arguments, span)?;
|
||||
}
|
||||
other => {
|
||||
return Err(ShellError::UnsupportedConfigValue {
|
||||
expected: "string, block, record, or list of commands".into(),
|
||||
value: format!("{}", other.get_type()),
|
||||
return Err(ShellError::RuntimeTypeMismatch {
|
||||
expected: Type::custom("string, closure, record, or list"),
|
||||
actual: other.get_type(),
|
||||
span: other.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let cwd = get_guaranteed_cwd(engine_state, stack);
|
||||
engine_state.merge_env(stack, cwd)?;
|
||||
engine_state.merge_env(stack)?;
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
@ -1,30 +1,9 @@
|
||||
use nu_path::AbsolutePathBuf;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack},
|
||||
Range, ShellError, Span, Value,
|
||||
};
|
||||
use std::ops::Bound;
|
||||
|
||||
pub fn get_init_cwd() -> AbsolutePathBuf {
|
||||
std::env::current_dir()
|
||||
.ok()
|
||||
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
|
||||
.or_else(|| {
|
||||
std::env::var("PWD")
|
||||
.ok()
|
||||
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
|
||||
})
|
||||
.or_else(nu_path::home_dir)
|
||||
.expect("Failed to get current working directory")
|
||||
}
|
||||
|
||||
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> AbsolutePathBuf {
|
||||
engine_state
|
||||
.cwd(Some(stack))
|
||||
.ok()
|
||||
.unwrap_or_else(get_init_cwd)
|
||||
}
|
||||
|
||||
type MakeRangeError = fn(&str, Span) -> ShellError;
|
||||
|
||||
/// Returns a inclusive pair of boundary in given `range`.
|
||||
@ -99,10 +78,10 @@ pub fn get_editor(
|
||||
get_editor_commandline(&config.buffer_editor, "$env.config.buffer_editor")
|
||||
{
|
||||
Ok(buff_editor)
|
||||
} else if let Some(value) = env_vars.get("EDITOR") {
|
||||
get_editor_commandline(value, "$env.EDITOR")
|
||||
} else if let Some(value) = env_vars.get("VISUAL") {
|
||||
get_editor_commandline(value, "$env.VISUAL")
|
||||
} else if let Some(value) = env_vars.get("EDITOR") {
|
||||
get_editor_commandline(value, "$env.EDITOR")
|
||||
} else {
|
||||
Err(ShellError::GenericError {
|
||||
error: "No editor configured".into(),
|
||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-cmd-extra"
|
||||
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-extra"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -16,13 +16,13 @@ bench = false
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-json = { version = "0.98.0", path = "../nu-json" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.98.0" }
|
||||
nu-pretty-hex = { version = "0.98.0", path = "../nu-pretty-hex" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.98.0" }
|
||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.101.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0", default-features = false }
|
||||
nu-json = { version = "0.101.0", path = "../nu-json" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.101.0" }
|
||||
nu-pretty-hex = { version = "0.101.0", path = "../nu-pretty-hex" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", default-features = false }
|
||||
nu-utils = { path = "../nu-utils", version = "0.101.0", default-features = false }
|
||||
|
||||
# Potential dependencies for extras
|
||||
heck = { workspace = true }
|
||||
@ -36,6 +36,6 @@ v_htmlescape = { workspace = true }
|
||||
itertools = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.98.0" }
|
||||
nu-command = { path = "../nu-command", version = "0.98.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.98.0" }
|
||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.101.0" }
|
||||
nu-command = { path = "../nu-command", version = "0.101.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.101.0" }
|
@ -203,7 +203,7 @@ pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value {
|
||||
Value::string(raw_string.trim(), span)
|
||||
}
|
||||
Value::Int { val, .. } => convert_to_smallest_number_type(*val, span),
|
||||
Value::Filesize { val, .. } => convert_to_smallest_number_type(*val, span),
|
||||
Value::Filesize { val, .. } => convert_to_smallest_number_type(val.get(), span),
|
||||
Value::Duration { val, .. } => convert_to_smallest_number_type(*val, span),
|
||||
Value::String { val, .. } => {
|
||||
let raw_bytes = val.as_bytes();
|
||||
|
@ -66,7 +66,7 @@ fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
match input {
|
||||
Value::Float { val, .. } => fmt_it_64(*val, span),
|
||||
Value::Int { val, .. } => fmt_it(*val, span),
|
||||
Value::Filesize { val, .. } => fmt_it(*val, span),
|
||||
Value::Filesize { val, .. } => fmt_it(val.get(), span),
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
Value::Error { .. } => input.clone(),
|
||||
other => Value::error(
|
||||
|
@ -25,7 +25,7 @@ impl Command for EachWhile {
|
||||
)])
|
||||
.required(
|
||||
"closure",
|
||||
SyntaxShape::Closure(Some(vec![SyntaxShape::Any, SyntaxShape::Int])),
|
||||
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
|
||||
"the closure to run",
|
||||
)
|
||||
.category(Category::Filters)
|
||||
|
@ -2,4 +2,4 @@ mod from;
|
||||
mod to;
|
||||
|
||||
pub(crate) use from::url::FromUrl;
|
||||
pub(crate) use to::html::ToHtml;
|
||||
pub use to::html::ToHtml;
|
||||
|
@ -9,6 +9,7 @@ mod strings;
|
||||
pub use bits::{
|
||||
Bits, BitsAnd, BitsInto, BitsNot, BitsOr, BitsRol, BitsRor, BitsShl, BitsShr, BitsXor,
|
||||
};
|
||||
pub use formats::ToHtml;
|
||||
pub use math::{MathArcCos, MathArcCosH, MathArcSin, MathArcSinH, MathArcTan, MathArcTanH};
|
||||
pub use math::{MathCos, MathCosH, MathSin, MathSinH, MathTan, MathTanH};
|
||||
pub use math::{MathExp, MathLn};
|
||||
@ -54,7 +55,8 @@ pub fn add_extra_command_context(mut engine_state: EngineState) -> EngineState {
|
||||
strings::str_::case::StrTitleCase
|
||||
);
|
||||
|
||||
bind_command!(formats::ToHtml, formats::FromUrl);
|
||||
bind_command!(ToHtml, formats::FromUrl);
|
||||
|
||||
// Bits
|
||||
bind_command! {
|
||||
Bits,
|
||||
|
@ -6,7 +6,7 @@ repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-lang"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-cmd-lang"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
@ -15,18 +15,29 @@ bench = false
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.98.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0", default-features = false }
|
||||
nu-parser = { path = "../nu-parser", version = "0.101.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", default-features = false }
|
||||
nu-utils = { path = "../nu-utils", version = "0.101.0", default-features = false }
|
||||
|
||||
itertools = { workspace = true }
|
||||
shadow-rs = { version = "0.34", default-features = false }
|
||||
shadow-rs = { version = "0.37", default-features = false }
|
||||
|
||||
[build-dependencies]
|
||||
shadow-rs = { version = "0.34", default-features = false }
|
||||
shadow-rs = { version = "0.37", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["os"]
|
||||
os = [
|
||||
"nu-engine/os",
|
||||
"nu-protocol/os",
|
||||
"nu-utils/os",
|
||||
]
|
||||
plugin = [
|
||||
"nu-protocol/plugin",
|
||||
"os",
|
||||
]
|
||||
|
||||
mimalloc = []
|
||||
trash-support = []
|
||||
sqlite = []
|
||||
|
@ -1,12 +1,13 @@
|
||||
use std::process::Command;
|
||||
|
||||
fn main() -> shadow_rs::SdResult<()> {
|
||||
fn main() {
|
||||
// Look up the current Git commit ourselves instead of relying on shadow_rs,
|
||||
// because shadow_rs does it in a really slow-to-compile way (it builds libgit2)
|
||||
let hash = get_git_hash().unwrap_or_default();
|
||||
println!("cargo:rustc-env=NU_COMMIT_HASH={hash}");
|
||||
|
||||
shadow_rs::new()
|
||||
shadow_rs::ShadowBuilder::builder()
|
||||
.build()
|
||||
.expect("shadow builder build should success");
|
||||
}
|
||||
|
||||
fn get_git_hash() -> Option<String> {
|
||||
|
@ -169,6 +169,7 @@ fn run(
|
||||
let origin = match stream.source() {
|
||||
ByteStreamSource::Read(_) => "unknown",
|
||||
ByteStreamSource::File(_) => "file",
|
||||
#[cfg(feature = "os")]
|
||||
ByteStreamSource::Child(_) => "external",
|
||||
};
|
||||
|
||||
|
@ -1,9 +1,8 @@
|
||||
use nu_engine::{command_prelude::*, get_eval_block_with_early_return, redirect_env};
|
||||
use nu_protocol::{
|
||||
engine::Closure,
|
||||
process::{ChildPipe, ChildProcess},
|
||||
ByteStream, ByteStreamSource, OutDest,
|
||||
};
|
||||
#[cfg(feature = "os")]
|
||||
use nu_protocol::process::{ChildPipe, ChildProcess};
|
||||
use nu_protocol::{engine::Closure, ByteStream, ByteStreamSource, OutDest};
|
||||
|
||||
use std::{
|
||||
io::{Cursor, Read},
|
||||
thread,
|
||||
@ -69,6 +68,33 @@ impl Command for Do {
|
||||
let block: Closure = call.req(engine_state, caller_stack, 0)?;
|
||||
let rest: Vec<Value> = call.rest(engine_state, caller_stack, 1)?;
|
||||
let ignore_all_errors = call.has_flag(engine_state, caller_stack, "ignore-errors")?;
|
||||
|
||||
if call.has_flag(engine_state, caller_stack, "ignore-shell-errors")? {
|
||||
nu_protocol::report_shell_warning(
|
||||
engine_state,
|
||||
&ShellError::GenericError {
|
||||
error: "Deprecated option".into(),
|
||||
msg: "`--ignore-shell-errors` is deprecated and will be removed in 0.102.0."
|
||||
.into(),
|
||||
span: Some(call.head),
|
||||
help: Some("Please use the `--ignore-errors(-i)`".into()),
|
||||
inner: vec![],
|
||||
},
|
||||
);
|
||||
}
|
||||
if call.has_flag(engine_state, caller_stack, "ignore-program-errors")? {
|
||||
nu_protocol::report_shell_warning(
|
||||
engine_state,
|
||||
&ShellError::GenericError {
|
||||
error: "Deprecated option".into(),
|
||||
msg: "`--ignore-program-errors` is deprecated and will be removed in 0.102.0."
|
||||
.into(),
|
||||
span: Some(call.head),
|
||||
help: Some("Please use the `--ignore-errors(-i)`".into()),
|
||||
inner: vec![],
|
||||
},
|
||||
);
|
||||
}
|
||||
let ignore_shell_errors = ignore_all_errors
|
||||
|| call.has_flag(engine_state, caller_stack, "ignore-shell-errors")?;
|
||||
let ignore_program_errors = ignore_all_errors
|
||||
@ -82,9 +108,6 @@ impl Command for Do {
|
||||
bind_args_to(&mut callee_stack, &block.signature, rest, head)?;
|
||||
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
|
||||
|
||||
// Applies to all block evaluation once set true
|
||||
callee_stack.use_ir = !caller_stack.has_env_var(engine_state, "NU_DISABLE_IR");
|
||||
|
||||
let result = eval_block_with_early_return(engine_state, &mut callee_stack, block, input);
|
||||
|
||||
if has_env {
|
||||
@ -95,6 +118,13 @@ impl Command for Do {
|
||||
match result {
|
||||
Ok(PipelineData::ByteStream(stream, metadata)) if capture_errors => {
|
||||
let span = stream.span();
|
||||
#[cfg(not(feature = "os"))]
|
||||
return Err(ShellError::DisabledOsSupport {
|
||||
msg: "Cannot create a thread to receive stdout message.".to_string(),
|
||||
span: Some(span),
|
||||
});
|
||||
|
||||
#[cfg(feature = "os")]
|
||||
match stream.into_child() {
|
||||
Ok(mut child) => {
|
||||
// Use a thread to receive stdout message.
|
||||
@ -147,6 +177,7 @@ impl Command for Do {
|
||||
None
|
||||
};
|
||||
|
||||
child.ignore_error(false);
|
||||
child.wait()?;
|
||||
|
||||
let mut child = ChildProcess::from_raw(None, None, None, span);
|
||||
@ -166,10 +197,14 @@ impl Command for Do {
|
||||
}
|
||||
Ok(PipelineData::ByteStream(mut stream, metadata))
|
||||
if ignore_program_errors
|
||||
&& !matches!(caller_stack.stdout(), OutDest::Pipe | OutDest::Capture) =>
|
||||
&& !matches!(
|
||||
caller_stack.stdout(),
|
||||
OutDest::Pipe | OutDest::PipeSeparate | OutDest::Value
|
||||
) =>
|
||||
{
|
||||
#[cfg(feature = "os")]
|
||||
if let ByteStreamSource::Child(child) = stream.source_mut() {
|
||||
child.set_exit_code(0)
|
||||
child.ignore_error(true);
|
||||
}
|
||||
Ok(PipelineData::ByteStream(stream, metadata))
|
||||
}
|
||||
@ -207,16 +242,6 @@ impl Command for Do {
|
||||
example: r#"do --ignore-errors { thisisnotarealcommand }"#,
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Run the closure and ignore shell errors",
|
||||
example: r#"do --ignore-shell-errors { thisisnotarealcommand }"#,
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Run the closure and ignore external program errors",
|
||||
example: r#"do --ignore-program-errors { nu --commands 'exit 1' }; echo "I'll still run""#,
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Abort the pipeline if a program returns a non-zero exit code",
|
||||
example: r#"do --capture-errors { nu --commands 'exit 1' } | myscarycommand"#,
|
||||
|
@ -63,7 +63,7 @@ impl Command for HideEnv {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Hide an environment variable",
|
||||
example: r#"$env.HZ_ENV_ABC = 1; hide-env HZ_ENV_ABC; 'HZ_ENV_ABC' in (env).name"#,
|
||||
example: r#"$env.HZ_ENV_ABC = 1; hide-env HZ_ENV_ABC; 'HZ_ENV_ABC' in $env"#,
|
||||
result: Some(Value::test_bool(false)),
|
||||
}]
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{engine::StateWorkingSet, OutDest};
|
||||
use nu_protocol::{engine::StateWorkingSet, ByteStreamSource, OutDest};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Ignore;
|
||||
@ -32,8 +32,14 @@ impl Command for Ignore {
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
input: PipelineData,
|
||||
mut input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
if let PipelineData::ByteStream(stream, _) = &mut input {
|
||||
#[cfg(feature = "os")]
|
||||
if let ByteStreamSource::Child(child) = stream.source_mut() {
|
||||
child.ignore_error(true);
|
||||
}
|
||||
}
|
||||
input.drain()?;
|
||||
Ok(PipelineData::empty())
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ impl Command for Let {
|
||||
|
||||
let block = engine_state.get_block(block_id);
|
||||
let eval_block = get_eval_block(engine_state);
|
||||
let stack = &mut stack.start_capture();
|
||||
let stack = &mut stack.start_collect_value();
|
||||
let pipeline_data = eval_block(engine_state, stack, block, input)?;
|
||||
let value = pipeline_data.into_value(call.head)?;
|
||||
|
||||
|
@ -63,7 +63,7 @@ impl Command for Mut {
|
||||
|
||||
let block = engine_state.get_block(block_id);
|
||||
let eval_block = get_eval_block(engine_state);
|
||||
let stack = &mut stack.start_capture();
|
||||
let stack = &mut stack.start_collect_value();
|
||||
let pipeline_data = eval_block(engine_state, stack, block, input)?;
|
||||
let value = pipeline_data.into_value(call.head)?;
|
||||
|
||||
|
@ -2,7 +2,7 @@ use nu_engine::{
|
||||
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env,
|
||||
};
|
||||
use nu_parser::trim_quotes_str;
|
||||
use nu_protocol::{ast::Expr, engine::CommandType};
|
||||
use nu_protocol::{ast::Expr, engine::CommandType, ModuleId};
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
@ -64,7 +64,7 @@ impl Command for OverlayUse {
|
||||
let mut name_arg: Spanned<String> = call.req(engine_state, caller_stack, 0)?;
|
||||
name_arg.item = trim_quotes_str(&name_arg.item).to_string();
|
||||
|
||||
let maybe_origin_module_id =
|
||||
let maybe_origin_module_id: Option<ModuleId> =
|
||||
if let Some(overlay_expr) = call.get_parser_info(caller_stack, "overlay_expr") {
|
||||
if let Expr::Overlay(module_id) = &overlay_expr.expr {
|
||||
*module_id
|
||||
|
@ -63,7 +63,7 @@ impl Command for Try {
|
||||
let eval_block = get_eval_block(engine_state);
|
||||
|
||||
let result = eval_block(engine_state, stack, try_block, input)
|
||||
.and_then(|pipeline| pipeline.write_to_out_dests(engine_state, stack));
|
||||
.and_then(|pipeline| pipeline.drain_to_out_dests(engine_state, stack));
|
||||
|
||||
match result {
|
||||
Err(err) => run_catch(err, head, catch_block, engine_state, stack, eval_block),
|
||||
@ -107,7 +107,7 @@ fn run_catch(
|
||||
|
||||
if let Some(catch) = catch {
|
||||
stack.set_last_error(&error);
|
||||
let error = error.into_value(span);
|
||||
let error = error.into_value(&StateWorkingSet::new(engine_state), span);
|
||||
let block = engine_state.get_block(catch.block_id);
|
||||
// Put the error value in the positional closure var
|
||||
if let Some(var) = block.signature.get_positional(0) {
|
||||
|
@ -98,15 +98,21 @@ This command is a parser keyword. For details, check:
|
||||
engine_state.get_span_contents(import_pattern.head.span),
|
||||
);
|
||||
|
||||
let maybe_file_path = find_in_dirs_env(
|
||||
let maybe_file_path_or_dir = find_in_dirs_env(
|
||||
&module_arg_str,
|
||||
engine_state,
|
||||
caller_stack,
|
||||
get_dirs_var_from_call(caller_stack, call),
|
||||
)?;
|
||||
let maybe_parent = maybe_file_path
|
||||
.as_ref()
|
||||
.and_then(|path| path.parent().map(|p| p.to_path_buf()));
|
||||
// module_arg_str maybe a directory, in this case
|
||||
// find_in_dirs_env returns a directory.
|
||||
let maybe_parent = maybe_file_path_or_dir.as_ref().and_then(|path| {
|
||||
if path.is_dir() {
|
||||
Some(path.to_path_buf())
|
||||
} else {
|
||||
path.parent().map(|p| p.to_path_buf())
|
||||
}
|
||||
});
|
||||
|
||||
let mut callee_stack = caller_stack
|
||||
.gather_captures(engine_state, &block.captures)
|
||||
@ -118,9 +124,15 @@ This command is a parser keyword. For details, check:
|
||||
callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||
}
|
||||
|
||||
if let Some(file_path) = maybe_file_path {
|
||||
let file_path = Value::string(file_path.to_string_lossy(), call.head);
|
||||
callee_stack.add_env_var("CURRENT_FILE".to_string(), file_path);
|
||||
if let Some(path) = maybe_file_path_or_dir {
|
||||
let module_file_path = if path.is_dir() {
|
||||
// the existence of `mod.nu` is verified in parsing time
|
||||
// so it's safe to use it here.
|
||||
Value::string(path.join("mod.nu").to_string_lossy(), call.head)
|
||||
} else {
|
||||
Value::string(path.to_string_lossy(), call.head)
|
||||
};
|
||||
callee_stack.add_env_var("CURRENT_FILE".to_string(), module_file_path);
|
||||
}
|
||||
|
||||
let eval_block = get_eval_block(engine_state);
|
||||
|
@ -116,24 +116,30 @@ pub fn version(engine_state: &EngineState, span: Span) -> Result<PipelineData, S
|
||||
Value::string(features_enabled().join(", "), span),
|
||||
);
|
||||
|
||||
// Get a list of plugin names and versions if present
|
||||
let installed_plugins = engine_state
|
||||
.plugins()
|
||||
.iter()
|
||||
.map(|x| {
|
||||
let name = x.identity().name();
|
||||
if let Some(version) = x.metadata().and_then(|m| m.version) {
|
||||
format!("{name} {version}")
|
||||
} else {
|
||||
name.into()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
#[cfg(not(feature = "plugin"))]
|
||||
let _ = engine_state;
|
||||
|
||||
record.push(
|
||||
"installed_plugins",
|
||||
Value::string(installed_plugins.join(", "), span),
|
||||
);
|
||||
#[cfg(feature = "plugin")]
|
||||
{
|
||||
// Get a list of plugin names and versions if present
|
||||
let installed_plugins = engine_state
|
||||
.plugins()
|
||||
.iter()
|
||||
.map(|x| {
|
||||
let name = x.identity().name();
|
||||
if let Some(version) = x.metadata().and_then(|m| m.version) {
|
||||
format!("{name} {version}")
|
||||
} else {
|
||||
name.into()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
record.push(
|
||||
"installed_plugins",
|
||||
Value::string(installed_plugins.join(", "), span),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(Value::record(record, span).into_pipeline_data())
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ pub fn eval_block(
|
||||
cwd: &std::path::Path,
|
||||
engine_state: &EngineState,
|
||||
) -> Value {
|
||||
let mut stack = Stack::new().capture();
|
||||
let mut stack = Stack::new().collect_value();
|
||||
|
||||
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
|
||||
|
||||
@ -143,13 +143,13 @@ pub fn check_example_evaluates_to_expected_output(
|
||||
cwd: &std::path::Path,
|
||||
engine_state: &mut Box<EngineState>,
|
||||
) {
|
||||
let mut stack = Stack::new().capture();
|
||||
let mut stack = Stack::new().collect_value();
|
||||
|
||||
// Set up PWD
|
||||
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
|
||||
|
||||
engine_state
|
||||
.merge_env(&mut stack, cwd)
|
||||
.merge_env(&mut stack)
|
||||
.expect("Error merging environment");
|
||||
|
||||
let empty_input = PipelineData::empty();
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![cfg_attr(not(feature = "os"), allow(unused))]
|
||||
#![doc = include_str!("../README.md")]
|
||||
mod core_commands;
|
||||
mod default_context;
|
||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-cmd-plugin"
|
||||
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-plugin"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -13,10 +13,10 @@ version = "0.98.0"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.98.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0", features = ["plugin"] }
|
||||
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.101.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", features = ["plugin"] }
|
||||
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.101.0" }
|
||||
|
||||
itertools = { workspace = true }
|
||||
|
||||
|
@ -1,10 +1,9 @@
|
||||
use crate::util::{get_plugin_dirs, modify_plugin_file};
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_plugin_engine::{GetPlugin, PersistentPlugin};
|
||||
use nu_protocol::{PluginGcConfig, PluginIdentity, PluginRegistryItem, RegisteredPlugin};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::util::{get_plugin_dirs, modify_plugin_file};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PluginAdd;
|
||||
|
||||
@ -31,7 +30,7 @@ impl Command for PluginAdd {
|
||||
)
|
||||
.required(
|
||||
"filename",
|
||||
SyntaxShape::Filepath,
|
||||
SyntaxShape::String,
|
||||
"Path to the executable for the plugin",
|
||||
)
|
||||
.category(Category::Plugin)
|
||||
@ -81,7 +80,6 @@ apparent the next time `nu` is next launched with that plugin registry file.
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let filename: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let shell: Option<Spanned<String>> = call.get_flag(engine_state, stack, "shell")?;
|
||||
|
||||
let cwd = engine_state.cwd(Some(stack))?;
|
||||
|
||||
// Check the current directory, or fall back to NU_PLUGIN_DIRS
|
||||
@ -121,7 +119,7 @@ apparent the next time `nu` is next launched with that plugin registry file.
|
||||
let metadata = interface.get_metadata()?;
|
||||
let commands = interface.get_signature()?;
|
||||
|
||||
modify_plugin_file(engine_state, stack, call.head, custom_path, |contents| {
|
||||
modify_plugin_file(engine_state, stack, call.head, &custom_path, |contents| {
|
||||
// Update the file with the received metadata and signatures
|
||||
let item = PluginRegistryItem::new(plugin.identity(), metadata, commands);
|
||||
contents.upsert_plugin(item);
|
||||
|
@ -1,5 +1,8 @@
|
||||
use itertools::Itertools;
|
||||
use itertools::{EitherOrBoth, Itertools};
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{IntoValue, PluginRegistryItemData};
|
||||
|
||||
use crate::util::read_plugin_file;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PluginList;
|
||||
@ -17,7 +20,7 @@ impl Command for PluginList {
|
||||
[
|
||||
("name".into(), Type::String),
|
||||
("version".into(), Type::String),
|
||||
("is_running".into(), Type::Bool),
|
||||
("status".into(), Type::String),
|
||||
("pid".into(), Type::Int),
|
||||
("filename".into(), Type::String),
|
||||
("shell".into(), Type::String),
|
||||
@ -26,11 +29,54 @@ impl Command for PluginList {
|
||||
.into(),
|
||||
),
|
||||
)
|
||||
.named(
|
||||
"plugin-config",
|
||||
SyntaxShape::Filepath,
|
||||
"Use a plugin registry file other than the one set in `$nu.plugin-path`",
|
||||
None,
|
||||
)
|
||||
.switch(
|
||||
"engine",
|
||||
"Show info for plugins that are loaded into the engine only.",
|
||||
Some('e'),
|
||||
)
|
||||
.switch(
|
||||
"registry",
|
||||
"Show info for plugins from the registry file only.",
|
||||
Some('r'),
|
||||
)
|
||||
.category(Category::Plugin)
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"List installed plugins."
|
||||
"List loaded and installed plugins."
|
||||
}
|
||||
|
||||
fn extra_description(&self) -> &str {
|
||||
r#"
|
||||
The `status` column will contain one of the following values:
|
||||
|
||||
- `added`: The plugin is present in the plugin registry file, but not in
|
||||
the engine.
|
||||
- `loaded`: The plugin is present both in the plugin registry file and in
|
||||
the engine, but is not running.
|
||||
- `running`: The plugin is currently running, and the `pid` column should
|
||||
contain its process ID.
|
||||
- `modified`: The plugin state present in the plugin registry file is different
|
||||
from the state in the engine.
|
||||
- `removed`: The plugin is still loaded in the engine, but is not present in
|
||||
the plugin registry file.
|
||||
- `invalid`: The data in the plugin registry file couldn't be deserialized,
|
||||
and the plugin most likely needs to be added again.
|
||||
|
||||
`running` takes priority over any other status. Unless `--registry` is used
|
||||
or the plugin has not been loaded yet, the values of `version`, `filename`,
|
||||
`shell`, and `commands` reflect the values in the engine and not the ones in
|
||||
the plugin registry file.
|
||||
|
||||
See also: `plugin use`
|
||||
"#
|
||||
.trim()
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
@ -45,7 +91,7 @@ impl Command for PluginList {
|
||||
result: Some(Value::test_list(vec![Value::test_record(record! {
|
||||
"name" => Value::test_string("inc"),
|
||||
"version" => Value::test_string(env!("CARGO_PKG_VERSION")),
|
||||
"is_running" => Value::test_bool(true),
|
||||
"status" => Value::test_string("running"),
|
||||
"pid" => Value::test_int(106480),
|
||||
"filename" => if cfg!(windows) {
|
||||
Value::test_string(r"C:\nu\plugins\nu_plugin_inc.exe")
|
||||
@ -67,58 +113,189 @@ impl Command for PluginList {
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
let custom_path = call.get_flag(engine_state, stack, "plugin-config")?;
|
||||
let engine_mode = call.has_flag(engine_state, stack, "engine")?;
|
||||
let registry_mode = call.has_flag(engine_state, stack, "registry")?;
|
||||
|
||||
// Group plugin decls by plugin identity
|
||||
let decls = engine_state.plugin_decls().into_group_map_by(|decl| {
|
||||
decl.plugin_identity()
|
||||
.expect("plugin decl should have identity")
|
||||
});
|
||||
let plugins_info = match (engine_mode, registry_mode) {
|
||||
// --engine and --registry together is equivalent to the default.
|
||||
(false, false) | (true, true) => {
|
||||
if engine_state.plugin_path.is_some() || custom_path.is_some() {
|
||||
let plugins_in_engine = get_plugins_in_engine(engine_state);
|
||||
let plugins_in_registry =
|
||||
get_plugins_in_registry(engine_state, stack, call.head, &custom_path)?;
|
||||
merge_plugin_info(plugins_in_engine, plugins_in_registry)
|
||||
} else {
|
||||
// Don't produce error when running nu --no-config-file
|
||||
get_plugins_in_engine(engine_state)
|
||||
}
|
||||
}
|
||||
(true, false) => get_plugins_in_engine(engine_state),
|
||||
(false, true) => get_plugins_in_registry(engine_state, stack, call.head, &custom_path)?,
|
||||
};
|
||||
|
||||
// Build plugins list
|
||||
let list = engine_state.plugins().iter().map(|plugin| {
|
||||
// Find commands that belong to the plugin
|
||||
let commands = decls.get(plugin.identity())
|
||||
.into_iter()
|
||||
.flat_map(|decls| {
|
||||
decls.iter().map(|decl| Value::string(decl.name(), head))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let pid = plugin
|
||||
.pid()
|
||||
.map(|p| Value::int(p as i64, head))
|
||||
.unwrap_or(Value::nothing(head));
|
||||
|
||||
let shell = plugin
|
||||
.identity()
|
||||
.shell()
|
||||
.map(|s| Value::string(s.to_string_lossy(), head))
|
||||
.unwrap_or(Value::nothing(head));
|
||||
|
||||
let metadata = plugin.metadata();
|
||||
let version = metadata
|
||||
.and_then(|m| m.version)
|
||||
.map(|s| Value::string(s, head))
|
||||
.unwrap_or(Value::nothing(head));
|
||||
|
||||
let record = record! {
|
||||
"name" => Value::string(plugin.identity().name(), head),
|
||||
"version" => version,
|
||||
"is_running" => Value::bool(plugin.is_running(), head),
|
||||
"pid" => pid,
|
||||
"filename" => Value::string(plugin.identity().filename().to_string_lossy(), head),
|
||||
"shell" => shell,
|
||||
"commands" => Value::list(commands, head),
|
||||
};
|
||||
|
||||
Value::record(record, head)
|
||||
}).collect();
|
||||
|
||||
Ok(Value::list(list, head).into_pipeline_data())
|
||||
Ok(plugins_info.into_value(call.head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, IntoValue, PartialOrd, Ord, PartialEq, Eq)]
|
||||
struct PluginInfo {
|
||||
name: String,
|
||||
version: Option<String>,
|
||||
status: PluginStatus,
|
||||
pid: Option<u32>,
|
||||
filename: String,
|
||||
shell: Option<String>,
|
||||
commands: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, IntoValue, PartialOrd, Ord, PartialEq, Eq)]
|
||||
#[nu_value(rename_all = "snake_case")]
|
||||
enum PluginStatus {
|
||||
Added,
|
||||
Loaded,
|
||||
Running,
|
||||
Modified,
|
||||
Removed,
|
||||
Invalid,
|
||||
}
|
||||
|
||||
fn get_plugins_in_engine(engine_state: &EngineState) -> Vec<PluginInfo> {
|
||||
// Group plugin decls by plugin identity
|
||||
let decls = engine_state.plugin_decls().into_group_map_by(|decl| {
|
||||
decl.plugin_identity()
|
||||
.expect("plugin decl should have identity")
|
||||
});
|
||||
|
||||
// Build plugins list
|
||||
engine_state
|
||||
.plugins()
|
||||
.iter()
|
||||
.map(|plugin| {
|
||||
// Find commands that belong to the plugin
|
||||
let commands = decls
|
||||
.get(plugin.identity())
|
||||
.into_iter()
|
||||
.flat_map(|decls| decls.iter().map(|decl| decl.name().to_owned()))
|
||||
.sorted()
|
||||
.collect();
|
||||
|
||||
PluginInfo {
|
||||
name: plugin.identity().name().into(),
|
||||
version: plugin.metadata().and_then(|m| m.version),
|
||||
status: if plugin.pid().is_some() {
|
||||
PluginStatus::Running
|
||||
} else {
|
||||
PluginStatus::Loaded
|
||||
},
|
||||
pid: plugin.pid(),
|
||||
filename: plugin.identity().filename().to_string_lossy().into_owned(),
|
||||
shell: plugin
|
||||
.identity()
|
||||
.shell()
|
||||
.map(|path| path.to_string_lossy().into_owned()),
|
||||
commands,
|
||||
}
|
||||
})
|
||||
.sorted()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_plugins_in_registry(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
span: Span,
|
||||
custom_path: &Option<Spanned<String>>,
|
||||
) -> Result<Vec<PluginInfo>, ShellError> {
|
||||
let plugin_file_contents = read_plugin_file(engine_state, stack, span, custom_path)?;
|
||||
|
||||
let plugins_info = plugin_file_contents
|
||||
.plugins
|
||||
.into_iter()
|
||||
.map(|plugin| {
|
||||
let mut info = PluginInfo {
|
||||
name: plugin.name,
|
||||
version: None,
|
||||
status: PluginStatus::Added,
|
||||
pid: None,
|
||||
filename: plugin.filename.to_string_lossy().into_owned(),
|
||||
shell: plugin.shell.map(|path| path.to_string_lossy().into_owned()),
|
||||
commands: vec![],
|
||||
};
|
||||
|
||||
if let PluginRegistryItemData::Valid { metadata, commands } = plugin.data {
|
||||
info.version = metadata.version;
|
||||
info.commands = commands
|
||||
.into_iter()
|
||||
.map(|command| command.sig.name)
|
||||
.sorted()
|
||||
.collect();
|
||||
} else {
|
||||
info.status = PluginStatus::Invalid;
|
||||
}
|
||||
info
|
||||
})
|
||||
.sorted()
|
||||
.collect();
|
||||
|
||||
Ok(plugins_info)
|
||||
}
|
||||
|
||||
/// If no options are provided, the command loads from both the plugin list in the engine and what's
|
||||
/// in the registry file. We need to reconcile the two to set the proper states and make sure that
|
||||
/// new plugins that were added to the plugin registry file show up.
|
||||
fn merge_plugin_info(
|
||||
from_engine: Vec<PluginInfo>,
|
||||
from_registry: Vec<PluginInfo>,
|
||||
) -> Vec<PluginInfo> {
|
||||
from_engine
|
||||
.into_iter()
|
||||
.merge_join_by(from_registry, |info_a, info_b| {
|
||||
info_a.name.cmp(&info_b.name)
|
||||
})
|
||||
.map(|either_or_both| match either_or_both {
|
||||
// Exists in the engine, but not in the registry file
|
||||
EitherOrBoth::Left(info) => PluginInfo {
|
||||
status: match info.status {
|
||||
PluginStatus::Running => info.status,
|
||||
// The plugin is not in the registry file, so it should be marked as `removed`
|
||||
_ => PluginStatus::Removed,
|
||||
},
|
||||
..info
|
||||
},
|
||||
// Exists in the registry file, but not in the engine
|
||||
EitherOrBoth::Right(info) => info,
|
||||
// Exists in both
|
||||
EitherOrBoth::Both(info_engine, info_registry) => PluginInfo {
|
||||
status: match (info_engine.status, info_registry.status) {
|
||||
// Above all, `running` should be displayed if the plugin is running
|
||||
(PluginStatus::Running, _) => PluginStatus::Running,
|
||||
// `invalid` takes precedence over other states because the user probably wants
|
||||
// to fix it
|
||||
(_, PluginStatus::Invalid) => PluginStatus::Invalid,
|
||||
// Display `modified` if the state in the registry is different somehow
|
||||
_ if info_engine.is_modified(&info_registry) => PluginStatus::Modified,
|
||||
// Otherwise, `loaded` (it's not running)
|
||||
_ => PluginStatus::Loaded,
|
||||
},
|
||||
..info_engine
|
||||
},
|
||||
})
|
||||
.sorted()
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl PluginInfo {
|
||||
/// True if the plugin info shows some kind of change (other than status/pid) relative to the
|
||||
/// other
|
||||
fn is_modified(&self, other: &PluginInfo) -> bool {
|
||||
self.name != other.name
|
||||
|| self.filename != other.filename
|
||||
|| self.shell != other.shell
|
||||
|| self.commands != other.commands
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ fixed with `plugin add`.
|
||||
|
||||
let filename = canonicalize_possible_filename_arg(engine_state, stack, &name.item);
|
||||
|
||||
modify_plugin_file(engine_state, stack, call.head, custom_path, |contents| {
|
||||
modify_plugin_file(engine_state, stack, call.head, &custom_path, |contents| {
|
||||
if let Some(index) = contents
|
||||
.plugins
|
||||
.iter()
|
||||
|
@ -6,18 +6,17 @@ use std::{
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
pub(crate) fn modify_plugin_file(
|
||||
fn get_plugin_registry_file_path(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
span: Span,
|
||||
custom_path: Option<Spanned<String>>,
|
||||
operate: impl FnOnce(&mut PluginRegistryFile) -> Result<(), ShellError>,
|
||||
) -> Result<(), ShellError> {
|
||||
custom_path: &Option<Spanned<String>>,
|
||||
) -> Result<PathBuf, ShellError> {
|
||||
#[allow(deprecated)]
|
||||
let cwd = current_dir(engine_state, stack)?;
|
||||
|
||||
let plugin_registry_file_path = if let Some(ref custom_path) = custom_path {
|
||||
nu_path::expand_path_with(&custom_path.item, cwd, true)
|
||||
if let Some(ref custom_path) = custom_path {
|
||||
Ok(nu_path::expand_path_with(&custom_path.item, cwd, true))
|
||||
} else {
|
||||
engine_state
|
||||
.plugin_path
|
||||
@ -28,8 +27,53 @@ pub(crate) fn modify_plugin_file(
|
||||
span: Some(span),
|
||||
help: Some("you may be running `nu` with --no-config-file".into()),
|
||||
inner: vec![],
|
||||
})?
|
||||
};
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn read_plugin_file(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
span: Span,
|
||||
custom_path: &Option<Spanned<String>>,
|
||||
) -> Result<PluginRegistryFile, ShellError> {
|
||||
let plugin_registry_file_path =
|
||||
get_plugin_registry_file_path(engine_state, stack, span, custom_path)?;
|
||||
|
||||
let file_span = custom_path.as_ref().map(|p| p.span).unwrap_or(span);
|
||||
|
||||
// Try to read the plugin file if it exists
|
||||
if fs::metadata(&plugin_registry_file_path).is_ok_and(|m| m.len() > 0) {
|
||||
PluginRegistryFile::read_from(
|
||||
File::open(&plugin_registry_file_path).map_err(|err| ShellError::IOErrorSpanned {
|
||||
msg: format!(
|
||||
"failed to read `{}`: {}",
|
||||
plugin_registry_file_path.display(),
|
||||
err
|
||||
),
|
||||
span: file_span,
|
||||
})?,
|
||||
Some(file_span),
|
||||
)
|
||||
} else if let Some(path) = custom_path {
|
||||
Err(ShellError::FileNotFound {
|
||||
file: path.item.clone(),
|
||||
span: path.span,
|
||||
})
|
||||
} else {
|
||||
Ok(PluginRegistryFile::default())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn modify_plugin_file(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
span: Span,
|
||||
custom_path: &Option<Spanned<String>>,
|
||||
operate: impl FnOnce(&mut PluginRegistryFile) -> Result<(), ShellError>,
|
||||
) -> Result<(), ShellError> {
|
||||
let plugin_registry_file_path =
|
||||
get_plugin_registry_file_path(engine_state, stack, span, custom_path)?;
|
||||
|
||||
let file_span = custom_path.as_ref().map(|p| p.span).unwrap_or(span);
|
||||
|
||||
@ -91,17 +135,24 @@ pub(crate) fn get_plugin_dirs(
|
||||
engine_state: &EngineState,
|
||||
stack: &Stack,
|
||||
) -> impl Iterator<Item = String> {
|
||||
// Get the NU_PLUGIN_DIRS constant or env var
|
||||
// Get the NU_PLUGIN_DIRS from the constant and/or env var
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
let value = working_set
|
||||
let dirs_from_const = working_set
|
||||
.find_variable(b"$NU_PLUGIN_DIRS")
|
||||
.and_then(|var_id| working_set.get_constant(var_id).ok().cloned())
|
||||
.or_else(|| stack.get_env_var(engine_state, "NU_PLUGIN_DIRS"));
|
||||
|
||||
// Get all of the strings in the list, if possible
|
||||
value
|
||||
.and_then(|var_id| working_set.get_constant(var_id).ok())
|
||||
.cloned() // TODO: avoid this clone
|
||||
.into_iter()
|
||||
.flat_map(|value| value.into_list().ok())
|
||||
.flatten()
|
||||
.flat_map(|list_item| list_item.coerce_into_string().ok())
|
||||
.flat_map(|list_item| list_item.coerce_into_string().ok());
|
||||
|
||||
let dirs_from_env = stack
|
||||
.get_env_var(engine_state, "NU_PLUGIN_DIRS")
|
||||
.cloned() // TODO: avoid this clone
|
||||
.into_iter()
|
||||
.flat_map(|value| value.into_list().ok())
|
||||
.flatten()
|
||||
.flat_map(|list_item| list_item.coerce_into_string().ok());
|
||||
|
||||
dirs_from_const.chain(dirs_from_env)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ repository = "https://github.com/nushell/nushell/tree/main/crates/nu-color-confi
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-color-config"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
@ -14,12 +14,12 @@ bench = false
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-json = { path = "../nu-json", version = "0.98.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", default-features = false }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0", default-features = false }
|
||||
nu-json = { path = "../nu-json", version = "0.101.0" }
|
||||
nu-ansi-term = { workspace = true }
|
||||
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.98.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.101.0" }
|
@ -5,7 +5,6 @@ use nu_protocol::{Config, Value};
|
||||
// The default colors for shapes, used when there is no config for them.
|
||||
pub fn default_shape_color(shape: &str) -> Style {
|
||||
match shape {
|
||||
"shape_and" => Style::new().fg(Color::Purple).bold(),
|
||||
"shape_binary" => Style::new().fg(Color::Purple).bold(),
|
||||
"shape_block" => Style::new().fg(Color::Blue).bold(),
|
||||
"shape_bool" => Style::new().fg(Color::LightCyan),
|
||||
@ -30,7 +29,6 @@ pub fn default_shape_color(shape: &str) -> Style {
|
||||
"shape_match_pattern" => Style::new().fg(Color::Green),
|
||||
"shape_nothing" => Style::new().fg(Color::LightCyan),
|
||||
"shape_operator" => Style::new().fg(Color::Yellow),
|
||||
"shape_or" => Style::new().fg(Color::Purple).bold(),
|
||||
"shape_pipe" => Style::new().fg(Color::Purple).bold(),
|
||||
"shape_range" => Style::new().fg(Color::Yellow).bold(),
|
||||
"shape_raw_string" => Style::new().fg(Color::LightMagenta).bold(),
|
||||
|
@ -2,9 +2,8 @@ use crate::{color_record_to_nustyle, lookup_ansi_color_style, text_style::Alignm
|
||||
use nu_ansi_term::{Color, Style};
|
||||
use nu_engine::ClosureEvalOnce;
|
||||
use nu_protocol::{
|
||||
cli_error::CliError,
|
||||
engine::{Closure, EngineState, Stack, StateWorkingSet},
|
||||
Span, Value,
|
||||
engine::{Closure, EngineState, Stack},
|
||||
report_shell_error, Span, Value,
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
@ -70,14 +69,8 @@ impl<'a> StyleComputer<'a> {
|
||||
_ => Style::default(),
|
||||
}
|
||||
}
|
||||
// This is basically a copy of nu_cli::report_error(), but that isn't usable due to
|
||||
// dependencies. While crudely spitting out a bunch of errors like this is not ideal,
|
||||
// currently hook closure errors behave roughly the same.
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"Error: {:?}",
|
||||
CliError(&e, &StateWorkingSet::new(self.engine_state))
|
||||
);
|
||||
Err(err) => {
|
||||
report_shell_error(self.engine_state, &err);
|
||||
Style::default()
|
||||
}
|
||||
}
|
||||
@ -230,7 +223,7 @@ fn test_computable_style_closure_basic() {
|
||||
];
|
||||
let actual_repl = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
||||
assert_eq!(actual_repl.err, "");
|
||||
assert_eq!(actual_repl.out, "[bell.obj, book.obj, candle.obj]");
|
||||
assert_eq!(actual_repl.out, r#"["bell.obj", "book.obj", "candle.obj"]"#);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-command"
|
||||
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-command"
|
||||
version = "0.98.0"
|
||||
version = "0.101.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -16,21 +16,21 @@ bench = false
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.98.0" }
|
||||
nu-color-config = { path = "../nu-color-config", version = "0.98.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.98.0" }
|
||||
nu-glob = { path = "../nu-glob", version = "0.98.0" }
|
||||
nu-json = { path = "../nu-json", version = "0.98.0" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.98.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.98.0" }
|
||||
nu-pretty-hex = { path = "../nu-pretty-hex", version = "0.98.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.98.0" }
|
||||
nu-system = { path = "../nu-system", version = "0.98.0" }
|
||||
nu-table = { path = "../nu-table", version = "0.98.0" }
|
||||
nu-term-grid = { path = "../nu-term-grid", version = "0.98.0" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.98.0" }
|
||||
nu-cmd-base = { path = "../nu-cmd-base", version = "0.101.0" }
|
||||
nu-color-config = { path = "../nu-color-config", version = "0.101.0" }
|
||||
nu-engine = { path = "../nu-engine", version = "0.101.0", default-features = false }
|
||||
nu-glob = { path = "../nu-glob", version = "0.101.0" }
|
||||
nu-json = { path = "../nu-json", version = "0.101.0" }
|
||||
nu-parser = { path = "../nu-parser", version = "0.101.0" }
|
||||
nu-path = { path = "../nu-path", version = "0.101.0" }
|
||||
nu-pretty-hex = { path = "../nu-pretty-hex", version = "0.101.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.101.0", default-features = false }
|
||||
nu-system = { path = "../nu-system", version = "0.101.0" }
|
||||
nu-table = { path = "../nu-table", version = "0.101.0" }
|
||||
nu-term-grid = { path = "../nu-term-grid", version = "0.101.0" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.101.0", default-features = false }
|
||||
nu-ansi-term = { workspace = true }
|
||||
nuon = { path = "../nuon", version = "0.98.0" }
|
||||
nuon = { path = "../nuon", version = "0.101.0" }
|
||||
|
||||
alphanumeric-sort = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
@ -43,7 +43,7 @@ chardetng = { workspace = true }
|
||||
chrono = { workspace = true, features = ["std", "unstable-locales", "clock"], default-features = false }
|
||||
chrono-humanize = { workspace = true }
|
||||
chrono-tz = { workspace = true }
|
||||
crossterm = { workspace = true }
|
||||
crossterm = { workspace = true, optional = true }
|
||||
csv = { workspace = true }
|
||||
dialoguer = { workspace = true, default-features = false, features = ["fuzzy-select"] }
|
||||
digest = { workspace = true, default-features = false }
|
||||
@ -61,24 +61,26 @@ lscolors = { workspace = true, default-features = false, features = ["nu-ansi-te
|
||||
md5 = { workspace = true }
|
||||
mime = { workspace = true }
|
||||
mime_guess = { workspace = true }
|
||||
multipart-rs = { workspace = true }
|
||||
native-tls = { workspace = true }
|
||||
notify-debouncer-full = { workspace = true, default-features = false }
|
||||
multipart-rs = { workspace = true, optional = true }
|
||||
native-tls = { workspace = true, optional = true }
|
||||
notify-debouncer-full = { workspace = true, default-features = false, optional = true }
|
||||
num-format = { workspace = true }
|
||||
num-traits = { workspace = true }
|
||||
once_cell = { workspace = true }
|
||||
open = { workspace = true }
|
||||
os_pipe = { workspace = true }
|
||||
oem_cp = { workspace = true }
|
||||
open = { workspace = true, optional = true }
|
||||
os_pipe = { workspace = true, optional = true }
|
||||
pathdiff = { workspace = true }
|
||||
percent-encoding = { workspace = true }
|
||||
print-positions = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
rand = { workspace = true, optional = true }
|
||||
getrandom = { workspace = true, optional = true }
|
||||
rayon = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
roxmltree = { workspace = true }
|
||||
rusqlite = { workspace = true, features = ["bundled", "backup", "chrono"], optional = true }
|
||||
rmp = { workspace = true }
|
||||
scopeguard = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true, features = ["preserve_order"] }
|
||||
serde_urlencoded = { workspace = true }
|
||||
@ -86,29 +88,29 @@ serde_yaml = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
sysinfo = { workspace = true }
|
||||
tabled = { workspace = true, features = ["ansi"], default-features = false }
|
||||
terminal_size = { workspace = true }
|
||||
titlecase = { workspace = true }
|
||||
toml = { workspace = true, features = ["preserve_order"] }
|
||||
unicode-segmentation = { workspace = true }
|
||||
ureq = { workspace = true, default-features = false, features = ["charset", "gzip", "json", "native-tls"] }
|
||||
ureq = { workspace = true, default-features = false, features = ["charset", "gzip", "json"] }
|
||||
url = { workspace = true }
|
||||
uu_cp = { workspace = true }
|
||||
uu_mkdir = { workspace = true }
|
||||
uu_mktemp = { workspace = true }
|
||||
uu_mv = { workspace = true }
|
||||
uu_uname = { workspace = true }
|
||||
uu_whoami = { workspace = true }
|
||||
uuid = { workspace = true, features = ["v4"] }
|
||||
uu_cp = { workspace = true, optional = true }
|
||||
uu_mkdir = { workspace = true, optional = true }
|
||||
uu_mktemp = { workspace = true, optional = true }
|
||||
uu_mv = { workspace = true, optional = true }
|
||||
uu_touch = { workspace = true, optional = true }
|
||||
uu_uname = { workspace = true, optional = true }
|
||||
uu_whoami = { workspace = true, optional = true }
|
||||
uuid = { workspace = true, features = ["v4"], optional = true }
|
||||
v_htmlescape = { workspace = true }
|
||||
wax = { workspace = true }
|
||||
which = { workspace = true }
|
||||
which = { workspace = true, optional = true }
|
||||
unicode-width = { workspace = true }
|
||||
data-encoding = { version = "2.6.0", features = ["alloc"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winreg = { workspace = true }
|
||||
|
||||
[target.'cfg(not(windows))'.dependencies]
|
||||
[target.'cfg(all(not(windows), not(target_arch = "wasm32")))'.dependencies]
|
||||
uucore = { workspace = true, features = ["mode"] }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
@ -134,13 +136,59 @@ features = [
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
plugin = ["nu-parser/plugin"]
|
||||
default = ["os"]
|
||||
os = [
|
||||
# include other features
|
||||
"js",
|
||||
"network",
|
||||
"nu-protocol/os",
|
||||
"nu-utils/os",
|
||||
|
||||
# os-dependant dependencies
|
||||
"crossterm",
|
||||
"notify-debouncer-full",
|
||||
"open",
|
||||
"os_pipe",
|
||||
"uu_cp",
|
||||
"uu_mkdir",
|
||||
"uu_mktemp",
|
||||
"uu_mv",
|
||||
"uu_touch",
|
||||
"uu_uname",
|
||||
"uu_whoami",
|
||||
"which",
|
||||
]
|
||||
|
||||
# The dependencies listed below need 'getrandom'.
|
||||
# They work with JS (usually with wasm-bindgen) or regular OS support.
|
||||
# Hence they are also put under the 'os' feature to avoid repetition.
|
||||
js = [
|
||||
"getrandom",
|
||||
"getrandom/js",
|
||||
"rand",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
# These dependencies require networking capabilities, especially the http
|
||||
# interface requires openssl which is not easy to embed into wasm,
|
||||
# using rustls could solve this issue.
|
||||
network = [
|
||||
"multipart-rs",
|
||||
"native-tls",
|
||||
"ureq/native-tls",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
plugin = [
|
||||
"nu-parser/plugin",
|
||||
"os",
|
||||
]
|
||||
sqlite = ["rusqlite"]
|
||||
trash-support = ["trash"]
|
||||
|
||||
[dev-dependencies]
|
||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.98.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.98.0" }
|
||||
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.101.0" }
|
||||
nu-test-support = { path = "../nu-test-support", version = "0.101.0" }
|
||||
|
||||
dirs = { workspace = true }
|
||||
mockito = { workspace = true, default-features = false }
|
||||
|
@ -1,4 +1,4 @@
|
||||
use nu_engine::{command_prelude::*, get_eval_expression};
|
||||
use nu_engine::command_prelude::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BytesBuild;
|
||||
@ -49,8 +49,7 @@ impl Command for BytesBuild {
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let mut output = vec![];
|
||||
let eval_expression = get_eval_expression(engine_state);
|
||||
for val in call.rest_iter_flattened(engine_state, stack, eval_expression, 0)? {
|
||||
for val in call.rest::<Value>(engine_state, stack, 0)? {
|
||||
let val_span = val.span();
|
||||
match val {
|
||||
Value::Binary { mut val, .. } => output.append(&mut val),
|
||||
|
@ -1,5 +1,5 @@
|
||||
use chrono::{DateTime, FixedOffset};
|
||||
use nu_protocol::{ShellError, Span, Value};
|
||||
use nu_protocol::{Filesize, ShellError, Span, Value};
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
/// A subset of [`Value`], which is hashable.
|
||||
@ -30,7 +30,7 @@ pub enum HashableValue {
|
||||
span: Span,
|
||||
},
|
||||
Filesize {
|
||||
val: i64,
|
||||
val: Filesize,
|
||||
span: Span,
|
||||
},
|
||||
Duration {
|
||||
@ -183,6 +183,7 @@ mod test {
|
||||
use nu_protocol::{
|
||||
ast::{CellPath, PathMember},
|
||||
engine::Closure,
|
||||
BlockId,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
|
||||
@ -197,7 +198,10 @@ mod test {
|
||||
(Value::int(1, span), HashableValue::Int { val: 1, span }),
|
||||
(
|
||||
Value::filesize(1, span),
|
||||
HashableValue::Filesize { val: 1, span },
|
||||
HashableValue::Filesize {
|
||||
val: 1.into(),
|
||||
span,
|
||||
},
|
||||
),
|
||||
(
|
||||
Value::duration(1, span),
|
||||
@ -244,7 +248,7 @@ mod test {
|
||||
Value::list(vec![Value::bool(true, span)], span),
|
||||
Value::closure(
|
||||
Closure {
|
||||
block_id: 0,
|
||||
block_id: BlockId::new(0),
|
||||
captures: Vec::new(),
|
||||
},
|
||||
span,
|
||||
|
@ -108,7 +108,7 @@ impl Command for Fill {
|
||||
},
|
||||
Example {
|
||||
description:
|
||||
"Fill a filesize on the left side to a width of 5 with the character '0'",
|
||||
"Fill a filesize on both sides to a width of 10 with the character '0'",
|
||||
example: "1kib | fill --alignment middle --character '0' --width 10",
|
||||
result: Some(Value::string("0001024000", Span::test_data())),
|
||||
},
|
||||
@ -167,7 +167,7 @@ fn fill(
|
||||
fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
match input {
|
||||
Value::Int { val, .. } => fill_int(*val, args, span),
|
||||
Value::Filesize { val, .. } => fill_int(*val, args, span),
|
||||
Value::Filesize { val, .. } => fill_int(val.get(), args, span),
|
||||
Value::Float { val, .. } => fill_float(*val, args, span),
|
||||
Value::String { val, .. } => fill_string(val, args, span),
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
|
@ -1,7 +1,7 @@
|
||||
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
||||
use nu_engine::command_prelude::*;
|
||||
|
||||
pub struct Arguments {
|
||||
struct Arguments {
|
||||
cell_paths: Option<Vec<CellPath>>,
|
||||
compact: bool,
|
||||
}
|
||||
@ -142,12 +142,12 @@ fn into_binary(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action(input: &Value, _args: &Arguments, span: Span) -> Value {
|
||||
fn action(input: &Value, _args: &Arguments, span: Span) -> Value {
|
||||
let value = match input {
|
||||
Value::Binary { .. } => input.clone(),
|
||||
Value::Int { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span),
|
||||
Value::Float { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span),
|
||||
Value::Filesize { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span),
|
||||
Value::Filesize { val, .. } => Value::binary(val.get().to_ne_bytes().to_vec(), span),
|
||||
Value::String { val, .. } => Value::binary(val.as_bytes().to_vec(), span),
|
||||
Value::Bool { val, .. } => Value::binary(i64::from(*val).to_ne_bytes().to_vec(), span),
|
||||
Value::Duration { val, .. } => Value::binary(val.to_ne_bytes().to_vec(), span),
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::{generate_strftime_list, parse_date_from_string};
|
||||
use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, NaiveTime, TimeZone, Utc};
|
||||
use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, TimeZone, Utc};
|
||||
use human_date_parser::{from_human_time, ParseResult};
|
||||
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
||||
use nu_engine::command_prelude::*;
|
||||
@ -185,11 +185,13 @@ impl Command for SubCommand {
|
||||
example: "'16.11.1984 8:00 am' | into datetime --format '%d.%m.%Y %H:%M %P'",
|
||||
#[allow(clippy::inconsistent_digit_grouping)]
|
||||
result: Some(Value::date(
|
||||
DateTime::from_naive_utc_and_offset(
|
||||
NaiveDateTime::parse_from_str("16.11.1984 8:00 am", "%d.%m.%Y %H:%M %P")
|
||||
.expect("date calculation should not fail in test"),
|
||||
*Local::now().offset(),
|
||||
),
|
||||
Local
|
||||
.from_local_datetime(
|
||||
&NaiveDateTime::parse_from_str("16.11.1984 8:00 am", "%d.%m.%Y %H:%M %P")
|
||||
.expect("date calculation should not fail in test"),
|
||||
)
|
||||
.unwrap()
|
||||
.with_timezone(Local::now().offset()),
|
||||
Span::test_data(),
|
||||
)),
|
||||
},
|
||||
@ -275,12 +277,13 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
if let Ok(date) = from_human_time(&input_val) {
|
||||
match date {
|
||||
ParseResult::Date(date) => {
|
||||
let time = NaiveTime::from_hms_opt(0, 0, 0).expect("valid time");
|
||||
let time = Local::now().time();
|
||||
let combined = date.and_time(time);
|
||||
let dt_fixed = DateTime::from_naive_utc_and_offset(
|
||||
combined,
|
||||
*Local::now().offset(),
|
||||
);
|
||||
let local_offset = *Local::now().offset();
|
||||
let dt_fixed =
|
||||
TimeZone::from_local_datetime(&local_offset, &combined)
|
||||
.single()
|
||||
.unwrap_or_default();
|
||||
return Value::date(dt_fixed, span);
|
||||
}
|
||||
ParseResult::DateTime(date) => {
|
||||
@ -289,10 +292,11 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
ParseResult::Time(time) => {
|
||||
let date = Local::now().date_naive();
|
||||
let combined = date.and_time(time);
|
||||
let dt_fixed = DateTime::from_naive_utc_and_offset(
|
||||
combined,
|
||||
*Local::now().offset(),
|
||||
);
|
||||
let local_offset = *Local::now().offset();
|
||||
let dt_fixed =
|
||||
TimeZone::from_local_datetime(&local_offset, &combined)
|
||||
.single()
|
||||
.unwrap_or_default();
|
||||
return Value::date(dt_fixed, span);
|
||||
}
|
||||
}
|
||||
@ -386,13 +390,15 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
Ok(d) => Value::date ( d, head ),
|
||||
Err(reason) => {
|
||||
match NaiveDateTime::parse_from_str(val, &dt.0) {
|
||||
Ok(d) => Value::date (
|
||||
DateTime::from_naive_utc_and_offset(
|
||||
d,
|
||||
*Local::now().offset(),
|
||||
),
|
||||
head,
|
||||
),
|
||||
Ok(d) => {
|
||||
let local_offset = *Local::now().offset();
|
||||
let dt_fixed =
|
||||
TimeZone::from_local_datetime(&local_offset, &d)
|
||||
.single()
|
||||
.unwrap_or_default();
|
||||
|
||||
Value::date (dt_fixed,head)
|
||||
}
|
||||
Err(_) => {
|
||||
Value::error (
|
||||
ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) },
|
||||
@ -503,7 +509,14 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn takes_a_date_format_without_timezone() {
|
||||
// Ignoring this test for now because we changed the human-date-parser to use
|
||||
// the users timezone instead of UTC. We may continue to tweak this behavior.
|
||||
// Another hacky solution is to set the timezone to UTC in the test, which works
|
||||
// on MacOS and Linux but hasn't been tested on Windows. Plus it kind of defeats
|
||||
// the purpose of a "without_timezone" test.
|
||||
// std::env::set_var("TZ", "UTC");
|
||||
let date_str = Value::test_string("16.11.1984 8:00 am");
|
||||
let fmt_options = Some(DatetimeFormat("%d.%m.%Y %H:%M %P".to_string()));
|
||||
let args = Arguments {
|
||||
@ -513,12 +526,16 @@ mod tests {
|
||||
};
|
||||
let actual = action(&date_str, &args, Span::test_data());
|
||||
let expected = Value::date(
|
||||
DateTime::from_naive_utc_and_offset(
|
||||
NaiveDateTime::parse_from_str("16.11.1984 8:00 am", "%d.%m.%Y %H:%M %P").unwrap(),
|
||||
*Local::now().offset(),
|
||||
),
|
||||
Local
|
||||
.from_local_datetime(
|
||||
&NaiveDateTime::parse_from_str("16.11.1984 8:00 am", "%d.%m.%Y %H:%M %P")
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
.with_timezone(Local::now().offset()),
|
||||
Span::test_data(),
|
||||
);
|
||||
|
||||
assert_eq!(actual, expected)
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,7 @@ impl Command for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
||||
let value_span = input.span();
|
||||
match input {
|
||||
Value::Filesize { .. } => input.clone(),
|
||||
|
@ -253,13 +253,13 @@ fn action(input: &Value, args: &Arguments, span: Span) -> Value {
|
||||
convert_int(input, span, radix)
|
||||
}
|
||||
}
|
||||
Value::Filesize { val, .. } => Value::int(*val, span),
|
||||
Value::Filesize { val, .. } => Value::int(val.get(), span),
|
||||
Value::Float { val, .. } => Value::int(
|
||||
{
|
||||
if radix == 10 {
|
||||
*val as i64
|
||||
} else {
|
||||
match convert_int(&Value::int(*val as i64, span), span, radix).as_i64() {
|
||||
match convert_int(&Value::int(*val as i64, span), span, radix).as_int() {
|
||||
Ok(v) => v,
|
||||
_ => {
|
||||
return Value::error(
|
||||
|
@ -99,6 +99,11 @@ impl Command for SubCommand {
|
||||
"timezone" => Value::test_string("+02:00"),
|
||||
})),
|
||||
},
|
||||
Example {
|
||||
description: "convert date components to table columns",
|
||||
example: "2020-04-12T22:10:57+02:00 | into record | transpose | transpose -r",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
use crate::parse_date_from_string;
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::PipelineIterator;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::{Regex, RegexBuilder};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IntoValue;
|
||||
@ -18,7 +18,7 @@ impl Command for IntoValue {
|
||||
.input_output_types(vec![(Type::table(), Type::table())])
|
||||
.named(
|
||||
"columns",
|
||||
SyntaxShape::Table(vec![]),
|
||||
SyntaxShape::List(Box::new(SyntaxShape::Any)),
|
||||
"list of columns to update",
|
||||
Some('c'),
|
||||
)
|
||||
@ -32,7 +32,11 @@ impl Command for IntoValue {
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Infer nushell datatype for each cell."
|
||||
"Infer Nushell datatype for each cell."
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["convert", "conversion"]
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -267,8 +271,9 @@ const DATETIME_DMY_PATTERN: &str = r#"(?x)
|
||||
$
|
||||
"#;
|
||||
|
||||
static DATETIME_DMY_RE: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(DATETIME_DMY_PATTERN).expect("datetime_dmy_pattern should be valid"));
|
||||
static DATETIME_DMY_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(DATETIME_DMY_PATTERN).expect("datetime_dmy_pattern should be valid")
|
||||
});
|
||||
const DATETIME_YMD_PATTERN: &str = r#"(?x)
|
||||
^
|
||||
['"]? # optional quotes
|
||||
@ -293,8 +298,9 @@ const DATETIME_YMD_PATTERN: &str = r#"(?x)
|
||||
['"]? # optional quotes
|
||||
$
|
||||
"#;
|
||||
static DATETIME_YMD_RE: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(DATETIME_YMD_PATTERN).expect("datetime_ymd_pattern should be valid"));
|
||||
static DATETIME_YMD_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(DATETIME_YMD_PATTERN).expect("datetime_ymd_pattern should be valid")
|
||||
});
|
||||
//2023-03-24 16:44:17.865147299 -05:00
|
||||
const DATETIME_YMDZ_PATTERN: &str = r#"(?x)
|
||||
^
|
||||
@ -327,23 +333,24 @@ const DATETIME_YMDZ_PATTERN: &str = r#"(?x)
|
||||
['"]? # optional quotes
|
||||
$
|
||||
"#;
|
||||
static DATETIME_YMDZ_RE: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(DATETIME_YMDZ_PATTERN).expect("datetime_ymdz_pattern should be valid"));
|
||||
static DATETIME_YMDZ_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(DATETIME_YMDZ_PATTERN).expect("datetime_ymdz_pattern should be valid")
|
||||
});
|
||||
|
||||
static FLOAT_RE: Lazy<Regex> = Lazy::new(|| {
|
||||
static FLOAT_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"^\s*[-+]?((\d*\.\d+)([eE][-+]?\d+)?|inf|NaN|(\d+)[eE][-+]?\d+|\d+\.)$")
|
||||
.expect("float pattern should be valid")
|
||||
});
|
||||
|
||||
static INTEGER_RE: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r"^\s*-?(\d+)$").expect("integer pattern should be valid"));
|
||||
static INTEGER_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"^\s*-?(\d+)$").expect("integer pattern should be valid"));
|
||||
|
||||
static INTEGER_WITH_DELIMS_RE: Lazy<Regex> = Lazy::new(|| {
|
||||
static INTEGER_WITH_DELIMS_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"^\s*-?(\d{1,3}([,_]\d{3})+)$")
|
||||
.expect("integer with delimiters pattern should be valid")
|
||||
});
|
||||
|
||||
static BOOLEAN_RE: Lazy<Regex> = Lazy::new(|| {
|
||||
static BOOLEAN_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
RegexBuilder::new(r"^\s*(true)$|^(false)$")
|
||||
.case_insensitive(true)
|
||||
.build()
|
||||
|
@ -359,7 +359,6 @@ fn nu_value_to_sqlite_type(val: &Value) -> Result<&'static str, ShellError> {
|
||||
| Type::Custom(_)
|
||||
| Type::Error
|
||||
| Type::List(_)
|
||||
| Type::ListStream
|
||||
| Type::Range
|
||||
| Type::Record(_)
|
||||
| Type::Signature
|
||||
|
@ -421,7 +421,7 @@ pub fn value_to_sql(value: Value) -> Result<Box<dyn rusqlite::ToSql>, ShellError
|
||||
Value::Bool { val, .. } => Box::new(val),
|
||||
Value::Int { val, .. } => Box::new(val),
|
||||
Value::Float { val, .. } => Box::new(val),
|
||||
Value::Filesize { val, .. } => Box::new(val),
|
||||
Value::Filesize { val, .. } => Box::new(val.get()),
|
||||
Value::Duration { val, .. } => Box::new(val),
|
||||
Value::Date { val, .. } => Box::new(val),
|
||||
Value::String { val, .. } => Box::new(val),
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::date::utils::parse_date_from_string;
|
||||
use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{report_parse_warning, ParseWarning};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
@ -17,7 +18,7 @@ impl Command for SubCommand {
|
||||
(Type::String, Type::record()),
|
||||
])
|
||||
.allow_variants_without_examples(true) // https://github.com/nushell/nushell/issues/7032
|
||||
.category(Category::Date)
|
||||
.category(Category::Deprecated)
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
@ -35,6 +36,17 @@ impl Command for SubCommand {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
report_parse_warning(
|
||||
&StateWorkingSet::new(engine_state),
|
||||
&ParseWarning::DeprecatedWarning {
|
||||
old_command: "date to-record".into(),
|
||||
new_suggestion: "see `into record` command examples".into(),
|
||||
span: head,
|
||||
url: "`help into record`".into(),
|
||||
},
|
||||
);
|
||||
|
||||
let head = call.head;
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::date::utils::parse_date_from_string;
|
||||
use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{report_parse_warning, ParseWarning};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
@ -17,7 +18,7 @@ impl Command for SubCommand {
|
||||
(Type::String, Type::table()),
|
||||
])
|
||||
.allow_variants_without_examples(true) // https://github.com/nushell/nushell/issues/7032
|
||||
.category(Category::Date)
|
||||
.category(Category::Deprecated)
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
@ -36,6 +37,16 @@ impl Command for SubCommand {
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
report_parse_warning(
|
||||
&StateWorkingSet::new(engine_state),
|
||||
&ParseWarning::DeprecatedWarning {
|
||||
old_command: "date to-table".into(),
|
||||
new_suggestion: "see `into record` command examples".into(),
|
||||
span: head,
|
||||
url: "`help into record`".into(),
|
||||
},
|
||||
);
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty { dst_span: head });
|
||||
|
@ -1,6 +1,7 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_parser::parse;
|
||||
use nu_protocol::engine::StateWorkingSet;
|
||||
use nu_parser::{flatten_block, parse};
|
||||
use nu_protocol::{engine::StateWorkingSet, record};
|
||||
use serde_json::{json, Value as JsonValue};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Ast;
|
||||
@ -16,109 +17,23 @@ impl Command for Ast {
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("ast")
|
||||
.input_output_types(vec![(Type::String, Type::record())])
|
||||
.input_output_types(vec![
|
||||
(Type::Nothing, Type::table()),
|
||||
(Type::Nothing, Type::record()),
|
||||
(Type::Nothing, Type::String),
|
||||
])
|
||||
.required(
|
||||
"pipeline",
|
||||
SyntaxShape::String,
|
||||
"The pipeline to print the ast for.",
|
||||
)
|
||||
.switch("json", "serialize to json", Some('j'))
|
||||
.switch("minify", "minify the nuon or json output", Some('m'))
|
||||
.switch("json", "Serialize to json", Some('j'))
|
||||
.switch("minify", "Minify the nuon or json output", Some('m'))
|
||||
.switch("flatten", "An easier to read version of the ast", Some('f'))
|
||||
.allow_variants_without_examples(true)
|
||||
.category(Category::Debug)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let pipeline: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let to_json = call.has_flag(engine_state, stack, "json")?;
|
||||
let minify = call.has_flag(engine_state, stack, "minify")?;
|
||||
let mut working_set = StateWorkingSet::new(engine_state);
|
||||
let block_output = parse(&mut working_set, None, pipeline.item.as_bytes(), false);
|
||||
let error_output = working_set.parse_errors.first();
|
||||
let block_span = match &block_output.span {
|
||||
Some(span) => span,
|
||||
None => &pipeline.span,
|
||||
};
|
||||
if to_json {
|
||||
// Get the block as json
|
||||
let serde_block_str = if minify {
|
||||
serde_json::to_string(&*block_output)
|
||||
} else {
|
||||
serde_json::to_string_pretty(&*block_output)
|
||||
};
|
||||
let block_json = match serde_block_str {
|
||||
Ok(json) => json,
|
||||
Err(e) => Err(ShellError::CantConvert {
|
||||
to_type: "string".to_string(),
|
||||
from_type: "block".to_string(),
|
||||
span: *block_span,
|
||||
help: Some(format!(
|
||||
"Error: {e}\nCan't convert {block_output:?} to string"
|
||||
)),
|
||||
})?,
|
||||
};
|
||||
// Get the error as json
|
||||
let serde_error_str = if minify {
|
||||
serde_json::to_string(&error_output)
|
||||
} else {
|
||||
serde_json::to_string_pretty(&error_output)
|
||||
};
|
||||
|
||||
let error_json = match serde_error_str {
|
||||
Ok(json) => json,
|
||||
Err(e) => Err(ShellError::CantConvert {
|
||||
to_type: "string".to_string(),
|
||||
from_type: "error".to_string(),
|
||||
span: *block_span,
|
||||
help: Some(format!(
|
||||
"Error: {e}\nCan't convert {error_output:?} to string"
|
||||
)),
|
||||
})?,
|
||||
};
|
||||
|
||||
// Create a new output record, merging the block and error
|
||||
let output_record = Value::record(
|
||||
record! {
|
||||
"block" => Value::string(block_json, *block_span),
|
||||
"error" => Value::string(error_json, Span::test_data()),
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
Ok(output_record.into_pipeline_data())
|
||||
} else {
|
||||
let block_value = Value::string(
|
||||
if minify {
|
||||
format!("{block_output:?}")
|
||||
} else {
|
||||
format!("{block_output:#?}")
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
let error_value = Value::string(
|
||||
if minify {
|
||||
format!("{error_output:?}")
|
||||
} else {
|
||||
format!("{error_output:#?}")
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
let output_record = Value::record(
|
||||
record! {
|
||||
"block" => block_value,
|
||||
"error" => error_value
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
Ok(output_record.into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
@ -147,8 +62,247 @@ impl Command for Ast {
|
||||
example: "ast 'for x in 1..10 { echo $x ' --json --minify",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Print the ast of a string flattened",
|
||||
example: r#"ast "'hello'" --flatten"#,
|
||||
result: Some(Value::test_list(vec![Value::test_record(record! {
|
||||
"content" => Value::test_string("'hello'"),
|
||||
"shape" => Value::test_string("shape_string"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(0),
|
||||
"end" => Value::test_int(7),}),
|
||||
})])),
|
||||
},
|
||||
Example {
|
||||
description: "Print the ast of a string flattened, as json, minified",
|
||||
example: r#"ast "'hello'" --flatten --json --minify"#,
|
||||
result: Some(Value::test_string(
|
||||
r#"[{"content":"'hello'","shape":"shape_string","span":{"start":0,"end":7}}]"#,
|
||||
)),
|
||||
},
|
||||
Example {
|
||||
description: "Print the ast of a pipeline flattened",
|
||||
example: r#"ast 'ls | sort-by type name -i' --flatten"#,
|
||||
result: Some(Value::test_list(vec![
|
||||
Value::test_record(record! {
|
||||
"content" => Value::test_string("ls"),
|
||||
"shape" => Value::test_string("shape_external"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(0),
|
||||
"end" => Value::test_int(2),}),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"content" => Value::test_string("|"),
|
||||
"shape" => Value::test_string("shape_pipe"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(3),
|
||||
"end" => Value::test_int(4),}),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"content" => Value::test_string("sort-by"),
|
||||
"shape" => Value::test_string("shape_internalcall"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(5),
|
||||
"end" => Value::test_int(12),}),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"content" => Value::test_string("type"),
|
||||
"shape" => Value::test_string("shape_string"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(13),
|
||||
"end" => Value::test_int(17),}),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"content" => Value::test_string("name"),
|
||||
"shape" => Value::test_string("shape_string"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(18),
|
||||
"end" => Value::test_int(22),}),
|
||||
}),
|
||||
Value::test_record(record! {
|
||||
"content" => Value::test_string("-i"),
|
||||
"shape" => Value::test_string("shape_flag"),
|
||||
"span" => Value::test_record(record! {
|
||||
"start" => Value::test_int(23),
|
||||
"end" => Value::test_int(25),}),
|
||||
}),
|
||||
])),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let pipeline: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||
let to_json = call.has_flag(engine_state, stack, "json")?;
|
||||
let minify = call.has_flag(engine_state, stack, "minify")?;
|
||||
let flatten = call.has_flag(engine_state, stack, "flatten")?;
|
||||
|
||||
let mut working_set = StateWorkingSet::new(engine_state);
|
||||
let offset = working_set.next_span_start();
|
||||
let parsed_block = parse(&mut working_set, None, pipeline.item.as_bytes(), false);
|
||||
|
||||
if flatten {
|
||||
let flat = flatten_block(&working_set, &parsed_block);
|
||||
if to_json {
|
||||
let mut json_val: JsonValue = json!([]);
|
||||
for (span, shape) in flat {
|
||||
let content =
|
||||
String::from_utf8_lossy(working_set.get_span_contents(span)).to_string();
|
||||
|
||||
let json = json!(
|
||||
{
|
||||
"content": content,
|
||||
"shape": shape.to_string(),
|
||||
"span": {
|
||||
"start": span.start.checked_sub(offset),
|
||||
"end": span.end.checked_sub(offset),
|
||||
},
|
||||
}
|
||||
);
|
||||
json_merge(&mut json_val, &json);
|
||||
}
|
||||
let json_string = if minify {
|
||||
if let Ok(json_str) = serde_json::to_string(&json_val) {
|
||||
json_str
|
||||
} else {
|
||||
"{}".to_string()
|
||||
}
|
||||
} else if let Ok(json_str) = serde_json::to_string_pretty(&json_val) {
|
||||
json_str
|
||||
} else {
|
||||
"{}".to_string()
|
||||
};
|
||||
|
||||
Ok(Value::string(json_string, pipeline.span).into_pipeline_data())
|
||||
} else {
|
||||
// let mut rec: Record = Record::new();
|
||||
let mut rec = vec![];
|
||||
for (span, shape) in flat {
|
||||
let content =
|
||||
String::from_utf8_lossy(working_set.get_span_contents(span)).to_string();
|
||||
let each_rec = record! {
|
||||
"content" => Value::test_string(content),
|
||||
"shape" => Value::test_string(shape.to_string()),
|
||||
"span" => Value::test_record(record!{
|
||||
"start" => Value::test_int(match span.start.checked_sub(offset) {
|
||||
Some(start) => start as i64,
|
||||
None => 0
|
||||
}),
|
||||
"end" => Value::test_int(match span.end.checked_sub(offset) {
|
||||
Some(end) => end as i64,
|
||||
None => 0
|
||||
}),
|
||||
}),
|
||||
};
|
||||
rec.push(Value::test_record(each_rec));
|
||||
}
|
||||
Ok(Value::list(rec, pipeline.span).into_pipeline_data())
|
||||
}
|
||||
} else {
|
||||
let error_output = working_set.parse_errors.first();
|
||||
let block_span = match &parsed_block.span {
|
||||
Some(span) => span,
|
||||
None => &pipeline.span,
|
||||
};
|
||||
if to_json {
|
||||
// Get the block as json
|
||||
let serde_block_str = if minify {
|
||||
serde_json::to_string(&*parsed_block)
|
||||
} else {
|
||||
serde_json::to_string_pretty(&*parsed_block)
|
||||
};
|
||||
let block_json = match serde_block_str {
|
||||
Ok(json) => json,
|
||||
Err(e) => Err(ShellError::CantConvert {
|
||||
to_type: "string".to_string(),
|
||||
from_type: "block".to_string(),
|
||||
span: *block_span,
|
||||
help: Some(format!(
|
||||
"Error: {e}\nCan't convert {parsed_block:?} to string"
|
||||
)),
|
||||
})?,
|
||||
};
|
||||
// Get the error as json
|
||||
let serde_error_str = if minify {
|
||||
serde_json::to_string(&error_output)
|
||||
} else {
|
||||
serde_json::to_string_pretty(&error_output)
|
||||
};
|
||||
|
||||
let error_json = match serde_error_str {
|
||||
Ok(json) => json,
|
||||
Err(e) => Err(ShellError::CantConvert {
|
||||
to_type: "string".to_string(),
|
||||
from_type: "error".to_string(),
|
||||
span: *block_span,
|
||||
help: Some(format!(
|
||||
"Error: {e}\nCan't convert {error_output:?} to string"
|
||||
)),
|
||||
})?,
|
||||
};
|
||||
|
||||
// Create a new output record, merging the block and error
|
||||
let output_record = Value::record(
|
||||
record! {
|
||||
"block" => Value::string(block_json, *block_span),
|
||||
"error" => Value::string(error_json, Span::test_data()),
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
Ok(output_record.into_pipeline_data())
|
||||
} else {
|
||||
let block_value = Value::string(
|
||||
if minify {
|
||||
format!("{parsed_block:?}")
|
||||
} else {
|
||||
format!("{parsed_block:#?}")
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
let error_value = Value::string(
|
||||
if minify {
|
||||
format!("{error_output:?}")
|
||||
} else {
|
||||
format!("{error_output:#?}")
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
let output_record = Value::record(
|
||||
record! {
|
||||
"block" => block_value,
|
||||
"error" => error_value
|
||||
},
|
||||
pipeline.span,
|
||||
);
|
||||
Ok(output_record.into_pipeline_data())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn json_merge(a: &mut JsonValue, b: &JsonValue) {
|
||||
match (a, b) {
|
||||
(JsonValue::Object(ref mut a), JsonValue::Object(b)) => {
|
||||
for (k, v) in b {
|
||||
json_merge(a.entry(k).or_insert(JsonValue::Null), v);
|
||||
}
|
||||
}
|
||||
(JsonValue::Array(ref mut a), JsonValue::Array(b)) => {
|
||||
a.extend(b.clone());
|
||||
}
|
||||
(JsonValue::Array(ref mut a), JsonValue::Object(b)) => {
|
||||
a.extend([JsonValue::Object(b.clone())]);
|
||||
}
|
||||
(a, b) => {
|
||||
*a = b.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -271,7 +271,7 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
|
||||
.join(" ")
|
||||
),
|
||||
//TODO: It would be good to drill deeper into closures.
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id),
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
|
||||
Value::Nothing { .. } => String::new(),
|
||||
Value::Error { error, .. } => format!("{error:?}"),
|
||||
Value::Binary { val, .. } => format!("{val:?}"),
|
||||
|
@ -101,7 +101,7 @@ fn all_columns(span: Span) -> Value {
|
||||
let environment = {
|
||||
let mut env_rec = Record::new();
|
||||
for val in p.environ() {
|
||||
if let Some((key, value)) = val.split_once('=') {
|
||||
if let Some((key, value)) = val.to_string_lossy().split_once('=') {
|
||||
let is_env_var_a_list = {
|
||||
{
|
||||
#[cfg(target_family = "windows")]
|
||||
@ -146,8 +146,8 @@ fn all_columns(span: Span) -> Value {
|
||||
"root" => root,
|
||||
"cwd" => cwd,
|
||||
"exe_path" => exe_path,
|
||||
"command" => Value::string(p.cmd().join(" "), span),
|
||||
"name" => Value::string(p.name(), span),
|
||||
"command" => Value::string(p.cmd().join(std::ffi::OsStr::new(" ")).to_string_lossy(), span),
|
||||
"name" => Value::string(p.name().to_string_lossy(), span),
|
||||
"environment" => environment,
|
||||
},
|
||||
span,
|
||||
@ -177,4 +177,9 @@ fn get_thread_id() -> u64 {
|
||||
{
|
||||
nix::sys::pthread::pthread_self() as u64
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
// wasm doesn't have any threads accessible, so we return 0 as a fallback
|
||||
0
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::inspect_table;
|
||||
use nu_engine::command_prelude::*;
|
||||
use terminal_size::{terminal_size, Height, Width};
|
||||
use nu_utils::terminal_size;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Inspect;
|
||||
@ -38,12 +38,9 @@ impl Command for Inspect {
|
||||
let original_input = input_val.clone();
|
||||
let description = input_val.get_type().to_string();
|
||||
|
||||
let (cols, _rows) = match terminal_size() {
|
||||
Some((w, h)) => (Width(w.0), Height(h.0)),
|
||||
None => (Width(0), Height(0)),
|
||||
};
|
||||
let (cols, _rows) = terminal_size().unwrap_or((0, 0));
|
||||
|
||||
let table = inspect_table::build_table(input_val, description, cols.0 as usize);
|
||||
let table = inspect_table::build_table(input_val, description, cols as usize);
|
||||
|
||||
// Note that this is printed to stderr. The reason for this is so it doesn't disrupt the regular nushell
|
||||
// tabular output. If we printed to stdout, nushell would get confused with two outputs.
|
||||
|
@ -152,7 +152,7 @@ fn truncate_data(
|
||||
let left_space = expected_width - width;
|
||||
let has_space_for_truncation_column = left_space > PAD;
|
||||
if !has_space_for_truncation_column {
|
||||
peak_count -= 1;
|
||||
peak_count = peak_count.saturating_sub(1);
|
||||
}
|
||||
|
||||
remove_columns(data, peak_count);
|
||||
@ -201,11 +201,18 @@ mod util {
|
||||
Value::Record { val: record, .. } => {
|
||||
let (cols, vals): (Vec<_>, Vec<_>) = record.into_owned().into_iter().unzip();
|
||||
(
|
||||
cols,
|
||||
vec![vals
|
||||
match cols.is_empty() {
|
||||
true => vec![String::from("")],
|
||||
false => cols,
|
||||
},
|
||||
match vals
|
||||
.into_iter()
|
||||
.map(|s| debug_string_without_formatting(&s))
|
||||
.collect()],
|
||||
.collect::<Vec<String>>()
|
||||
{
|
||||
vals if vals.is_empty() => vec![],
|
||||
vals => vec![vals],
|
||||
},
|
||||
)
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
|
@ -10,6 +10,7 @@ mod metadata_set;
|
||||
mod profile;
|
||||
mod timeit;
|
||||
mod view;
|
||||
mod view_blocks;
|
||||
mod view_files;
|
||||
mod view_ir;
|
||||
mod view_source;
|
||||
@ -27,6 +28,7 @@ pub use metadata_set::MetadataSet;
|
||||
pub use profile::DebugProfile;
|
||||
pub use timeit::TimeIt;
|
||||
pub use view::View;
|
||||
pub use view_blocks::ViewBlocks;
|
||||
pub use view_files::ViewFiles;
|
||||
pub use view_ir::ViewIr;
|
||||
pub use view_source::ViewSource;
|
||||
|
@ -30,8 +30,6 @@ impl Command for DebugProfile {
|
||||
"Collect pipeline element output values",
|
||||
Some('v'),
|
||||
)
|
||||
.switch("expr", "Collect expression types", Some('x'))
|
||||
.switch("instructions", "Collect IR instructions", Some('i'))
|
||||
.switch("lines", "Collect line numbers", Some('l'))
|
||||
.named(
|
||||
"max-depth",
|
||||
@ -48,37 +46,52 @@ impl Command for DebugProfile {
|
||||
}
|
||||
|
||||
fn extra_description(&self) -> &str {
|
||||
r#"The profiler profiles every evaluated pipeline element inside a closure, stepping into all
|
||||
r#"The profiler profiles every evaluated instruction inside a closure, stepping into all
|
||||
commands calls and other blocks/closures.
|
||||
|
||||
The output can be heavily customized. By default, the following columns are included:
|
||||
- depth : Depth of the pipeline element. Each entered block adds one level of depth. How many
|
||||
- depth : Depth of the instruction. Each entered block adds one level of depth. How many
|
||||
blocks deep to step into is controlled with the --max-depth option.
|
||||
- id : ID of the pipeline element
|
||||
- parent_id : ID of the parent element
|
||||
- source : Source code of the pipeline element. If the element has multiple lines, only the
|
||||
first line is used and `...` is appended to the end. Full source code can be shown
|
||||
with the --expand-source flag.
|
||||
- duration_ms : How long it took to run the pipeline element in milliseconds.
|
||||
- (optional) span : Span of the element. Can be viewed via the `view span` command. Enabled with
|
||||
the --spans flag.
|
||||
- (optional) expr : The type of expression of the pipeline element. Enabled with the --expr flag.
|
||||
- (optional) output : The output value of the pipeline element. Enabled with the --values flag.
|
||||
- id : ID of the instruction
|
||||
- parent_id : ID of the instruction that created the parent scope
|
||||
- source : Source code that generated the instruction. If the source code has multiple lines,
|
||||
only the first line is used and `...` is appended to the end. Full source code can
|
||||
be shown with the --expand-source flag.
|
||||
- pc : The index of the instruction within the block.
|
||||
- instruction : The pretty printed instruction being evaluated.
|
||||
- duration_ms : How long it took to run the instruction in milliseconds.
|
||||
- (optional) span : Span associated with the instruction. Can be viewed via the `view span`
|
||||
command. Enabled with the --spans flag.
|
||||
- (optional) output : The output value of the instruction. Enabled with the --values flag.
|
||||
|
||||
To illustrate the depth and IDs, consider `debug profile { if true { echo 'spam' } }`. There are
|
||||
three pipeline elements:
|
||||
To illustrate the depth and IDs, consider `debug profile { do { if true { echo 'spam' } } }`. A unique ID is generated each time an instruction is executed, and there are two levels of depth:
|
||||
|
||||
depth id parent_id
|
||||
0 0 0 debug profile { do { if true { 'spam' } } }
|
||||
1 1 0 if true { 'spam' }
|
||||
2 2 1 'spam'
|
||||
```
|
||||
depth id parent_id source pc instruction
|
||||
0 0 0 debug profile { do { if true { 'spam' } } } 0 <start>
|
||||
1 1 0 { if true { 'spam' } } 0 load-literal %1, closure(2164)
|
||||
1 2 0 { if true { 'spam' } } 1 push-positional %1
|
||||
1 3 0 { do { if true { 'spam' } } } 2 redirect-out caller
|
||||
1 4 0 { do { if true { 'spam' } } } 3 redirect-err caller
|
||||
1 5 0 do 4 call decl 7 "do", %0
|
||||
2 6 5 true 0 load-literal %1, bool(true)
|
||||
2 7 5 if 1 not %1
|
||||
2 8 5 if 2 branch-if %1, 5
|
||||
2 9 5 'spam' 3 load-literal %0, string("spam")
|
||||
2 10 5 if 4 jump 6
|
||||
2 11 5 { if true { 'spam' } } 6 return %0
|
||||
1 12 0 { do { if true { 'spam' } } } 5 return %0
|
||||
```
|
||||
|
||||
Each block entered increments depth by 1 and each block left decrements it by one. This way you can
|
||||
control the profiling granularity. Passing --max-depth=1 to the above would stop at
|
||||
`if true { 'spam' }`. The id is used to identify each element. The parent_id tells you that 'spam'
|
||||
was spawned from `if true { 'spam' }` which was spawned from the root `debug profile { ... }`.
|
||||
control the profiling granularity. Passing --max-depth=1 to the above would stop inside the `do`
|
||||
at `if true { 'spam' }`. The id is used to identify each element. The parent_id tells you that the
|
||||
instructions inside the block are being executed because of `do` (5), which in turn was spawned from
|
||||
the root `debug profile { ... }`.
|
||||
|
||||
Note: In some cases, the ordering of piepeline elements might not be intuitive. For example,
|
||||
For a better understanding of how instructions map to source code, see the `view ir` command.
|
||||
|
||||
Note: In some cases, the ordering of pipeline elements might not be intuitive. For example,
|
||||
`[ a bb cc ] | each { $in | str length }` involves some implicit collects and lazy evaluation
|
||||
confusing the id/parent_id hierarchy. The --expr flag is helpful for investigating these issues."#
|
||||
}
|
||||
@ -94,8 +107,6 @@ confusing the id/parent_id hierarchy. The --expr flag is helpful for investigati
|
||||
let collect_spans = call.has_flag(engine_state, stack, "spans")?;
|
||||
let collect_expanded_source = call.has_flag(engine_state, stack, "expanded-source")?;
|
||||
let collect_values = call.has_flag(engine_state, stack, "values")?;
|
||||
let collect_exprs = call.has_flag(engine_state, stack, "expr")?;
|
||||
let collect_instructions = call.has_flag(engine_state, stack, "instructions")?;
|
||||
let collect_lines = call.has_flag(engine_state, stack, "lines")?;
|
||||
let max_depth = call
|
||||
.get_flag(engine_state, stack, "max-depth")?
|
||||
@ -108,8 +119,8 @@ confusing the id/parent_id hierarchy. The --expr flag is helpful for investigati
|
||||
collect_source: true,
|
||||
collect_expanded_source,
|
||||
collect_values,
|
||||
collect_exprs,
|
||||
collect_instructions,
|
||||
collect_exprs: false,
|
||||
collect_instructions: true,
|
||||
collect_lines,
|
||||
},
|
||||
call.span(),
|
||||
|
@ -1,4 +1,5 @@
|
||||
use nu_engine::{command_prelude::*, get_eval_block, get_eval_expression_with_input};
|
||||
use nu_engine::{command_prelude::*, ClosureEvalOnce};
|
||||
use nu_protocol::engine::Closure;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -10,16 +11,18 @@ impl Command for TimeIt {
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Time the running time of a block."
|
||||
"Time how long it takes a closure to run."
|
||||
}
|
||||
|
||||
fn extra_description(&self) -> &str {
|
||||
"Any pipeline input given to this command is passed to the closure. Note that streaming inputs may affect timing results, and it is recommended to add a `collect` command before this if the input is a stream.
|
||||
|
||||
This command will bubble up any errors encountered when running the closure. The return pipeline of the closure is collected into a value and then discarded."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("timeit")
|
||||
.required(
|
||||
"command",
|
||||
SyntaxShape::OneOf(vec![SyntaxShape::Block, SyntaxShape::Expression]),
|
||||
"The command or block to run.",
|
||||
)
|
||||
.required("command", SyntaxShape::Closure(None), "The closure to run.")
|
||||
.input_output_types(vec![
|
||||
(Type::Any, Type::Duration),
|
||||
(Type::Nothing, Type::Duration),
|
||||
@ -46,51 +49,38 @@ impl Command for TimeIt {
|
||||
// reset outdest, so the command can write to stdout and stderr.
|
||||
let stack = &mut stack.push_redirection(None, None);
|
||||
|
||||
let command_to_run = call.positional_nth(stack, 0);
|
||||
let closure: Closure = call.req(engine_state, stack, 0)?;
|
||||
let closure = ClosureEvalOnce::new_preserve_out_dest(engine_state, stack, closure);
|
||||
|
||||
// Get the start time after all other computation has been done.
|
||||
let start_time = Instant::now();
|
||||
closure.run_with_input(input)?.into_value(call.head)?;
|
||||
let time = start_time.elapsed();
|
||||
|
||||
if let Some(command_to_run) = command_to_run {
|
||||
if let Some(block_id) = command_to_run.as_block() {
|
||||
let eval_block = get_eval_block(engine_state);
|
||||
let block = engine_state.get_block(block_id);
|
||||
eval_block(engine_state, stack, block, input)?
|
||||
} else {
|
||||
let eval_expression_with_input = get_eval_expression_with_input(engine_state);
|
||||
let expression = &command_to_run.clone();
|
||||
eval_expression_with_input(engine_state, stack, expression, input)?
|
||||
}
|
||||
} else {
|
||||
PipelineData::empty()
|
||||
}
|
||||
.into_value(call.head)?;
|
||||
|
||||
let end_time = Instant::now();
|
||||
|
||||
let output = Value::duration(
|
||||
end_time.saturating_duration_since(start_time).as_nanos() as i64,
|
||||
call.head,
|
||||
);
|
||||
|
||||
let output = Value::duration(time.as_nanos() as i64, call.head);
|
||||
Ok(output.into_pipeline_data())
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Times a command within a closure",
|
||||
description: "Time a closure containing one command",
|
||||
example: "timeit { sleep 500ms }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Times a command using an existing input",
|
||||
example: "http get https://www.nushell.sh/book/ | timeit { split chars }",
|
||||
description: "Time a closure with an input value",
|
||||
example: "'A really long string' | timeit { split chars }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Times a command invocation",
|
||||
example: "timeit ls -la",
|
||||
description: "Time a closure with an input stream",
|
||||
example: "open some_file.txt | collect | timeit { split chars }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Time a closure containing a pipeline",
|
||||
example: "timeit { open some_file.txt | split chars }",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user