mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Compare commits
181 Commits
Author | SHA1 | Date | |
---|---|---|---|
b0e5723a68 | |||
9273bb3f72 | |||
f7d3ccfc70 | |||
d86350af80 | |||
14512988ba | |||
33e1120add | |||
3278d290be | |||
daec3fc3d3 | |||
a6ba58ec41 | |||
65327e0e7e | |||
3ed3712fdc | |||
f46962d236 | |||
aa4778ff07 | |||
e81689f2c0 | |||
4656310a1c | |||
34e58bc5d6 | |||
fbe9d6f529 | |||
e266590813 | |||
b27148d14b | |||
4858a9a817 | |||
3ec53e544c | |||
c52d45cb97 | |||
11531b7630 | |||
a098a27837 | |||
2591bd8c63 | |||
a03fb946d9 | |||
9c58f2a522 | |||
3cb9147f22 | |||
f1d72e2670 | |||
f1e7a01b2e | |||
b88ace4cde | |||
34d7c17e78 | |||
3f1824111d | |||
fbae137442 | |||
9850424251 | |||
918ec9daa8 | |||
7b502a4c7f | |||
7b07e976b8 | |||
e45b169cba | |||
5ebfa10495 | |||
3f93dc2f1d | |||
a43514deb2 | |||
ab77bf3289 | |||
0afe1e4e67 | |||
ef26d539a7 | |||
fce8581321 | |||
ba6abd77c9 | |||
a7295c8f1b | |||
2a310ef187 | |||
530e250573 | |||
6fbc76bc0f | |||
884382bac4 | |||
d97975e9fa | |||
839b264261 | |||
7ef4e5f940 | |||
646aace05b | |||
772ad896c8 | |||
9c4bbe3c63 | |||
c5ca839294 | |||
5337a6dffa | |||
56ce10347e | |||
37bc90c62a | |||
ad7522bba0 | |||
bbcf374886 | |||
99c42582fe | |||
5a56d47f25 | |||
529c98085a | |||
2b955f82b7 | |||
1843fdc060 | |||
ec4e3a6d5c | |||
4ab468e65f | |||
1d18f6947e | |||
df3b6d9d26 | |||
4bbdb73668 | |||
62d3497bbb | |||
e614970c08 | |||
d931331b57 | |||
f18da2609a | |||
2ef9cc118e | |||
33674d3a98 | |||
0167649e6f | |||
cc263ee15d | |||
a4809f2e68 | |||
21770367e2 | |||
6145f734b7 | |||
9d8d305e9d | |||
eb55fd2383 | |||
613d2fb8df | |||
8783742060 | |||
20528e96c7 | |||
3b6c4c1bb5 | |||
cb18dd5200 | |||
ccebdd7a7f | |||
c3efb12733 | |||
d885258dc7 | |||
2da915d0c7 | |||
ae64c58f59 | |||
ff6868b329 | |||
47ef193600 | |||
c2f4969d4f | |||
08c98967e0 | |||
8d091f6f83 | |||
58094987ff | |||
ce26ef97e4 | |||
8f9bd4a299 | |||
45dd7d8770 | |||
0f10d984c3 | |||
2e5d981a09 | |||
c74254c2cb | |||
271fda7c91 | |||
0e5886ace1 | |||
4b89c5f900 | |||
dcab255d59 | |||
fc8512be39 | |||
e10ef4aaae | |||
0b70ca8451 | |||
555d9ee763 | |||
121b801baa | |||
9adcecbbf1 | |||
9f131d998d | |||
cd0a04f02a | |||
aaf5684f9c | |||
2f0cb044a5 | |||
8b55757a0b | |||
84fae6e07e | |||
63e220a763 | |||
a96fc21f88 | |||
1ba5b25b29 | |||
a871f2344a | |||
a217bc0715 | |||
34ab4d8360 | |||
48f1c3a49e | |||
692376e830 | |||
cc99df5ef1 | |||
d255a2a050 | |||
c07835f3ad | |||
78a5067434 | |||
cdeb8de75d | |||
606547ecb4 | |||
3b809b38e8 | |||
7c49a42b68 | |||
87823b0cb5 | |||
ebf845f431 | |||
ce6df93d05 | |||
e7958bebac | |||
233afebdf0 | |||
56069af42d | |||
376d22e331 | |||
7fc8ff60fd | |||
1f4791a191 | |||
2ac7a4d48d | |||
01386f4d58 | |||
1086fbe9b5 | |||
a83bd4ab20 | |||
26caf7e1b2 | |||
dd2a0e35f4 | |||
6a4eabf5c7 | |||
0e2c888f73 | |||
c140da5740 | |||
586c0ea3d8 | |||
d6f4189c7b | |||
7a820b1304 | |||
767201c40d | |||
3c3614a120 | |||
9e24e452a5 | |||
2cffff0c1b | |||
cf2e9cf481 | |||
6b2c7a4c86 | |||
98e199f7b5 | |||
10e463180e | |||
c9d0003818 | |||
e2a21afca8 | |||
2ea209bcc0 | |||
e049ca8ebf | |||
9037a9467b | |||
4c6cf36aa5 | |||
c92211c016 | |||
8bd6b5b913 | |||
b67fe31544 | |||
c8adb06ca7 | |||
9695331eed |
2
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
2
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@ -53,7 +53,7 @@ body:
|
|||||||
| features | clipboard-cli, ctrlc, dataframe, default, rustyline, term, trash, uuid, which, zip |
|
| features | clipboard-cli, ctrlc, dataframe, default, rustyline, term, trash, uuid, which, zip |
|
||||||
| installed_plugins | binaryview, chart bar, chart line, fetch, from bson, from sqlite, inc, match, post, ps, query json, s3, selector, start, sys, textview, to bson, to sqlite, tree, xpath |
|
| installed_plugins | binaryview, chart bar, chart line, fetch, from bson, from sqlite, inc, match, post, ps, query json, s3, selector, start, sys, textview, to bson, to sqlite, tree, xpath |
|
||||||
validations:
|
validations:
|
||||||
required: false
|
required: true
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: context
|
id: context
|
||||||
attributes:
|
attributes:
|
||||||
|
1
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
1
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
@ -1,5 +1,6 @@
|
|||||||
name: Feature Request
|
name: Feature Request
|
||||||
description: "When you want a new feature for something that doesn't already exist"
|
description: "When you want a new feature for something that doesn't already exist"
|
||||||
|
labels: "enhancement"
|
||||||
body:
|
body:
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: problem
|
id: problem
|
||||||
|
21
.github/ISSUE_TEMPLATE/question.yml
vendored
Normal file
21
.github/ISSUE_TEMPLATE/question.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
name: Question
|
||||||
|
description: "When you have a question to ask"
|
||||||
|
labels: "question"
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: problem
|
||||||
|
attributes:
|
||||||
|
label: Question
|
||||||
|
description: Leave your question here
|
||||||
|
placeholder: |
|
||||||
|
A clear and concise question
|
||||||
|
Example: Is there any equivalent of bash's $CDPATH in Nu?
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: context
|
||||||
|
attributes:
|
||||||
|
label: Additional context and details
|
||||||
|
description: Add any other context, screenshots or other media that will help us understand your question here, if needed.
|
||||||
|
validations:
|
||||||
|
required: false
|
56
.github/workflows/ci.yml
vendored
56
.github/workflows/ci.yml
vendored
@ -23,12 +23,13 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Setup Rust toolchain
|
- name: Setup Rust toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
# makes ci use rust-toolchain.toml
|
||||||
profile: minimal
|
# with:
|
||||||
toolchain: ${{ matrix.rust }}
|
# profile: minimal
|
||||||
override: true
|
# toolchain: ${{ matrix.rust }}
|
||||||
components: rustfmt, clippy
|
# override: true
|
||||||
|
# components: rustfmt, clippy
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v1
|
- uses: Swatinem/rust-cache@v1
|
||||||
with:
|
with:
|
||||||
@ -74,17 +75,16 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Setup Rust toolchain
|
- name: Setup Rust toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
# makes ci use rust-toolchain.toml
|
||||||
profile: minimal
|
# with:
|
||||||
toolchain: ${{ matrix.rust }}
|
# profile: minimal
|
||||||
override: true
|
# toolchain: ${{ matrix.rust }}
|
||||||
|
# override: true
|
||||||
|
|
||||||
# Temporarily disabled; the cache was getting huge (2.6GB compressed) on Windows and causing issues.
|
- uses: Swatinem/rust-cache@v1
|
||||||
# TODO: investigate why the cache was so big
|
with:
|
||||||
# - uses: Swatinem/rust-cache@v1
|
key: ${{ matrix.style }}v3 # increment this to bust the cache if needed
|
||||||
# with:
|
|
||||||
# key: ${{ matrix.style }}v3 # increment this to bust the cache if needed
|
|
||||||
|
|
||||||
- name: Tests
|
- name: Tests
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
@ -111,11 +111,12 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Setup Rust toolchain
|
- name: Setup Rust toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
# makes ci use rust-toolchain.toml
|
||||||
profile: minimal
|
# with:
|
||||||
toolchain: ${{ matrix.rust }}
|
# profile: minimal
|
||||||
override: true
|
# toolchain: ${{ matrix.rust }}
|
||||||
|
# override: true
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v1
|
- uses: Swatinem/rust-cache@v1
|
||||||
with:
|
with:
|
||||||
@ -135,7 +136,7 @@ jobs:
|
|||||||
- run: python -m pip install tox
|
- run: python -m pip install tox
|
||||||
|
|
||||||
- name: Install virtualenv
|
- name: Install virtualenv
|
||||||
run: git clone https://github.com/pypa/virtualenv.git
|
run: git clone https://github.com/pypa/virtualenv.git
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Test Nushell in virtualenv
|
- name: Test Nushell in virtualenv
|
||||||
@ -161,11 +162,12 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Setup Rust toolchain
|
- name: Setup Rust toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
with:
|
# makes ci use rust-toolchain.toml
|
||||||
profile: minimal
|
# with:
|
||||||
toolchain: ${{ matrix.rust }}
|
# profile: minimal
|
||||||
override: true
|
# toolchain: ${{ matrix.rust }}
|
||||||
|
# override: true
|
||||||
|
|
||||||
- name: Clippy
|
- name: Clippy
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
|
41
.github/workflows/manual.yml
vendored
Normal file
41
.github/workflows/manual.yml
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# This is a basic workflow that is manually triggered
|
||||||
|
# Don't run it unless you know what you are doing
|
||||||
|
|
||||||
|
name: Manual Workflow for Winget Submission
|
||||||
|
|
||||||
|
# Controls when the action will run. Workflow runs when manually triggered using the UI
|
||||||
|
# or API.
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
# Inputs the workflow accepts.
|
||||||
|
inputs:
|
||||||
|
ver:
|
||||||
|
# Friendly description to be shown in the UI instead of 'ver'
|
||||||
|
description: 'The nushell version to release'
|
||||||
|
# Default value if no value is explicitly provided
|
||||||
|
default: '0.66.0'
|
||||||
|
# Input has to be provided for the workflow to run
|
||||||
|
required: true
|
||||||
|
uri:
|
||||||
|
# Friendly description to be shown in the UI instead of 'uri'
|
||||||
|
description: 'The nushell windows .msi package URI to publish'
|
||||||
|
# Default value if no value is explicitly provided
|
||||||
|
default: 'https://github.com/nushell/nushell/releases/download/0.66.0/nu-0.66.0-x86_64-pc-windows-msvc.msi'
|
||||||
|
# Input has to be provided for the workflow to run
|
||||||
|
required: true
|
||||||
|
|
||||||
|
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
|
||||||
|
jobs:
|
||||||
|
# This workflow contains a single job
|
||||||
|
rls-winget-pkg:
|
||||||
|
name: Publish winget package manually
|
||||||
|
# The type of runner that the job will run on
|
||||||
|
runs-on: windows-latest
|
||||||
|
|
||||||
|
# Steps represent a sequence of tasks that will be executed as part of the job
|
||||||
|
steps:
|
||||||
|
# Runs commands using the runners shell
|
||||||
|
- name: Submit package to Windows Package Manager Community Repository Manually
|
||||||
|
run: |
|
||||||
|
iwr https://github.com/microsoft/winget-create/releases/download/v1.0.4.0/wingetcreate.exe -OutFile wingetcreate.exe
|
||||||
|
.\wingetcreate.exe update Nushell.Nushell -s -v ${{ github.event.inputs.ver }} -u ${{ github.event.inputs.uri }} -t ${{ secrets.NUSHELL_PAT }}
|
6
.github/workflows/release-pkg.nu
vendored
6
.github/workflows/release-pkg.nu
vendored
@ -41,7 +41,7 @@ if $os in ['ubuntu-latest', 'macos-latest'] {
|
|||||||
} else {
|
} else {
|
||||||
# musl-tools to fix 'Failed to find tool. Is `musl-gcc` installed?'
|
# musl-tools to fix 'Failed to find tool. Is `musl-gcc` installed?'
|
||||||
# Actually just for x86_64-unknown-linux-musl target
|
# Actually just for x86_64-unknown-linux-musl target
|
||||||
sudo apt install musl-tools -y
|
if $os == 'ubuntu-latest' { sudo apt install musl-tools -y }
|
||||||
cargo-build-nu $flags
|
cargo-build-nu $flags
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,7 +113,7 @@ if $os in ['ubuntu-latest', 'macos-latest'] {
|
|||||||
cd $src; hr-line
|
cd $src; hr-line
|
||||||
# Wix need the binaries be stored in target/release/
|
# Wix need the binaries be stored in target/release/
|
||||||
cp -r $'($dist)/*' target/release/
|
cp -r $'($dist)/*' target/release/
|
||||||
cargo install cargo-wix --version 0.3.2
|
cargo install cargo-wix --version 0.3.3
|
||||||
cargo wix --no-build --nocapture --package nu --output $wixRelease
|
cargo wix --no-build --nocapture --package nu --output $wixRelease
|
||||||
echo $'::set-output name=archive::($wixRelease)'
|
echo $'::set-output name=archive::($wixRelease)'
|
||||||
|
|
||||||
@ -143,7 +143,7 @@ def 'hr-line' [
|
|||||||
--blank-line(-b): bool
|
--blank-line(-b): bool
|
||||||
] {
|
] {
|
||||||
print $'(ansi g)---------------------------------------------------------------------------->(ansi reset)'
|
print $'(ansi g)---------------------------------------------------------------------------->(ansi reset)'
|
||||||
if $blank-line { char nl }
|
if $blank_line { char nl }
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the specified env key's value or ''
|
# Get the specified env key's value or ''
|
||||||
|
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@ -70,9 +70,9 @@ jobs:
|
|||||||
target: ${{ matrix.target }}
|
target: ${{ matrix.target }}
|
||||||
|
|
||||||
- name: Setup Nushell
|
- name: Setup Nushell
|
||||||
uses: hustcer/setup-nu@v1
|
uses: hustcer/setup-nu@v2
|
||||||
with:
|
with:
|
||||||
version: 0.63.0
|
version: 0.67.0
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
2
.github/workflows/winget-submission.yml
vendored
2
.github/workflows/winget-submission.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Submit package to Windows Package Manager Community Repository
|
- name: Submit package to Windows Package Manager Community Repository
|
||||||
run: |
|
run: |
|
||||||
iwr https://aka.ms/wingetcreate/latest -OutFile wingetcreate.exe
|
iwr https://github.com/microsoft/winget-create/releases/download/v1.0.4.0/wingetcreate.exe -OutFile wingetcreate.exe
|
||||||
$github = Get-Content '${{ github.event_path }}' | ConvertFrom-Json
|
$github = Get-Content '${{ github.event_path }}' | ConvertFrom-Json
|
||||||
$installerUrl = $github.release.assets | Where-Object -Property name -match 'windows-msvc.msi' | Select -ExpandProperty browser_download_url -First 1
|
$installerUrl = $github.release.assets | Where-Object -Property name -match 'windows-msvc.msi' | Select -ExpandProperty browser_download_url -First 1
|
||||||
.\wingetcreate.exe update Nushell.Nushell -s -v $github.release.tag_name -u $installerUrl -t ${{ secrets.NUSHELL_PAT }}
|
.\wingetcreate.exe update Nushell.Nushell -s -v $github.release.tag_name -u $installerUrl -t ${{ secrets.NUSHELL_PAT }}
|
||||||
|
6
.gitignore
vendored
6
.gitignore
vendored
@ -29,3 +29,9 @@ debian/nu/
|
|||||||
# Coverage tools
|
# Coverage tools
|
||||||
lcov.info
|
lcov.info
|
||||||
tarpaulin-report.html
|
tarpaulin-report.html
|
||||||
|
|
||||||
|
# Visual Studio
|
||||||
|
.vs/*
|
||||||
|
*.rsproj
|
||||||
|
*.rsproj.user
|
||||||
|
*.sln
|
@ -66,3 +66,9 @@ cargo build
|
|||||||
```shell
|
```shell
|
||||||
cargo run --release --features=extra -- --log-level trace
|
cargo run --release --features=extra -- --log-level trace
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- To redirect trace logs to a file, enable the `--log-target file` switch.
|
||||||
|
```shell
|
||||||
|
cargo run --release --features=extra -- --log-level trace --log-target file
|
||||||
|
open $"($nu.temp-path)/nu-($nu.pid).log"
|
||||||
|
```
|
||||||
|
602
Cargo.lock
generated
602
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
44
Cargo.toml
44
Cargo.toml
@ -11,7 +11,7 @@ name = "nu"
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://github.com/nushell/nushell"
|
repository = "https://github.com/nushell/nushell"
|
||||||
rust-version = "1.60"
|
rust-version = "1.60"
|
||||||
version = "0.66.1"
|
version = "0.68.0"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
@ -33,30 +33,31 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = { version = "0.4.19", features = ["serde"] }
|
chrono = { version = "0.4.21", features = ["serde"] }
|
||||||
crossterm = "0.23.0"
|
crossterm = "0.24.0"
|
||||||
ctrlc = "3.2.1"
|
ctrlc = "3.2.1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
miette = "5.1.0"
|
miette = "5.1.0"
|
||||||
nu-ansi-term = "0.46.0"
|
nu-ansi-term = "0.46.0"
|
||||||
nu-cli = { path="./crates/nu-cli", version = "0.66.1" }
|
nu-cli = { path="./crates/nu-cli", version = "0.68.0" }
|
||||||
nu-color-config = { path = "./crates/nu-color-config", version = "0.66.1" }
|
nu-color-config = { path = "./crates/nu-color-config", version = "0.68.0" }
|
||||||
nu-command = { path="./crates/nu-command", version = "0.66.1" }
|
nu-command = { path="./crates/nu-command", version = "0.68.0" }
|
||||||
nu-engine = { path="./crates/nu-engine", version = "0.66.1" }
|
nu-engine = { path="./crates/nu-engine", version = "0.68.0" }
|
||||||
nu-json = { path="./crates/nu-json", version = "0.66.1" }
|
nu-json = { path="./crates/nu-json", version = "0.68.0" }
|
||||||
nu-parser = { path="./crates/nu-parser", version = "0.66.1" }
|
nu-parser = { path="./crates/nu-parser", version = "0.68.0" }
|
||||||
nu-path = { path="./crates/nu-path", version = "0.66.1" }
|
nu-path = { path="./crates/nu-path", version = "0.68.0" }
|
||||||
nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.66.1" }
|
nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.68.0" }
|
||||||
nu-pretty-hex = { path = "./crates/nu-pretty-hex", version = "0.66.1" }
|
nu-pretty-hex = { path = "./crates/nu-pretty-hex", version = "0.68.0" }
|
||||||
nu-protocol = { path = "./crates/nu-protocol", version = "0.66.1" }
|
nu-protocol = { path = "./crates/nu-protocol", version = "0.68.0" }
|
||||||
nu-system = { path = "./crates/nu-system", version = "0.66.1" }
|
nu-system = { path = "./crates/nu-system", version = "0.68.0" }
|
||||||
nu-table = { path = "./crates/nu-table", version = "0.66.1" }
|
nu-table = { path = "./crates/nu-table", version = "0.68.0" }
|
||||||
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.66.1" }
|
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.68.0" }
|
||||||
nu-utils = { path = "./crates/nu-utils", version = "0.66.1" }
|
nu-utils = { path = "./crates/nu-utils", version = "0.68.0" }
|
||||||
reedline = { version = "0.9.0", features = ["bashisms", "sqlite"]}
|
reedline = { version = "0.11.0", features = ["bashisms", "sqlite"]}
|
||||||
pretty_env_logger = "0.4.0"
|
|
||||||
rayon = "1.5.1"
|
rayon = "1.5.1"
|
||||||
is_executable = "1.0.1"
|
is_executable = "1.0.1"
|
||||||
|
simplelog = "0.12.0"
|
||||||
|
time = "0.3.12"
|
||||||
|
|
||||||
[target.'cfg(not(target_os = "windows"))'.dependencies]
|
[target.'cfg(not(target_os = "windows"))'.dependencies]
|
||||||
# Our dependencies don't use OpenSSL on Windows
|
# Our dependencies don't use OpenSSL on Windows
|
||||||
@ -64,13 +65,13 @@ openssl = { version = "0.10.38", features = ["vendored"], optional = true }
|
|||||||
signal-hook = { version = "0.3.14", default-features = false }
|
signal-hook = { version = "0.3.14", default-features = false }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
nu-test-support = { path="./crates/nu-test-support", version = "0.66.1" }
|
nu-test-support = { path="./crates/nu-test-support", version = "0.68.0" }
|
||||||
tempfile = "3.2.0"
|
tempfile = "3.2.0"
|
||||||
assert_cmd = "2.0.2"
|
assert_cmd = "2.0.2"
|
||||||
pretty_assertions = "1.0.0"
|
pretty_assertions = "1.0.0"
|
||||||
serial_test = "0.8.0"
|
serial_test = "0.8.0"
|
||||||
hamcrest2 = "0.3.0"
|
hamcrest2 = "0.3.0"
|
||||||
rstest = "0.15.0"
|
rstest = {version = "0.15.0", default-features = false}
|
||||||
itertools = "0.10.3"
|
itertools = "0.10.3"
|
||||||
|
|
||||||
[target.'cfg(windows)'.build-dependencies]
|
[target.'cfg(windows)'.build-dependencies]
|
||||||
@ -120,3 +121,4 @@ debug = false
|
|||||||
[[bin]]
|
[[bin]]
|
||||||
name = "nu"
|
name = "nu"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ NU_PLUGINS=(
|
|||||||
'nu_plugin_gstat'
|
'nu_plugin_gstat'
|
||||||
'nu_plugin_inc'
|
'nu_plugin_inc'
|
||||||
'nu_plugin_query'
|
'nu_plugin_query'
|
||||||
|
'nu_plugin_custom_values'
|
||||||
)
|
)
|
||||||
|
|
||||||
echo "Building nushell"
|
echo "Building nushell"
|
||||||
|
@ -24,9 +24,13 @@ cargo build
|
|||||||
@echo.
|
@echo.
|
||||||
|
|
||||||
@cd ..\..\crates\nu_plugin_query
|
@cd ..\..\crates\nu_plugin_query
|
||||||
|
|
||||||
echo Building nu_plugin_query.exe
|
echo Building nu_plugin_query.exe
|
||||||
cargo build
|
cargo build
|
||||||
@echo.
|
@echo.
|
||||||
|
|
||||||
|
@cd ..\..\crates\nu_plugin_custom_values
|
||||||
|
echo Building nu_plugin_custom_values.exe
|
||||||
|
cargo build
|
||||||
|
@echo.
|
||||||
|
|
||||||
@cd ..\..
|
@cd ..\..
|
@ -11,6 +11,7 @@ let plugins = [
|
|||||||
nu_plugin_gstat,
|
nu_plugin_gstat,
|
||||||
nu_plugin_query,
|
nu_plugin_query,
|
||||||
nu_plugin_example,
|
nu_plugin_example,
|
||||||
|
nu_plugin_custom_values,
|
||||||
]
|
]
|
||||||
|
|
||||||
for plugin in $plugins {
|
for plugin in $plugins {
|
||||||
|
@ -1,36 +1,39 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["The Nushell Project Developers"]
|
authors = ["The Nushell Project Developers"]
|
||||||
description = "CLI-related functionality for Nushell"
|
description = "CLI-related functionality for Nushell"
|
||||||
|
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cli"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu-cli"
|
name = "nu-cli"
|
||||||
version = "0.66.1"
|
version = "0.68.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
nu-test-support = { path="../nu-test-support", version = "0.66.1" }
|
nu-test-support = { path="../nu-test-support", version = "0.68.0" }
|
||||||
nu-command = { path = "../nu-command", version = "0.66.1" }
|
nu-command = { path = "../nu-command", version = "0.68.0" }
|
||||||
rstest = "0.15.0"
|
rstest = {version = "0.15.0", default-features = false}
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-engine = { path = "../nu-engine", version = "0.66.1" }
|
nu-engine = { path = "../nu-engine", version = "0.68.0" }
|
||||||
nu-path = { path = "../nu-path", version = "0.66.1" }
|
nu-path = { path = "../nu-path", version = "0.68.0" }
|
||||||
nu-parser = { path = "../nu-parser", version = "0.66.1" }
|
nu-parser = { path = "../nu-parser", version = "0.68.0" }
|
||||||
nu-protocol = { path = "../nu-protocol", version = "0.66.1" }
|
nu-protocol = { path = "../nu-protocol", version = "0.68.0" }
|
||||||
nu-utils = { path = "../nu-utils", version = "0.66.1" }
|
nu-utils = { path = "../nu-utils", version = "0.68.0" }
|
||||||
nu-ansi-term = "0.46.0"
|
nu-ansi-term = "0.46.0"
|
||||||
nu-color-config = { path = "../nu-color-config", version = "0.66.1" }
|
nu-color-config = { path = "../nu-color-config", version = "0.68.0" }
|
||||||
reedline = { version = "0.9.0", features = ["bashisms", "sqlite"]}
|
reedline = { version = "0.11.0", features = ["bashisms", "sqlite"]}
|
||||||
crossterm = "0.23.0"
|
|
||||||
miette = { version = "5.1.0", features = ["fancy"] }
|
|
||||||
thiserror = "1.0.31"
|
|
||||||
fuzzy-matcher = "0.3.7"
|
|
||||||
|
|
||||||
chrono = "0.4.19"
|
atty = "0.2.14"
|
||||||
|
chrono = "0.4.21"
|
||||||
|
crossterm = "0.24.0"
|
||||||
|
fancy-regex = "0.10.0"
|
||||||
|
fuzzy-matcher = "0.3.7"
|
||||||
is_executable = "1.0.1"
|
is_executable = "1.0.1"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
regex = "1.5.4"
|
miette = { version = "5.1.0", features = ["fancy"] }
|
||||||
sysinfo = "0.24.1"
|
strip-ansi-escapes = "0.1.1"
|
||||||
|
sysinfo = "0.25.2"
|
||||||
|
thiserror = "1.0.31"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
plugin = []
|
plugin = []
|
||||||
|
@ -11,6 +11,7 @@ pub struct CommandCompletion {
|
|||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
flattened: Vec<(Span, FlatShape)>,
|
flattened: Vec<(Span, FlatShape)>,
|
||||||
flat_shape: FlatShape,
|
flat_shape: FlatShape,
|
||||||
|
force_completion_after_space: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandCompletion {
|
impl CommandCompletion {
|
||||||
@ -19,11 +20,13 @@ impl CommandCompletion {
|
|||||||
_: &StateWorkingSet,
|
_: &StateWorkingSet,
|
||||||
flattened: Vec<(Span, FlatShape)>,
|
flattened: Vec<(Span, FlatShape)>,
|
||||||
flat_shape: FlatShape,
|
flat_shape: FlatShape,
|
||||||
|
force_completion_after_space: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
engine_state,
|
engine_state,
|
||||||
flattened,
|
flattened,
|
||||||
flat_shape,
|
flat_shape,
|
||||||
|
force_completion_after_space,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +119,8 @@ impl CommandCompletion {
|
|||||||
|
|
||||||
let partial = working_set.get_span_contents(span);
|
let partial = working_set.get_span_contents(span);
|
||||||
let partial = String::from_utf8_lossy(partial).to_string();
|
let partial = String::from_utf8_lossy(partial).to_string();
|
||||||
let results = if find_externals {
|
|
||||||
|
if find_externals {
|
||||||
let results_external = self
|
let results_external = self
|
||||||
.external_command_completion(&partial, match_algorithm)
|
.external_command_completion(&partial, match_algorithm)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -148,9 +152,7 @@ impl CommandCompletion {
|
|||||||
results
|
results
|
||||||
} else {
|
} else {
|
||||||
results
|
results
|
||||||
};
|
}
|
||||||
|
|
||||||
results
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,6 +209,10 @@ impl Completer for CommandCompletion {
|
|||||||
|| ((span.end - span.start) == 0)
|
|| ((span.end - span.start) == 0)
|
||||||
{
|
{
|
||||||
// we're in a gap or at a command
|
// we're in a gap or at a command
|
||||||
|
if working_set.get_span_contents(span).is_empty() && !self.force_completion_after_space
|
||||||
|
{
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
self.complete_commands(
|
self.complete_commands(
|
||||||
working_set,
|
working_set,
|
||||||
span,
|
span,
|
||||||
|
@ -2,10 +2,11 @@ use crate::completions::{
|
|||||||
CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion,
|
CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion,
|
||||||
DotNuCompletion, FileCompletion, FlagCompletion, MatchAlgorithm, VariableCompletion,
|
DotNuCompletion, FileCompletion, FlagCompletion, MatchAlgorithm, VariableCompletion,
|
||||||
};
|
};
|
||||||
|
use nu_engine::eval_block;
|
||||||
use nu_parser::{flatten_expression, parse, FlatShape};
|
use nu_parser::{flatten_expression, parse, FlatShape};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
Span,
|
BlockId, PipelineData, Span, Value,
|
||||||
};
|
};
|
||||||
use reedline::{Completer as ReedlineCompleter, Suggestion};
|
use reedline::{Completer as ReedlineCompleter, Suggestion};
|
||||||
use std::str;
|
use std::str;
|
||||||
@ -56,6 +57,67 @@ impl NuCompleter {
|
|||||||
suggestions
|
suggestions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn external_completion(
|
||||||
|
&self,
|
||||||
|
block_id: BlockId,
|
||||||
|
spans: Vec<String>,
|
||||||
|
offset: usize,
|
||||||
|
span: Span,
|
||||||
|
) -> Vec<Suggestion> {
|
||||||
|
let stack = self.stack.clone();
|
||||||
|
let block = self.engine_state.get_block(block_id);
|
||||||
|
let mut callee_stack = stack.gather_captures(&block.captures);
|
||||||
|
|
||||||
|
// Line
|
||||||
|
if let Some(pos_arg) = block.signature.required_positional.get(0) {
|
||||||
|
if let Some(var_id) = pos_arg.var_id {
|
||||||
|
callee_stack.add_var(
|
||||||
|
var_id,
|
||||||
|
Value::List {
|
||||||
|
vals: spans
|
||||||
|
.into_iter()
|
||||||
|
.map(|it| Value::String {
|
||||||
|
val: it,
|
||||||
|
span: Span::unknown(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
span: Span::unknown(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = eval_block(
|
||||||
|
&self.engine_state,
|
||||||
|
&mut callee_stack,
|
||||||
|
block,
|
||||||
|
PipelineData::new(span),
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(pd) => {
|
||||||
|
let value = pd.into_value(span);
|
||||||
|
if let Value::List { vals, span: _ } = value {
|
||||||
|
let result = map_value_completions(
|
||||||
|
vals.iter(),
|
||||||
|
Span {
|
||||||
|
start: span.start,
|
||||||
|
end: span.end,
|
||||||
|
},
|
||||||
|
offset,
|
||||||
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => println!("failed to eval completer block: {}", err),
|
||||||
|
}
|
||||||
|
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
|
||||||
fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
|
fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
|
||||||
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||||||
let offset = working_set.next_span_start();
|
let offset = working_set.next_span_start();
|
||||||
@ -63,14 +125,32 @@ impl NuCompleter {
|
|||||||
let initial_line = line.to_string();
|
let initial_line = line.to_string();
|
||||||
new_line.push(b'a');
|
new_line.push(b'a');
|
||||||
let pos = offset + pos;
|
let pos = offset + pos;
|
||||||
|
let config = self.engine_state.get_config();
|
||||||
|
|
||||||
let (output, _err) = parse(&mut working_set, Some("completer"), &new_line, false, &[]);
|
let (output, _err) = parse(&mut working_set, Some("completer"), &new_line, false, &[]);
|
||||||
|
|
||||||
for pipeline in output.pipelines.into_iter() {
|
for pipeline in output.pipelines.into_iter() {
|
||||||
for expr in pipeline.expressions {
|
for expr in pipeline.expressions {
|
||||||
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
|
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
|
||||||
let span_offset: usize = alias_offset.iter().sum();
|
let span_offset: usize = alias_offset.iter().sum();
|
||||||
|
let mut spans: Vec<String> = vec![];
|
||||||
|
|
||||||
for (flat_idx, flat) in flattened.iter().enumerate() {
|
for (flat_idx, flat) in flattened.iter().enumerate() {
|
||||||
|
// Read the current spam to string
|
||||||
|
let current_span = working_set.get_span_contents(flat.0).to_vec();
|
||||||
|
let current_span_str = String::from_utf8_lossy(¤t_span);
|
||||||
|
|
||||||
|
// Skip the last 'a' as span item
|
||||||
|
if flat_idx == flattened.len() - 1 {
|
||||||
|
let mut chars = current_span_str.chars();
|
||||||
|
chars.next_back();
|
||||||
|
let current_span_str = chars.as_str().to_owned();
|
||||||
|
spans.push(current_span_str.to_string());
|
||||||
|
} else {
|
||||||
|
spans.push(current_span_str.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete based on the last span
|
||||||
if pos + span_offset >= flat.0.start && pos + span_offset < flat.0.end {
|
if pos + span_offset >= flat.0.start && pos + span_offset < flat.0.end {
|
||||||
// Context variables
|
// Context variables
|
||||||
let most_left_var =
|
let most_left_var =
|
||||||
@ -113,8 +193,38 @@ impl NuCompleter {
|
|||||||
|
|
||||||
// Flags completion
|
// Flags completion
|
||||||
if prefix.starts_with(b"-") {
|
if prefix.starts_with(b"-") {
|
||||||
let mut completer = FlagCompletion::new(expr);
|
// Try to complete flag internally
|
||||||
|
let mut completer = FlagCompletion::new(expr.clone());
|
||||||
|
let result = self.process_completion(
|
||||||
|
&mut completer,
|
||||||
|
&working_set,
|
||||||
|
prefix.clone(),
|
||||||
|
new_span,
|
||||||
|
offset,
|
||||||
|
pos,
|
||||||
|
);
|
||||||
|
|
||||||
|
if !result.is_empty() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We got no results for internal completion
|
||||||
|
// now we can check if external completer is set and use it
|
||||||
|
if let Some(block_id) = config.external_completer {
|
||||||
|
return self.external_completion(block_id, spans, offset, new_span);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// specially check if it is currently empty - always complete commands
|
||||||
|
if flat_idx == 0 && working_set.get_span_contents(new_span).is_empty() {
|
||||||
|
let mut completer = CommandCompletion::new(
|
||||||
|
self.engine_state.clone(),
|
||||||
|
&working_set,
|
||||||
|
flattened.clone(),
|
||||||
|
// flat_idx,
|
||||||
|
FlatShape::String,
|
||||||
|
true,
|
||||||
|
);
|
||||||
return self.process_completion(
|
return self.process_completion(
|
||||||
&mut completer,
|
&mut completer,
|
||||||
&working_set,
|
&working_set,
|
||||||
@ -125,7 +235,7 @@ impl NuCompleter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Completions that depends on the previous expression (e.g: use, source)
|
// Completions that depends on the previous expression (e.g: use, source-env)
|
||||||
if flat_idx > 0 {
|
if flat_idx > 0 {
|
||||||
if let Some(previous_expr) = flattened.get(flat_idx - 1) {
|
if let Some(previous_expr) = flattened.get(flat_idx - 1) {
|
||||||
// Read the content for the previous expression
|
// Read the content for the previous expression
|
||||||
@ -133,7 +243,7 @@ impl NuCompleter {
|
|||||||
working_set.get_span_contents(previous_expr.0).to_vec();
|
working_set.get_span_contents(previous_expr.0).to_vec();
|
||||||
|
|
||||||
// Completion for .nu files
|
// Completion for .nu files
|
||||||
if prev_expr_str == b"use" || prev_expr_str == b"source" {
|
if prev_expr_str == b"use" || prev_expr_str == b"source-env" {
|
||||||
let mut completer =
|
let mut completer =
|
||||||
DotNuCompletion::new(self.engine_state.clone());
|
DotNuCompletion::new(self.engine_state.clone());
|
||||||
|
|
||||||
@ -212,9 +322,10 @@ impl NuCompleter {
|
|||||||
flattened.clone(),
|
flattened.clone(),
|
||||||
// flat_idx,
|
// flat_idx,
|
||||||
flat_shape.clone(),
|
flat_shape.clone(),
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
let out: Vec<_> = self.process_completion(
|
let mut out: Vec<_> = self.process_completion(
|
||||||
&mut completer,
|
&mut completer,
|
||||||
&working_set,
|
&working_set,
|
||||||
prefix.clone(),
|
prefix.clone(),
|
||||||
@ -223,21 +334,30 @@ impl NuCompleter {
|
|||||||
pos,
|
pos,
|
||||||
);
|
);
|
||||||
|
|
||||||
if out.is_empty() {
|
if !out.is_empty() {
|
||||||
let mut completer =
|
return out;
|
||||||
FileCompletion::new(self.engine_state.clone());
|
|
||||||
|
|
||||||
return self.process_completion(
|
|
||||||
&mut completer,
|
|
||||||
&working_set,
|
|
||||||
prefix,
|
|
||||||
new_span,
|
|
||||||
offset,
|
|
||||||
pos,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
// Check for file completion
|
||||||
|
let mut completer = FileCompletion::new(self.engine_state.clone());
|
||||||
|
out = self.process_completion(
|
||||||
|
&mut completer,
|
||||||
|
&working_set,
|
||||||
|
prefix,
|
||||||
|
new_span,
|
||||||
|
offset,
|
||||||
|
pos,
|
||||||
|
);
|
||||||
|
|
||||||
|
if !out.is_empty() {
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to complete using an exnternal compelter (if set)
|
||||||
|
if let Some(block_id) = config.external_completer {
|
||||||
|
return self
|
||||||
|
.external_completion(block_id, spans, offset, new_span);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -245,7 +365,7 @@ impl NuCompleter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return vec![];
|
vec![]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,3 +503,65 @@ fn most_left_variable(
|
|||||||
|
|
||||||
Some((var, sublevels))
|
Some((var, sublevels))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn map_value_completions<'a>(
|
||||||
|
list: impl Iterator<Item = &'a Value>,
|
||||||
|
span: Span,
|
||||||
|
offset: usize,
|
||||||
|
) -> Vec<Suggestion> {
|
||||||
|
list.filter_map(move |x| {
|
||||||
|
// Match for string values
|
||||||
|
if let Ok(s) = x.as_string() {
|
||||||
|
return Some(Suggestion {
|
||||||
|
value: s,
|
||||||
|
description: None,
|
||||||
|
extra: None,
|
||||||
|
span: reedline::Span {
|
||||||
|
start: span.start - offset,
|
||||||
|
end: span.end - offset,
|
||||||
|
},
|
||||||
|
append_whitespace: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match for record values
|
||||||
|
if let Ok((cols, vals)) = x.as_record() {
|
||||||
|
let mut suggestion = Suggestion {
|
||||||
|
value: String::from(""), // Initialize with empty string
|
||||||
|
description: None,
|
||||||
|
extra: None,
|
||||||
|
span: reedline::Span {
|
||||||
|
start: span.start - offset,
|
||||||
|
end: span.end - offset,
|
||||||
|
},
|
||||||
|
append_whitespace: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Iterate the cols looking for `value` and `description`
|
||||||
|
cols.iter().zip(vals).for_each(|it| {
|
||||||
|
// Match `value` column
|
||||||
|
if it.0 == "value" {
|
||||||
|
// Convert the value to string
|
||||||
|
if let Ok(val_str) = it.1.as_string() {
|
||||||
|
// Update the suggestion value
|
||||||
|
suggestion.value = val_str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match `description` column
|
||||||
|
if it.0 == "description" {
|
||||||
|
// Convert the value to string
|
||||||
|
if let Ok(desc_str) = it.1.as_string() {
|
||||||
|
// Update the suggestion value
|
||||||
|
suggestion.description = Some(desc_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Some(suggestion);
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
@ -8,6 +8,8 @@ use nu_protocol::{
|
|||||||
use reedline::Suggestion;
|
use reedline::Suggestion;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::completer::map_value_completions;
|
||||||
|
|
||||||
pub struct CustomCompletion {
|
pub struct CustomCompletion {
|
||||||
engine_state: Arc<EngineState>,
|
engine_state: Arc<EngineState>,
|
||||||
stack: Stack,
|
stack: Stack,
|
||||||
@ -26,69 +28,6 @@ impl CustomCompletion {
|
|||||||
sort_by: SortBy::None,
|
sort_by: SortBy::None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_completions<'a>(
|
|
||||||
&self,
|
|
||||||
list: impl Iterator<Item = &'a Value>,
|
|
||||||
span: Span,
|
|
||||||
offset: usize,
|
|
||||||
) -> Vec<Suggestion> {
|
|
||||||
list.filter_map(move |x| {
|
|
||||||
// Match for string values
|
|
||||||
if let Ok(s) = x.as_string() {
|
|
||||||
return Some(Suggestion {
|
|
||||||
value: s,
|
|
||||||
description: None,
|
|
||||||
extra: None,
|
|
||||||
span: reedline::Span {
|
|
||||||
start: span.start - offset,
|
|
||||||
end: span.end - offset,
|
|
||||||
},
|
|
||||||
append_whitespace: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match for record values
|
|
||||||
if let Ok((cols, vals)) = x.as_record() {
|
|
||||||
let mut suggestion = Suggestion {
|
|
||||||
value: String::from(""), // Initialize with empty string
|
|
||||||
description: None,
|
|
||||||
extra: None,
|
|
||||||
span: reedline::Span {
|
|
||||||
start: span.start - offset,
|
|
||||||
end: span.end - offset,
|
|
||||||
},
|
|
||||||
append_whitespace: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Iterate the cols looking for `value` and `description`
|
|
||||||
cols.iter().zip(vals).for_each(|it| {
|
|
||||||
// Match `value` column
|
|
||||||
if it.0 == "value" {
|
|
||||||
// Convert the value to string
|
|
||||||
if let Ok(val_str) = it.1.as_string() {
|
|
||||||
// Update the suggestion value
|
|
||||||
suggestion.value = val_str;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match `description` column
|
|
||||||
if it.0 == "description" {
|
|
||||||
// Convert the value to string
|
|
||||||
if let Ok(desc_str) = it.1.as_string() {
|
|
||||||
// Update the suggestion value
|
|
||||||
suggestion.description = Some(desc_str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return Some(suggestion);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Completer for CustomCompletion {
|
impl Completer for CustomCompletion {
|
||||||
@ -144,7 +83,7 @@ impl Completer for CustomCompletion {
|
|||||||
.and_then(|val| {
|
.and_then(|val| {
|
||||||
val.as_list()
|
val.as_list()
|
||||||
.ok()
|
.ok()
|
||||||
.map(|it| self.map_completions(it.iter(), span, offset))
|
.map(|it| map_value_completions(it.iter(), span, offset))
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let options = value.get_data_by_key("options");
|
let options = value.get_data_by_key("options");
|
||||||
@ -189,7 +128,7 @@ impl Completer for CustomCompletion {
|
|||||||
|
|
||||||
completions
|
completions
|
||||||
}
|
}
|
||||||
Value::List { vals, .. } => self.map_completions(vals.iter(), span, offset),
|
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
|
||||||
_ => vec![],
|
_ => vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use {
|
use {
|
||||||
nu_ansi_term::{ansi::RESET, Style},
|
nu_ansi_term::{ansi::RESET, Style},
|
||||||
reedline::{
|
reedline::{
|
||||||
menu_functions::string_difference, Completer, LineBuffer, Menu, MenuEvent, MenuTextStyle,
|
menu_functions::string_difference, Completer, Editor, Menu, MenuEvent, MenuTextStyle,
|
||||||
Painter, Suggestion,
|
Painter, Suggestion, UndoBehavior,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -459,7 +459,7 @@ impl Menu for DescriptionMenu {
|
|||||||
fn can_partially_complete(
|
fn can_partially_complete(
|
||||||
&mut self,
|
&mut self,
|
||||||
_values_updated: bool,
|
_values_updated: bool,
|
||||||
_line_buffer: &mut LineBuffer,
|
_editor: &mut Editor,
|
||||||
_completer: &mut dyn Completer,
|
_completer: &mut dyn Completer,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
false
|
false
|
||||||
@ -481,19 +481,21 @@ impl Menu for DescriptionMenu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Updates menu values
|
/// Updates menu values
|
||||||
fn update_values(&mut self, line_buffer: &mut LineBuffer, completer: &mut dyn Completer) {
|
fn update_values(&mut self, editor: &mut Editor, completer: &mut dyn Completer) {
|
||||||
if self.only_buffer_difference {
|
if self.only_buffer_difference {
|
||||||
if let Some(old_string) = &self.input {
|
if let Some(old_string) = &self.input {
|
||||||
let (start, input) = string_difference(line_buffer.get_buffer(), old_string);
|
let (start, input) = string_difference(editor.get_buffer(), old_string);
|
||||||
if !input.is_empty() {
|
if !input.is_empty() {
|
||||||
self.reset_position();
|
self.reset_position();
|
||||||
self.values = completer.complete(input, start);
|
self.values = completer.complete(input, start);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let trimmed_buffer = line_buffer.get_buffer().replace('\n', " ");
|
let trimmed_buffer = editor.get_buffer().replace('\n', " ");
|
||||||
self.values =
|
self.values = completer.complete(
|
||||||
completer.complete(trimmed_buffer.as_str(), line_buffer.insertion_point());
|
trimmed_buffer.as_str(),
|
||||||
|
editor.line_buffer().insertion_point(),
|
||||||
|
);
|
||||||
self.reset_position();
|
self.reset_position();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -502,7 +504,7 @@ impl Menu for DescriptionMenu {
|
|||||||
/// collected from the completer
|
/// collected from the completer
|
||||||
fn update_working_details(
|
fn update_working_details(
|
||||||
&mut self,
|
&mut self,
|
||||||
line_buffer: &mut LineBuffer,
|
editor: &mut Editor,
|
||||||
completer: &mut dyn Completer,
|
completer: &mut dyn Completer,
|
||||||
painter: &Painter,
|
painter: &Painter,
|
||||||
) {
|
) {
|
||||||
@ -560,13 +562,13 @@ impl Menu for DescriptionMenu {
|
|||||||
match event {
|
match event {
|
||||||
MenuEvent::Activate(_) => {
|
MenuEvent::Activate(_) => {
|
||||||
self.reset_position();
|
self.reset_position();
|
||||||
self.input = Some(line_buffer.get_buffer().to_string());
|
self.input = Some(editor.get_buffer().to_string());
|
||||||
self.update_values(line_buffer, completer);
|
self.update_values(editor, completer);
|
||||||
}
|
}
|
||||||
MenuEvent::Deactivate => self.active = false,
|
MenuEvent::Deactivate => self.active = false,
|
||||||
MenuEvent::Edit(_) => {
|
MenuEvent::Edit(_) => {
|
||||||
self.reset_position();
|
self.reset_position();
|
||||||
self.update_values(line_buffer, completer);
|
self.update_values(editor, completer);
|
||||||
self.update_examples()
|
self.update_examples()
|
||||||
}
|
}
|
||||||
MenuEvent::NextElement => {
|
MenuEvent::NextElement => {
|
||||||
@ -627,27 +629,28 @@ impl Menu for DescriptionMenu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The buffer gets replaced in the Span location
|
/// The buffer gets replaced in the Span location
|
||||||
fn replace_in_buffer(&self, line_buffer: &mut LineBuffer) {
|
fn replace_in_buffer(&self, editor: &mut Editor) {
|
||||||
if let Some(Suggestion { value, span, .. }) = self.get_value() {
|
if let Some(Suggestion { value, span, .. }) = self.get_value() {
|
||||||
let start = span.start.min(line_buffer.len());
|
let start = span.start.min(editor.line_buffer().len());
|
||||||
let end = span.end.min(line_buffer.len());
|
let end = span.end.min(editor.line_buffer().len());
|
||||||
|
|
||||||
let string_len = if let Some(example_index) = self.example_index {
|
let replacement = if let Some(example_index) = self.example_index {
|
||||||
let example = self
|
self.examples
|
||||||
.examples
|
|
||||||
.get(example_index)
|
.get(example_index)
|
||||||
.expect("the example index is always checked");
|
.expect("the example index is always checked")
|
||||||
|
|
||||||
line_buffer.replace(start..end, example);
|
|
||||||
example.len()
|
|
||||||
} else {
|
} else {
|
||||||
line_buffer.replace(start..end, &value);
|
&value
|
||||||
value.len()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut offset = line_buffer.insertion_point();
|
editor.edit_buffer(
|
||||||
offset += string_len.saturating_sub(end.saturating_sub(start));
|
|lb| {
|
||||||
line_buffer.set_insertion_point(offset);
|
lb.replace_range(start..end, replacement);
|
||||||
|
let mut offset = lb.insertion_point();
|
||||||
|
offset += lb.len().saturating_sub(end.saturating_sub(start));
|
||||||
|
lb.set_insertion_point(offset);
|
||||||
|
},
|
||||||
|
UndoBehavior::CreateUndoPoint,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -814,7 +814,6 @@ fn event_from_record(
|
|||||||
) -> Result<ReedlineEvent, ShellError> {
|
) -> Result<ReedlineEvent, ShellError> {
|
||||||
let event = match name {
|
let event = match name {
|
||||||
"none" => ReedlineEvent::None,
|
"none" => ReedlineEvent::None,
|
||||||
"actionhandler" => ReedlineEvent::ActionHandler,
|
|
||||||
"clearscreen" => ReedlineEvent::ClearScreen,
|
"clearscreen" => ReedlineEvent::ClearScreen,
|
||||||
"clearscrollback" => ReedlineEvent::ClearScrollback,
|
"clearscrollback" => ReedlineEvent::ClearScrollback,
|
||||||
"historyhintcomplete" => ReedlineEvent::HistoryHintComplete,
|
"historyhintcomplete" => ReedlineEvent::HistoryHintComplete,
|
||||||
|
@ -5,6 +5,7 @@ use crate::{
|
|||||||
util::{eval_source, get_guaranteed_cwd, report_error, report_error_new},
|
util::{eval_source, get_guaranteed_cwd, report_error, report_error_new},
|
||||||
NuHighlighter, NuValidator, NushellPrompt,
|
NuHighlighter, NuValidator, NushellPrompt,
|
||||||
};
|
};
|
||||||
|
use fancy_regex::Regex;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use log::{info, trace, warn};
|
use log::{info, trace, warn};
|
||||||
use miette::{IntoDiagnostic, Result};
|
use miette::{IntoDiagnostic, Result};
|
||||||
@ -14,12 +15,13 @@ use nu_parser::{lex, parse};
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::PathMember,
|
ast::PathMember,
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
BlockId, HistoryFileFormat, PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId,
|
format_duration, BlockId, HistoryFileFormat, PipelineData, PositionalArg, ShellError, Span,
|
||||||
|
Spanned, Type, Value, VarId,
|
||||||
};
|
};
|
||||||
use reedline::{DefaultHinter, Emacs, SqliteBackedHistory, Vi};
|
use reedline::{DefaultHinter, Emacs, SqliteBackedHistory, Vi};
|
||||||
use regex::Regex;
|
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::{sync::atomic::Ordering, time::Instant};
|
use std::{sync::atomic::Ordering, time::Instant};
|
||||||
|
use strip_ansi_escapes::strip;
|
||||||
use sysinfo::SystemExt;
|
use sysinfo::SystemExt;
|
||||||
|
|
||||||
// According to Daniel Imms @Tyriar, we need to do these this way:
|
// According to Daniel Imms @Tyriar, we need to do these this way:
|
||||||
@ -37,9 +39,20 @@ pub fn evaluate_repl(
|
|||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
nushell_path: &str,
|
nushell_path: &str,
|
||||||
is_perf_true: bool,
|
is_perf_true: bool,
|
||||||
|
prerun_command: Option<Spanned<String>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
use reedline::{FileBackedHistory, Reedline, Signal};
|
use reedline::{FileBackedHistory, Reedline, Signal};
|
||||||
|
|
||||||
|
// Guard against invocation without a connected terminal.
|
||||||
|
// reedline / crossterm event polling will fail without a connected tty
|
||||||
|
if !atty::is(atty::Stream::Stdin) {
|
||||||
|
return Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::NotFound,
|
||||||
|
"Nushell launched as interactive REPL but STDIN is not a TTY, either launch in a valid terminal or provide arguments to invoke a script!",
|
||||||
|
))
|
||||||
|
.into_diagnostic();
|
||||||
|
}
|
||||||
|
|
||||||
let mut entry_num = 0;
|
let mut entry_num = 0;
|
||||||
|
|
||||||
let mut nu_prompt = NushellPrompt::new();
|
let mut nu_prompt = NushellPrompt::new();
|
||||||
@ -119,6 +132,36 @@ pub fn evaluate_repl(
|
|||||||
|
|
||||||
let sys = sysinfo::System::new();
|
let sys = sysinfo::System::new();
|
||||||
|
|
||||||
|
let show_banner = config.show_banner;
|
||||||
|
let use_ansi = config.use_ansi_coloring;
|
||||||
|
if show_banner {
|
||||||
|
let banner = get_banner(engine_state, stack);
|
||||||
|
if use_ansi {
|
||||||
|
println!("{}", banner);
|
||||||
|
} else {
|
||||||
|
let stripped_string = {
|
||||||
|
if let Ok(bytes) = strip(&banner) {
|
||||||
|
String::from_utf8_lossy(&bytes).to_string()
|
||||||
|
} else {
|
||||||
|
banner
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{}", stripped_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(s) = prerun_command {
|
||||||
|
eval_source(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
s.item.as_bytes(),
|
||||||
|
&format!("entry #{}", entry_num),
|
||||||
|
PipelineData::new(Span::new(0, 0)),
|
||||||
|
);
|
||||||
|
engine_state.merge_env(stack, get_guaranteed_cwd(engine_state, stack))?;
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if is_perf_true {
|
if is_perf_true {
|
||||||
info!(
|
info!(
|
||||||
@ -291,7 +334,8 @@ pub fn evaluate_repl(
|
|||||||
Ok(Signal::Success(s)) => {
|
Ok(Signal::Success(s)) => {
|
||||||
let history_supports_meta =
|
let history_supports_meta =
|
||||||
matches!(config.history_file_format, HistoryFileFormat::Sqlite);
|
matches!(config.history_file_format, HistoryFileFormat::Sqlite);
|
||||||
if history_supports_meta && !s.is_empty() {
|
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
|
||||||
|
{
|
||||||
line_editor
|
line_editor
|
||||||
.update_last_command_context(&|mut c| {
|
.update_last_command_context(&|mut c| {
|
||||||
c.start_timestamp = Some(chrono::Utc::now());
|
c.start_timestamp = Some(chrono::Utc::now());
|
||||||
@ -312,24 +356,7 @@ pub fn evaluate_repl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if shell_integration {
|
if shell_integration {
|
||||||
run_ansi_sequence(RESET_APPLICATION_MODE)?;
|
|
||||||
run_ansi_sequence(PRE_EXECUTE_MARKER)?;
|
run_ansi_sequence(PRE_EXECUTE_MARKER)?;
|
||||||
// if let Some(cwd) = stack.get_env_var(engine_state, "PWD") {
|
|
||||||
// let path = cwd.as_string()?;
|
|
||||||
// // Try to abbreviate string for windows title
|
|
||||||
// let maybe_abbrev_path = if let Some(p) = nu_path::home_dir() {
|
|
||||||
// path.replace(&p.as_path().display().to_string(), "~")
|
|
||||||
// } else {
|
|
||||||
// path
|
|
||||||
// };
|
|
||||||
|
|
||||||
// // Set window title too
|
|
||||||
// // https://tldp.org/HOWTO/Xterm-Title-3.html
|
|
||||||
// // ESC]0;stringBEL -- Set icon name and window title to string
|
|
||||||
// // ESC]1;stringBEL -- Set icon name to string
|
|
||||||
// // ESC]2;stringBEL -- Set window title to string
|
|
||||||
// run_ansi_sequence(&format!("\x1b]2;{}\x07", maybe_abbrev_path))?;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_time = Instant::now();
|
let start_time = Instant::now();
|
||||||
@ -391,9 +418,23 @@ pub fn evaluate_repl(
|
|||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let last_shell = stack.get_env_var(engine_state, "NUSHELL_LAST_SHELL");
|
||||||
|
let last_shell = if let Some(v) = last_shell {
|
||||||
|
v.as_integer().unwrap_or_default() as usize
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
shells[current_shell] = Value::String { val: path, span };
|
shells[current_shell] = Value::String { val: path, span };
|
||||||
|
|
||||||
stack.add_env_var("NUSHELL_SHELLS".into(), Value::List { vals: shells, span });
|
stack.add_env_var("NUSHELL_SHELLS".into(), Value::List { vals: shells, span });
|
||||||
|
stack.add_env_var(
|
||||||
|
"NUSHELL_LAST_SHELL".into(),
|
||||||
|
Value::Int {
|
||||||
|
val: last_shell as i64,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
trace!("eval source: {}", s);
|
trace!("eval source: {}", s);
|
||||||
|
|
||||||
@ -415,7 +456,8 @@ pub fn evaluate_repl(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if history_supports_meta && !s.is_empty() {
|
if history_supports_meta && !s.is_empty() && line_editor.has_last_command_context()
|
||||||
|
{
|
||||||
line_editor
|
line_editor
|
||||||
.update_last_command_context(&|mut c| {
|
.update_last_command_context(&|mut c| {
|
||||||
c.duration = Some(cmd_duration);
|
c.duration = Some(cmd_duration);
|
||||||
@ -445,6 +487,7 @@ pub fn evaluate_repl(
|
|||||||
// ESC]2;stringBEL -- Set window title to string
|
// ESC]2;stringBEL -- Set window title to string
|
||||||
run_ansi_sequence(&format!("\x1b]2;{}\x07", maybe_abbrev_path))?;
|
run_ansi_sequence(&format!("\x1b]2;{}\x07", maybe_abbrev_path))?;
|
||||||
}
|
}
|
||||||
|
run_ansi_sequence(RESET_APPLICATION_MODE)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Signal::CtrlC) => {
|
Ok(Signal::CtrlC) => {
|
||||||
@ -465,6 +508,10 @@ pub fn evaluate_repl(
|
|||||||
let message = err.to_string();
|
let message = err.to_string();
|
||||||
if !message.contains("duration") {
|
if !message.contains("duration") {
|
||||||
println!("Error: {:?}", err);
|
println!("Error: {:?}", err);
|
||||||
|
// TODO: Identify possible error cases where a hard failure is preferable
|
||||||
|
// Ignoring and reporting could hide bigger problems
|
||||||
|
// e.g. https://github.com/nushell/nushell/issues/6452
|
||||||
|
// Alternatively only allow that expected failures let the REPL loop
|
||||||
}
|
}
|
||||||
if shell_integration {
|
if shell_integration {
|
||||||
run_ansi_sequence(&get_command_finished_marker(stack, engine_state))?;
|
run_ansi_sequence(&get_command_finished_marker(stack, engine_state))?;
|
||||||
@ -476,6 +523,115 @@ pub fn evaluate_repl(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_banner(engine_state: &mut EngineState, stack: &mut Stack) -> String {
|
||||||
|
let age = match eval_string_with_input(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
None,
|
||||||
|
"(date now) - ('05/10/2019' | into datetime)",
|
||||||
|
) {
|
||||||
|
Ok(Value::Duration { val, .. }) => format_duration(val),
|
||||||
|
_ => "".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let banner = format!(
|
||||||
|
r#"{} __ ,
|
||||||
|
{} .--()°'.' {}Welcome to {}Nushell{},
|
||||||
|
{}'|, . ,' {}based on the {}nu{} language,
|
||||||
|
{} !_-(_\ {}where all data is structured!
|
||||||
|
|
||||||
|
Please join our {}Discord{} community at {}https://discord.gg/NtAbbGn{}
|
||||||
|
Our {}GitHub{} repository is at {}https://github.com/nushell/nushell{}
|
||||||
|
Our {}Documentation{} is located at {}http://nushell.sh{}
|
||||||
|
{}Tweet{} us at {}@nu_shell{}
|
||||||
|
|
||||||
|
{}Nushell{} has been around for:
|
||||||
|
{}
|
||||||
|
|
||||||
|
{}You can disable this banner using the {}config nu{}{} command
|
||||||
|
to modify the config.nu file and setting show_banner to false.
|
||||||
|
|
||||||
|
let-env config {{
|
||||||
|
show_banner: false
|
||||||
|
...
|
||||||
|
}}{}
|
||||||
|
"#,
|
||||||
|
"\x1b[32m", //start line 1 green
|
||||||
|
"\x1b[32m", //start line 2
|
||||||
|
"\x1b[0m", //before welcome
|
||||||
|
"\x1b[32m", //before nushell
|
||||||
|
"\x1b[0m", //after nushell
|
||||||
|
"\x1b[32m", //start line 3
|
||||||
|
"\x1b[0m", //before based
|
||||||
|
"\x1b[32m", //before nu
|
||||||
|
"\x1b[0m", //after nu
|
||||||
|
"\x1b[32m", //start line 4
|
||||||
|
"\x1b[0m", //before where
|
||||||
|
"\x1b[35m", //before Discord purple
|
||||||
|
"\x1b[0m", //after Discord
|
||||||
|
"\x1b[35m", //before Discord URL
|
||||||
|
"\x1b[0m", //after Discord URL
|
||||||
|
"\x1b[1;32m", //before GitHub green_bold
|
||||||
|
"\x1b[0m", //after GitHub
|
||||||
|
"\x1b[1;32m", //before GitHub URL
|
||||||
|
"\x1b[0m", //after GitHub URL
|
||||||
|
"\x1b[32m", //before Documentation
|
||||||
|
"\x1b[0m", //after Documentation
|
||||||
|
"\x1b[32m", //before Documentation URL
|
||||||
|
"\x1b[0m", //after Documentation URL
|
||||||
|
"\x1b[36m", //before Tweet blue
|
||||||
|
"\x1b[0m", //after Tweet
|
||||||
|
"\x1b[1;36m", //before @nu_shell cyan_bold
|
||||||
|
"\x1b[0m", //after @nu_shell
|
||||||
|
"\x1b[32m", //before Nushell
|
||||||
|
"\x1b[0m", //after Nushell
|
||||||
|
age,
|
||||||
|
"\x1b[2;37m", //before banner disable dim white
|
||||||
|
"\x1b[2;36m", //before config nu dim cyan
|
||||||
|
"\x1b[0m", //after config nu
|
||||||
|
"\x1b[2;37m", //after config nu dim white
|
||||||
|
"\x1b[0m", //after banner disable
|
||||||
|
);
|
||||||
|
|
||||||
|
banner
|
||||||
|
}
|
||||||
|
|
||||||
|
// Taken from Nana's simple_eval
|
||||||
|
/// Evaluate a block of Nu code, optionally with input.
|
||||||
|
/// For example, source="$in * 2" will multiply the value in input by 2.
|
||||||
|
pub fn eval_string_with_input(
|
||||||
|
engine_state: &mut EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
input: Option<Value>,
|
||||||
|
source: &str,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
let (block, delta) = {
|
||||||
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
let (output, _) = parse(&mut working_set, None, source.as_bytes(), false, &[]);
|
||||||
|
|
||||||
|
(output, working_set.render())
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(err) = engine_state.merge_delta(delta) {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let input_as_pipeline_data = match input {
|
||||||
|
Some(input) => PipelineData::Value(input, None),
|
||||||
|
None => PipelineData::new(Span::test_data()),
|
||||||
|
};
|
||||||
|
|
||||||
|
eval_block(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
&block,
|
||||||
|
input_as_pipeline_data,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.map(|x| x.into_value(Span::test_data()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) -> String {
|
pub fn get_command_finished_marker(stack: &Stack, engine_state: &EngineState) -> String {
|
||||||
let exit_code = stack
|
let exit_code = stack
|
||||||
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
.get_env_var(engine_state, "LAST_EXIT_CODE")
|
||||||
@ -658,9 +814,6 @@ pub fn eval_hook(
|
|||||||
for var_id in var_ids.iter() {
|
for var_id in var_ids.iter() {
|
||||||
stack.vars.remove(var_id);
|
stack.vars.remove(var_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
let cwd = get_guaranteed_cwd(engine_state, stack);
|
|
||||||
engine_state.merge_env(stack, cwd)?;
|
|
||||||
}
|
}
|
||||||
Value::Block {
|
Value::Block {
|
||||||
val: block_id,
|
val: block_id,
|
||||||
@ -668,8 +821,6 @@ pub fn eval_hook(
|
|||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
run_hook_block(engine_state, stack, block_id, arguments, block_span)?;
|
run_hook_block(engine_state, stack, block_id, arguments, block_span)?;
|
||||||
let cwd = get_guaranteed_cwd(engine_state, stack);
|
|
||||||
engine_state.merge_env(stack, cwd)?;
|
|
||||||
}
|
}
|
||||||
other => {
|
other => {
|
||||||
return Err(ShellError::UnsupportedConfigValue(
|
return Err(ShellError::UnsupportedConfigValue(
|
||||||
@ -697,6 +848,9 @@ pub fn eval_hook(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let cwd = get_guaranteed_cwd(engine_state, stack);
|
||||||
|
engine_state.merge_env(stack, cwd)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -789,7 +943,7 @@ lazy_static! {
|
|||||||
fn looks_like_path(orig: &str) -> bool {
|
fn looks_like_path(orig: &str) -> bool {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
{
|
{
|
||||||
if DRIVE_PATH_REGEX.is_match(orig) {
|
if DRIVE_PATH_REGEX.is_match(orig).unwrap_or(false) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use support::{match_suggestions, new_engine};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn alias_of_command_and_flags() {
|
|
||||||
let (dir, _, 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());
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let suggestions = completer.complete("ll t", 4);
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec!["test_a\\".to_string(), "test_b\\".to_string()];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec!["test_a/".to_string(), "test_b/".to_string()];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn alias_of_basic_command() {
|
|
||||||
let (dir, _, 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());
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let suggestions = completer.complete("ll t", 4);
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec!["test_a\\".to_string(), "test_b\\".to_string()];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec!["test_a/".to_string(), "test_b/".to_string()];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn alias_of_another_alias() {
|
|
||||||
let (dir, _, 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());
|
|
||||||
// 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());
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let suggestions = completer.complete("lf t", 4);
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec!["test_a\\".to_string(), "test_b\\".to_string()];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec!["test_a/".to_string(), "test_b/".to_string()];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
660
crates/nu-cli/tests/completions.rs
Normal file
660
crates/nu-cli/tests/completions.rs
Normal file
@ -0,0 +1,660 @@
|
|||||||
|
pub mod support;
|
||||||
|
|
||||||
|
use nu_cli::NuCompleter;
|
||||||
|
use nu_parser::parse;
|
||||||
|
use nu_protocol::engine::StateWorkingSet;
|
||||||
|
use reedline::{Completer, Suggestion};
|
||||||
|
use rstest::{fixture, rstest};
|
||||||
|
use support::{file, folder, match_suggestions, new_engine};
|
||||||
|
|
||||||
|
#[fixture]
|
||||||
|
fn completer() -> NuCompleter {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, _, 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());
|
||||||
|
|
||||||
|
// Instantiate a new completer
|
||||||
|
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[fixture]
|
||||||
|
fn completer_strings() -> NuCompleter {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, _, 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());
|
||||||
|
|
||||||
|
// Instantiate a new completer
|
||||||
|
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn variables_completions_double_dash_argument(mut completer: NuCompleter) {
|
||||||
|
let suggestions = completer.complete("tst --", 6);
|
||||||
|
let expected: Vec<String> = vec!["--help".into(), "--mod".into()];
|
||||||
|
// dbg!(&expected, &suggestions);
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn variables_completions_single_dash_argument(mut completer: NuCompleter) {
|
||||||
|
let suggestions = completer.complete("tst -", 5);
|
||||||
|
let expected: Vec<String> = vec!["--help".into(), "--mod".into(), "-h".into(), "-s".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn variables_completions_command(mut completer_strings: NuCompleter) {
|
||||||
|
let suggestions = completer_strings.complete("my-command ", 9);
|
||||||
|
let expected: Vec<String> = vec!["my-command".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn variables_completions_subcommands(mut completer_strings: NuCompleter) {
|
||||||
|
let suggestions = completer_strings.complete("my-command ", 11);
|
||||||
|
let expected: Vec<String> = vec!["cat".into(), "dog".into(), "eel".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn variables_completions_subcommands_2(mut completer_strings: NuCompleter) {
|
||||||
|
let suggestions = completer_strings.complete("my-command ", 11);
|
||||||
|
let expected: Vec<String> = vec!["cat".into(), "dog".into(), "eel".into()];
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dotnu_completions() {
|
||||||
|
// Create a new engine
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
// Instatiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
// Test source completion
|
||||||
|
let completion_str = "source-env ".to_string();
|
||||||
|
let suggestions = completer.complete(&completion_str, completion_str.len());
|
||||||
|
|
||||||
|
assert_eq!(1, suggestions.len());
|
||||||
|
assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value);
|
||||||
|
|
||||||
|
// Test use completion
|
||||||
|
let completion_str = "use ".to_string();
|
||||||
|
let suggestions = completer.complete(&completion_str, completion_str.len());
|
||||||
|
|
||||||
|
assert_eq!(1, suggestions.len());
|
||||||
|
assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn external_completer_trailing_space() {
|
||||||
|
// https://github.com/nushell/nushell/issues/6378
|
||||||
|
let block = "let external_completer = {|spans| $spans}";
|
||||||
|
let input = "gh alias ".to_string();
|
||||||
|
|
||||||
|
let suggestions = run_external_completion(&block, &input);
|
||||||
|
assert_eq!(3, suggestions.len());
|
||||||
|
assert_eq!("gh", suggestions.get(0).unwrap().value);
|
||||||
|
assert_eq!("alias", suggestions.get(1).unwrap().value);
|
||||||
|
assert_eq!("", suggestions.get(2).unwrap().value);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_completer_no_trailing_space() {
|
||||||
|
let block = "let external_completer = {|spans| $spans}";
|
||||||
|
let input = "gh alias".to_string();
|
||||||
|
|
||||||
|
let suggestions = run_external_completion(&block, &input);
|
||||||
|
assert_eq!(2, suggestions.len());
|
||||||
|
assert_eq!("gh", suggestions.get(0).unwrap().value);
|
||||||
|
assert_eq!("alias", suggestions.get(1).unwrap().value);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn external_completer_pass_flags() {
|
||||||
|
let block = "let external_completer = {|spans| $spans}";
|
||||||
|
let input = "gh api --".to_string();
|
||||||
|
|
||||||
|
let suggestions = run_external_completion(&block, &input);
|
||||||
|
assert_eq!(3, suggestions.len());
|
||||||
|
assert_eq!("gh", suggestions.get(0).unwrap().value);
|
||||||
|
assert_eq!("api", suggestions.get(1).unwrap().value);
|
||||||
|
assert_eq!("--", suggestions.get(2).unwrap().value);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_completions() {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, dir_str, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
// Instatiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
// Test completions for the current folder
|
||||||
|
let target_dir = format!("cp {}", dir_str);
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
file(dir.join("nushell")),
|
||||||
|
folder(dir.join("test_a")),
|
||||||
|
folder(dir.join("test_b")),
|
||||||
|
folder(dir.join("another")),
|
||||||
|
file(dir.join("custom_completion.nu")),
|
||||||
|
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!("cp {}", 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]
|
||||||
|
fn command_ls_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "ls ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn command_open_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "open ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn command_rm_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "rm ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn command_cp_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "cp ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn command_save_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "save ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn command_touch_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "touch ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn command_watch_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "watch ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn flag_completions() {
|
||||||
|
// Create a new engine
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
// Instatiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
// Test completions for the 'ls' flags
|
||||||
|
let suggestions = completer.complete("ls -", 4);
|
||||||
|
|
||||||
|
assert_eq!(14, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec![
|
||||||
|
"--all".into(),
|
||||||
|
"--directory".into(),
|
||||||
|
"--du".into(),
|
||||||
|
"--full-paths".into(),
|
||||||
|
"--help".into(),
|
||||||
|
"--long".into(),
|
||||||
|
"--short-names".into(),
|
||||||
|
"-D".into(),
|
||||||
|
"-a".into(),
|
||||||
|
"-d".into(),
|
||||||
|
"-f".into(),
|
||||||
|
"-h".into(),
|
||||||
|
"-l".into(),
|
||||||
|
"-s".into(),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn folder_completions() {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, dir_str, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
// Instatiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
// Test completions for the current folder
|
||||||
|
let target_dir = format!("cd {}", dir_str);
|
||||||
|
let suggestions = completer.complete(&target_dir, target_dir.len());
|
||||||
|
|
||||||
|
// Create the expected values
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
folder(dir.join("test_a")),
|
||||||
|
folder(dir.join("test_b")),
|
||||||
|
folder(dir.join("another")),
|
||||||
|
folder(dir.join(".hidden_folder")),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Match the results
|
||||||
|
match_suggestions(expected_paths, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn variables_completions() {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, _, 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());
|
||||||
|
|
||||||
|
// Instatiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
// Test completions for $nu
|
||||||
|
let suggestions = completer.complete("$nu.", 4);
|
||||||
|
|
||||||
|
assert_eq!(9, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec![
|
||||||
|
"config-path".into(),
|
||||||
|
"env-path".into(),
|
||||||
|
"history-path".into(),
|
||||||
|
"home-path".into(),
|
||||||
|
"loginshell-path".into(),
|
||||||
|
"os-info".into(),
|
||||||
|
"pid".into(),
|
||||||
|
"scope".into(),
|
||||||
|
"temp-path".into(),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
|
||||||
|
// Test completions for $nu.h (filter)
|
||||||
|
let suggestions = completer.complete("$nu.h", 5);
|
||||||
|
|
||||||
|
assert_eq!(2, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec!["history-path".into(), "home-path".into()];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
|
||||||
|
// Test completions for custom var
|
||||||
|
let suggestions = completer.complete("$actor.", 7);
|
||||||
|
|
||||||
|
assert_eq!(2, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec!["age".into(), "name".into()];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
|
||||||
|
// Test completions for custom var (filtering)
|
||||||
|
let suggestions = completer.complete("$actor.n", 8);
|
||||||
|
|
||||||
|
assert_eq!(1, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec!["name".into()];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
|
||||||
|
// Test completions for $env
|
||||||
|
let suggestions = completer.complete("$env.", 5);
|
||||||
|
|
||||||
|
assert_eq!(2, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec!["PWD".into(), "TEST".into()];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
|
||||||
|
// Test completions for $env
|
||||||
|
let suggestions = completer.complete("$env.T", 6);
|
||||||
|
|
||||||
|
assert_eq!(1, suggestions.len());
|
||||||
|
|
||||||
|
let expected: Vec<String> = vec!["TEST".into()];
|
||||||
|
|
||||||
|
// Match results
|
||||||
|
match_suggestions(expected, suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alias_of_command_and_flags() {
|
||||||
|
let (dir, _, 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());
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let suggestions = completer.complete("ll t", 4);
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec!["test_a\\".to_string(), "test_b\\".to_string()];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec!["test_a/".to_string(), "test_b/".to_string()];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alias_of_basic_command() {
|
||||||
|
let (dir, _, 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());
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let suggestions = completer.complete("ll t", 4);
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec!["test_a\\".to_string(), "test_b\\".to_string()];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec!["test_a/".to_string(), "test_b/".to_string()];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn alias_of_another_alias() {
|
||||||
|
let (dir, _, 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());
|
||||||
|
// 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());
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let suggestions = completer.complete("lf t", 4);
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec!["test_a\\".to_string(), "test_b\\".to_string()];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec!["test_a/".to_string(), "test_b/".to_string()];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_external_completion(block: &str, input: &str) -> Vec<Suggestion> {
|
||||||
|
// Create a new engine
|
||||||
|
let (dir, _, mut engine_state, mut stack) = new_engine();
|
||||||
|
let (_, delta) = {
|
||||||
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||||
|
let (block, err) = parse(&mut working_set, None, block.as_bytes(), false, &[]);
|
||||||
|
assert!(err.is_none());
|
||||||
|
|
||||||
|
(block, working_set.render())
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(engine_state.merge_delta(delta).is_ok());
|
||||||
|
|
||||||
|
// Merge environment into the permanent state
|
||||||
|
assert!(engine_state.merge_env(&mut stack, &dir).is_ok());
|
||||||
|
|
||||||
|
let latest_block_id = engine_state.num_blocks() - 1;
|
||||||
|
|
||||||
|
// Change config adding the external completer
|
||||||
|
let mut config = engine_state.get_config().clone();
|
||||||
|
config.external_completer = Some(latest_block_id);
|
||||||
|
engine_state.set_config(&config);
|
||||||
|
|
||||||
|
// Instatiate a new completer
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine_state), stack);
|
||||||
|
|
||||||
|
completer.complete(&input, input.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_command_completion() {
|
||||||
|
let (_, _, engine, stack) = new_engine();
|
||||||
|
|
||||||
|
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
||||||
|
|
||||||
|
let target_dir = "thiscommanddoesnotexist ";
|
||||||
|
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a\\".to_string(),
|
||||||
|
"test_b\\".to_string(),
|
||||||
|
"another\\".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder\\".to_string(),
|
||||||
|
];
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
let expected_paths: Vec<String> = vec![
|
||||||
|
"nushell".to_string(),
|
||||||
|
"test_a/".to_string(),
|
||||||
|
"test_b/".to_string(),
|
||||||
|
"another/".to_string(),
|
||||||
|
"custom_completion.nu".to_string(),
|
||||||
|
".hidden_file".to_string(),
|
||||||
|
".hidden_folder/".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
match_suggestions(expected_paths, suggestions)
|
||||||
|
}
|
@ -1,69 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use rstest::{fixture, rstest};
|
|
||||||
use support::{match_suggestions, new_engine};
|
|
||||||
|
|
||||||
#[fixture]
|
|
||||||
fn completer() -> NuCompleter {
|
|
||||||
// Create a new engine
|
|
||||||
let (dir, _, 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());
|
|
||||||
|
|
||||||
// Instantiate a new completer
|
|
||||||
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fixture]
|
|
||||||
fn completer_strings() -> NuCompleter {
|
|
||||||
// Create a new engine
|
|
||||||
let (dir, _, 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());
|
|
||||||
|
|
||||||
// Instantiate a new completer
|
|
||||||
NuCompleter::new(std::sync::Arc::new(engine), stack)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
fn variables_completions_double_dash_argument(mut completer: NuCompleter) {
|
|
||||||
let suggestions = completer.complete("tst --", 6);
|
|
||||||
let expected: Vec<String> = vec!["--help".into(), "--mod".into()];
|
|
||||||
// dbg!(&expected, &suggestions);
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
fn variables_completions_single_dash_argument(mut completer: NuCompleter) {
|
|
||||||
let suggestions = completer.complete("tst -", 5);
|
|
||||||
let expected: Vec<String> = vec!["--help".into(), "--mod".into(), "-h".into(), "-s".into()];
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
fn variables_completions_command(mut completer_strings: NuCompleter) {
|
|
||||||
let suggestions = completer_strings.complete("my-command ", 9);
|
|
||||||
let expected: Vec<String> = vec!["my-command".into()];
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
fn variables_completions_subcommands(mut completer_strings: NuCompleter) {
|
|
||||||
let suggestions = completer_strings.complete("my-command ", 11);
|
|
||||||
let expected: Vec<String> = vec!["cat".into(), "dog".into(), "eel".into()];
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
fn variables_completions_subcommands_2(mut completer_strings: NuCompleter) {
|
|
||||||
let suggestions = completer_strings.complete("my-command ", 11);
|
|
||||||
let expected: Vec<String> = vec!["cat".into(), "dog".into(), "eel".into()];
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use support::new_engine;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dotnu_completions() {
|
|
||||||
// Create a new engine
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
// Instatiate a new completer
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
// Test source completion
|
|
||||||
let completion_str = "source ".to_string();
|
|
||||||
let suggestions = completer.complete(&completion_str, completion_str.len());
|
|
||||||
|
|
||||||
assert_eq!(1, suggestions.len());
|
|
||||||
assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value);
|
|
||||||
|
|
||||||
// Test use completion
|
|
||||||
let completion_str = "use ".to_string();
|
|
||||||
let suggestions = completer.complete(&completion_str, completion_str.len());
|
|
||||||
|
|
||||||
assert_eq!(1, suggestions.len());
|
|
||||||
assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value);
|
|
||||||
}
|
|
@ -1,272 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use support::{file, folder, match_suggestions, new_engine};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn file_completions() {
|
|
||||||
// Create a new engine
|
|
||||||
let (dir, dir_str, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
// Instatiate a new completer
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
// Test completions for the current folder
|
|
||||||
let target_dir = format!("cp {}", dir_str);
|
|
||||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
|
||||||
|
|
||||||
// Create the expected values
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
file(dir.join("nushell")),
|
|
||||||
folder(dir.join("test_a")),
|
|
||||||
folder(dir.join("test_b")),
|
|
||||||
folder(dir.join("another")),
|
|
||||||
file(dir.join("custom_completion.nu")),
|
|
||||||
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!("cp {}", 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]
|
|
||||||
fn command_ls_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "ls ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn command_open_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "open ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn command_rm_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "rm ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn command_cp_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "cp ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn command_save_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "save ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn command_touch_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "touch ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn command_watch_completion() {
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
let target_dir = "watch ";
|
|
||||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a\\".to_string(),
|
|
||||||
"test_b\\".to_string(),
|
|
||||||
"another\\".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder\\".to_string(),
|
|
||||||
];
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
"nushell".to_string(),
|
|
||||||
"test_a/".to_string(),
|
|
||||||
"test_b/".to_string(),
|
|
||||||
"another/".to_string(),
|
|
||||||
"custom_completion.nu".to_string(),
|
|
||||||
".hidden_file".to_string(),
|
|
||||||
".hidden_folder/".to_string(),
|
|
||||||
];
|
|
||||||
|
|
||||||
match_suggestions(expected_paths, suggestions)
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use support::{match_suggestions, new_engine};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn flag_completions() {
|
|
||||||
// Create a new engine
|
|
||||||
let (_, _, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
// Instatiate a new completer
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
// Test completions for the 'ls' flags
|
|
||||||
let suggestions = completer.complete("ls -", 4);
|
|
||||||
|
|
||||||
assert_eq!(14, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec![
|
|
||||||
"--all".into(),
|
|
||||||
"--directory".into(),
|
|
||||||
"--du".into(),
|
|
||||||
"--full-paths".into(),
|
|
||||||
"--help".into(),
|
|
||||||
"--long".into(),
|
|
||||||
"--short-names".into(),
|
|
||||||
"-D".into(),
|
|
||||||
"-a".into(),
|
|
||||||
"-d".into(),
|
|
||||||
"-f".into(),
|
|
||||||
"-h".into(),
|
|
||||||
"-l".into(),
|
|
||||||
"-s".into(),
|
|
||||||
];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use support::{folder, match_suggestions, new_engine};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn folder_completions() {
|
|
||||||
// Create a new engine
|
|
||||||
let (dir, dir_str, engine, stack) = new_engine();
|
|
||||||
|
|
||||||
// Instatiate a new completer
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
// Test completions for the current folder
|
|
||||||
let target_dir = format!("cd {}", dir_str);
|
|
||||||
let suggestions = completer.complete(&target_dir, target_dir.len());
|
|
||||||
|
|
||||||
// Create the expected values
|
|
||||||
let expected_paths: Vec<String> = vec![
|
|
||||||
folder(dir.join("test_a")),
|
|
||||||
folder(dir.join("test_b")),
|
|
||||||
folder(dir.join("another")),
|
|
||||||
folder(dir.join(".hidden_folder")),
|
|
||||||
];
|
|
||||||
|
|
||||||
// Match the results
|
|
||||||
match_suggestions(expected_paths, suggestions);
|
|
||||||
}
|
|
@ -1,88 +0,0 @@
|
|||||||
pub mod support;
|
|
||||||
|
|
||||||
use nu_cli::NuCompleter;
|
|
||||||
use reedline::Completer;
|
|
||||||
use support::{match_suggestions, new_engine};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn variables_completions() {
|
|
||||||
// Create a new engine
|
|
||||||
let (dir, _, 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());
|
|
||||||
|
|
||||||
// Instatiate a new completer
|
|
||||||
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
|
|
||||||
|
|
||||||
// Test completions for $nu
|
|
||||||
let suggestions = completer.complete("$nu.", 4);
|
|
||||||
|
|
||||||
assert_eq!(9, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec![
|
|
||||||
"config-path".into(),
|
|
||||||
"env-path".into(),
|
|
||||||
"history-path".into(),
|
|
||||||
"home-path".into(),
|
|
||||||
"loginshell-path".into(),
|
|
||||||
"os-info".into(),
|
|
||||||
"pid".into(),
|
|
||||||
"scope".into(),
|
|
||||||
"temp-path".into(),
|
|
||||||
];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
|
|
||||||
// Test completions for $nu.h (filter)
|
|
||||||
let suggestions = completer.complete("$nu.h", 5);
|
|
||||||
|
|
||||||
assert_eq!(2, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec!["history-path".into(), "home-path".into()];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
|
|
||||||
// Test completions for custom var
|
|
||||||
let suggestions = completer.complete("$actor.", 7);
|
|
||||||
|
|
||||||
assert_eq!(2, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec!["age".into(), "name".into()];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
|
|
||||||
// Test completions for custom var (filtering)
|
|
||||||
let suggestions = completer.complete("$actor.n", 8);
|
|
||||||
|
|
||||||
assert_eq!(1, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec!["name".into()];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
|
|
||||||
// Test completions for $env
|
|
||||||
let suggestions = completer.complete("$env.", 5);
|
|
||||||
|
|
||||||
assert_eq!(2, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec!["PWD".into(), "TEST".into()];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
|
|
||||||
// Test completions for $env
|
|
||||||
let suggestions = completer.complete("$env.T", 6);
|
|
||||||
|
|
||||||
assert_eq!(1, suggestions.len());
|
|
||||||
|
|
||||||
let expected: Vec<String> = vec!["TEST".into()];
|
|
||||||
|
|
||||||
// Match results
|
|
||||||
match_suggestions(expected, suggestions);
|
|
||||||
}
|
|
@ -1,14 +1,15 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["The Nushell Project Developers"]
|
authors = ["The Nushell Project Developers"]
|
||||||
description = "Color configuration code used by Nushell"
|
description = "Color configuration code used by Nushell"
|
||||||
|
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-color-config"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu-color-config"
|
name = "nu-color-config"
|
||||||
version = "0.66.1"
|
version = "0.68.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-protocol = { path = "../nu-protocol", version = "0.66.1" }
|
nu-protocol = { path = "../nu-protocol", version = "0.68.0" }
|
||||||
nu-ansi-term = "0.46.0"
|
nu-ansi-term = "0.46.0"
|
||||||
nu-json = { path = "../nu-json", version = "0.66.1" }
|
nu-json = { path = "../nu-json", version = "0.68.0" }
|
||||||
nu-table = { path = "../nu-table", version = "0.66.1" }
|
nu-table = { path = "../nu-table", version = "0.68.0" }
|
||||||
serde = { version="1.0.123", features=["derive"] }
|
serde = { version="1.0.123", features=["derive"] }
|
||||||
|
@ -11,12 +11,12 @@ pub struct NuStyle {
|
|||||||
pub fn parse_nustyle(nu_style: NuStyle) -> Style {
|
pub fn parse_nustyle(nu_style: NuStyle) -> Style {
|
||||||
// get the nu_ansi_term::Color foreground color
|
// get the nu_ansi_term::Color foreground color
|
||||||
let fg_color = match nu_style.fg {
|
let fg_color = match nu_style.fg {
|
||||||
Some(fg) => color_from_hex(&fg).expect("error with foreground color"),
|
Some(fg) => color_from_hex(&fg).unwrap_or_default(),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
// get the nu_ansi_term::Color background color
|
// get the nu_ansi_term::Color background color
|
||||||
let bg_color = match nu_style.bg {
|
let bg_color = match nu_style.bg {
|
||||||
Some(bg) => color_from_hex(&bg).expect("error with background color"),
|
Some(bg) => color_from_hex(&bg).unwrap_or_default(),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
// get the attributes
|
// get the attributes
|
||||||
|
@ -1,29 +1,31 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["The Nushell Project Developers"]
|
authors = ["The Nushell Project Developers"]
|
||||||
description = "Nushell's built-in commands"
|
description = "Nushell's built-in commands"
|
||||||
|
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-command"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
name = "nu-command"
|
name = "nu-command"
|
||||||
version = "0.66.1"
|
version = "0.68.0"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nu-color-config = { path = "../nu-color-config", version = "0.66.1" }
|
nu-color-config = { path = "../nu-color-config", version = "0.68.0" }
|
||||||
nu-engine = { path = "../nu-engine", version = "0.66.1" }
|
nu-engine = { path = "../nu-engine", version = "0.68.0" }
|
||||||
nu-glob = { path = "../nu-glob", version = "0.66.1" }
|
nu-glob = { path = "../nu-glob", version = "0.68.0" }
|
||||||
nu-json = { path = "../nu-json", version = "0.66.1" }
|
nu-json = { path = "../nu-json", version = "0.68.0" }
|
||||||
nu-parser = { path = "../nu-parser", version = "0.66.1" }
|
nu-parser = { path = "../nu-parser", version = "0.68.0" }
|
||||||
nu-path = { path = "../nu-path", version = "0.66.1" }
|
nu-path = { path = "../nu-path", version = "0.68.0" }
|
||||||
nu-pretty-hex = { path = "../nu-pretty-hex", version = "0.66.1" }
|
nu-pretty-hex = { path = "../nu-pretty-hex", version = "0.68.0" }
|
||||||
nu-protocol = { path = "../nu-protocol", version = "0.66.1" }
|
nu-protocol = { path = "../nu-protocol", version = "0.68.0" }
|
||||||
nu-system = { path = "../nu-system", version = "0.66.1" }
|
nu-system = { path = "../nu-system", version = "0.68.0" }
|
||||||
nu-table = { path = "../nu-table", version = "0.66.1" }
|
nu-table = { path = "../nu-table", version = "0.68.0" }
|
||||||
nu-term-grid = { path = "../nu-term-grid", version = "0.66.1" }
|
nu-term-grid = { path = "../nu-term-grid", version = "0.68.0" }
|
||||||
nu-test-support = { path = "../nu-test-support", version = "0.66.1" }
|
nu-test-support = { path = "../nu-test-support", version = "0.68.0" }
|
||||||
nu-utils = { path = "../nu-utils", version = "0.66.1" }
|
nu-utils = { path = "../nu-utils", version = "0.68.0" }
|
||||||
nu-ansi-term = "0.46.0"
|
nu-ansi-term = "0.46.0"
|
||||||
|
num-format = { version = "0.4.0" }
|
||||||
|
|
||||||
# Potential dependencies for extras
|
# Potential dependencies for extras
|
||||||
alphanumeric-sort = "1.4.4"
|
alphanumeric-sort = "1.4.4"
|
||||||
@ -31,16 +33,17 @@ base64 = "0.13.0"
|
|||||||
byteorder = "1.4.3"
|
byteorder = "1.4.3"
|
||||||
bytesize = "1.1.0"
|
bytesize = "1.1.0"
|
||||||
calamine = "0.18.0"
|
calamine = "0.18.0"
|
||||||
chrono = { version = "0.4.19", features = ["serde"] }
|
chrono = { version = "0.4.21", features = ["serde", "unstable-locales"] }
|
||||||
chrono-humanize = "0.2.1"
|
chrono-humanize = "0.2.1"
|
||||||
chrono-tz = "0.6.1"
|
chrono-tz = "0.6.3"
|
||||||
crossterm = "0.23.0"
|
crossterm = "0.24.0"
|
||||||
csv = "1.1.6"
|
csv = "1.1.6"
|
||||||
dialoguer = "0.9.0"
|
dialoguer = "0.9.0"
|
||||||
digest = "0.10.0"
|
digest = "0.10.0"
|
||||||
dtparse = "1.2.0"
|
dtparse = "1.2.0"
|
||||||
eml-parser = "0.1.0"
|
eml-parser = "0.1.0"
|
||||||
encoding_rs = "0.8.30"
|
encoding_rs = "0.8.30"
|
||||||
|
fancy-regex = "0.10.0"
|
||||||
filesize = "0.2.0"
|
filesize = "0.2.0"
|
||||||
filetime = "0.2.15"
|
filetime = "0.2.15"
|
||||||
fs_extra = "1.2.0"
|
fs_extra = "1.2.0"
|
||||||
@ -52,30 +55,30 @@ is-root = "0.1.2"
|
|||||||
itertools = "0.10.0"
|
itertools = "0.10.0"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
lscolors = { version = "0.10.0", features = ["crossterm"]}
|
lscolors = { version = "0.12.0", features = ["crossterm"]}
|
||||||
md5 = { package = "md-5", version = "0.10.0" }
|
md5 = { package = "md-5", version = "0.10.0" }
|
||||||
meval = "0.2.0"
|
meval = "0.2.0"
|
||||||
mime = "0.3.16"
|
mime = "0.3.16"
|
||||||
notify = "4.0.17"
|
notify = "4.0.17"
|
||||||
num = { version = "0.4.0", optional = true }
|
num = { version = "0.4.0", optional = true }
|
||||||
|
num-traits = "0.2.14"
|
||||||
pathdiff = "0.2.1"
|
pathdiff = "0.2.1"
|
||||||
powierza-coefficient = "1.0.1"
|
powierza-coefficient = "1.0.1"
|
||||||
quick-xml = "0.23.0"
|
quick-xml = "0.23.0"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
rayon = "1.5.1"
|
rayon = "1.5.1"
|
||||||
regex = "1.5.4"
|
|
||||||
reqwest = {version = "0.11", features = ["blocking", "json"] }
|
reqwest = {version = "0.11", features = ["blocking", "json"] }
|
||||||
roxmltree = "0.14.0"
|
roxmltree = "0.14.0"
|
||||||
rust-embed = "6.3.0"
|
rust-embed = "6.3.0"
|
||||||
serde = { version="1.0.123", features=["derive"] }
|
serde = { version="1.0.123", features=["derive"] }
|
||||||
serde_ini = "0.2.0"
|
serde_ini = "0.2.0"
|
||||||
serde_urlencoded = "0.7.0"
|
serde_urlencoded = "0.7.0"
|
||||||
serde_yaml = "0.8.16"
|
serde_yaml = "0.9.4"
|
||||||
sha2 = "0.10.0"
|
sha2 = "0.10.0"
|
||||||
# Disable default features b/c the default features build Git (very slow to compile)
|
# Disable default features b/c the default features build Git (very slow to compile)
|
||||||
shadow-rs = { version = "0.16.1", default-features = false }
|
shadow-rs = { version = "0.16.1", default-features = false }
|
||||||
strip-ansi-escapes = "0.1.1"
|
strip-ansi-escapes = "0.1.1"
|
||||||
sysinfo = "0.24.6"
|
sysinfo = "0.25.2"
|
||||||
terminal_size = "0.2.1"
|
terminal_size = "0.2.1"
|
||||||
thiserror = "1.0.31"
|
thiserror = "1.0.31"
|
||||||
titlecase = "2.0.0"
|
titlecase = "2.0.0"
|
||||||
@ -83,8 +86,8 @@ toml = "0.5.8"
|
|||||||
unicode-segmentation = "1.8.0"
|
unicode-segmentation = "1.8.0"
|
||||||
url = "2.2.1"
|
url = "2.2.1"
|
||||||
uuid = { version = "1.1.2", features = ["v4"] }
|
uuid = { version = "1.1.2", features = ["v4"] }
|
||||||
which = { version = "4.2.2", optional = true }
|
which = { version = "4.3.0", optional = true }
|
||||||
reedline = { version = "0.9.0", features = ["bashisms", "sqlite"]}
|
reedline = { version = "0.11.0", features = ["bashisms", "sqlite"]}
|
||||||
wax = { version = "0.5.0", features = ["diagnostics"] }
|
wax = { version = "0.5.0", features = ["diagnostics"] }
|
||||||
rusqlite = { version = "0.28.0", features = ["bundled"], optional = true }
|
rusqlite = { version = "0.28.0", features = ["bundled"], optional = true }
|
||||||
sqlparser = { version = "0.16.0", features = ["serde"], optional = true }
|
sqlparser = { version = "0.16.0", features = ["serde"], optional = true }
|
||||||
@ -98,15 +101,33 @@ version = "2.1.3"
|
|||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
[dependencies.polars]
|
[dependencies.polars]
|
||||||
version = "0.22.8"
|
version = "0.23.2"
|
||||||
# path = "../../../../polars/polars"
|
|
||||||
optional = true
|
optional = true
|
||||||
features = [
|
features = [
|
||||||
"default", "to_dummies", "parquet", "json", "serde", "serde-lazy",
|
"arg_where",
|
||||||
"object", "checked_arithmetic", "strings", "cum_agg", "is_in",
|
"checked_arithmetic",
|
||||||
"rolling_window", "strings", "rows", "random",
|
"concat_str",
|
||||||
"dtype-datetime", "dtype-struct", "lazy", "cross_join",
|
"cross_join",
|
||||||
"dynamic_groupby", "dtype-categorical", "concat_str"
|
"csv-file",
|
||||||
|
"cum_agg",
|
||||||
|
"default",
|
||||||
|
"dtype-datetime",
|
||||||
|
"dtype-struct",
|
||||||
|
"dtype-categorical",
|
||||||
|
"dynamic_groupby",
|
||||||
|
"is_in",
|
||||||
|
"json",
|
||||||
|
"lazy",
|
||||||
|
"object",
|
||||||
|
"parquet",
|
||||||
|
"random",
|
||||||
|
"rolling_window",
|
||||||
|
"rows",
|
||||||
|
"serde",
|
||||||
|
"serde-lazy",
|
||||||
|
"strings",
|
||||||
|
"strings",
|
||||||
|
"to_dummies",
|
||||||
]
|
]
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies.windows]
|
[target.'cfg(windows)'.dependencies.windows]
|
||||||
@ -131,6 +152,7 @@ shadow-rs = { version = "0.16.1", default-features = false }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
hamcrest2 = "0.3.0"
|
hamcrest2 = "0.3.0"
|
||||||
dirs-next = "2.0.0"
|
dirs-next = "2.0.0"
|
||||||
|
proptest = "1.0.0"
|
||||||
quickcheck = "1.0.3"
|
quickcheck = "1.0.3"
|
||||||
quickcheck_macros = "1.0.0"
|
quickcheck_macros = "1.0.0"
|
||||||
rstest = "0.15.0"
|
rstest = {version = "0.15.0", default-features = false}
|
||||||
|
@ -3,16 +3,18 @@ use std::process::Command;
|
|||||||
fn main() -> shadow_rs::SdResult<()> {
|
fn main() -> shadow_rs::SdResult<()> {
|
||||||
// Look up the current Git commit ourselves instead of relying on shadow_rs,
|
// 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)
|
// because shadow_rs does it in a really slow-to-compile way (it builds libgit2)
|
||||||
let hash = get_git_hash().expect("failed to get latest git commit hash");
|
let hash = get_git_hash().unwrap_or_default();
|
||||||
println!("cargo:rustc-env=NU_COMMIT_HASH={}", hash);
|
println!("cargo:rustc-env=NU_COMMIT_HASH={}", hash);
|
||||||
|
|
||||||
shadow_rs::new()
|
shadow_rs::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_git_hash() -> Result<String, std::io::Error> {
|
fn get_git_hash() -> Option<String> {
|
||||||
let out = Command::new("git").args(["rev-parse", "HEAD"]).output()?;
|
Command::new("git")
|
||||||
Ok(String::from_utf8(out.stdout)
|
.args(["rev-parse", "HEAD"])
|
||||||
.expect("could not convert stdout to string")
|
.output()
|
||||||
.trim()
|
.ok()
|
||||||
.to_string())
|
.filter(|output| output.status.success())
|
||||||
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
|
.map(|hash| hash.trim().to_string())
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
# Seeds for failure cases proptest has generated in the past. It is
|
||||||
|
# automatically read and these particular cases re-run before any
|
||||||
|
# novel cases are generated.
|
||||||
|
#
|
||||||
|
# It is recommended to check this file in to source control so that
|
||||||
|
# everyone who runs the test benefits from these saved cases.
|
||||||
|
cc 96a80ecd19729fb43a7b7bb2766b37d6083ba73b16abb97075875e3cfcdc763f # shrinks to c = '"'
|
||||||
|
cc 4146602559ea717a02bcef3c6d73cdf613c30d0c3f92c48e26c79b9a1544e027 # shrinks to c = '\\'
|
||||||
|
cc 80532a0ee73df456a719b9e3cce1ae5f3d26009dde819cbaf16f8e0cb6709705 # shrinks to c = ':'
|
||||||
|
cc cdb88505686eea3c74c36f282fd29b2b68bc118ded4ebfc36f9838d174bd7653 # shrinks to c = '`'
|
||||||
|
cc 0f534d55f9771e8810b9c4252a4168abfaec1a35e1b0cac12dbaf726d295a08c # shrinks to c = '\0'
|
||||||
|
cc 5d31bcbab722acd1f4e23ca3a4f95ff309a636b45a73ca8ae9f820d93ff57acc # shrinks to c = '{'
|
||||||
|
cc 5afec063bc96160d681d77f90041b67ef5cfdea4dcbd12d984fd828fbeb4b421 # shrinks to c = '#'
|
||||||
|
cc f919beb3ee5c70e756a15635d65ded7d44f3ae58b5e86b6c09e814d5d8cdd506 # shrinks to c = ';'
|
||||||
|
cc ec00f39b8d45dfd8808947a56af5e50ba5a0ef7c951723b45377815a02e515b1 # shrinks to c = '('
|
||||||
|
cc 25b773cdf4c24179151fa86244c7de4136e05df9e94e6ee77a336ebfd8764444 # shrinks to c = '|'
|
||||||
|
cc 94dc0d54b97d59e1c0f4cb11bdccb3823a1bb908cbc3fd643ee8f067169fad72 # shrinks to c = '0'
|
||||||
|
cc c9d0051fb1e5a8bdc1d4f5a3dceac1b4b465827d1dff4fc3a3755baae6a7bb48 # shrinks to c = '$'
|
||||||
|
cc 14ec40d2eb5bd2663e9b11bb49fb2120852f9ea71678c69d732161412b55a3ec # shrinks to s = ""
|
||||||
|
cc d4afccc51ed9d421bdb7e1723e273dfb6e77c3a449489671a496db234e87c5ed # shrinks to c = '\r'
|
||||||
|
cc 515a56d73eb1b69290ef4c714b629421989879aebd57991bd2c2bf11294353b1 # shrinks to s = "\\\\𐊀{"
|
||||||
|
cc 111566990fffa432acd2dbc845141b0e7870f97125c7621e3ddf142204568246 # shrinks to s = "'\":`"
|
||||||
|
cc 0424c33000d9188be96b3049046eb052770b2158bf5ebb0c98656d7145e8aca9 # shrinks to s = "0"
|
100
crates/nu-command/src/bits/and.rs
Normal file
100
crates/nu-command/src/bits/and.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits and"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits and")
|
||||||
|
.required(
|
||||||
|
"target",
|
||||||
|
SyntaxShape::Int,
|
||||||
|
"target integer to perform bit and",
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Performs bitwise and for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["logic and"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let target: i64 = call.req(engine_state, stack, 0)?;
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, target, head),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Apply bits and to two numbers",
|
||||||
|
example: "2 | bits and 2",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 2,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Apply logical and to a list of numbers",
|
||||||
|
example: "[4 3 2] | bits and 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(0), Value::test_int(2), Value::test_int(2)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, target: i64, head: Span) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => Value::Int {
|
||||||
|
val: val & target,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
49
crates/nu-command/src/bits/bits_.rs
Normal file
49
crates/nu-command/src/bits/bits_.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use nu_engine::get_full_help;
|
||||||
|
use nu_protocol::{
|
||||||
|
ast::Call,
|
||||||
|
engine::{Command, EngineState, Stack},
|
||||||
|
Category, IntoPipelineData, PipelineData, Signature, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Bits;
|
||||||
|
|
||||||
|
impl Command for Bits {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits").category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Various commands for working with bits"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
Ok(Value::String {
|
||||||
|
val: get_full_help(&Bits.signature(), &Bits.examples(), engine_state, stack),
|
||||||
|
span: call.head,
|
||||||
|
}
|
||||||
|
.into_pipeline_data())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::Bits;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(Bits {})
|
||||||
|
}
|
||||||
|
}
|
99
crates/nu-command/src/bits/mod.rs
Normal file
99
crates/nu-command/src/bits/mod.rs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
mod and;
|
||||||
|
mod bits_;
|
||||||
|
mod not;
|
||||||
|
mod or;
|
||||||
|
mod rotate_left;
|
||||||
|
mod rotate_right;
|
||||||
|
mod shift_left;
|
||||||
|
mod shift_right;
|
||||||
|
mod xor;
|
||||||
|
|
||||||
|
use nu_protocol::Spanned;
|
||||||
|
|
||||||
|
pub use and::SubCommand as BitsAnd;
|
||||||
|
pub use bits_::Bits;
|
||||||
|
pub use not::SubCommand as BitsNot;
|
||||||
|
pub use or::SubCommand as BitsOr;
|
||||||
|
pub use rotate_left::SubCommand as BitsRotateLeft;
|
||||||
|
pub use rotate_right::SubCommand as BitsRotateRight;
|
||||||
|
pub use shift_left::SubCommand as BitsShiftLeft;
|
||||||
|
pub use shift_right::SubCommand as BitsShiftRight;
|
||||||
|
pub use xor::SubCommand as BitsXor;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum NumberBytes {
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
Four,
|
||||||
|
Eight,
|
||||||
|
Auto,
|
||||||
|
Invalid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum InputNumType {
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
Four,
|
||||||
|
Eight,
|
||||||
|
SignedOne,
|
||||||
|
SignedTwo,
|
||||||
|
SignedFour,
|
||||||
|
SignedEight,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_number_bytes(number_bytes: &Option<Spanned<String>>) -> NumberBytes {
|
||||||
|
match number_bytes.as_ref() {
|
||||||
|
None => NumberBytes::Eight,
|
||||||
|
Some(size) => match size.item.as_str() {
|
||||||
|
"1" => NumberBytes::One,
|
||||||
|
"2" => NumberBytes::Two,
|
||||||
|
"4" => NumberBytes::Four,
|
||||||
|
"8" => NumberBytes::Eight,
|
||||||
|
"auto" => NumberBytes::Auto,
|
||||||
|
_ => NumberBytes::Invalid,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_input_num_type(val: i64, signed: bool, number_size: NumberBytes) -> InputNumType {
|
||||||
|
if signed || val < 0 {
|
||||||
|
match number_size {
|
||||||
|
NumberBytes::One => InputNumType::SignedOne,
|
||||||
|
NumberBytes::Two => InputNumType::SignedTwo,
|
||||||
|
NumberBytes::Four => InputNumType::SignedFour,
|
||||||
|
NumberBytes::Eight => InputNumType::SignedEight,
|
||||||
|
NumberBytes::Auto => {
|
||||||
|
if val <= 0x7F && val >= -(2i64.pow(7)) {
|
||||||
|
InputNumType::SignedOne
|
||||||
|
} else if val <= 0x7FFF && val >= -(2i64.pow(15)) {
|
||||||
|
InputNumType::SignedTwo
|
||||||
|
} else if val <= 0x7FFFFFFF && val >= -(2i64.pow(31)) {
|
||||||
|
InputNumType::SignedFour
|
||||||
|
} else {
|
||||||
|
InputNumType::SignedEight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NumberBytes::Invalid => InputNumType::SignedFour,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match number_size {
|
||||||
|
NumberBytes::One => InputNumType::One,
|
||||||
|
NumberBytes::Two => InputNumType::Two,
|
||||||
|
NumberBytes::Four => InputNumType::Four,
|
||||||
|
NumberBytes::Eight => InputNumType::Eight,
|
||||||
|
NumberBytes::Auto => {
|
||||||
|
if val <= 0xFF {
|
||||||
|
InputNumType::One
|
||||||
|
} else if val <= 0xFFFF {
|
||||||
|
InputNumType::Two
|
||||||
|
} else if val <= 0xFFFFFFFF {
|
||||||
|
InputNumType::Four
|
||||||
|
} else {
|
||||||
|
InputNumType::Eight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NumberBytes::Invalid => InputNumType::Four,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
163
crates/nu-command/src/bits/not.rs
Normal file
163
crates/nu-command/src/bits/not.rs
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
use super::{get_number_bytes, NumberBytes};
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits not"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits not")
|
||||||
|
.switch(
|
||||||
|
"signed",
|
||||||
|
"always treat input number as a signed number",
|
||||||
|
Some('s'),
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"number-bytes",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the size of unsigned number in bytes, it can be 1, 2, 4, 8, auto",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Performs logical negation on each bit"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["negation"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let signed = call.has_flag("signed");
|
||||||
|
let number_bytes: Option<Spanned<String>> =
|
||||||
|
call.get_flag(engine_state, stack, "number-bytes")?;
|
||||||
|
let bytes_len = get_number_bytes(&number_bytes);
|
||||||
|
if let NumberBytes::Invalid = bytes_len {
|
||||||
|
if let Some(val) = number_bytes {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(),
|
||||||
|
val.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, head, signed, bytes_len),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Apply logical negation to a list of numbers",
|
||||||
|
example: "[4 3 2] | bits not",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_int(140737488355323),
|
||||||
|
Value::test_int(140737488355324),
|
||||||
|
Value::test_int(140737488355325),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description:
|
||||||
|
"Apply logical negation to a list of numbers, treat input as 2 bytes number",
|
||||||
|
example: "[4 3 2] | bits not -n 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_int(65531),
|
||||||
|
Value::test_int(65532),
|
||||||
|
Value::test_int(65533),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description:
|
||||||
|
"Apply logical negation to a list of numbers, treat input as signed number",
|
||||||
|
example: "[4 3 2] | bits not -s",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_int(-5),
|
||||||
|
Value::test_int(-4),
|
||||||
|
Value::test_int(-3),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, head: Span, signed: bool, number_size: NumberBytes) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => {
|
||||||
|
if signed || val < 0 {
|
||||||
|
Value::Int { val: !val, span }
|
||||||
|
} else {
|
||||||
|
use NumberBytes::*;
|
||||||
|
let out_val = match number_size {
|
||||||
|
One => !val & 0x00_00_00_00_00_FF,
|
||||||
|
Two => !val & 0x00_00_00_00_FF_FF,
|
||||||
|
Four => !val & 0x00_00_FF_FF_FF_FF,
|
||||||
|
Eight => !val & 0x7F_FF_FF_FF_FF_FF,
|
||||||
|
Auto => {
|
||||||
|
if val <= 0xFF {
|
||||||
|
!val & 0x00_00_00_00_00_FF
|
||||||
|
} else if val <= 0xFF_FF {
|
||||||
|
!val & 0x00_00_00_00_FF_FF
|
||||||
|
} else if val <= 0xFF_FF_FF_FF {
|
||||||
|
!val & 0x00_00_FF_FF_FF_FF
|
||||||
|
} else {
|
||||||
|
!val & 0x7F_FF_FF_FF_FF_FF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// This case shouldn't happen here, as it's handled before
|
||||||
|
Invalid => 0,
|
||||||
|
};
|
||||||
|
Value::Int { val: out_val, span }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only numerical values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
100
crates/nu-command/src/bits/or.rs
Normal file
100
crates/nu-command/src/bits/or.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits or"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits or")
|
||||||
|
.required(
|
||||||
|
"target",
|
||||||
|
SyntaxShape::Int,
|
||||||
|
"target integer to perform bit or",
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Performs bitwise or for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["logic or"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let target: i64 = call.req(engine_state, stack, 0)?;
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, target, head),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Apply bits or to two numbers",
|
||||||
|
example: "2 | bits or 6",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 6,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Apply logical or to a list of numbers",
|
||||||
|
example: "[8 3 2] | bits or 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(10), Value::test_int(3), Value::test_int(2)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, target: i64, head: Span) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => Value::Int {
|
||||||
|
val: val | target,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
156
crates/nu-command/src/bits/rotate_left.rs
Normal file
156
crates/nu-command/src/bits/rotate_left.rs
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
use super::{get_input_num_type, get_number_bytes, InputNumType, NumberBytes};
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
use num_traits::int::PrimInt;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits rol"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits rol")
|
||||||
|
.required("bits", SyntaxShape::Int, "number of bits to rotate left")
|
||||||
|
.switch(
|
||||||
|
"signed",
|
||||||
|
"always treat input number as a signed number",
|
||||||
|
Some('s'),
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"number-bytes",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the word size in number of bytes, it can be 1, 2, 4, 8, auto, default value `8`",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Bitwise rotate left for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["rotate left"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let bits: usize = call.req(engine_state, stack, 0)?;
|
||||||
|
let signed = call.has_flag("signed");
|
||||||
|
let number_bytes: Option<Spanned<String>> =
|
||||||
|
call.get_flag(engine_state, stack, "number-bytes")?;
|
||||||
|
let bytes_len = get_number_bytes(&number_bytes);
|
||||||
|
if let NumberBytes::Invalid = bytes_len {
|
||||||
|
if let Some(val) = number_bytes {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(),
|
||||||
|
val.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, bits, head, signed, bytes_len),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Rotate left a number with 2 bits",
|
||||||
|
example: "17 | bits rol 2",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 68,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Rotate left a list of numbers with 2 bits",
|
||||||
|
example: "[5 3 2] | bits rol 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(20), Value::test_int(12), Value::test_int(8)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_rotate_left<T: Display + PrimInt>(val: T, bits: u32, span: Span) -> Value
|
||||||
|
where
|
||||||
|
i64: std::convert::TryFrom<T>,
|
||||||
|
{
|
||||||
|
let rotate_result = i64::try_from(val.rotate_left(bits));
|
||||||
|
match rotate_result {
|
||||||
|
Ok(val) => Value::Int { val, span },
|
||||||
|
Err(_) => Value::Error {
|
||||||
|
error: ShellError::GenericError(
|
||||||
|
"Rotate left result beyond the range of 64 bit signed number".to_string(),
|
||||||
|
format!(
|
||||||
|
"{} of the specified number of bytes rotate left {} bits exceed limit",
|
||||||
|
val, bits
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: NumberBytes) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => {
|
||||||
|
use InputNumType::*;
|
||||||
|
// let bits = (((bits % 64) + 64) % 64) as u32;
|
||||||
|
let bits = bits as u32;
|
||||||
|
let input_type = get_input_num_type(val, signed, number_size);
|
||||||
|
match input_type {
|
||||||
|
One => get_rotate_left(val as u8, bits, span),
|
||||||
|
Two => get_rotate_left(val as u16, bits, span),
|
||||||
|
Four => get_rotate_left(val as u32, bits, span),
|
||||||
|
Eight => get_rotate_left(val as u64, bits, span),
|
||||||
|
SignedOne => get_rotate_left(val as i8, bits, span),
|
||||||
|
SignedTwo => get_rotate_left(val as i16, bits, span),
|
||||||
|
SignedFour => get_rotate_left(val as i32, bits, span),
|
||||||
|
SignedEight => get_rotate_left(val as i64, bits, span),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
160
crates/nu-command/src/bits/rotate_right.rs
Normal file
160
crates/nu-command/src/bits/rotate_right.rs
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
use super::{get_input_num_type, get_number_bytes, InputNumType, NumberBytes};
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
use num_traits::int::PrimInt;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits ror"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits ror")
|
||||||
|
.required("bits", SyntaxShape::Int, "number of bits to rotate right")
|
||||||
|
.switch(
|
||||||
|
"signed",
|
||||||
|
"always treat input number as a signed number",
|
||||||
|
Some('s'),
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"number-bytes",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the word size in number of bytes, it can be 1, 2, 4, 8, auto, default value `8`",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Bitwise rotate right for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["rotate right"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let bits: usize = call.req(engine_state, stack, 0)?;
|
||||||
|
let signed = call.has_flag("signed");
|
||||||
|
let number_bytes: Option<Spanned<String>> =
|
||||||
|
call.get_flag(engine_state, stack, "number-bytes")?;
|
||||||
|
let bytes_len = get_number_bytes(&number_bytes);
|
||||||
|
if let NumberBytes::Invalid = bytes_len {
|
||||||
|
if let Some(val) = number_bytes {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(),
|
||||||
|
val.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, bits, head, signed, bytes_len),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Rotate right a number with 60 bits",
|
||||||
|
example: "17 | bits ror 60",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 272,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Rotate right a list of numbers of one byte",
|
||||||
|
example: "[15 33 92] | bits ror 2 -n 1",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_int(195),
|
||||||
|
Value::test_int(72),
|
||||||
|
Value::test_int(23),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_rotate_right<T: Display + PrimInt>(val: T, bits: u32, span: Span) -> Value
|
||||||
|
where
|
||||||
|
i64: std::convert::TryFrom<T>,
|
||||||
|
{
|
||||||
|
let rotate_result = i64::try_from(val.rotate_right(bits));
|
||||||
|
match rotate_result {
|
||||||
|
Ok(val) => Value::Int { val, span },
|
||||||
|
Err(_) => Value::Error {
|
||||||
|
error: ShellError::GenericError(
|
||||||
|
"Rotate right result beyond the range of 64 bit signed number".to_string(),
|
||||||
|
format!(
|
||||||
|
"{} of the specified number of bytes rotate right {} bits exceed limit",
|
||||||
|
val, bits
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: NumberBytes) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => {
|
||||||
|
use InputNumType::*;
|
||||||
|
// let bits = (((bits % 64) + 64) % 64) as u32;
|
||||||
|
let bits = bits as u32;
|
||||||
|
let input_type = get_input_num_type(val, signed, number_size);
|
||||||
|
match input_type {
|
||||||
|
One => get_rotate_right(val as u8, bits, span),
|
||||||
|
Two => get_rotate_right(val as u16, bits, span),
|
||||||
|
Four => get_rotate_right(val as u32, bits, span),
|
||||||
|
Eight => get_rotate_right(val as u64, bits, span),
|
||||||
|
SignedOne => get_rotate_right(val as i8, bits, span),
|
||||||
|
SignedTwo => get_rotate_right(val as i16, bits, span),
|
||||||
|
SignedFour => get_rotate_right(val as i32, bits, span),
|
||||||
|
SignedEight => get_rotate_right(val as i64, bits, span),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
188
crates/nu-command/src/bits/shift_left.rs
Normal file
188
crates/nu-command/src/bits/shift_left.rs
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
use super::{get_input_num_type, get_number_bytes, InputNumType, NumberBytes};
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
use num_traits::CheckedShl;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits shl"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits shl")
|
||||||
|
.required("bits", SyntaxShape::Int, "number of bits to shift left")
|
||||||
|
.switch(
|
||||||
|
"signed",
|
||||||
|
"always treat input number as a signed number",
|
||||||
|
Some('s'),
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"number-bytes",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the word size in number of bytes, it can be 1, 2, 4, 8, auto, default value `8`",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Bitwise shift left for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["shift left"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let bits: usize = call.req(engine_state, stack, 0)?;
|
||||||
|
let signed = call.has_flag("signed");
|
||||||
|
let number_bytes: Option<Spanned<String>> =
|
||||||
|
call.get_flag(engine_state, stack, "number-bytes")?;
|
||||||
|
let bytes_len = get_number_bytes(&number_bytes);
|
||||||
|
if let NumberBytes::Invalid = bytes_len {
|
||||||
|
if let Some(val) = number_bytes {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(),
|
||||||
|
val.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, bits, head, signed, bytes_len),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Shift left a number by 7 bits",
|
||||||
|
example: "2 | bits shl 7",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 256,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Shift left a number with 1 byte by 7 bits",
|
||||||
|
example: "2 | bits shl 7 -n 1",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 0,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Shift left a signed number by 1 bit",
|
||||||
|
example: "0x7F | bits shl 1 -s",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 254,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Shift left a list of numbers",
|
||||||
|
example: "[5 3 2] | bits shl 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(20), Value::test_int(12), Value::test_int(8)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_shift_left<T: CheckedShl + Display + Copy>(val: T, bits: u32, span: Span) -> Value
|
||||||
|
where
|
||||||
|
i64: std::convert::TryFrom<T>,
|
||||||
|
{
|
||||||
|
match val.checked_shl(bits) {
|
||||||
|
Some(val) => {
|
||||||
|
let shift_result = i64::try_from(val);
|
||||||
|
match shift_result {
|
||||||
|
Ok(val) => Value::Int { val, span },
|
||||||
|
Err(_) => Value::Error {
|
||||||
|
error: ShellError::GenericError(
|
||||||
|
"Shift left result beyond the range of 64 bit signed number".to_string(),
|
||||||
|
format!(
|
||||||
|
"{} of the specified number of bytes shift left {} bits exceed limit",
|
||||||
|
val, bits
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => Value::Error {
|
||||||
|
error: ShellError::GenericError(
|
||||||
|
"Shift left failed".to_string(),
|
||||||
|
format!(
|
||||||
|
"{} shift left {} bits failed, you may shift too many bits",
|
||||||
|
val, bits
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: NumberBytes) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => {
|
||||||
|
use InputNumType::*;
|
||||||
|
// let bits = (((bits % 64) + 64) % 64) as u32;
|
||||||
|
let bits = bits as u32;
|
||||||
|
let input_type = get_input_num_type(val, signed, number_size);
|
||||||
|
match input_type {
|
||||||
|
One => get_shift_left(val as u8, bits, span),
|
||||||
|
Two => get_shift_left(val as u16, bits, span),
|
||||||
|
Four => get_shift_left(val as u32, bits, span),
|
||||||
|
Eight => get_shift_left(val as u64, bits, span),
|
||||||
|
SignedOne => get_shift_left(val as i8, bits, span),
|
||||||
|
SignedTwo => get_shift_left(val as i16, bits, span),
|
||||||
|
SignedFour => get_shift_left(val as i32, bits, span),
|
||||||
|
SignedEight => get_shift_left(val as i64, bits, span),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
172
crates/nu-command/src/bits/shift_right.rs
Normal file
172
crates/nu-command/src/bits/shift_right.rs
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
use super::{get_input_num_type, get_number_bytes, InputNumType, NumberBytes};
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
use num_traits::CheckedShr;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits shr"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits shr")
|
||||||
|
.required("bits", SyntaxShape::Int, "number of bits to shift right")
|
||||||
|
.switch(
|
||||||
|
"signed",
|
||||||
|
"always treat input number as a signed number",
|
||||||
|
Some('s'),
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"number-bytes",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the word size in number of bytes, it can be 1, 2, 4, 8, auto, default value `8`",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Bitwise shift right for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["shift right"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let bits: usize = call.req(engine_state, stack, 0)?;
|
||||||
|
let signed = call.has_flag("signed");
|
||||||
|
let number_bytes: Option<Spanned<String>> =
|
||||||
|
call.get_flag(engine_state, stack, "number-bytes")?;
|
||||||
|
let bytes_len = get_number_bytes(&number_bytes);
|
||||||
|
if let NumberBytes::Invalid = bytes_len {
|
||||||
|
if let Some(val) = number_bytes {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"Only 1, 2, 4, 8, or 'auto' bytes are supported as word sizes".to_string(),
|
||||||
|
val.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, bits, head, signed, bytes_len),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Shift right a number with 2 bits",
|
||||||
|
example: "8 | bits shr 2",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 2,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Shift right a list of numbers",
|
||||||
|
example: "[15 35 2] | bits shr 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(3), Value::test_int(8), Value::test_int(0)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_shift_right<T: CheckedShr + Display + Copy>(val: T, bits: u32, span: Span) -> Value
|
||||||
|
where
|
||||||
|
i64: std::convert::TryFrom<T>,
|
||||||
|
{
|
||||||
|
match val.checked_shr(bits) {
|
||||||
|
Some(val) => {
|
||||||
|
let shift_result = i64::try_from(val);
|
||||||
|
match shift_result {
|
||||||
|
Ok(val) => Value::Int { val, span },
|
||||||
|
Err(_) => Value::Error {
|
||||||
|
error: ShellError::GenericError(
|
||||||
|
"Shift right result beyond the range of 64 bit signed number".to_string(),
|
||||||
|
format!(
|
||||||
|
"{} of the specified number of bytes shift right {} bits exceed limit",
|
||||||
|
val, bits
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => Value::Error {
|
||||||
|
error: ShellError::GenericError(
|
||||||
|
"Shift right failed".to_string(),
|
||||||
|
format!(
|
||||||
|
"{} shift right {} bits failed, you may shift too many bits",
|
||||||
|
val, bits
|
||||||
|
),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, bits: usize, head: Span, signed: bool, number_size: NumberBytes) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => {
|
||||||
|
use InputNumType::*;
|
||||||
|
// let bits = (((bits % 64) + 64) % 64) as u32;
|
||||||
|
let bits = bits as u32;
|
||||||
|
let input_type = get_input_num_type(val, signed, number_size);
|
||||||
|
match input_type {
|
||||||
|
One => get_shift_right(val as u8, bits, span),
|
||||||
|
Two => get_shift_right(val as u16, bits, span),
|
||||||
|
Four => get_shift_right(val as u32, bits, span),
|
||||||
|
Eight => get_shift_right(val as u64, bits, span),
|
||||||
|
SignedOne => get_shift_right(val as i8, bits, span),
|
||||||
|
SignedTwo => get_shift_right(val as i16, bits, span),
|
||||||
|
SignedFour => get_shift_right(val as i32, bits, span),
|
||||||
|
SignedEight => get_shift_right(val as i64, bits, span),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
100
crates/nu-command/src/bits/xor.rs
Normal file
100
crates/nu-command/src/bits/xor.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"bits xor"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("bits xor")
|
||||||
|
.required(
|
||||||
|
"target",
|
||||||
|
SyntaxShape::Int,
|
||||||
|
"target integer to perform bit xor",
|
||||||
|
)
|
||||||
|
.category(Category::Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Performs bitwise xor for integers"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["logic xor"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let target: i64 = call.req(engine_state, stack, 0)?;
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |value| operate(value, target, head),
|
||||||
|
engine_state.ctrlc.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Apply bits xor to two numbers",
|
||||||
|
example: "2 | bits xor 2",
|
||||||
|
result: Some(Value::Int {
|
||||||
|
val: 0,
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Apply logical xor to a list of numbers",
|
||||||
|
example: "[8 3 2] | bits xor 2",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_int(10), Value::test_int(1), Value::test_int(0)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(value: Value, target: i64, head: Span) -> Value {
|
||||||
|
match value {
|
||||||
|
Value::Int { val, span } => Value::Int {
|
||||||
|
val: val ^ target,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
other => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Only integer values are supported, input type: {:?}",
|
||||||
|
other.get_type()
|
||||||
|
),
|
||||||
|
other.span().unwrap_or(head),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
}
|
@ -47,7 +47,7 @@ impl Command for BytesAdd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"add specified bytes to the input"
|
"Add specified bytes to the input"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
@ -49,7 +49,7 @@ impl Command for BytesIndexOf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["pattern", "match", "find", "search", "index"]
|
vec!["pattern", "match", "find", "search"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -39,7 +39,7 @@ impl Command for BytesLen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["len", "size", "count"]
|
vec!["size", "count"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -41,7 +41,7 @@ impl Command for BytesRemove {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"remove bytes"
|
"Remove bytes"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
@ -29,7 +29,7 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["convert", "binary", "bytes", "bin"]
|
vec!["convert", "bytes"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -103,7 +103,7 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["convert", "date", "time", "timezone", "UTC"]
|
vec!["convert", "timezone", "UTC"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
@ -311,7 +311,7 @@ fn action(
|
|||||||
Some(dt) => match DateTime::parse_from_str(val, &dt.0) {
|
Some(dt) => match DateTime::parse_from_str(val, &dt.0) {
|
||||||
Ok(d) => Value::Date { val: d, span: head },
|
Ok(d) => Value::Date { val: d, span: head },
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
return Value::Error {
|
Value::Error {
|
||||||
error: ShellError::CantConvert(
|
error: ShellError::CantConvert(
|
||||||
format!("could not parse as datetime using format '{}'", dt.0),
|
format!("could not parse as datetime using format '{}'", dt.0),
|
||||||
reason.to_string(),
|
reason.to_string(),
|
||||||
|
@ -42,7 +42,7 @@ impl Command for SubCommand {
|
|||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Convert string to integer in table",
|
description: "Convert string to decimal in table",
|
||||||
example: "[[num]; ['5.01']] | into decimal num",
|
example: "[[num]; ['5.01']] | into decimal num",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![Value::Record {
|
vals: vec![Value::Record {
|
||||||
@ -54,15 +54,20 @@ impl Command for SubCommand {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Convert string to integer",
|
description: "Convert string to decimal",
|
||||||
example: "'1.345' | into decimal",
|
example: "'1.345' | into decimal",
|
||||||
result: Some(Value::test_float(1.345)),
|
result: Some(Value::test_float(1.345)),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Convert decimal to integer",
|
description: "Convert decimal to decimal",
|
||||||
example: "'-5.9' | into decimal",
|
example: "'-5.9' | into decimal",
|
||||||
result: Some(Value::test_float(-5.9)),
|
result: Some(Value::test_float(-5.9)),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert boolean to decimal",
|
||||||
|
example: "true | into decimal",
|
||||||
|
result: Some(Value::test_float(1.0)),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -118,6 +123,13 @@ fn action(input: &Value, head: Span) -> Value {
|
|||||||
val: *v as f64,
|
val: *v as f64,
|
||||||
span: *span,
|
span: *span,
|
||||||
},
|
},
|
||||||
|
Value::Bool { val: b, span } => Value::Float {
|
||||||
|
val: match b {
|
||||||
|
true => 1.0,
|
||||||
|
false => 0.0,
|
||||||
|
},
|
||||||
|
span: *span,
|
||||||
|
},
|
||||||
other => {
|
other => {
|
||||||
let span = other.span();
|
let span = other.span();
|
||||||
match span {
|
match span {
|
||||||
|
@ -3,7 +3,8 @@ use nu_parser::parse_duration_bytes;
|
|||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::{Call, CellPath, Expr},
|
ast::{Call, CellPath, Expr},
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Unit, Value,
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Unit,
|
||||||
|
Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -16,6 +17,12 @@ impl Command for SubCommand {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("into duration")
|
Signature::build("into duration")
|
||||||
|
.named(
|
||||||
|
"convert",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"convert duration into another duration",
|
||||||
|
Some('c'),
|
||||||
|
)
|
||||||
.rest(
|
.rest(
|
||||||
"rest",
|
"rest",
|
||||||
SyntaxShape::CellPath,
|
SyntaxShape::CellPath,
|
||||||
@ -106,6 +113,15 @@ impl Command for SubCommand {
|
|||||||
span,
|
span,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert string to a named duration",
|
||||||
|
example: "'7min' | into duration --convert sec",
|
||||||
|
result: Some(Value::String {
|
||||||
|
val: "420 sec".to_string(),
|
||||||
|
span,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,17 +133,21 @@ fn into_duration(
|
|||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let head = call.head;
|
let head = call.head;
|
||||||
|
let convert_to_unit: Option<Spanned<String>> = call.get_flag(engine_state, stack, "convert")?;
|
||||||
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||||
|
|
||||||
input.map(
|
input.map(
|
||||||
move |v| {
|
move |v| {
|
||||||
if column_paths.is_empty() {
|
if column_paths.is_empty() {
|
||||||
action(&v, head)
|
action(&v, &convert_to_unit, head)
|
||||||
} else {
|
} else {
|
||||||
let mut ret = v;
|
let mut ret = v;
|
||||||
for path in &column_paths {
|
for path in &column_paths {
|
||||||
let r =
|
let d = convert_to_unit.clone();
|
||||||
ret.update_cell_path(&path.members, Box::new(move |old| action(old, head)));
|
let r = ret.update_cell_path(
|
||||||
|
&path.members,
|
||||||
|
Box::new(move |old| action(old, &d, head)),
|
||||||
|
);
|
||||||
if let Err(error) = r {
|
if let Err(error) = r {
|
||||||
return Value::Error { error };
|
return Value::Error { error };
|
||||||
}
|
}
|
||||||
@ -140,6 +160,148 @@ fn into_duration(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn convert_str_from_unit_to_unit(
|
||||||
|
val: i64,
|
||||||
|
from_unit: &str,
|
||||||
|
to_unit: &str,
|
||||||
|
span: Span,
|
||||||
|
value_span: Span,
|
||||||
|
) -> Result<i64, ShellError> {
|
||||||
|
match (from_unit, to_unit) {
|
||||||
|
("ns", "ns") => Ok(val),
|
||||||
|
("ns", "us") => Ok(val / 1000),
|
||||||
|
("ns", "ms") => Ok(val / 1000 / 1000),
|
||||||
|
("ns", "sec") => Ok(val / 1000 / 1000 / 1000),
|
||||||
|
("ns", "min") => Ok(val / 1000 / 1000 / 1000 / 60),
|
||||||
|
("ns", "hr") => Ok(val / 1000 / 1000 / 1000 / 60 / 60),
|
||||||
|
("ns", "day") => Ok(val / 1000 / 1000 / 1000 / 60 / 60 / 24),
|
||||||
|
("ns", "wk") => Ok(val / 1000 / 1000 / 1000 / 60 / 60 / 24 / 7),
|
||||||
|
("ns", "month") => Ok(val / 1000 / 1000 / 1000 / 60 / 60 / 24 / 30),
|
||||||
|
("ns", "yr") => Ok(val / 1000 / 1000 / 1000 / 60 / 60 / 24 / 365),
|
||||||
|
("ns", "dec") => Ok(val / 10 / 1000 / 1000 / 1000 / 60 / 60 / 24 / 365),
|
||||||
|
|
||||||
|
("us", "ns") => Ok(val * 1000),
|
||||||
|
("us", "us") => Ok(val),
|
||||||
|
("us", "ms") => Ok(val / 1000),
|
||||||
|
("us", "sec") => Ok(val / 1000 / 1000),
|
||||||
|
("us", "min") => Ok(val / 1000 / 1000 / 60),
|
||||||
|
("us", "hr") => Ok(val / 1000 / 1000 / 60 / 60),
|
||||||
|
("us", "day") => Ok(val / 1000 / 1000 / 60 / 60 / 24),
|
||||||
|
("us", "wk") => Ok(val / 1000 / 1000 / 60 / 60 / 24 / 7),
|
||||||
|
("us", "month") => Ok(val / 1000 / 1000 / 60 / 60 / 24 / 30),
|
||||||
|
("us", "yr") => Ok(val / 1000 / 1000 / 60 / 60 / 24 / 365),
|
||||||
|
("us", "dec") => Ok(val / 10 / 1000 / 1000 / 60 / 60 / 24 / 365),
|
||||||
|
|
||||||
|
("ms", "ns") => Ok(val * 1000 * 1000),
|
||||||
|
("ms", "us") => Ok(val * 1000),
|
||||||
|
("ms", "ms") => Ok(val),
|
||||||
|
("ms", "sec") => Ok(val / 1000),
|
||||||
|
("ms", "min") => Ok(val / 1000 / 60),
|
||||||
|
("ms", "hr") => Ok(val / 1000 / 60 / 60),
|
||||||
|
("ms", "day") => Ok(val / 1000 / 60 / 60 / 24),
|
||||||
|
("ms", "wk") => Ok(val / 1000 / 60 / 60 / 24 / 7),
|
||||||
|
("ms", "month") => Ok(val / 1000 / 60 / 60 / 24 / 30),
|
||||||
|
("ms", "yr") => Ok(val / 1000 / 60 / 60 / 24 / 365),
|
||||||
|
("ms", "dec") => Ok(val / 10 / 1000 / 60 / 60 / 24 / 365),
|
||||||
|
|
||||||
|
("sec", "ns") => Ok(val * 1000 * 1000 * 1000),
|
||||||
|
("sec", "us") => Ok(val * 1000 * 1000),
|
||||||
|
("sec", "ms") => Ok(val * 1000),
|
||||||
|
("sec", "sec") => Ok(val),
|
||||||
|
("sec", "min") => Ok(val / 60),
|
||||||
|
("sec", "hr") => Ok(val / 60 / 60),
|
||||||
|
("sec", "day") => Ok(val / 60 / 60 / 24),
|
||||||
|
("sec", "wk") => Ok(val / 60 / 60 / 24 / 7),
|
||||||
|
("sec", "month") => Ok(val / 60 / 60 / 24 / 30),
|
||||||
|
("sec", "yr") => Ok(val / 60 / 60 / 24 / 365),
|
||||||
|
("sec", "dec") => Ok(val / 10 / 60 / 60 / 24 / 365),
|
||||||
|
|
||||||
|
("min", "ns") => Ok(val * 1000 * 1000 * 1000 * 60),
|
||||||
|
("min", "us") => Ok(val * 1000 * 1000 * 60),
|
||||||
|
("min", "ms") => Ok(val * 1000 * 60),
|
||||||
|
("min", "sec") => Ok(val * 60),
|
||||||
|
("min", "min") => Ok(val),
|
||||||
|
("min", "hr") => Ok(val / 60),
|
||||||
|
("min", "day") => Ok(val / 60 / 24),
|
||||||
|
("min", "wk") => Ok(val / 60 / 24 / 7),
|
||||||
|
("min", "month") => Ok(val / 60 / 24 / 30),
|
||||||
|
("min", "yr") => Ok(val / 60 / 24 / 365),
|
||||||
|
("min", "dec") => Ok(val / 10 / 60 / 24 / 365),
|
||||||
|
|
||||||
|
("hr", "ns") => Ok(val * 1000 * 1000 * 1000 * 60 * 60),
|
||||||
|
("hr", "us") => Ok(val * 1000 * 1000 * 60 * 60),
|
||||||
|
("hr", "ms") => Ok(val * 1000 * 60 * 60),
|
||||||
|
("hr", "sec") => Ok(val * 60 * 60),
|
||||||
|
("hr", "min") => Ok(val * 60),
|
||||||
|
("hr", "hr") => Ok(val),
|
||||||
|
("hr", "day") => Ok(val / 24),
|
||||||
|
("hr", "wk") => Ok(val / 24 / 7),
|
||||||
|
("hr", "month") => Ok(val / 24 / 30),
|
||||||
|
("hr", "yr") => Ok(val / 24 / 365),
|
||||||
|
("hr", "dec") => Ok(val / 10 / 24 / 365),
|
||||||
|
|
||||||
|
("day", "ns") => Ok(val * 1000 * 1000 * 1000 * 60 * 60 * 24),
|
||||||
|
("day", "us") => Ok(val * 1000 * 1000 * 60 * 60 * 24),
|
||||||
|
("day", "ms") => Ok(val * 1000 * 60 * 60 * 24),
|
||||||
|
("day", "sec") => Ok(val * 60 * 60 * 24),
|
||||||
|
("day", "min") => Ok(val * 60 * 24),
|
||||||
|
("day", "hr") => Ok(val * 24),
|
||||||
|
("day", "day") => Ok(val),
|
||||||
|
("day", "wk") => Ok(val / 7),
|
||||||
|
("day", "month") => Ok(val / 30),
|
||||||
|
("day", "yr") => Ok(val / 365),
|
||||||
|
("day", "dec") => Ok(val / 10 / 365),
|
||||||
|
|
||||||
|
("wk", "ns") => Ok(val * 1000 * 1000 * 1000 * 60 * 60 * 24 * 7),
|
||||||
|
("wk", "us") => Ok(val * 1000 * 1000 * 60 * 60 * 24 * 7),
|
||||||
|
("wk", "ms") => Ok(val * 1000 * 60 * 60 * 24 * 7),
|
||||||
|
("wk", "sec") => Ok(val * 60 * 60 * 24 * 7),
|
||||||
|
("wk", "min") => Ok(val * 60 * 24 * 7),
|
||||||
|
("wk", "hr") => Ok(val * 24 * 7),
|
||||||
|
("wk", "day") => Ok(val * 7),
|
||||||
|
("wk", "wk") => Ok(val),
|
||||||
|
("wk", "month") => Ok(val / 4),
|
||||||
|
("wk", "yr") => Ok(val / 52),
|
||||||
|
("wk", "dec") => Ok(val / 10 / 52),
|
||||||
|
|
||||||
|
("month", "ns") => Ok(val * 1000 * 1000 * 1000 * 60 * 60 * 24 * 30),
|
||||||
|
("month", "us") => Ok(val * 1000 * 1000 * 60 * 60 * 24 * 30),
|
||||||
|
("month", "ms") => Ok(val * 1000 * 60 * 60 * 24 * 30),
|
||||||
|
("month", "sec") => Ok(val * 60 * 60 * 24 * 30),
|
||||||
|
("month", "min") => Ok(val * 60 * 24 * 30),
|
||||||
|
("month", "hr") => Ok(val * 24 * 30),
|
||||||
|
("month", "day") => Ok(val * 30),
|
||||||
|
("month", "wk") => Ok(val * 4),
|
||||||
|
("month", "month") => Ok(val),
|
||||||
|
("month", "yr") => Ok(val / 12),
|
||||||
|
("month", "dec") => Ok(val / 10 / 12),
|
||||||
|
|
||||||
|
("yr", "ns") => Ok(val * 1000 * 1000 * 1000 * 60 * 60 * 24 * 365),
|
||||||
|
("yr", "us") => Ok(val * 1000 * 1000 * 60 * 60 * 24 * 365),
|
||||||
|
("yr", "ms") => Ok(val * 1000 * 60 * 60 * 24 * 365),
|
||||||
|
("yr", "sec") => Ok(val * 60 * 60 * 24 * 365),
|
||||||
|
("yr", "min") => Ok(val * 60 * 24 * 365),
|
||||||
|
("yr", "hr") => Ok(val * 24 * 365),
|
||||||
|
("yr", "day") => Ok(val * 365),
|
||||||
|
("yr", "wk") => Ok(val * 52),
|
||||||
|
("yr", "month") => Ok(val * 12),
|
||||||
|
("yr", "yr") => Ok(val),
|
||||||
|
("yr", "dec") => Ok(val / 10),
|
||||||
|
|
||||||
|
_ => Err(ShellError::CantConvertWithValue(
|
||||||
|
"string duration".to_string(),
|
||||||
|
"string duration".to_string(),
|
||||||
|
to_unit.to_string(),
|
||||||
|
span,
|
||||||
|
value_span,
|
||||||
|
Some(
|
||||||
|
"supported units are ns, us, ms, sec, min, hr, day, wk, month, yr and dec"
|
||||||
|
.to_string(),
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn string_to_duration(s: &str, span: Span, value_span: Span) -> Result<i64, ShellError> {
|
fn string_to_duration(s: &str, span: Span, value_span: Span) -> Result<i64, ShellError> {
|
||||||
if let Some(expression) = parse_duration_bytes(s.as_bytes(), span) {
|
if let Some(expression) = parse_duration_bytes(s.as_bytes(), span) {
|
||||||
if let Expr::ValueWithUnit(value, unit) = expression.expr {
|
if let Expr::ValueWithUnit(value, unit) = expression.expr {
|
||||||
@ -168,20 +330,105 @@ fn string_to_duration(s: &str, span: Span, value_span: Span) -> Result<i64, Shel
|
|||||||
s.to_string(),
|
s.to_string(),
|
||||||
span,
|
span,
|
||||||
value_span,
|
value_span,
|
||||||
Some("supported units are ns, us, ms, sec, min, hr, day, and wk".to_string()),
|
Some(
|
||||||
|
"supported units are ns, us, ms, sec, min, hr, day, wk, month, yr and dec".to_string(),
|
||||||
|
),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn action(input: &Value, span: Span) -> Value {
|
fn string_to_unit_duration(
|
||||||
|
s: &str,
|
||||||
|
span: Span,
|
||||||
|
value_span: Span,
|
||||||
|
) -> Result<(&str, i64), ShellError> {
|
||||||
|
if let Some(expression) = parse_duration_bytes(s.as_bytes(), span) {
|
||||||
|
if let Expr::ValueWithUnit(value, unit) = expression.expr {
|
||||||
|
if let Expr::Int(x) = value.expr {
|
||||||
|
match unit.item {
|
||||||
|
Unit::Nanosecond => return Ok(("ns", x)),
|
||||||
|
Unit::Microsecond => return Ok(("us", x)),
|
||||||
|
Unit::Millisecond => return Ok(("ms", x)),
|
||||||
|
Unit::Second => return Ok(("sec", x)),
|
||||||
|
Unit::Minute => return Ok(("min", x)),
|
||||||
|
Unit::Hour => return Ok(("hr", x)),
|
||||||
|
Unit::Day => return Ok(("day", x)),
|
||||||
|
Unit::Week => return Ok(("wk", x)),
|
||||||
|
Unit::Month => return Ok(("month", x)), //30 days to a month
|
||||||
|
Unit::Year => return Ok(("yr", x)), //365 days to a year
|
||||||
|
Unit::Decade => return Ok(("dec", x)), //365 days to a year
|
||||||
|
|
||||||
|
_ => return Ok(("ns", 0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(ShellError::CantConvertWithValue(
|
||||||
|
"duration".to_string(),
|
||||||
|
"string".to_string(),
|
||||||
|
s.to_string(),
|
||||||
|
span,
|
||||||
|
value_span,
|
||||||
|
Some(
|
||||||
|
"supported units are ns, us, ms, sec, min, hr, day, wk, month, yr and dec".to_string(),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn action(input: &Value, convert_to_unit: &Option<Spanned<String>>, span: Span) -> Value {
|
||||||
match input {
|
match input {
|
||||||
Value::Duration { .. } => input.clone(),
|
Value::Duration {
|
||||||
|
val: _val_num,
|
||||||
|
span: _value_span,
|
||||||
|
} => {
|
||||||
|
if let Some(_to_unit) = convert_to_unit {
|
||||||
|
Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
"Cannot convert from a Value::Duration right now. Try making it a string."
|
||||||
|
.into(),
|
||||||
|
span,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
input.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
Value::String {
|
Value::String {
|
||||||
val,
|
val,
|
||||||
span: value_span,
|
span: value_span,
|
||||||
} => match string_to_duration(val, span, *value_span) {
|
} => {
|
||||||
Ok(val) => Value::Duration { val, span },
|
if let Some(to_unit) = convert_to_unit {
|
||||||
Err(error) => Value::Error { error },
|
if let Ok(dur) = string_to_unit_duration(val, span, *value_span) {
|
||||||
},
|
let from_unit = dur.0;
|
||||||
|
let duration = dur.1;
|
||||||
|
match convert_str_from_unit_to_unit(
|
||||||
|
duration,
|
||||||
|
from_unit,
|
||||||
|
&to_unit.item,
|
||||||
|
span,
|
||||||
|
*value_span,
|
||||||
|
) {
|
||||||
|
Ok(d) => Value::String {
|
||||||
|
val: format!("{} {}", d, &to_unit.item),
|
||||||
|
span: *value_span,
|
||||||
|
},
|
||||||
|
Err(e) => Value::Error { error: e },
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput(
|
||||||
|
"'into duration' does not support this string input".into(),
|
||||||
|
span,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match string_to_duration(val, span, *value_span) {
|
||||||
|
Ok(val) => Value::Duration { val, span },
|
||||||
|
Err(error) => Value::Error { error },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => Value::Error {
|
_ => Value::Error {
|
||||||
error: ShellError::UnsupportedInput(
|
error: ShellError::UnsupportedInput(
|
||||||
"'into duration' does not support this input".into(),
|
"'into duration' does not support this input".into(),
|
||||||
@ -207,8 +454,9 @@ mod test {
|
|||||||
let span = Span::test_data();
|
let span = Span::test_data();
|
||||||
let word = Value::test_string("3ns");
|
let word = Value::test_string("3ns");
|
||||||
let expected = Value::Duration { val: 3, span };
|
let expected = Value::Duration { val: 3, span };
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,8 +468,9 @@ mod test {
|
|||||||
val: 4 * 1000,
|
val: 4 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,8 +482,9 @@ mod test {
|
|||||||
val: 5 * 1000 * 1000,
|
val: 5 * 1000 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,8 +496,9 @@ mod test {
|
|||||||
val: 1000 * 1000 * 1000,
|
val: 1000 * 1000 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,8 +510,9 @@ mod test {
|
|||||||
val: 7 * 60 * 1000 * 1000 * 1000,
|
val: 7 * 60 * 1000 * 1000 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,8 +524,9 @@ mod test {
|
|||||||
val: 42 * 60 * 60 * 1000 * 1000 * 1000,
|
val: 42 * 60 * 60 * 1000 * 1000 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,8 +538,9 @@ mod test {
|
|||||||
val: 123 * 24 * 60 * 60 * 1000 * 1000 * 1000,
|
val: 123 * 24 * 60 * 60 * 1000 * 1000 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,8 +552,9 @@ mod test {
|
|||||||
val: 3 * 7 * 24 * 60 * 60 * 1000 * 1000 * 1000,
|
val: 3 * 7 * 24 * 60 * 60 * 1000 * 1000 * 1000,
|
||||||
span,
|
span,
|
||||||
};
|
};
|
||||||
|
let convert_duration = None;
|
||||||
|
|
||||||
let actual = action(&word, span);
|
let actual = action(&word, &convert_duration, span);
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["convert", "number", "size", "bytes"]
|
vec!["convert", "number", "bytes"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -5,8 +5,8 @@ use nu_protocol::{
|
|||||||
into_code, Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature,
|
into_code, Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature,
|
||||||
Span, SyntaxShape, Value,
|
Span, SyntaxShape, Value,
|
||||||
};
|
};
|
||||||
|
use nu_utils::get_system_locale;
|
||||||
// TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml)
|
use num_format::ToFormattedString;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
@ -38,7 +38,7 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["convert", "str", "text"]
|
vec!["convert", "text"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -216,21 +216,8 @@ pub fn action(
|
|||||||
) -> Value {
|
) -> Value {
|
||||||
match input {
|
match input {
|
||||||
Value::Int { val, .. } => {
|
Value::Int { val, .. } => {
|
||||||
let res = if group_digits {
|
let decimal_value = digits.unwrap_or(0) as usize;
|
||||||
format_int(*val) // int.to_formatted_string(*locale)
|
let res = format_int(*val, group_digits, decimal_value);
|
||||||
} else if let Some(dig) = digits {
|
|
||||||
let mut val_with_trailing_zeroes = val.to_string();
|
|
||||||
if dig != 0 {
|
|
||||||
val_with_trailing_zeroes.push('.');
|
|
||||||
}
|
|
||||||
for _ in 0..dig {
|
|
||||||
val_with_trailing_zeroes.push('0');
|
|
||||||
}
|
|
||||||
val_with_trailing_zeroes
|
|
||||||
} else {
|
|
||||||
val.to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
Value::String { val: res, span }
|
Value::String { val: res, span }
|
||||||
}
|
}
|
||||||
Value::Float { val, .. } => {
|
Value::Float { val, .. } => {
|
||||||
@ -305,21 +292,29 @@ pub fn action(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn format_int(int: i64) -> String {
|
|
||||||
int.to_string()
|
|
||||||
|
|
||||||
// TODO once platform-specific dependencies are stable (see Cargo.toml)
|
fn format_int(int: i64, group_digits: bool, decimals: usize) -> String {
|
||||||
// #[cfg(windows)]
|
let locale = get_system_locale();
|
||||||
// {
|
|
||||||
// int.to_formatted_string(&Locale::en)
|
let str = if group_digits {
|
||||||
// }
|
int.to_formatted_string(&locale)
|
||||||
// #[cfg(not(windows))]
|
} else {
|
||||||
// {
|
int.to_string()
|
||||||
// match SystemLocale::default() {
|
};
|
||||||
// Ok(locale) => int.to_formatted_string(&locale),
|
|
||||||
// Err(_) => int.to_formatted_string(&Locale::en),
|
if decimals > 0 {
|
||||||
// }
|
let decimal_point = locale.decimal();
|
||||||
// }
|
|
||||||
|
format!(
|
||||||
|
"{}{decimal_point}{dummy:0<decimals$}",
|
||||||
|
str,
|
||||||
|
decimal_point = decimal_point,
|
||||||
|
dummy = "",
|
||||||
|
decimals = decimals
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
str
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -27,7 +27,7 @@ impl Command for Alias {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
77
crates/nu-command/src/core_commands/ast.rs
Normal file
77
crates/nu-command/src/core_commands/ast.rs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_parser::parse;
|
||||||
|
use nu_protocol::{
|
||||||
|
ast::Call,
|
||||||
|
engine::{Command, EngineState, Stack, StateWorkingSet},
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Ast;
|
||||||
|
|
||||||
|
impl Command for Ast {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"ast"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Print the abstract syntax tree (ast) for a pipeline."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("ast")
|
||||||
|
.required(
|
||||||
|
"pipeline",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the pipeline to print the ast for",
|
||||||
|
)
|
||||||
|
.category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let pipeline: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||||
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
|
||||||
|
let (output, err) = parse(&mut working_set, None, pipeline.item.as_bytes(), false, &[]);
|
||||||
|
eprintln!("output: {:#?}\nerror: {:#?}", output, err);
|
||||||
|
|
||||||
|
Ok(PipelineData::new(head))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Print the ast of a string",
|
||||||
|
example: "ast 'hello'",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Print the ast of a pipeline",
|
||||||
|
example: "ast 'ls | where name =~ README'",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Print the ast of a pipeline with an error",
|
||||||
|
example: "ast 'for x in 1..10 { echo $x '",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use super::Ast;
|
||||||
|
use crate::test_examples;
|
||||||
|
test_examples(Ast {})
|
||||||
|
}
|
||||||
|
}
|
@ -28,7 +28,7 @@ impl Command for Def {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -28,7 +28,7 @@ impl Command for DefEnv {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html
|
https://www.nushell.sh/book/thinking_in_nu.html
|
||||||
|
|
||||||
=== EXTRA NOTE ===
|
=== EXTRA NOTE ===
|
||||||
All blocks are scoped, including variable definition and environment variable changes.
|
All blocks are scoped, including variable definition and environment variable changes.
|
||||||
|
@ -29,7 +29,7 @@ impl Command for ErrorMake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["err", "panic", "crash", "throw"]
|
vec!["panic", "crash", "throw"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -18,12 +18,12 @@ impl Command for ExportCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Export custom commands or environment variables from a module."
|
"Export definitions or environment variables from a module."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -27,7 +27,7 @@ impl Command for ExportAlias {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -28,7 +28,7 @@ impl Command for ExportDef {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -28,7 +28,7 @@ impl Command for ExportDefEnv {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html
|
https://www.nushell.sh/book/thinking_in_nu.html
|
||||||
|
|
||||||
=== EXTRA NOTE ===
|
=== EXTRA NOTE ===
|
||||||
All blocks are scoped, including variable definition and environment variable changes.
|
All blocks are scoped, including variable definition and environment variable changes.
|
||||||
|
@ -3,9 +3,9 @@ use nu_protocol::engine::{Command, EngineState, Stack};
|
|||||||
use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
|
use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ExportEnv;
|
pub struct ExportEnvModule;
|
||||||
|
|
||||||
impl Command for ExportEnv {
|
impl Command for ExportEnvModule {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"export env"
|
"export env"
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ impl Command for ExportEnv {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -23,7 +23,7 @@ impl Command for ExportExtern {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
56
crates/nu-command/src/core_commands/export_use.rs
Normal file
56
crates/nu-command/src/core_commands/export_use.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ExportUse;
|
||||||
|
|
||||||
|
impl Command for ExportUse {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"export use"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Use definitions from a module and export them from this module"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("export use")
|
||||||
|
.required("pattern", SyntaxShape::ImportPattern, "import pattern")
|
||||||
|
.category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
r#"This command is a parser keyword. For details, check:
|
||||||
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
_engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
Ok(PipelineData::new(call.head))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Re-export a command from another module",
|
||||||
|
example: r#"module spam { export def foo [] { "foo" } }
|
||||||
|
module eggs { export use spam foo }
|
||||||
|
use eggs foo
|
||||||
|
foo
|
||||||
|
"#,
|
||||||
|
result: Some(Value::String {
|
||||||
|
val: "foo".to_string(),
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,7 @@ impl Command for Extern {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -46,7 +46,7 @@ impl Command for For {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use fancy_regex::Regex;
|
||||||
use nu_ansi_term::{
|
use nu_ansi_term::{
|
||||||
Color::{Default, Red, White},
|
Color::{Default, Red, White},
|
||||||
Style,
|
Style,
|
||||||
@ -11,7 +12,6 @@ use nu_protocol::{
|
|||||||
ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
};
|
};
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Help;
|
pub struct Help;
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ fn help(
|
|||||||
|
|
||||||
cols.push("is_custom".into());
|
cols.push("is_custom".into());
|
||||||
vals.push(Value::Bool {
|
vals.push(Value::Bool {
|
||||||
val: decl.get_block_id().is_some(),
|
val: decl.is_custom_command(),
|
||||||
span: head,
|
span: head,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -243,7 +243,7 @@ fn help(
|
|||||||
|
|
||||||
cols.push("is_custom".into());
|
cols.push("is_custom".into());
|
||||||
vals.push(Value::Bool {
|
vals.push(Value::Bool {
|
||||||
val: decl.get_block_id().is_some(),
|
val: decl.is_custom_command(),
|
||||||
span: head,
|
span: head,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -350,7 +350,7 @@ pub fn highlight_search_string(
|
|||||||
string_style: &Style,
|
string_style: &Style,
|
||||||
) -> Result<String, ShellError> {
|
) -> Result<String, ShellError> {
|
||||||
let regex_string = format!("(?i){}", needle);
|
let regex_string = format!("(?i){}", needle);
|
||||||
let regex = match regex::Regex::new(®ex_string) {
|
let regex = match Regex::new(®ex_string) {
|
||||||
Ok(regex) => regex,
|
Ok(regex) => regex,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(ShellError::GenericError(
|
return Err(ShellError::GenericError(
|
||||||
@ -367,21 +367,34 @@ pub fn highlight_search_string(
|
|||||||
let mut highlighted = String::new();
|
let mut highlighted = String::new();
|
||||||
|
|
||||||
for cap in regex.captures_iter(haystack) {
|
for cap in regex.captures_iter(haystack) {
|
||||||
let start = match cap.get(0) {
|
match cap {
|
||||||
Some(cap) => cap.start(),
|
Ok(capture) => {
|
||||||
None => 0,
|
let start = match capture.get(0) {
|
||||||
};
|
Some(acap) => acap.start(),
|
||||||
let end = match cap.get(0) {
|
None => 0,
|
||||||
Some(cap) => cap.end(),
|
};
|
||||||
None => 0,
|
let end = match capture.get(0) {
|
||||||
};
|
Some(acap) => acap.end(),
|
||||||
highlighted.push_str(
|
None => 0,
|
||||||
&string_style
|
};
|
||||||
.paint(&haystack[last_match_end..start])
|
highlighted.push_str(
|
||||||
.to_string(),
|
&string_style
|
||||||
);
|
.paint(&haystack[last_match_end..start])
|
||||||
highlighted.push_str(&style.paint(&haystack[start..end]).to_string());
|
.to_string(),
|
||||||
last_match_end = end;
|
);
|
||||||
|
highlighted.push_str(&style.paint(&haystack[start..end]).to_string());
|
||||||
|
last_match_end = end;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err(ShellError::GenericError(
|
||||||
|
"Error with regular expression capture".into(),
|
||||||
|
e.to_string(),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
highlighted.push_str(&string_style.paint(&haystack[last_match_end..]).to_string());
|
highlighted.push_str(&string_style.paint(&haystack[last_match_end..]).to_string());
|
||||||
|
@ -19,14 +19,14 @@ impl Command for Hide {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Hide symbols in the current scope"
|
"Hide definitions in the current scope"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"Symbols are hidden by priority: First aliases, then custom commands, then environment variables.
|
r#"Definitions are hidden by priority: First aliases, then custom commands.
|
||||||
|
|
||||||
This command is a parser keyword. For details, check:
|
This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
75
crates/nu-command/src/core_commands/hide_env.rs
Normal file
75
crates/nu-command/src/core_commands/hide_env.rs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
did_you_mean, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
|
||||||
|
SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct HideEnv;
|
||||||
|
|
||||||
|
impl Command for HideEnv {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"hide-env"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("hide-env")
|
||||||
|
.rest(
|
||||||
|
"name",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"environment variable names to hide",
|
||||||
|
)
|
||||||
|
.switch(
|
||||||
|
"ignore-errors",
|
||||||
|
"do not throw an error if an environment variable was not found",
|
||||||
|
Some('i'),
|
||||||
|
)
|
||||||
|
.category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Hide environment variables in the current scope"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let env_var_names: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
|
||||||
|
let ignore_errors = call.has_flag("ignore-errors");
|
||||||
|
|
||||||
|
for name in env_var_names {
|
||||||
|
if stack.remove_env_var(engine_state, &name.item).is_none() && !ignore_errors {
|
||||||
|
let all_names: Vec<String> = stack
|
||||||
|
.get_env_var_names(engine_state)
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
if let Some(closest_match) = did_you_mean(&all_names, &name.item) {
|
||||||
|
return Err(ShellError::DidYouMeanCustom(
|
||||||
|
format!("Environment variable '{}' not found", name.item),
|
||||||
|
closest_match,
|
||||||
|
name.span,
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::EnvVarNotFoundAtRuntime(name.item, name.span));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(PipelineData::new(call.head))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Hide an environment variable",
|
||||||
|
example: r#"let-env HZ_ENV_ABC = 1; hide-env HZ_ENV_ABC; 'HZ_ENV_ABC' in (env).name"#,
|
||||||
|
result: Some(Value::boolean(false, Span::test_data())),
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
@ -35,7 +35,7 @@ impl Command for If {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -18,6 +18,10 @@ impl Command for Ignore {
|
|||||||
Signature::build("ignore").category(Category::Core)
|
Signature::build("ignore").category(Category::Core)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["silent", "quiet", "out-null"]
|
||||||
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
&self,
|
&self,
|
||||||
_engine_state: &EngineState,
|
_engine_state: &EngineState,
|
||||||
|
@ -28,7 +28,7 @@ impl Command for Let {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
mod alias;
|
mod alias;
|
||||||
|
mod ast;
|
||||||
mod debug;
|
mod debug;
|
||||||
mod def;
|
mod def;
|
||||||
mod def_env;
|
mod def_env;
|
||||||
@ -12,21 +13,23 @@ mod export_def;
|
|||||||
mod export_def_env;
|
mod export_def_env;
|
||||||
mod export_env;
|
mod export_env;
|
||||||
mod export_extern;
|
mod export_extern;
|
||||||
|
mod export_use;
|
||||||
mod extern_;
|
mod extern_;
|
||||||
mod for_;
|
mod for_;
|
||||||
pub mod help;
|
pub mod help;
|
||||||
mod hide;
|
mod hide;
|
||||||
|
mod hide_env;
|
||||||
mod if_;
|
mod if_;
|
||||||
mod ignore;
|
mod ignore;
|
||||||
mod let_;
|
mod let_;
|
||||||
mod metadata;
|
mod metadata;
|
||||||
mod module;
|
mod module;
|
||||||
pub(crate) mod overlay;
|
pub(crate) mod overlay;
|
||||||
mod source;
|
|
||||||
mod use_;
|
mod use_;
|
||||||
mod version;
|
mod version;
|
||||||
|
|
||||||
pub use alias::Alias;
|
pub use alias::Alias;
|
||||||
|
pub use ast::Ast;
|
||||||
pub use debug::Debug;
|
pub use debug::Debug;
|
||||||
pub use def::Def;
|
pub use def::Def;
|
||||||
pub use def_env::DefEnv;
|
pub use def_env::DefEnv;
|
||||||
@ -38,19 +41,20 @@ pub use export::ExportCommand;
|
|||||||
pub use export_alias::ExportAlias;
|
pub use export_alias::ExportAlias;
|
||||||
pub use export_def::ExportDef;
|
pub use export_def::ExportDef;
|
||||||
pub use export_def_env::ExportDefEnv;
|
pub use export_def_env::ExportDefEnv;
|
||||||
pub use export_env::ExportEnv;
|
pub use export_env::ExportEnvModule;
|
||||||
pub use export_extern::ExportExtern;
|
pub use export_extern::ExportExtern;
|
||||||
|
pub use export_use::ExportUse;
|
||||||
pub use extern_::Extern;
|
pub use extern_::Extern;
|
||||||
pub use for_::For;
|
pub use for_::For;
|
||||||
pub use help::Help;
|
pub use help::Help;
|
||||||
pub use hide::Hide;
|
pub use hide::Hide;
|
||||||
|
pub use hide_env::HideEnv;
|
||||||
pub use if_::If;
|
pub use if_::If;
|
||||||
pub use ignore::Ignore;
|
pub use ignore::Ignore;
|
||||||
pub use let_::Let;
|
pub use let_::Let;
|
||||||
pub use metadata::Metadata;
|
pub use metadata::Metadata;
|
||||||
pub use module::Module;
|
pub use module::Module;
|
||||||
pub use overlay::*;
|
pub use overlay::*;
|
||||||
pub use source::Source;
|
|
||||||
pub use use_::Use;
|
pub use use_::Use;
|
||||||
pub use version::Version;
|
pub use version::Version;
|
||||||
#[cfg(feature = "plugin")]
|
#[cfg(feature = "plugin")]
|
||||||
|
@ -27,7 +27,7 @@ impl Command for Module {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -1,148 +0,0 @@
|
|||||||
use nu_engine::{eval_block, CallExt};
|
|
||||||
use nu_protocol::ast::Call;
|
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
||||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape};
|
|
||||||
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct OverlayAdd;
|
|
||||||
|
|
||||||
impl Command for OverlayAdd {
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
"overlay add"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
|
||||||
"Add definitions from a module as an overlay"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
|
||||||
Signature::build("overlay add")
|
|
||||||
.required(
|
|
||||||
"name",
|
|
||||||
SyntaxShape::String,
|
|
||||||
"Module name to create overlay for",
|
|
||||||
)
|
|
||||||
// TODO:
|
|
||||||
// .switch(
|
|
||||||
// "prefix",
|
|
||||||
// "Prepend module name to the imported symbols",
|
|
||||||
// Some('p'),
|
|
||||||
// )
|
|
||||||
.category(Category::Core)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
|
||||||
r#"This command is a parser keyword. For details, check:
|
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(
|
|
||||||
&self,
|
|
||||||
engine_state: &EngineState,
|
|
||||||
stack: &mut Stack,
|
|
||||||
call: &Call,
|
|
||||||
_input: PipelineData,
|
|
||||||
) -> Result<PipelineData, ShellError> {
|
|
||||||
let name_arg: Spanned<String> = call.req(engine_state, stack, 0)?;
|
|
||||||
|
|
||||||
let maybe_overlay_name = if engine_state
|
|
||||||
.find_overlay(name_arg.item.as_bytes())
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
Some(name_arg.item.clone())
|
|
||||||
} else if let Some(os_str) = Path::new(&name_arg.item).file_stem() {
|
|
||||||
os_str.to_str().map(|name| name.to_string())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(overlay_name) = maybe_overlay_name {
|
|
||||||
if let Some(overlay_id) = engine_state.find_overlay(overlay_name.as_bytes()) {
|
|
||||||
let old_module_id = engine_state.get_overlay(overlay_id).origin;
|
|
||||||
|
|
||||||
stack.add_overlay(overlay_name.clone());
|
|
||||||
|
|
||||||
if let Some(new_module_id) = engine_state.find_module(overlay_name.as_bytes(), &[])
|
|
||||||
{
|
|
||||||
if !stack.has_env_overlay(&overlay_name, engine_state)
|
|
||||||
|| (old_module_id != new_module_id)
|
|
||||||
{
|
|
||||||
// Add environment variables only if:
|
|
||||||
// a) adding a new overlay
|
|
||||||
// b) refreshing an active overlay (the origin module changed)
|
|
||||||
let module = engine_state.get_module(new_module_id);
|
|
||||||
|
|
||||||
for (name, block_id) in module.env_vars() {
|
|
||||||
let name = if let Ok(s) = String::from_utf8(name.clone()) {
|
|
||||||
s
|
|
||||||
} else {
|
|
||||||
return Err(ShellError::NonUtf8(call.head));
|
|
||||||
};
|
|
||||||
|
|
||||||
let block = engine_state.get_block(block_id);
|
|
||||||
|
|
||||||
let val = eval_block(
|
|
||||||
engine_state,
|
|
||||||
stack,
|
|
||||||
block,
|
|
||||||
PipelineData::new(call.head),
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
)?
|
|
||||||
.into_value(call.head);
|
|
||||||
|
|
||||||
stack.add_env_var(name, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(ShellError::OverlayNotFoundAtRuntime(
|
|
||||||
name_arg.item,
|
|
||||||
name_arg.span,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Err(ShellError::OverlayNotFoundAtRuntime(
|
|
||||||
name_arg.item,
|
|
||||||
name_arg.span,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(PipelineData::new(call.head))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
|
||||||
vec![
|
|
||||||
Example {
|
|
||||||
description: "Create an overlay from a module",
|
|
||||||
example: r#"module spam { export def foo [] { "foo" } }
|
|
||||||
overlay add spam"#,
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
Example {
|
|
||||||
description: "Create an overlay from a file",
|
|
||||||
example: r#"echo 'export env FOO { "foo" }' | save spam.nu
|
|
||||||
overlay add spam.nu"#,
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_examples() {
|
|
||||||
use crate::test_examples;
|
|
||||||
|
|
||||||
test_examples(OverlayAdd {})
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,7 +23,7 @@ impl Command for Overlay {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -4,20 +4,20 @@ use nu_protocol::engine::{Command, EngineState, Stack};
|
|||||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape};
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OverlayRemove;
|
pub struct OverlayHide;
|
||||||
|
|
||||||
impl Command for OverlayRemove {
|
impl Command for OverlayHide {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"overlay remove"
|
"overlay hide"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Remove an active overlay"
|
"Hide an active overlay"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("overlay remove")
|
Signature::build("overlay hide")
|
||||||
.optional("name", SyntaxShape::String, "Overlay to remove")
|
.optional("name", SyntaxShape::String, "Overlay to hide")
|
||||||
.switch(
|
.switch(
|
||||||
"keep-custom",
|
"keep-custom",
|
||||||
"Keep all newly added symbols within the next activated overlay",
|
"Keep all newly added symbols within the next activated overlay",
|
||||||
@ -26,7 +26,7 @@ impl Command for OverlayRemove {
|
|||||||
.named(
|
.named(
|
||||||
"keep-env",
|
"keep-env",
|
||||||
SyntaxShape::List(Box::new(SyntaxShape::String)),
|
SyntaxShape::List(Box::new(SyntaxShape::String)),
|
||||||
"List of environment variables to keep from the removed overlay",
|
"List of environment variables to keep from the hidden overlay",
|
||||||
Some('e'),
|
Some('e'),
|
||||||
)
|
)
|
||||||
.category(Category::Core)
|
.category(Category::Core)
|
||||||
@ -34,7 +34,7 @@ impl Command for OverlayRemove {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
@ -110,31 +110,31 @@ impl Command for OverlayRemove {
|
|||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Remove an overlay created from a module",
|
description: "Hide an overlay created from a module",
|
||||||
example: r#"module spam { export def foo [] { "foo" } }
|
example: r#"module spam { export def foo [] { "foo" } }
|
||||||
overlay add spam
|
overlay use spam
|
||||||
overlay remove spam"#,
|
overlay hide spam"#,
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Remove an overlay created from a file",
|
description: "Hide an overlay created from a file",
|
||||||
example: r#"echo 'export alias f = "foo"' | save spam.nu
|
example: r#"echo 'export alias f = "foo"' | save spam.nu
|
||||||
overlay add spam.nu
|
overlay use spam.nu
|
||||||
overlay remove spam"#,
|
overlay hide spam"#,
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Remove the last activated overlay",
|
description: "Hide the last activated overlay",
|
||||||
example: r#"module spam { export env FOO { "foo" } }
|
example: r#"module spam { export env FOO { "foo" } }
|
||||||
overlay add spam
|
overlay use spam
|
||||||
overlay remove"#,
|
overlay hide"#,
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Keep the current working directory when removing an overlay",
|
description: "Keep the current working directory when removing an overlay",
|
||||||
example: r#"overlay new spam
|
example: r#"overlay new spam
|
||||||
cd some-dir
|
cd some-dir
|
||||||
overlay remove --keep-env [ PWD ] spam"#,
|
overlay hide --keep-env [ PWD ] spam"#,
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
]
|
]
|
@ -4,8 +4,6 @@ use nu_protocol::{
|
|||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
use log::trace;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OverlayList;
|
pub struct OverlayList;
|
||||||
|
|
||||||
@ -28,41 +26,17 @@ impl Command for OverlayList {
|
|||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
&self,
|
&self,
|
||||||
engine_state: &EngineState,
|
_engine_state: &EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let active_overlays_parser: Vec<Value> = engine_state
|
|
||||||
.active_overlay_names(&[])
|
|
||||||
.iter()
|
|
||||||
.map(|s| Value::string(String::from_utf8_lossy(s), call.head))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let active_overlays_engine: Vec<Value> = stack
|
let active_overlays_engine: Vec<Value> = stack
|
||||||
.active_overlays
|
.active_overlays
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| Value::string(s, call.head))
|
.map(|s| Value::string(s, call.head))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Check if the overlays in the engine match the overlays in the parser
|
|
||||||
if (active_overlays_parser.len() != active_overlays_engine.len())
|
|
||||||
|| active_overlays_parser
|
|
||||||
.iter()
|
|
||||||
.zip(active_overlays_engine.iter())
|
|
||||||
.any(|(op, oe)| op != oe)
|
|
||||||
{
|
|
||||||
trace!("parser overlays: {:?}", active_overlays_parser);
|
|
||||||
trace!("engine overlays: {:?}", active_overlays_engine);
|
|
||||||
|
|
||||||
return Err(ShellError::NushellFailedSpannedHelp(
|
|
||||||
"Overlay mismatch".into(),
|
|
||||||
"Active overlays do not match between the engine and the parser.".into(),
|
|
||||||
call.head,
|
|
||||||
"Run Nushell with --log-level=trace to see what went wrong.".into(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Value::List {
|
Ok(Value::List {
|
||||||
vals: active_overlays_engine,
|
vals: active_overlays_engine,
|
||||||
span: call.head,
|
span: call.head,
|
||||||
@ -74,7 +48,7 @@ impl Command for OverlayList {
|
|||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Get the last activated overlay",
|
description: "Get the last activated overlay",
|
||||||
example: r#"module spam { export def foo [] { "foo" } }
|
example: r#"module spam { export def foo [] { "foo" } }
|
||||||
overlay add spam
|
overlay use spam
|
||||||
overlay list | last"#,
|
overlay list | last"#,
|
||||||
result: Some(Value::String {
|
result: Some(Value::String {
|
||||||
val: "spam".to_string(),
|
val: "spam".to_string(),
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
mod add;
|
|
||||||
mod command;
|
mod command;
|
||||||
|
mod hide;
|
||||||
mod list;
|
mod list;
|
||||||
mod new;
|
mod new;
|
||||||
mod remove;
|
mod use_;
|
||||||
|
|
||||||
pub use add::OverlayAdd;
|
|
||||||
pub use command::Overlay;
|
pub use command::Overlay;
|
||||||
|
pub use hide::OverlayHide;
|
||||||
pub use list::OverlayList;
|
pub use list::OverlayList;
|
||||||
pub use new::OverlayNew;
|
pub use new::OverlayNew;
|
||||||
pub use remove::OverlayRemove;
|
pub use use_::OverlayUse;
|
||||||
|
@ -31,7 +31,7 @@ impl Command for OverlayNew {
|
|||||||
r#"The command will first create an empty module, then add it as an overlay.
|
r#"The command will first create an empty module, then add it as an overlay.
|
||||||
|
|
||||||
This command is a parser keyword. For details, check:
|
This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
225
crates/nu-command/src/core_commands/overlay/use_.rs
Normal file
225
crates/nu-command/src/core_commands/overlay/use_.rs
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
use nu_engine::{eval_block, find_in_dirs_env, redirect_env, CallExt};
|
||||||
|
use nu_protocol::ast::{Call, Expr};
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct OverlayUse;
|
||||||
|
|
||||||
|
impl Command for OverlayUse {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"overlay use"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Use definitions from a module as an overlay"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("overlay use")
|
||||||
|
.required(
|
||||||
|
"name",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"Module name to use overlay for",
|
||||||
|
)
|
||||||
|
.optional(
|
||||||
|
"as",
|
||||||
|
SyntaxShape::Keyword(b"as".to_vec(), Box::new(SyntaxShape::String)),
|
||||||
|
"as keyword followed by a new name",
|
||||||
|
)
|
||||||
|
.switch(
|
||||||
|
"prefix",
|
||||||
|
"Prepend module name to the imported commands and aliases",
|
||||||
|
Some('p'),
|
||||||
|
)
|
||||||
|
.category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
r#"This command is a parser keyword. For details, check:
|
||||||
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
caller_stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let name_arg: Spanned<String> = call.req(engine_state, caller_stack, 0)?;
|
||||||
|
|
||||||
|
let origin_module_id = if let Some(overlay_expr) = call.positional_nth(0) {
|
||||||
|
if let Expr::Overlay(module_id) = overlay_expr.expr {
|
||||||
|
module_id
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::NushellFailedSpanned(
|
||||||
|
"Not an overlay".to_string(),
|
||||||
|
"requires an overlay (path or a string)".to_string(),
|
||||||
|
overlay_expr.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::NushellFailedSpanned(
|
||||||
|
"Missing positional".to_string(),
|
||||||
|
"missing required overlay".to_string(),
|
||||||
|
call.head,
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
let overlay_name = if let Some(kw_expression) = call.positional_nth(1) {
|
||||||
|
// If renamed via the 'as' keyword, use the new name as the overlay name
|
||||||
|
if let Some(new_name_expression) = kw_expression.as_keyword() {
|
||||||
|
if let Some(new_name) = new_name_expression.as_string() {
|
||||||
|
new_name
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::NushellFailedSpanned(
|
||||||
|
"Wrong keyword type".to_string(),
|
||||||
|
"keyword argument not a string".to_string(),
|
||||||
|
new_name_expression.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::NushellFailedSpanned(
|
||||||
|
"Wrong keyword type".to_string(),
|
||||||
|
"keyword argument not a keyword".to_string(),
|
||||||
|
kw_expression.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else if engine_state
|
||||||
|
.find_overlay(name_arg.item.as_bytes())
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
name_arg.item.clone()
|
||||||
|
} else if let Some(os_str) = Path::new(&name_arg.item).file_stem() {
|
||||||
|
if let Some(name) = os_str.to_str() {
|
||||||
|
name.to_string()
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::NonUtf8(name_arg.span));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::OverlayNotFoundAtRuntime(
|
||||||
|
name_arg.item,
|
||||||
|
name_arg.span,
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
caller_stack.add_overlay(overlay_name);
|
||||||
|
|
||||||
|
if let Some(module_id) = origin_module_id {
|
||||||
|
// Add environment variables only if:
|
||||||
|
// a) adding a new overlay
|
||||||
|
// b) refreshing an active overlay (the origin module changed)
|
||||||
|
|
||||||
|
let module = engine_state.get_module(module_id);
|
||||||
|
|
||||||
|
for (name, block_id) in module.env_vars() {
|
||||||
|
let name = if let Ok(s) = String::from_utf8(name.clone()) {
|
||||||
|
s
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::NonUtf8(call.head));
|
||||||
|
};
|
||||||
|
|
||||||
|
let block = engine_state.get_block(block_id);
|
||||||
|
|
||||||
|
let val = eval_block(
|
||||||
|
engine_state,
|
||||||
|
caller_stack,
|
||||||
|
block,
|
||||||
|
PipelineData::new(call.head),
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
)?
|
||||||
|
.into_value(call.head);
|
||||||
|
|
||||||
|
caller_stack.add_env_var(name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate the export-env block (if any) and keep its environment
|
||||||
|
if let Some(block_id) = module.env_block {
|
||||||
|
let maybe_path = find_in_dirs_env(&name_arg.item, engine_state, caller_stack)?;
|
||||||
|
|
||||||
|
if let Some(path) = &maybe_path {
|
||||||
|
// Set the currently evaluated directory, if the argument is a valid path
|
||||||
|
let mut parent = path.clone();
|
||||||
|
parent.pop();
|
||||||
|
|
||||||
|
let file_pwd = Value::String {
|
||||||
|
val: parent.to_string_lossy().to_string(),
|
||||||
|
span: call.head,
|
||||||
|
};
|
||||||
|
|
||||||
|
caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
let block = engine_state.get_block(block_id);
|
||||||
|
let mut callee_stack = caller_stack.gather_captures(&block.captures);
|
||||||
|
|
||||||
|
let _ = eval_block(
|
||||||
|
engine_state,
|
||||||
|
&mut callee_stack,
|
||||||
|
block,
|
||||||
|
input,
|
||||||
|
call.redirect_stdout,
|
||||||
|
call.redirect_stderr,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Merge the block's environment to the current stack
|
||||||
|
redirect_env(engine_state, caller_stack, &callee_stack);
|
||||||
|
|
||||||
|
if maybe_path.is_some() {
|
||||||
|
// Remove the file-relative PWD, if the argument is a valid path
|
||||||
|
caller_stack.remove_env_var(engine_state, "FILE_PWD");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(PipelineData::new(call.head))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Create an overlay from a module",
|
||||||
|
example: r#"module spam { export def foo [] { "foo" } }
|
||||||
|
overlay use spam
|
||||||
|
foo"#,
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Create an overlay with a prefix",
|
||||||
|
example: r#"echo 'export def foo { "foo" }'
|
||||||
|
overlay use --prefix spam
|
||||||
|
spam foo"#,
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Create an overlay from a file",
|
||||||
|
example: r#"echo 'export env FOO { "foo" }' | save spam.nu
|
||||||
|
overlay use spam.nu
|
||||||
|
$env.FOO"#,
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(OverlayUse {})
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,7 @@ impl Command for Register {
|
|||||||
.required_named(
|
.required_named(
|
||||||
"encoding",
|
"encoding",
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"Encoding used to communicate with plugin. Options: [capnp, json]",
|
"Encoding used to communicate with plugin. Options: [json, msgpack]",
|
||||||
Some('e'),
|
Some('e'),
|
||||||
)
|
)
|
||||||
.optional(
|
.optional(
|
||||||
@ -43,7 +43,7 @@ impl Command for Register {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -25,7 +25,7 @@ impl Command for Use {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This command is a parser keyword. For details, check:
|
r#"This command is a parser keyword. For details, check:
|
||||||
https://www.nushell.sh/book/thinking_in_nushell.html"#
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_parser_keyword(&self) -> bool {
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
@ -32,17 +32,16 @@ impl Command for AliasDb {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Creates an alias for a selected table",
|
description: "Creates an alias for a selected table",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1
|
||||||
| select a
|
| select a
|
||||||
| from table_1
|
|
||||||
| as t1
|
| as t1
|
||||||
| describe"#,
|
| describe"#,
|
||||||
result: Some(Value::Record {
|
result: Some(Value::Record {
|
||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -55,22 +54,20 @@ impl Command for AliasDb {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Creates an alias for a derived table",
|
description: "Creates an alias for a derived table",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table (
|
||||||
| select a
|
open db.sqlite
|
||||||
| from (
|
| from table table_a
|
||||||
open db.mysql
|
|
||||||
| into db
|
|
||||||
| select a b
|
| select a b
|
||||||
| from table_a
|
|
||||||
)
|
)
|
||||||
|
| select a
|
||||||
| as t1
|
| as t1
|
||||||
| describe"#,
|
| describe"#,
|
||||||
result: Some(Value::Record {
|
result: Some(Value::Record {
|
||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -148,15 +145,13 @@ fn alias_db(
|
|||||||
Vec::new(),
|
Vec::new(),
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
s => {
|
s => Err(ShellError::GenericError(
|
||||||
return Err(ShellError::GenericError(
|
"Connection doesn't define a query".into(),
|
||||||
"Connection doesn't define a query".into(),
|
format!("Expected a connection with query. Got {}", s),
|
||||||
format!("Expected a connection with query. Got {}", s),
|
Some(call.head),
|
||||||
Some(call.head),
|
None,
|
||||||
None,
|
Vec::new(),
|
||||||
Vec::new(),
|
)),
|
||||||
))
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,10 +38,9 @@ impl Command for AndDb {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Selects a column from a database with an AND clause",
|
description: "Selects a column from a database with an AND clause",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1
|
||||||
| select a
|
| select a
|
||||||
| from table_1
|
|
||||||
| where ((field a) > 1)
|
| where ((field a) > 1)
|
||||||
| and ((field b) == 1)
|
| and ((field b) == 1)
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -49,7 +48,7 @@ impl Command for AndDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -62,10 +61,9 @@ impl Command for AndDb {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Creates a AND clause combined with an expression AND",
|
description: "Creates a AND clause combined with an expression AND",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1
|
||||||
| select a
|
| select a
|
||||||
| from table_1
|
|
||||||
| where ((field a) > 1 | and ((field a) < 10))
|
| where ((field a) > 1 | and ((field a) < 10))
|
||||||
| and ((field b) == 1)
|
| and ((field b) == 1)
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -73,7 +71,7 @@ impl Command for AndDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
@ -28,13 +28,13 @@ impl Command for CollectDb {
|
|||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Collect from a select query",
|
description: "Collect from a select query",
|
||||||
example: "open foo.db | into db | select a | from table_1 | collect",
|
example: "open foo.db | from table table_1 db | select a | collect",
|
||||||
result: None,
|
result: None,
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "collect"]
|
vec!["database"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -28,7 +28,7 @@ impl Command for DescribeDb {
|
|||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Describe SQLite database constructed query",
|
description: "Describe SQLite database constructed query",
|
||||||
example: "open foo.db | into db | select col_1 | from table_1 | describe",
|
example: "open foo.db | from table table_1 | select col_1 | describe",
|
||||||
result: Some(Value::Record {
|
result: Some(Value::Record {
|
||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
@ -47,7 +47,7 @@ impl Command for DescribeDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "SQLite", "describe"]
|
vec!["database", "SQLite"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
@ -15,7 +15,7 @@ pub struct FromDb;
|
|||||||
|
|
||||||
impl Command for FromDb {
|
impl Command for FromDb {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"from"
|
"from table"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
@ -35,24 +35,24 @@ impl Command for FromDb {
|
|||||||
"Alias for the selected table",
|
"Alias for the selected table",
|
||||||
Some('a'),
|
Some('a'),
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("database".into()))
|
.input_type(Type::Any)
|
||||||
.output_type(Type::Custom("database".into()))
|
.output_type(Type::Custom("database".into()))
|
||||||
.category(Category::Custom("database".into()))
|
.category(Category::Custom("database".into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "from"]
|
vec!["database"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Selects a table from database",
|
description: "Selects a table from database",
|
||||||
example: "open db.mysql | into db | from table_a | describe",
|
example: "open db.sqlite | from table table_a | describe",
|
||||||
result: Some(Value::Record {
|
result: Some(Value::Record {
|
||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
@ -42,9 +42,8 @@ impl Command for GroupByDb {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "groups by column a and calculates the max",
|
description: "groups by column a and calculates the max",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_a
|
||||||
| from table_a
|
|
||||||
| select (fn max a)
|
| select (fn max a)
|
||||||
| group-by a
|
| group-by a
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -52,7 +51,7 @@ impl Command for GroupByDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -65,9 +64,8 @@ impl Command for GroupByDb {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "groups by column column a and counts records",
|
description: "groups by column column a and counts records",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_a
|
||||||
| from table_a
|
|
||||||
| select (fn count *)
|
| select (fn count *)
|
||||||
| group-by a
|
| group-by a
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -75,7 +73,7 @@ impl Command for GroupByDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
344
crates/nu-command/src/database/commands/into_sqlite.rs
Normal file
344
crates/nu-command/src/database/commands/into_sqlite.rs
Normal file
@ -0,0 +1,344 @@
|
|||||||
|
use crate::database::values::sqlite::open_sqlite_db;
|
||||||
|
use itertools::Itertools;
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
|
||||||
|
Spanned, SyntaxShape, Type, Value,
|
||||||
|
};
|
||||||
|
use std::iter;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct IntoSqliteDb;
|
||||||
|
|
||||||
|
impl Command for IntoSqliteDb {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"into sqlite"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("into sqlite")
|
||||||
|
.required(
|
||||||
|
"file_name",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"Specify the filename to save the database to",
|
||||||
|
)
|
||||||
|
.named(
|
||||||
|
"table_name",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"Specify table name to store the data in",
|
||||||
|
Some('t'),
|
||||||
|
)
|
||||||
|
.category(Category::Conversions)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
operate(engine_state, stack, call, input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Convert table into a sqlite database"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["convert", "database"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Convert ls entries into a sqlite database with 'main' as the table name",
|
||||||
|
example: "ls | into sqlite my_ls.db",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert ls entries into a sqlite database with 'my_table' as the table name",
|
||||||
|
example: "ls | into sqlite my_ls.db -t my_table",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert table literal into a sqlite database with 'main' as the table name",
|
||||||
|
example: "[[name]; [-----] [someone] [=====] [somename] ['(((((']] | into sqlite filename.db",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert a variety of values in table literal form into a sqlite database",
|
||||||
|
example: "[one 2 5.2 six true 100mib 25sec] | into sqlite variety.db",
|
||||||
|
result: None,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn operate(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let span = call.head;
|
||||||
|
let config = engine_state.get_config();
|
||||||
|
let file_name: Spanned<String> = call.req(engine_state, stack, 0)?;
|
||||||
|
let table_name: Option<Spanned<String>> = call.get_flag(engine_state, stack, "table_name")?;
|
||||||
|
|
||||||
|
// collect the input into a value
|
||||||
|
let table_entries = input.into_value(span);
|
||||||
|
|
||||||
|
match action(&table_entries, table_name, file_name, config, span) {
|
||||||
|
Ok(val) => Ok(val.into_pipeline_data()),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn action(
|
||||||
|
input: &Value,
|
||||||
|
table: Option<Spanned<String>>,
|
||||||
|
file: Spanned<String>,
|
||||||
|
config: &Config,
|
||||||
|
span: Span,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
let table_name = if let Some(table_name) = table {
|
||||||
|
table_name.item
|
||||||
|
} else {
|
||||||
|
"main".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
match input {
|
||||||
|
Value::List { vals, span } => {
|
||||||
|
// find the column names, and sqlite data types
|
||||||
|
let columns = get_columns_with_sqlite_types(vals);
|
||||||
|
|
||||||
|
let table_columns_creation = columns
|
||||||
|
.iter()
|
||||||
|
.map(|(name, sql_type)| format!("{} {}", name, sql_type))
|
||||||
|
.join(",");
|
||||||
|
|
||||||
|
// get the values
|
||||||
|
let table_values = vals
|
||||||
|
.iter()
|
||||||
|
.map(|list_value| {
|
||||||
|
format!(
|
||||||
|
"({})",
|
||||||
|
match list_value {
|
||||||
|
Value::Record {
|
||||||
|
cols: _,
|
||||||
|
vals,
|
||||||
|
span: _,
|
||||||
|
} => {
|
||||||
|
vals.iter()
|
||||||
|
.map(|rec_val| {
|
||||||
|
format!(
|
||||||
|
"'{}'",
|
||||||
|
nu_value_to_string(rec_val.clone(), "", config)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.join(",")
|
||||||
|
}
|
||||||
|
// Number formats so keep them without quotes
|
||||||
|
Value::Int { val: _, span: _ }
|
||||||
|
| Value::Float { val: _, span: _ }
|
||||||
|
| Value::Filesize { val: _, span: _ }
|
||||||
|
| Value::Duration { val: _, span: _ } =>
|
||||||
|
nu_value_to_string(list_value.clone(), "", config),
|
||||||
|
_ =>
|
||||||
|
// String formats so add quotes around them
|
||||||
|
format!("'{}'", nu_value_to_string(list_value.clone(), "", config)),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.join(",");
|
||||||
|
|
||||||
|
// create the sqlite database table
|
||||||
|
let conn = open_sqlite_db(Path::new(&file.item), file.span)?;
|
||||||
|
|
||||||
|
// create a string for sql table creation
|
||||||
|
let create_statement = format!(
|
||||||
|
"CREATE TABLE IF NOT EXISTS {} ({})",
|
||||||
|
table_name, table_columns_creation
|
||||||
|
);
|
||||||
|
|
||||||
|
// prepare the string as a sqlite statement
|
||||||
|
let mut stmt = conn.prepare(&create_statement).map_err(|e| {
|
||||||
|
ShellError::GenericError(
|
||||||
|
"Failed to prepare SQLite statement".into(),
|
||||||
|
e.to_string(),
|
||||||
|
Some(file.span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// execute the statement
|
||||||
|
stmt.execute([]).map_err(|e| {
|
||||||
|
ShellError::GenericError(
|
||||||
|
"Failed to execute SQLite statement".into(),
|
||||||
|
e.to_string(),
|
||||||
|
Some(file.span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// use normal sql to create the table
|
||||||
|
// insert into table_name
|
||||||
|
// values
|
||||||
|
// ('xx', 'yy', 'zz'),
|
||||||
|
// ('aa', 'bb', 'cc'),
|
||||||
|
// ('dd', 'ee', 'ff')
|
||||||
|
|
||||||
|
// create the string for inserting data into the table
|
||||||
|
let insert_statement = format!("INSERT INTO {} VALUES {}", table_name, table_values);
|
||||||
|
|
||||||
|
// prepare the string as a sqlite statement
|
||||||
|
let mut stmt = conn.prepare(&insert_statement).map_err(|e| {
|
||||||
|
ShellError::GenericError(
|
||||||
|
"Failed to prepare SQLite statement".into(),
|
||||||
|
e.to_string(),
|
||||||
|
Some(file.span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// execute the statement
|
||||||
|
stmt.execute([]).map_err(|e| {
|
||||||
|
ShellError::GenericError(
|
||||||
|
"Failed to execute SQLite statement".into(),
|
||||||
|
e.to_string(),
|
||||||
|
Some(file.span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// and we're done
|
||||||
|
Ok(Value::Nothing { span: *span })
|
||||||
|
}
|
||||||
|
_ => Err(ShellError::UnsupportedInput(
|
||||||
|
format!(
|
||||||
|
"Expected a list but instead received a {}",
|
||||||
|
input.get_type()
|
||||||
|
),
|
||||||
|
span,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is taken from to text local_into_string but tweaks it a bit so that certain formatting does not happen
|
||||||
|
fn nu_value_to_string(value: Value, separator: &str, config: &Config) -> String {
|
||||||
|
match value {
|
||||||
|
Value::Bool { val, .. } => val.to_string(),
|
||||||
|
Value::Int { val, .. } => val.to_string(),
|
||||||
|
Value::Float { val, .. } => val.to_string(),
|
||||||
|
Value::Filesize { val, .. } => val.to_string(),
|
||||||
|
Value::Duration { val, .. } => val.to_string(),
|
||||||
|
Value::Date { val, .. } => val.to_string(),
|
||||||
|
Value::Range { val, .. } => {
|
||||||
|
format!(
|
||||||
|
"{}..{}",
|
||||||
|
nu_value_to_string(val.from, ", ", config),
|
||||||
|
nu_value_to_string(val.to, ", ", config)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Value::String { val, .. } => {
|
||||||
|
// don't store ansi escape sequences in the database
|
||||||
|
let stripped = {
|
||||||
|
match strip_ansi_escapes::strip(&val) {
|
||||||
|
Ok(item) => String::from_utf8(item).unwrap_or(val),
|
||||||
|
Err(_) => val,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// escape single quotes
|
||||||
|
stripped.replace('\'', "''")
|
||||||
|
}
|
||||||
|
Value::List { vals: val, .. } => val
|
||||||
|
.iter()
|
||||||
|
.map(|x| nu_value_to_string(x.clone(), ", ", config))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(separator),
|
||||||
|
Value::Record { cols, vals, .. } => cols
|
||||||
|
.iter()
|
||||||
|
.zip(vals.iter())
|
||||||
|
.map(|(x, y)| format!("{}: {}", x, nu_value_to_string(y.clone(), ", ", config)))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(separator),
|
||||||
|
Value::Block { val, .. } => format!("<Block {}>", val),
|
||||||
|
Value::Nothing { .. } => String::new(),
|
||||||
|
Value::Error { error } => format!("{:?}", error),
|
||||||
|
Value::Binary { val, .. } => format!("{:?}", val),
|
||||||
|
Value::CellPath { val, .. } => val.into_string(),
|
||||||
|
Value::CustomValue { val, .. } => val.value_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:
|
||||||
|
// NULL. The value is a NULL value.
|
||||||
|
// INTEGER. The value is a signed integer, stored in 0, 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
|
||||||
|
// REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
|
||||||
|
// TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
|
||||||
|
// BLOB. The value is a blob of data, stored exactly as it was input.
|
||||||
|
fn nu_type_to_sqlite_type(nu_type: Type) -> &'static str {
|
||||||
|
match nu_type {
|
||||||
|
Type::Int => "INTEGER",
|
||||||
|
Type::Float => "REAL",
|
||||||
|
Type::String => "TEXT",
|
||||||
|
Type::Bool => "TEXT",
|
||||||
|
Type::Nothing => "NULL",
|
||||||
|
Type::Filesize => "INTEGER",
|
||||||
|
Type::Date => "TEXT",
|
||||||
|
_ => "TEXT",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_columns_with_sqlite_types(input: &[Value]) -> Vec<(String, String)> {
|
||||||
|
let mut columns: Vec<(String, String)> = vec![];
|
||||||
|
let mut added = false;
|
||||||
|
|
||||||
|
for item in input {
|
||||||
|
// let sqlite_type = nu_type_to_sqlite_type(item.get_type());
|
||||||
|
// eprintln!(
|
||||||
|
// "item_type: {:?}, sqlite_type: {:?}",
|
||||||
|
// item.get_type(),
|
||||||
|
// sqlite_type
|
||||||
|
// );
|
||||||
|
|
||||||
|
if let Value::Record { cols, vals, .. } = item {
|
||||||
|
for (c, v) in iter::zip(cols, vals) {
|
||||||
|
if !columns.iter().any(|(name, _)| name == c) {
|
||||||
|
columns.push((
|
||||||
|
c.to_string(),
|
||||||
|
nu_type_to_sqlite_type(v.get_type()).to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// force every other type to a string
|
||||||
|
if !added {
|
||||||
|
columns.push(("value".to_string(), "TEXT".to_string()));
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
columns
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
// use super::{action, IntoSqliteDb};
|
||||||
|
// use nu_protocol::Type::Error;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(IntoSqliteDb {})
|
||||||
|
}
|
||||||
|
}
|
@ -47,24 +47,23 @@ impl Command for JoinDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "join"]
|
vec!["database"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "joins two tables on col_b",
|
description: "joins two tables on col_b",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1 --as t1
|
||||||
| select col_a
|
|
||||||
| from table_1 --as t1
|
|
||||||
| join table_2 col_b --as t2
|
| join table_2 col_b --as t2
|
||||||
|
| select col_a
|
||||||
| describe"#,
|
| describe"#,
|
||||||
result: Some(Value::Record {
|
result: Some(Value::Record {
|
||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -78,22 +77,20 @@ impl Command for JoinDb {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "joins a table with a derived table using aliases",
|
description: "joins a table with a derived table using aliases",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1 --as t1
|
||||||
| select col_a
|
|
||||||
| from table_1 --as t1
|
|
||||||
| join (
|
| join (
|
||||||
open db.mysql
|
open db.sqlite
|
||||||
| into db
|
| from table table_2
|
||||||
| select col_c
|
| select col_c
|
||||||
| from table_2
|
|
||||||
) ((field t1.col_a) == (field t2.col_c)) --as t2 --right
|
) ((field t1.col_a) == (field t2.col_c)) --as t2 --right
|
||||||
|
| select col_a
|
||||||
| describe"#,
|
| describe"#,
|
||||||
result: Some(Value::Record {
|
result: Some(Value::Record {
|
||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
@ -34,15 +34,14 @@ impl Command for LimitDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "limit"]
|
vec!["database", "head", "tail"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Limits selection from table",
|
description: "Limits selection from table",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_a
|
||||||
| from table_a
|
|
||||||
| select a
|
| select a
|
||||||
| limit 10
|
| limit 10
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -50,7 +49,7 @@ impl Command for LimitDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
@ -7,6 +7,7 @@ mod collect;
|
|||||||
mod describe;
|
mod describe;
|
||||||
mod from;
|
mod from;
|
||||||
mod group_by;
|
mod group_by;
|
||||||
|
mod into_sqlite;
|
||||||
mod join;
|
mod join;
|
||||||
mod limit;
|
mod limit;
|
||||||
mod open;
|
mod open;
|
||||||
@ -22,16 +23,16 @@ mod where_;
|
|||||||
mod testing;
|
mod testing;
|
||||||
use testing::TestingDb;
|
use testing::TestingDb;
|
||||||
|
|
||||||
use nu_protocol::engine::StateWorkingSet;
|
|
||||||
|
|
||||||
use alias::AliasDb;
|
use alias::AliasDb;
|
||||||
use and::AndDb;
|
use and::AndDb;
|
||||||
use collect::CollectDb;
|
use collect::CollectDb;
|
||||||
pub(crate) use describe::DescribeDb;
|
pub(crate) use describe::DescribeDb;
|
||||||
pub(crate) use from::FromDb;
|
pub(crate) use from::FromDb;
|
||||||
use group_by::GroupByDb;
|
use group_by::GroupByDb;
|
||||||
|
use into_sqlite::IntoSqliteDb;
|
||||||
use join::JoinDb;
|
use join::JoinDb;
|
||||||
use limit::LimitDb;
|
use limit::LimitDb;
|
||||||
|
use nu_protocol::engine::StateWorkingSet;
|
||||||
use open::OpenDb;
|
use open::OpenDb;
|
||||||
use or::OrDb;
|
use or::OrDb;
|
||||||
use order_by::OrderByDb;
|
use order_by::OrderByDb;
|
||||||
@ -60,6 +61,7 @@ pub fn add_commands_decls(working_set: &mut StateWorkingSet) {
|
|||||||
DescribeDb,
|
DescribeDb,
|
||||||
FromDb,
|
FromDb,
|
||||||
GroupByDb,
|
GroupByDb,
|
||||||
|
IntoSqliteDb,
|
||||||
JoinDb,
|
JoinDb,
|
||||||
LimitDb,
|
LimitDb,
|
||||||
OpenDb,
|
OpenDb,
|
||||||
|
@ -29,7 +29,7 @@ impl Command for OpenDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "open"]
|
vec!["database"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
@ -38,10 +38,9 @@ impl Command for OrDb {
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "selects a column from a database with an OR clause",
|
description: "selects a column from a database with an OR clause",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1
|
||||||
| select a
|
| select a
|
||||||
| from table_1
|
|
||||||
| where ((field a) > 1)
|
| where ((field a) > 1)
|
||||||
| or ((field b) == 1)
|
| or ((field b) == 1)
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -49,7 +48,7 @@ impl Command for OrDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -62,10 +61,9 @@ impl Command for OrDb {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Creates an OR clause in the column names and a column",
|
description: "Creates an OR clause in the column names and a column",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_1
|
||||||
| select a
|
| select a
|
||||||
| from table_1
|
|
||||||
| where ((field a) > 1 | or ((field a) < 10))
|
| where ((field a) > 1 | or ((field a) < 10))
|
||||||
| or ((field b) == 1)
|
| or ((field b) == 1)
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -73,7 +71,7 @@ impl Command for OrDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
@ -37,16 +37,15 @@ impl Command for OrderByDb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["database", "order-by"]
|
vec!["database"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "orders query by a column",
|
description: "orders query by a column",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_a
|
||||||
| from table_a
|
|
||||||
| select a
|
| select a
|
||||||
| order-by a
|
| order-by a
|
||||||
| describe"#,
|
| describe"#,
|
||||||
@ -54,7 +53,7 @@ impl Command for OrderByDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
@ -67,9 +66,8 @@ impl Command for OrderByDb {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "orders query by column a ascending and by column b",
|
description: "orders query by column a ascending and by column b",
|
||||||
example: r#"open db.mysql
|
example: r#"open db.sqlite
|
||||||
| into db
|
| from table table_a
|
||||||
| from table_a
|
|
||||||
| select a
|
| select a
|
||||||
| order-by a --ascending
|
| order-by a --ascending
|
||||||
| order-by b
|
| order-by b
|
||||||
@ -78,7 +76,7 @@ impl Command for OrderByDb {
|
|||||||
cols: vec!["connection".into(), "query".into()],
|
cols: vec!["connection".into(), "query".into()],
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::String {
|
Value::String {
|
||||||
val: "db.mysql".into(),
|
val: "db.sqlite".into(),
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
},
|
},
|
||||||
Value::String {
|
Value::String {
|
||||||
|
@ -13,17 +13,17 @@ pub struct QueryDb;
|
|||||||
|
|
||||||
impl Command for QueryDb {
|
impl Command for QueryDb {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"query"
|
"query db"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.required(
|
.required(
|
||||||
"query",
|
"SQL",
|
||||||
SyntaxShape::String,
|
SyntaxShape::String,
|
||||||
"SQL to execute against the database",
|
"SQL to execute against the database",
|
||||||
)
|
)
|
||||||
.input_type(Type::Custom("database".into()))
|
.input_type(Type::Any)
|
||||||
.output_type(Type::Any)
|
.output_type(Type::Any)
|
||||||
.category(Category::Custom("database".into()))
|
.category(Category::Custom("database".into()))
|
||||||
}
|
}
|
||||||
@ -34,8 +34,8 @@ impl Command for QueryDb {
|
|||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Execute a query statement using the database connection",
|
description: "Execute SQL against a SQLite database",
|
||||||
example: r#"open foo.db | into db | query "SELECT * FROM Bar""#,
|
example: r#"open foo.db | query db "SELECT * FROM Bar""#,
|
||||||
result: None,
|
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