Compare commits

..

1 Commits

Author SHA1 Message Date
b52ec7f7b2 Bump quick-xml from 0.30.0 to 0.31.0
Bumps [quick-xml](https://github.com/tafia/quick-xml) from 0.30.0 to 0.31.0.
- [Release notes](https://github.com/tafia/quick-xml/releases)
- [Changelog](https://github.com/tafia/quick-xml/blob/master/Changelog.md)
- [Commits](https://github.com/tafia/quick-xml/compare/v0.30.0...v0.31.0)

---
updated-dependencies:
- dependency-name: quick-xml
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-11-20 20:25:39 +00:00
1397 changed files with 55711 additions and 94861 deletions

14
.github/.typos.toml vendored Normal file
View File

@ -0,0 +1,14 @@
[files]
extend-exclude = ["crates/nu-command/tests/commands/table.rs", "*.tsv", "*.json", "*.txt"]
[default.extend-words]
# Ignore false-positives
nd = "nd"
fo = "fo"
ons = "ons"
ba = "ba"
Plasticos = "Plasticos"
IIF = "IIF"
numer = "numer"
ratatui = "ratatui"
doas = "doas"

View File

@ -0,0 +1,11 @@
---
name: standard library bug or feature report
about: Used to submit issues related to the nu standard library
title: ''
labels: ['needs-triage', 'std-library']
assignees: ''
---
**Describe the bug or feature**
A clear and concise description of what the bug is.

View File

@ -11,10 +11,6 @@ updates:
directory: "/" directory: "/"
schedule: schedule:
interval: "weekly" interval: "weekly"
# We release on Tuesdays and open dependabot PRs will rebase after the
# version bump and thus consume unnecessary workers during release, thus
# let's open new ones on Wednesday
day: "wednesday"
ignore: ignore:
- dependency-name: "*" - dependency-name: "*"
update-types: ["version-update:semver-patch"] update-types: ["version-update:semver-patch"]
@ -22,4 +18,3 @@ updates:
directory: "/" directory: "/"
schedule: schedule:
interval: "weekly" interval: "weekly"
day: "wednesday"

View File

@ -26,7 +26,7 @@ Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library
> **Note** > **Note**
> from `nushell` you can also use the `toolkit` as follows > from `nushell` you can also use the `toolkit` as follows

View File

@ -19,7 +19,7 @@ jobs:
# Prevent sudden announcement of a new advisory from failing ci: # Prevent sudden announcement of a new advisory from failing ci:
continue-on-error: true continue-on-error: true
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- uses: rustsec/audit-check@v1.4.1 - uses: rustsec/audit-check@v1.4.1
with: with:
token: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,12 +0,0 @@
let toolchain_spec = open rust-toolchain.toml | get toolchain.channel
let msrv_spec = open Cargo.toml | get package.rust-version
# This check is conservative in the sense that we use `rust-toolchain.toml`'s
# override to ensure that this is the upper-bound for the minimum supported
# rust version
if $toolchain_spec != $msrv_spec {
print -e "Mismatching rust compiler versions specified in `Cargo.toml` and `rust-toolchain.toml`"
print -e $"Cargo.toml: ($msrv_spec)"
print -e $"rust-toolchain.toml: ($toolchain_spec)"
exit 1
}

View File

@ -10,11 +10,7 @@ env:
NUSHELL_CARGO_PROFILE: ci NUSHELL_CARGO_PROFILE: ci
NU_LOG_LEVEL: DEBUG NU_LOG_LEVEL: DEBUG
# If changing these settings also change toolkit.nu # If changing these settings also change toolkit.nu
CLIPPY_OPTIONS: "-D warnings -D clippy::unwrap_used -D clippy::unchecked_duration_subtraction" CLIPPY_OPTIONS: "-D warnings -D clippy::unwrap_used"
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}
cancel-in-progress: true
jobs: jobs:
fmt-clippy: fmt-clippy:
@ -24,65 +20,77 @@ jobs:
# Pinning to Ubuntu 20.04 because building on newer Ubuntu versions causes linux-gnu # Pinning to Ubuntu 20.04 because building on newer Ubuntu versions causes linux-gnu
# builds to link against a too-new-for-many-Linux-installs glibc version. Consider # builds to link against a too-new-for-many-Linux-installs glibc version. Consider
# revisiting this when 20.04 is closer to EOL (April 2025) # revisiting this when 20.04 is closer to EOL (April 2025)
# platform: [windows-latest, macos-latest, ubuntu-20.04]
# Using macOS 13 runner because 14 is based on the M1 and has half as much RAM (7 GB, feature: [default, dataframe, extra]
# instead of 14 GB) which is too little for us right now. Revisit when `dfr` commands are include:
# removed and we're only building the `polars` plugin instead - feature: default
platform: [windows-latest, macos-13, ubuntu-20.04] flags: ""
- feature: dataframe
flags: "--features=dataframe"
- feature: extra
flags: "--features=extra"
exclude:
- platform: windows-latest
feature: dataframe
- platform: macos-latest
feature: dataframe
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
with:
rustflags: ""
- name: cargo fmt - name: cargo fmt
run: cargo fmt --all -- --check run: cargo fmt --all -- --check
# If changing these settings also change toolkit.nu # If changing these settings also change toolkit.nu
- name: Clippy - name: Clippy
run: cargo clippy --workspace --exclude nu_plugin_* -- $CLIPPY_OPTIONS run: cargo clippy --workspace ${{ matrix.flags }} --exclude nu_plugin_* -- $CLIPPY_OPTIONS
# In tests we don't have to deny unwrap # In tests we don't have to deny unwrap
- name: Clippy of tests - name: Clippy of tests
run: cargo clippy --tests --workspace --exclude nu_plugin_* -- -D warnings run: cargo clippy --tests --workspace ${{ matrix.flags }} --exclude nu_plugin_* -- -D warnings
- name: Clippy of benchmarks
run: cargo clippy --benches --workspace --exclude nu_plugin_* -- -D warnings
tests: tests:
strategy: strategy:
fail-fast: true fail-fast: true
matrix: matrix:
platform: [windows-latest, macos-latest, ubuntu-20.04] platform: [windows-latest, macos-latest, ubuntu-20.04]
feature: [default, dataframe, extra]
include: include:
- default-flags: "" - feature: default
# linux CI cannot handle clipboard feature flags: ""
- platform: ubuntu-20.04 - feature: dataframe
default-flags: "--no-default-features --features=default-no-clipboard" flags: "--features=dataframe"
- feature: extra
flags: "--features=extra"
exclude:
- platform: windows-latest
feature: dataframe
- platform: macos-latest
feature: dataframe
- platform: windows-latest
feature: extra
- platform: macos-latest
feature: extra
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
with:
rustflags: ""
- name: Tests - name: Tests
run: cargo test --workspace --profile ci --exclude nu_plugin_* ${{ matrix.default-flags }} run: cargo test --workspace --profile ci --exclude nu_plugin_* ${{ matrix.flags }}
- name: Check for clean repo
shell: bash
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
git status --porcelain
exit 1
else
echo "no changes in working directory";
fi
std-lib-and-python-virtualenv: std-lib-and-python-virtualenv:
strategy: strategy:
@ -95,22 +103,21 @@ jobs:
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
with:
rustflags: ""
- name: Install Nushell - name: Install Nushell
run: cargo install --path . --locked --no-default-features run: cargo install --path . --locked --no-default-features
- name: Standard library tests - name: Standard library tests
run: nu -c 'use crates/nu-std/testing.nu; testing run-tests --path crates/nu-std' run: nu -c 'use std testing; testing run-tests --path crates/nu-std'
- name: Ensure that Cargo.toml MSRV and rust-toolchain.toml use the same version
run: nu .github/workflows/check-msrv.nu
- name: Setup Python - name: Setup Python
uses: actions/setup-python@v5 uses: actions/setup-python@v4
with: with:
python-version: "3.10" python-version: "3.10"
@ -122,48 +129,24 @@ jobs:
run: nu scripts/test_virtualenv.nu run: nu scripts/test_virtualenv.nu
shell: bash shell: bash
- name: Check for clean repo
shell: bash
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
git status --porcelain
exit 1
else
echo "no changes in working directory";
fi
plugins: plugins:
strategy: strategy:
fail-fast: true fail-fast: true
matrix: matrix:
# Using macOS 13 runner because 14 is based on the M1 and has half as much RAM (7 GB, platform: [windows-latest, macos-latest, ubuntu-20.04]
# instead of 14 GB) which is too little for us right now.
#
# Failure occurring with clippy for rust 1.77.2
platform: [windows-latest, macos-13, ubuntu-20.04]
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
with:
rustflags: ""
- name: Clippy - name: Clippy
run: cargo clippy --package nu_plugin_* -- $CLIPPY_OPTIONS run: cargo clippy --package nu_plugin_* ${{ matrix.flags }} -- $CLIPPY_OPTIONS
- name: Tests - name: Tests
run: cargo test --profile ci --package nu_plugin_* run: cargo test --profile ci --package nu_plugin_*
- name: Check for clean repo
shell: bash
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
git status --porcelain
exit 1
else
echo "no changes in working directory";
fi

View File

@ -27,7 +27,7 @@ jobs:
# if: github.repository == 'nushell/nightly' # if: github.repository == 'nushell/nightly'
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4.1.6 uses: actions/checkout@v4
if: github.repository == 'nushell/nightly' if: github.repository == 'nushell/nightly'
with: with:
ref: main ref: main
@ -36,10 +36,12 @@ jobs:
token: ${{ secrets.WORKFLOW_TOKEN }} token: ${{ secrets.WORKFLOW_TOKEN }}
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.10 uses: hustcer/setup-nu@v3.8
if: github.repository == 'nushell/nightly' if: github.repository == 'nushell/nightly'
with: with:
version: 0.93.0 version: 0.86.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Synchronize the main branch of nightly repo with the main branch of Nushell official repo # Synchronize the main branch of nightly repo with the main branch of Nushell official repo
- name: Prepare for Nightly Release - name: Prepare for Nightly Release
@ -84,35 +86,46 @@ jobs:
include: include:
- target: aarch64-apple-darwin - target: aarch64-apple-darwin
os: macos-latest os: macos-latest
target_rustflags: ''
- target: x86_64-apple-darwin - target: x86_64-apple-darwin
os: macos-latest os: macos-latest
target_rustflags: ''
- target: x86_64-pc-windows-msvc - target: x86_64-pc-windows-msvc
extra: 'bin' extra: 'bin'
os: windows-latest os: windows-latest
target_rustflags: ''
- target: x86_64-pc-windows-msvc - target: x86_64-pc-windows-msvc
extra: msi extra: msi
os: windows-latest os: windows-latest
target_rustflags: ''
- target: aarch64-pc-windows-msvc - target: aarch64-pc-windows-msvc
extra: 'bin' extra: 'bin'
os: windows-latest os: windows-latest
target_rustflags: ''
- target: aarch64-pc-windows-msvc - target: aarch64-pc-windows-msvc
extra: msi extra: msi
os: windows-latest os: windows-latest
target_rustflags: ''
- target: x86_64-unknown-linux-gnu - target: x86_64-unknown-linux-gnu
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: x86_64-unknown-linux-musl - target: x86_64-unknown-linux-musl
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: aarch64-unknown-linux-gnu - target: aarch64-unknown-linux-gnu
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: armv7-unknown-linux-gnueabihf - target: armv7-unknown-linux-gnueabihf
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: riscv64gc-unknown-linux-gnu - target: riscv64gc-unknown-linux-gnu
os: ubuntu-latest os: ubuntu-20.04
target_rustflags: ''
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
with: with:
ref: main ref: main
fetch-depth: 0 fetch-depth: 0
@ -122,28 +135,32 @@ jobs:
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135` # WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
with: with:
rustflags: '' rustflags: ''
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.10 uses: hustcer/setup-nu@v3.8
with: with:
version: 0.93.0 version: 0.86.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release Nu Binary - name: Release Nu Binary
id: nu id: nu
run: nu .github/workflows/release-pkg.nu run: nu .github/workflows/release-pkg.nu
env: env:
RELEASE_TYPE: standard
OS: ${{ matrix.os }} OS: ${{ matrix.os }}
REF: ${{ github.ref }} REF: ${{ github.ref }}
TARGET: ${{ matrix.target }} TARGET: ${{ matrix.target }}
_EXTRA_: ${{ matrix.extra }} _EXTRA_: ${{ matrix.extra }}
TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }}
- name: Create an Issue for Release Failure - name: Create an Issue for Release Failure
if: ${{ failure() }} if: ${{ failure() }}
uses: JasonEtco/create-an-issue@v2.9.2 uses: JasonEtco/create-an-issue@v2.9.1
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
@ -161,7 +178,7 @@ jobs:
# REF: https://github.com/marketplace/actions/gh-release # REF: https://github.com/marketplace/actions/gh-release
# Create a release only in nushell/nightly repo # Create a release only in nushell/nightly repo
- name: Publish Archive - name: Publish Archive
uses: softprops/action-gh-release@v2.0.5 uses: softprops/action-gh-release@v0.1.15
if: ${{ startsWith(github.repository, 'nushell/nightly') }} if: ${{ startsWith(github.repository, 'nushell/nightly') }}
with: with:
prerelease: true prerelease: true
@ -171,6 +188,124 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
full:
name: Full
needs: prepare
strategy:
fail-fast: false
matrix:
target:
- aarch64-apple-darwin
- x86_64-apple-darwin
- x86_64-pc-windows-msvc
- aarch64-pc-windows-msvc
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
- aarch64-unknown-linux-gnu
extra: ['bin']
include:
- target: aarch64-apple-darwin
os: macos-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-apple-darwin
os: macos-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: aarch64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: aarch64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe,extra'
- target: x86_64-unknown-linux-musl
os: ubuntu-20.04
target_rustflags: '--features=dataframe,extra'
- target: aarch64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe,extra'
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
- name: Update Rust Toolchain Target
run: |
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
- name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
with:
rustflags: ''
- name: Setup Nushell
uses: hustcer/setup-nu@v3.8
with:
version: 0.86.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release Nu Binary
id: nu
run: nu .github/workflows/release-pkg.nu
env:
RELEASE_TYPE: full
OS: ${{ matrix.os }}
REF: ${{ github.ref }}
TARGET: ${{ matrix.target }}
_EXTRA_: ${{ matrix.extra }}
TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }}
- name: Create an Issue for Release Failure
if: ${{ failure() }}
uses: JasonEtco/create-an-issue@v2.9.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
update_existing: true
search_existing: open
filename: .github/AUTO_ISSUE_TEMPLATE/nightly-build-fail.md
- name: Set Outputs of Short SHA
id: vars
run: |
echo "date=$(date -u +'%Y-%m-%d')" >> $GITHUB_OUTPUT
sha_short=$(git rev-parse --short HEAD)
echo "sha_short=${sha_short:0:7}" >> $GITHUB_OUTPUT
# REF: https://github.com/marketplace/actions/gh-release
# Create a release only in nushell/nightly repo
- name: Publish Archive
uses: softprops/action-gh-release@v0.1.15
if: ${{ startsWith(github.repository, 'nushell/nightly') }}
with:
draft: false
prerelease: true
name: Nu-nightly-${{ steps.vars.outputs.date }}-${{ steps.vars.outputs.sha_short }}
tag_name: nightly-${{ steps.vars.outputs.sha_short }}
body: |
This is a NIGHTLY build of Nushell.
It is NOT recommended for production use.
files: ${{ steps.nu.outputs.archive }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
cleanup: cleanup:
name: Cleanup name: Cleanup
# Should only run in nushell/nightly repo # Should only run in nushell/nightly repo
@ -181,14 +316,16 @@ jobs:
- name: Waiting for Release - name: Waiting for Release
run: sleep 1800 run: sleep 1800
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
with: with:
ref: main ref: main
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.10 uses: hustcer/setup-nu@v3.8
with: with:
version: 0.93.0 version: 0.86.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Keep the last a few releases # Keep the last a few releases
- name: Delete Older Releases - name: Delete Older Releases

View File

@ -9,6 +9,7 @@
# Instructions for manually creating an MSI for Winget Releases when they fail # Instructions for manually creating an MSI for Winget Releases when they fail
# Added 2022-11-29 when Windows packaging wouldn't work # Added 2022-11-29 when Windows packaging wouldn't work
# Updated again on 2023-02-23 because msis are still failing validation # Updated again on 2023-02-23 because msis are still failing validation
# Update on 2023-10-18 to use RELEASE_TYPE env var to determine if full or not
# To run this manual for windows here are the steps I take # To run this manual for windows here are the steps I take
# checkout the release you want to publish # checkout the release you want to publish
# 1. git checkout 0.86.0 # 1. git checkout 0.86.0
@ -16,26 +17,28 @@
# 2. $env:CARGO_TARGET_DIR = "" # 2. $env:CARGO_TARGET_DIR = ""
# 2. hide-env CARGO_TARGET_DIR # 2. hide-env CARGO_TARGET_DIR
# 3. $env.TARGET = 'x86_64-pc-windows-msvc' # 3. $env.TARGET = 'x86_64-pc-windows-msvc'
# 4. $env.GITHUB_WORKSPACE = 'D:\nushell' # 4. $env.TARGET_RUSTFLAGS = ''
# 5. $env.GITHUB_OUTPUT = 'D:\nushell\output\out.txt' # 5. $env.GITHUB_WORKSPACE = 'D:\nushell'
# 6. $env.OS = 'windows-latest' # 6. $env.GITHUB_OUTPUT = 'D:\nushell\output\out.txt'
# 7. $env.OS = 'windows-latest'
# 8. $env.RELEASE_TYPE = '' # There is full and '' for normal releases
# make sure 7z.exe is in your path https://www.7-zip.org/download.html # make sure 7z.exe is in your path https://www.7-zip.org/download.html
# 7. $env.Path = ($env.Path | append 'c:\apps\7-zip') # 9. $env.Path = ($env.Path | append 'c:\apps\7-zip')
# make sure aria2c.exe is in your path https://github.com/aria2/aria2 # make sure aria2c.exe is in your path https://github.com/aria2/aria2
# 8. $env.Path = ($env.Path | append 'c:\path\to\aria2c') # 10. $env.Path = ($env.Path | append 'c:\path\to\aria2c')
# make sure you have the wixtools installed https://wixtoolset.org/ # make sure you have the wixtools installed https://wixtoolset.org/
# 9. $env.Path = ($env.Path | append 'C:\Users\dschroeder\AppData\Local\tauri\WixTools') # 11. $env.Path = ($env.Path | append 'C:\Users\dschroeder\AppData\Local\tauri\WixTools')
# You need to run the release-pkg twice. The first pass, with _EXTRA_ as 'bin', makes the output # You need to run the release-pkg twice. The first pass, with _EXTRA_ as 'bin', makes the output
# folder and builds everything. The second pass, that generates the msi file, with _EXTRA_ as 'msi' # folder and builds everything. The second pass, that generates the msi file, with _EXTRA_ as 'msi'
# 10. $env._EXTRA_ = 'bin' # 12. $env._EXTRA_ = 'bin'
# 11. source .github\workflows\release-pkg.nu # 13. source .github\workflows\release-pkg.nu
# 12. cd .. # 14. cd ..
# 13. $env._EXTRA_ = 'msi' # 15. $env._EXTRA_ = 'msi'
# 14. source .github\workflows\release-pkg.nu # 16. source .github\workflows\release-pkg.nu
# After msi is generated, you have to update winget-pkgs repo, you'll need to patch the release # After msi is generated, you have to update winget-pkgs repo, you'll need to patch the release
# by deleting the existing msi and uploading this new msi. Then you'll need to update the hash # by deleting the existing msi and uploading this new msi. Then you'll need to update the hash
# on the winget-pkgs PR. To generate the hash, run this command # on the winget-pkgs PR. To generate the hash, run this command
# 15. open target\wix\nu-0.74.0-x86_64-pc-windows-msvc.msi | hash sha256 # 17. open target\wix\nu-0.74.0-x86_64-pc-windows-msvc.msi | hash sha256
# Then, just take the output and put it in the winget-pkgs PR for the hash on the msi # Then, just take the output and put it in the winget-pkgs PR for the hash on the msi
@ -45,15 +48,31 @@ let os = $env.OS
let target = $env.TARGET let target = $env.TARGET
# Repo source dir like `/home/runner/work/nushell/nushell` # Repo source dir like `/home/runner/work/nushell/nushell`
let src = $env.GITHUB_WORKSPACE let src = $env.GITHUB_WORKSPACE
let flags = $env.TARGET_RUSTFLAGS
let dist = $'($env.GITHUB_WORKSPACE)/output' let dist = $'($env.GITHUB_WORKSPACE)/output'
let version = (open Cargo.toml | get package.version) let version = (open Cargo.toml | get package.version)
print $'Debugging info:' print $'Debugging info:'
print { version: $version, bin: $bin, os: $os, target: $target, src: $src, dist: $dist }; hr-line -b print { version: $version, bin: $bin, os: $os, releaseType: $env.RELEASE_TYPE, target: $target, src: $src, flags: $flags, dist: $dist }; hr-line -b
# Rename the full release name so that we won't break the existing scripts for standard release downloading, such as:
# curl -s https://api.github.com/repos/chmln/sd/releases/latest | grep browser_download_url | cut -d '"' -f 4 | grep x86_64-unknown-linux-musl
const FULL_RLS_NAMING = {
x86_64-apple-darwin: 'x86_64-darwin-full',
aarch64-apple-darwin: 'aarch64-darwin-full',
x86_64-unknown-linux-gnu: 'x86_64-linux-gnu-full',
x86_64-pc-windows-msvc: 'x86_64-windows-msvc-full',
x86_64-unknown-linux-musl: 'x86_64-linux-musl-full',
aarch64-unknown-linux-gnu: 'aarch64-linux-gnu-full',
aarch64-pc-windows-msvc: 'aarch64-windows-msvc-full',
riscv64gc-unknown-linux-gnu: 'riscv64-linux-gnu-full',
armv7-unknown-linux-gnueabihf: 'armv7-linux-gnueabihf-full',
}
# $env # $env
let USE_UBUNTU = $os starts-with ubuntu let USE_UBUNTU = 'ubuntu-20.04'
let FULL_NAME = $FULL_RLS_NAMING | get -i $target | default 'unknown-target-full'
print $'(char nl)Packaging ($bin) v($version) for ($target) in ($src)...'; hr-line -b print $'(char nl)Packaging ($bin) v($version) for ($target) in ($src)...'; hr-line -b
if not ('Cargo.lock' | path exists) { cargo generate-lockfile } if not ('Cargo.lock' | path exists) { cargo generate-lockfile }
@ -63,8 +82,8 @@ print $'Start building ($bin)...'; hr-line
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Build for Ubuntu and macOS # Build for Ubuntu and macOS
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
if $os in ['macos-latest'] or $USE_UBUNTU { if $os in [$USE_UBUNTU, 'macos-latest'] {
if $USE_UBUNTU { if $os == $USE_UBUNTU {
sudo apt update sudo apt update
sudo apt-get install libxcb-composite0-dev -y sudo apt-get install libxcb-composite0-dev -y
} }
@ -72,23 +91,23 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
'aarch64-unknown-linux-gnu' => { 'aarch64-unknown-linux-gnu' => {
sudo apt-get install gcc-aarch64-linux-gnu -y sudo apt-get install gcc-aarch64-linux-gnu -y
$env.CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = 'aarch64-linux-gnu-gcc' $env.CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = 'aarch64-linux-gnu-gcc'
cargo-build-nu cargo-build-nu $flags
} }
'riscv64gc-unknown-linux-gnu' => { 'riscv64gc-unknown-linux-gnu' => {
sudo apt-get install gcc-riscv64-linux-gnu -y sudo apt-get install gcc-riscv64-linux-gnu -y
$env.CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER = 'riscv64-linux-gnu-gcc' $env.CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER = 'riscv64-linux-gnu-gcc'
cargo-build-nu cargo-build-nu $flags
} }
'armv7-unknown-linux-gnueabihf' => { 'armv7-unknown-linux-gnueabihf' => {
sudo apt-get install pkg-config gcc-arm-linux-gnueabihf -y sudo apt-get install pkg-config gcc-arm-linux-gnueabihf -y
$env.CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER = 'arm-linux-gnueabihf-gcc' $env.CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER = 'arm-linux-gnueabihf-gcc'
cargo-build-nu cargo-build-nu $flags
} }
_ => { _ => {
# 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
if $USE_UBUNTU { sudo apt install musl-tools -y } if $os == $USE_UBUNTU { sudo apt install musl-tools -y }
cargo-build-nu cargo-build-nu $flags
} }
} }
} }
@ -97,7 +116,7 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
# Build for Windows without static-link-openssl feature # Build for Windows without static-link-openssl feature
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
if $os in ['windows-latest'] { if $os in ['windows-latest'] {
cargo-build-nu cargo-build-nu $flags
} }
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
@ -109,22 +128,18 @@ let executable = $'target/($target)/release/($bin)*($suffix)'
print $'Current executable file: ($executable)' print $'Current executable file: ($executable)'
cd $src; mkdir $dist; cd $src; mkdir $dist;
rm -rf ...(glob $'target/($target)/release/*.d') ...(glob $'target/($target)/release/nu_pretty_hex*') rm -rf $'target/($target)/release/*.d' $'target/($target)/release/nu_pretty_hex*'
print $'(char nl)All executable files:'; hr-line print $'(char nl)All executable files:'; hr-line
# We have to use `print` here to make sure the command output is displayed # We have to use `print` here to make sure the command output is displayed
print (ls -f ($executable | into glob)); sleep 1sec print (ls -f $executable); sleep 1sec
print $'(char nl)Copying release files...'; hr-line print $'(char nl)Copying release files...'; hr-line
"To use the included Nushell plugins, register the binaries with the `plugin add` command to tell Nu where to find the plugin. "To use Nu plugins, use the register command to tell Nu where to find the plugin. For example:
Then you can use `plugin use` to load the plugin into your session.
For example:
> plugin add ./nu_plugin_query > register ./nu_plugin_query" | save $'($dist)/README.txt' -f
> plugin use query [LICENSE $executable] | each {|it| cp -rv $it $dist } | flatten
# Sleep a few seconds to make sure the cp process finished successfully
For more information, refer to https://www.nushell.sh/book/plugins.html sleep 3sec
" | save $'($dist)/README.txt' -f
[LICENSE ...(glob $executable)] | each {|it| cp -rv $it $dist } | flatten
print $'(char nl)Check binary release version detail:'; hr-line print $'(char nl)Check binary release version detail:'; hr-line
let ver = if $os == 'windows-latest' { let ver = if $os == 'windows-latest' {
@ -133,23 +148,23 @@ let ver = if $os == 'windows-latest' {
(do -i { ./output/nu -c 'version' }) | str join (do -i { ./output/nu -c 'version' }) | str join
} }
if ($ver | str trim | is-empty) { if ($ver | str trim | is-empty) {
print $'(ansi r)Incompatible Nu binary: The binary cross compiled is not runnable on current arch...(ansi reset)' print $'(ansi r)Incompatible nu binary...(ansi reset)'
} else { print $ver } } else { print $ver }
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Create a release archive and send it to output for the following steps # Create a release archive and send it to output for the following steps
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
cd $dist; print $'(char nl)Creating release archive...'; hr-line cd $dist; print $'(char nl)Creating release archive...'; hr-line
if $os in ['macos-latest'] or $USE_UBUNTU { if $os in [$USE_UBUNTU, 'macos-latest'] {
let files = (ls | get name) let files = (ls | get name)
let dest = $'($bin)-($version)-($target)' let dest = if $env.RELEASE_TYPE == 'full' { $'($bin)-($version)-($FULL_NAME)' } else { $'($bin)-($version)-($target)' }
let archive = $'($dist)/($dest).tar.gz' let archive = $'($dist)/($dest).tar.gz'
mkdir $dest mkdir $dest
$files | each {|it| cp -v $it $dest } $files | each {|it| mv $it $dest } | ignore
print $'(char nl)(ansi g)Archive contents:(ansi reset)'; hr-line; ls $dest | print print $'(char nl)(ansi g)Archive contents:(ansi reset)'; hr-line; ls $dest
tar -czf $archive $dest tar -czf $archive $dest
print $'archive: ---> ($archive)'; ls $archive print $'archive: ---> ($archive)'; ls $archive
@ -158,7 +173,7 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
} else if $os == 'windows-latest' { } else if $os == 'windows-latest' {
let releaseStem = $'($bin)-($version)-($target)' let releaseStem = if $env.RELEASE_TYPE == 'full' { $'($bin)-($version)-($FULL_NAME)' } else { $'($bin)-($version)-($target)' }
print $'(char nl)Download less related stuffs...'; hr-line print $'(char nl)Download less related stuffs...'; hr-line
aria2c https://github.com/jftuga/less-Windows/releases/download/less-v608/less.exe -o less.exe aria2c https://github.com/jftuga/less-Windows/releases/download/less-v608/less.exe -o less.exe
@ -168,12 +183,11 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
if (get-env _EXTRA_) == 'msi' { if (get-env _EXTRA_) == 'msi' {
let wixRelease = $'($src)/target/wix/($releaseStem).msi' let wixRelease = $'($src)/target/wix/($releaseStem).msi'
print $'(char nl)Start creating Windows msi package with the following contents...' print $'(char nl)Start creating Windows msi package...'
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)/*' | into glob) target/release/ cp -r $'($dist)/*' target/release/
ls target/release/* | print cargo install cargo-wix --version 0.3.4
cargo install cargo-wix --version 0.3.8
cargo wix --no-build --nocapture --package nu --output $wixRelease cargo wix --no-build --nocapture --package nu --output $wixRelease
# Workaround for https://github.com/softprops/action-gh-release/issues/280 # Workaround for https://github.com/softprops/action-gh-release/issues/280
let archive = ($wixRelease | str replace --all '\' '/') let archive = ($wixRelease | str replace --all '\' '/')
@ -182,9 +196,9 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
} else { } else {
print $'(char nl)(ansi g)Archive contents:(ansi reset)'; hr-line; ls | print print $'(char nl)(ansi g)Archive contents:(ansi reset)'; hr-line; ls
let archive = $'($dist)/($releaseStem).zip' let archive = $'($dist)/($releaseStem).zip'
7z a $archive ...(glob *) 7z a $archive *
let pkg = (ls -f $archive | get name) let pkg = (ls -f $archive | get name)
if not ($pkg | is-empty) { if not ($pkg | is-empty) {
# Workaround for https://github.com/softprops/action-gh-release/issues/280 # Workaround for https://github.com/softprops/action-gh-release/issues/280
@ -195,11 +209,19 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
} }
} }
def 'cargo-build-nu' [] { def 'cargo-build-nu' [ options: string ] {
if $os == 'windows-latest' { if ($options | str trim | is-empty) {
cargo build --release --all --target $target if $os == 'windows-latest' {
cargo build --release --all --target $target
} else {
cargo build --release --all --target $target --features=static-link-openssl
}
} else { } else {
cargo build --release --all --target $target --features=static-link-openssl if $os == 'windows-latest' {
cargo build --release --all --target $target $options
} else {
cargo build --release --all --target $target --features=static-link-openssl $options
}
} }
} }

View File

@ -18,7 +18,6 @@ jobs:
name: Std name: Std
strategy: strategy:
fail-fast: false
matrix: matrix:
target: target:
- aarch64-apple-darwin - aarch64-apple-darwin
@ -34,64 +33,169 @@ jobs:
include: include:
- target: aarch64-apple-darwin - target: aarch64-apple-darwin
os: macos-latest os: macos-latest
target_rustflags: ''
- target: x86_64-apple-darwin - target: x86_64-apple-darwin
os: macos-latest os: macos-latest
target_rustflags: ''
- target: x86_64-pc-windows-msvc - target: x86_64-pc-windows-msvc
extra: 'bin' extra: 'bin'
os: windows-latest os: windows-latest
target_rustflags: ''
- target: x86_64-pc-windows-msvc - target: x86_64-pc-windows-msvc
extra: msi extra: msi
os: windows-latest os: windows-latest
target_rustflags: ''
- target: aarch64-pc-windows-msvc - target: aarch64-pc-windows-msvc
extra: 'bin' extra: 'bin'
os: windows-latest os: windows-latest
target_rustflags: ''
- target: aarch64-pc-windows-msvc - target: aarch64-pc-windows-msvc
extra: msi extra: msi
os: windows-latest os: windows-latest
target_rustflags: ''
- target: x86_64-unknown-linux-gnu - target: x86_64-unknown-linux-gnu
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: x86_64-unknown-linux-musl - target: x86_64-unknown-linux-musl
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: aarch64-unknown-linux-gnu - target: aarch64-unknown-linux-gnu
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: armv7-unknown-linux-gnueabihf - target: armv7-unknown-linux-gnueabihf
os: ubuntu-20.04 os: ubuntu-20.04
target_rustflags: ''
- target: riscv64gc-unknown-linux-gnu - target: riscv64gc-unknown-linux-gnu
os: ubuntu-latest os: ubuntu-20.04
target_rustflags: ''
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
steps: steps:
- uses: actions/checkout@v4.1.6 - uses: actions/checkout@v4
- name: Update Rust Toolchain Target - name: Update Rust Toolchain Target
run: | run: |
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
- name: Setup Rust toolchain - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0 uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135` # WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
with: with:
cache: false
rustflags: '' rustflags: ''
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.10 uses: hustcer/setup-nu@v3.8
with: with:
version: 0.93.0 version: 0.86.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release Nu Binary - name: Release Nu Binary
id: nu id: nu
run: nu .github/workflows/release-pkg.nu run: nu .github/workflows/release-pkg.nu
env: env:
RELEASE_TYPE: standard
OS: ${{ matrix.os }} OS: ${{ matrix.os }}
REF: ${{ github.ref }} REF: ${{ github.ref }}
TARGET: ${{ matrix.target }} TARGET: ${{ matrix.target }}
_EXTRA_: ${{ matrix.extra }} _EXTRA_: ${{ matrix.extra }}
TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }}
# REF: https://github.com/marketplace/actions/gh-release # REF: https://github.com/marketplace/actions/gh-release
- name: Publish Archive - name: Publish Archive
uses: softprops/action-gh-release@v2.0.5 uses: softprops/action-gh-release@v0.1.15
if: ${{ startsWith(github.ref, 'refs/tags/') }}
with:
draft: true
files: ${{ steps.nu.outputs.archive }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
full:
name: Full
strategy:
fail-fast: false
matrix:
target:
- aarch64-apple-darwin
- x86_64-apple-darwin
- x86_64-pc-windows-msvc
- aarch64-pc-windows-msvc
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
- aarch64-unknown-linux-gnu
extra: ['bin']
include:
- target: aarch64-apple-darwin
os: macos-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-apple-darwin
os: macos-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: aarch64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: aarch64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe,extra'
- target: x86_64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe,extra'
- target: x86_64-unknown-linux-musl
os: ubuntu-20.04
target_rustflags: '--features=dataframe,extra'
- target: aarch64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe,extra'
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Update Rust Toolchain Target
run: |
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
- name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.5.0
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
with:
rustflags: ''
- name: Setup Nushell
uses: hustcer/setup-nu@v3.8
with:
version: 0.86.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Release Nu Binary
id: nu
run: nu .github/workflows/release-pkg.nu
env:
RELEASE_TYPE: full
OS: ${{ matrix.os }}
REF: ${{ github.ref }}
TARGET: ${{ matrix.target }}
_EXTRA_: ${{ matrix.extra }}
TARGET_RUSTFLAGS: ${{ matrix.target_rustflags }}
# REF: https://github.com/marketplace/actions/gh-release
- name: Publish Archive
uses: softprops/action-gh-release@v0.1.15
if: ${{ startsWith(github.ref, 'refs/tags/') }} if: ${{ startsWith(github.ref, 'refs/tags/') }}
with: with:
draft: true draft: true

View File

@ -7,7 +7,9 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout Actions Repository - name: Checkout Actions Repository
uses: actions/checkout@v4.1.6 uses: actions/checkout@v4
- name: Check spelling - name: Check spelling
uses: crate-ci/typos@v1.21.0 uses: crate-ci/typos@v1.16.23
with:
config: ./.github/.typos.toml

View File

@ -10,16 +10,11 @@ Welcome to Nushell and thank you for considering contributing!
- [Useful commands](#useful-commands) - [Useful commands](#useful-commands)
- [Debugging tips](#debugging-tips) - [Debugging tips](#debugging-tips)
- [Git etiquette](#git-etiquette) - [Git etiquette](#git-etiquette)
- [Our Rust style](#our-rust-style)
- [Generally discouraged](#generally-discouraged)
- [Things we want to get better at](#things-we-want-to-get-better-at)
- [License](#license) - [License](#license)
## Other helpful resources
More resources can be found in the nascent [developer documentation](devdocs/README.md) in this repo.
- [Developer FAQ](devdocs/FAQ.md)
- [Platform support policy](devdocs/PLATFORM_SUPPORT.md)
- [Our Rust style](devdocs/rust_style.md)
## Proposing design changes ## Proposing design changes
First of all, before diving into the code, if you want to create a new feature, change something significantly, and especially if the change is user-facing, it is a good practice to first get an approval from the core team before starting to work on it. First of all, before diving into the code, if you want to create a new feature, change something significantly, and especially if the change is user-facing, it is a good practice to first get an approval from the core team before starting to work on it.
@ -55,6 +50,7 @@ It is good practice to cover your changes with a test. Also, try to think about
Tests can be found in different places: Tests can be found in different places:
* `/tests` * `/tests`
* `src/tests`
* command examples * command examples
* crate-specific tests * crate-specific tests
@ -67,64 +63,74 @@ Read cargo's documentation for more details: https://doc.rust-lang.org/cargo/ref
- Build and run Nushell: - Build and run Nushell:
```nushell ```shell
cargo run cargo run
``` ```
- Build and run with dataframe support.
```shell
cargo run --features=dataframe
```
- Run Clippy on Nushell: - Run Clippy on Nushell:
```nushell ```shell
cargo clippy --workspace -- -D warnings -D clippy::unwrap_used cargo clippy --workspace -- -D warnings -D clippy::unwrap_used
``` ```
or via the `toolkit.nu` command: or via the `toolkit.nu` command:
```nushell ```shell
use toolkit.nu clippy use toolkit.nu clippy
clippy clippy
``` ```
- Run all tests: - Run all tests:
```nushell ```shell
cargo test --workspace cargo test --workspace
``` ```
along with dataframe tests
```shell
cargo test --workspace --features=dataframe
```
or via the `toolkit.nu` command: or via the `toolkit.nu` command:
```nushell ```shell
use toolkit.nu test use toolkit.nu test
test test
``` ```
- Run all tests for a specific command - Run all tests for a specific command
```nushell ```shell
cargo test --package nu-cli --test main -- commands::<command_name_here> cargo test --package nu-cli --test main -- commands::<command_name_here>
``` ```
- Check to see if there are code formatting issues - Check to see if there are code formatting issues
```nushell ```shell
cargo fmt --all -- --check cargo fmt --all -- --check
``` ```
or via the `toolkit.nu` command: or via the `toolkit.nu` command:
```nushell ```shell
use toolkit.nu fmt use toolkit.nu fmt
fmt --check fmt --check
``` ```
- Format the code in the project - Format the code in the project
```nushell ```shell
cargo fmt --all cargo fmt --all
``` ```
or via the `toolkit.nu` command: or via the `toolkit.nu` command:
```nushell ```shell
use toolkit.nu fmt use toolkit.nu fmt
fmt fmt
``` ```
- Set up `git` hooks to check formatting and run `clippy` before committing and pushing: - Set up `git` hooks to check formatting and run `clippy` before committing and pushing:
```nushell ```shell
use toolkit.nu setup-git-hooks use toolkit.nu setup-git-hooks
setup-git-hooks setup-git-hooks
``` ```
@ -134,12 +140,12 @@ Read cargo's documentation for more details: https://doc.rust-lang.org/cargo/ref
- To view verbose logs when developing, enable the `trace` log level. - To view verbose logs when developing, enable the `trace` log level.
```nushell ```shell
cargo run --release -- --log-level trace cargo run --release -- --log-level trace
``` ```
- To redirect trace logs to a file, enable the `--log-target file` switch. - To redirect trace logs to a file, enable the `--log-target file` switch.
```nushell ```shell
cargo run --release -- --log-level trace --log-target file cargo run --release -- --log-level trace --log-target file
open $"($nu.temp-path)/nu-($nu.pid).log" open $"($nu.temp-path)/nu-($nu.pid).log"
``` ```
@ -230,6 +236,51 @@ You can help us to make the review process a smooth experience:
- Choose what simplifies having confidence in the conflict resolution and the review. **Merge commits in your branch are OK** in the squash model. - Choose what simplifies having confidence in the conflict resolution and the review. **Merge commits in your branch are OK** in the squash model.
- Feel free to notify your reviewers or affected PR authors if your change might cause larger conflicts with another change. - Feel free to notify your reviewers or affected PR authors if your change might cause larger conflicts with another change.
- During the rollup of multiple PRs, we may choose to resolve merge conflicts and CI failures ourselves. (Allow maintainers to push to your branch to enable us to do this quickly.) - During the rollup of multiple PRs, we may choose to resolve merge conflicts and CI failures ourselves. (Allow maintainers to push to your branch to enable us to do this quickly.)
## Our Rust style
To make the collaboration on a project the scale of Nushell easy, we want to work towards a style of Rust code that can easily be understood by all of our contributors. We conservatively rely on most of [`clippy`s suggestions](https://github.com/rust-lang/rust-clippy) to get to the holy grail of "idiomatic" code. Good code in our eyes is not the most clever use of all available language features or with the most unique personal touch but readable and strikes a balance between being concise, and also unsurprising and explicit in the places where it matters.
One example of this philosophy is that we generally avoid to fight the borrow-checker in our data model but rather try to get to a correct and simple solution first and then figure out where we should reuse data to achieve the necessary performance. As we are still pre-1.0 this served us well to be able to quickly refactor or change larger parts of the code base.
### Generally discouraged
#### `+nightly` language features or things only available in the most recent `+stable`
To make life for the people easier that maintain the Nushell packages in various distributions with their own release cycle of `rustc` we typically rely on slightly older Rust versions. We do not make explicit guarantees how far back in the past we live but you can find out in our [`rust-toolchain.toml`](https://github.com/nushell/nushell/blob/main/rust-toolchain.toml)
(As a rule of thumb this has been typically been approximately 2 releases behind the newest stable compiler.)
The use of nightly features is prohibited.
#### Panicking
As Nushell aims to provide a reliable foundational way for folks to interact with their computer, we cannot carelessly crash the execution of their work by panicking Nushell.
Thus panicking is not an allowed error handling strategy for anything that could be triggered by user input OR behavior of the outside system. If Nushell panics this is a bug or we are against all odds already in an unrecoverable state (The system stopped cooperating, we went out of memory). The use of `.unwrap()` is thus outright banned and any uses of `.expect()` or related panicking macros like `unreachable!` should include a helpful description which assumptions have been violated.
#### `unsafe` code
For any use of `unsafe` code we need to require even higher standards and additional review. If you add or alter `unsafe` blocks you have to be familiar with the promises you need to uphold as found in the [Rustonomicon](https://doc.rust-lang.org/nomicon/intro.html). All `unsafe` uses should include `// SAFETY:` comments explaining how the invariants are upheld and thus alerting you what to watch out for when making a change.
##### FFI with system calls and the outside world
As a shell Nushell needs to interact with system APIs in several places, for which FFI code with unsafe blocks may be necessary. In some cases this can be handled by safe API wrapper crates but in some cases we may choose to directly do those calls.
If you do so you need to document the system behavior on top of the Rust memory model guarantees that you uphold. This means documenting whether using a particular system call is safe to use in a particular context and all failure cases are properly recovered.
##### Implementing self-contained data structures
Another motivation for reaching to `unsafe` code might be to try to implement a particular data structure that is not expressible on safe `std` library APIs. Doing so in the Nushell code base would have to clear a high bar for need based on profiling results. Also you should first do a survey of the [crate ecosystem](https://crates.io) that there doesn't exist a usable well vetted crate that already provides safe APIs to the desired datastructure.
##### Make things go faster by removing checks
This is probably a bad idea if you feel tempted to do so. Don't
#### Macros
Another advanced feature people feel tempted to use to work around perceived limitations of Rusts syntax and we are not particularly fans of are custom macros.
They have clear downsides not only in terms of readability if they locally introduce a different syntax. Most tooling apart from the compiler will struggle more with them. This limits for example consistent automatic formatting or automated refactors with `rust-analyzer`.
That you can fluently read `macro_rules!` is less likely than regular code. This can lead people to introduce funky behavior when using a macro. Be it because a macro is not following proper hygiene rules or because it introduces excessive work at compile time.
So we generally discourage the addition of macros. In a lot of cases your macro may start do something that can be expressed with functions or generics in a much more reusable fashion.
The only exceptions we may allow need to demonstrate that the macro can fix something that is otherwise extremely unreadable, error-prone, or consistently worse at compile time.
### Things we want to get better at
These are things we did pretty liberally to get Nushell off the ground, that make things harder for a high quality stable product. You may run across them but shouldn't take them as an endorsed example.
#### Liberal use of third-party dependencies
The amazing variety of crates on [crates.io](https://crates.io) allowed us to quickly get Nushell into a feature rich state but it left us with a bunch of baggage to clean up.
Each dependency introduces a compile time cost and duplicated code can add to the overall binary size. Also vetting more for correct and secure implementations takes unreasonably more time as this is also a continuous process of reacting to updates or potential vulnerabilities.
Thus we only want to accept dependencies that are essential and well tested implementations of a particular requirement of Nushells codebase.
Also as a project for the move to 1.0 we will try to unify among a set of dependencies if they possibly implement similar things in an area. We don't need three different crates with potentially perfect fit for three problems but rather one reliable crate with a maximized overlap between what it provides and what we need.
We will favor crates that are well tested and used and promise to be more stable and still frequently maintained.
#### Deeply nested code
As Nushell uses a lot of enums in its internal data representation there are a lot of `match` expressions. Combined with the need to handle a lot of edge cases and be defensive about any errors this has led to some absolutely hard to read deeply nested code (e.g. in the parser but also in the implementation of several commands).
This can be observed both as a "rightward drift" where the main part of the code is found after many levels of indentations or by long function bodies with several layers of branching with seemingly repeated branching inside the higher branch level.
This can also be exacerbated by "quick" bugfixes/enhancements that may just try to add a special case to catch a previously unexpected condition. The likelihood of introducing a bug in a sea of code duplication is high.
To combat this, consider using the early-`return` pattern to reject invalid data early in one place instead of building a tree through Rust's expression constructs with a lot of duplicated paths. Unpacking data into a type that expresses that the necessary things already have been checked and using functions to properly deal with separate and common behavior can also help.
## License ## License

3393
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,8 +10,8 @@ homepage = "https://www.nushell.sh"
license = "MIT" license = "MIT"
name = "nu" name = "nu"
repository = "https://github.com/nushell/nushell" repository = "https://github.com/nushell/nushell"
rust-version = "1.77.2" rust-version = "1.60"
version = "0.94.2" version = "0.87.2"
# 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
@ -24,232 +24,99 @@ pkg-fmt = "zip"
[workspace] [workspace]
members = [ members = [
"crates/nu-cli", "crates/nu-cli",
"crates/nu-engine", "crates/nu-engine",
"crates/nu-parser", "crates/nu-parser",
"crates/nu-system", "crates/nu-system",
"crates/nu-cmd-base", "crates/nu-cmd-base",
"crates/nu-cmd-extra", "crates/nu-cmd-extra",
"crates/nu-cmd-lang", "crates/nu-cmd-lang",
"crates/nu-cmd-plugin", "crates/nu-cmd-dataframe",
"crates/nu-command", "crates/nu-command",
"crates/nu-color-config", "crates/nu-lsp",
"crates/nu-explore", "crates/nu-protocol",
"crates/nu-json", "crates/nu-plugin",
"crates/nu-lsp", "crates/nu_plugin_inc",
"crates/nu-pretty-hex", "crates/nu_plugin_gstat",
"crates/nu-protocol", "crates/nu_plugin_example",
"crates/nu-plugin", "crates/nu_plugin_query",
"crates/nu-plugin-core", "crates/nu_plugin_custom_values",
"crates/nu-plugin-engine", "crates/nu_plugin_formats",
"crates/nu-plugin-protocol", "crates/nu-std",
"crates/nu-plugin-test-support", "crates/nu-utils",
"crates/nu_plugin_inc",
"crates/nu_plugin_gstat",
"crates/nu_plugin_example",
"crates/nu_plugin_query",
"crates/nu_plugin_custom_values",
"crates/nu_plugin_formats",
"crates/nu_plugin_polars",
"crates/nu_plugin_stress_internals",
"crates/nu-std",
"crates/nu-table",
"crates/nu-term-grid",
"crates/nu-test-support",
"crates/nu-utils",
"crates/nuon",
] ]
[workspace.dependencies]
alphanumeric-sort = "1.5"
ansi-str = "0.8"
anyhow = "1.0.82"
base64 = "0.22.1"
bracoxide = "0.1.2"
brotli = "5.0"
byteorder = "1.5"
bytesize = "1.3"
calamine = "0.24.0"
chardetng = "0.1.17"
chrono = { default-features = false, version = "0.4.34" }
chrono-humanize = "0.2.3"
chrono-tz = "0.8"
crossbeam-channel = "0.5.8"
crossterm = "0.27"
csv = "1.3"
ctrlc = "3.4"
dialoguer = { default-features = false, version = "0.11" }
digest = { default-features = false, version = "0.10" }
dirs-next = "2.0"
dtparse = "2.0"
encoding_rs = "0.8"
fancy-regex = "0.13"
filesize = "0.2"
filetime = "0.2"
fs_extra = "1.3"
fuzzy-matcher = "0.3"
hamcrest2 = "0.3"
heck = "0.5.0"
human-date-parser = "0.1.1"
indexmap = "2.2"
indicatif = "0.17"
interprocess = "2.1.0"
is_executable = "1.0"
itertools = "0.12"
libc = "0.2"
libproc = "0.14"
log = "0.4"
lru = "0.12"
lscolors = { version = "0.17", default-features = false }
lsp-server = "0.7.5"
lsp-types = "0.95.0"
mach2 = "0.4"
md5 = { version = "0.10", package = "md-5" }
miette = "7.2"
mime = "0.3"
mime_guess = "2.0"
mockito = { version = "1.4", default-features = false }
native-tls = "0.2"
nix = { version = "0.28", default-features = false }
notify-debouncer-full = { version = "0.3", default-features = false }
nu-ansi-term = "0.50.0"
num-format = "0.4"
num-traits = "0.2"
omnipath = "0.1"
once_cell = "1.18"
open = "5.1"
os_pipe = { version = "1.1", features = ["io_safety"] }
pathdiff = "0.2"
percent-encoding = "2"
pretty_assertions = "1.4"
print-positions = "0.6"
procfs = "0.16.0"
pwd = "1.3"
quick-xml = "0.31.0"
quickcheck = "1.0"
quickcheck_macros = "1.0"
rand = "0.8"
ratatui = "0.26"
rayon = "1.10"
reedline = "0.32.0"
regex = "1.9.5"
rmp = "0.8"
rmp-serde = "1.3"
ropey = "1.6.1"
roxmltree = "0.19"
rstest = { version = "0.18", default-features = false }
rusqlite = "0.31"
rust-embed = "8.4.0"
same-file = "1.0"
serde = { version = "1.0", default-features = false }
serde_json = "1.0"
serde_urlencoded = "0.7.1"
serde_yaml = "0.9"
sha2 = "0.10"
strip-ansi-escapes = "0.2.0"
sysinfo = "0.30"
tabled = { version = "0.14.0", default-features = false }
tempfile = "3.10"
terminal_size = "0.3"
titlecase = "2.0"
toml = "0.8"
trash = "3.3"
umask = "2.1"
unicode-segmentation = "1.11"
unicode-width = "0.1"
ureq = { version = "2.9", default-features = false }
url = "2.2"
uu_cp = "0.0.25"
uu_mkdir = "0.0.25"
uu_mktemp = "0.0.25"
uu_mv = "0.0.25"
uu_whoami = "0.0.25"
uu_uname = "0.0.25"
uucore = "0.0.25"
uuid = "1.8.0"
v_htmlescape = "0.15.0"
wax = "0.6"
which = "6.0.0"
windows = "0.54"
winreg = "0.52"
[dependencies] [dependencies]
nu-cli = { path = "./crates/nu-cli", version = "0.94.2" } nu-cli = { path = "./crates/nu-cli", version = "0.87.2" }
nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.94.2" } nu-color-config = { path = "./crates/nu-color-config", version = "0.87.2" }
nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.94.2" } nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.87.2" }
nu-cmd-plugin = { path = "./crates/nu-cmd-plugin", version = "0.94.2", optional = true } nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.87.2" }
nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.94.2" } nu-cmd-dataframe = { path = "./crates/nu-cmd-dataframe", version = "0.87.2", features = ["dataframe"], optional = true }
nu-command = { path = "./crates/nu-command", version = "0.94.2" } nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.87.2", optional = true }
nu-engine = { path = "./crates/nu-engine", version = "0.94.2" } nu-command = { path = "./crates/nu-command", version = "0.87.2" }
nu-explore = { path = "./crates/nu-explore", version = "0.94.2" } nu-engine = { path = "./crates/nu-engine", version = "0.87.2" }
nu-lsp = { path = "./crates/nu-lsp/", version = "0.94.2" } nu-explore = { path = "./crates/nu-explore", version = "0.87.2" }
nu-parser = { path = "./crates/nu-parser", version = "0.94.2" } nu-json = { path = "./crates/nu-json", version = "0.87.2" }
nu-path = { path = "./crates/nu-path", version = "0.94.2" } nu-lsp = { path = "./crates/nu-lsp/", version = "0.87.2" }
nu-plugin-engine = { path = "./crates/nu-plugin-engine", optional = true, version = "0.94.2" } nu-parser = { path = "./crates/nu-parser", version = "0.87.2" }
nu-protocol = { path = "./crates/nu-protocol", version = "0.94.2" } nu-path = { path = "./crates/nu-path", version = "0.87.2" }
nu-std = { path = "./crates/nu-std", version = "0.94.2" } nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.87.2" }
nu-system = { path = "./crates/nu-system", version = "0.94.2" } nu-pretty-hex = { path = "./crates/nu-pretty-hex", version = "0.87.2" }
nu-utils = { path = "./crates/nu-utils", version = "0.94.2" } nu-protocol = { path = "./crates/nu-protocol", version = "0.87.2" }
nu-system = { path = "./crates/nu-system", version = "0.87.2" }
nu-table = { path = "./crates/nu-table", version = "0.87.2" }
nu-term-grid = { path = "./crates/nu-term-grid", version = "0.87.2" }
nu-std = { path = "./crates/nu-std", version = "0.87.2" }
nu-utils = { path = "./crates/nu-utils", version = "0.87.2" }
nu-ansi-term = "0.49.0"
reedline = { version = "0.26.0", features = ["bashisms", "sqlite"] }
reedline = { workspace = true, features = ["bashisms", "sqlite"] } crossterm = "0.27"
ctrlc = "3.4"
crossterm = { workspace = true } log = "0.4"
ctrlc = { workspace = true } miette = { version = "5.10", features = ["fancy-no-backtrace"] }
log = { workspace = true } mimalloc = { version = "0.1.37", default-features = false, optional = true }
miette = { workspace = true, features = ["fancy-no-backtrace", "fancy"] } serde_json = "1.0"
mimalloc = { version = "0.1.42", default-features = false, optional = true }
serde_json = { workspace = true }
simplelog = "0.12" simplelog = "0.12"
time = "0.3" time = "0.3"
[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
openssl = { version = "0.10", features = ["vendored"], optional = true } openssl = { version = "0.10", features = ["vendored"], optional = true }
signal-hook = { version = "0.3", default-features = false }
[target.'cfg(windows)'.build-dependencies] [target.'cfg(windows)'.build-dependencies]
winresource = "0.1" winresource = "0.1"
[target.'cfg(target_family = "unix")'.dependencies] [target.'cfg(target_family = "unix")'.dependencies]
nix = { workspace = true, default-features = false, features = [ nix = { version = "0.27", default-features = false, features = [
"signal", "signal",
"process", "process",
"fs", "fs",
"term", "term",
] } ] }
[dev-dependencies] [dev-dependencies]
nu-test-support = { path = "./crates/nu-test-support", version = "0.94.2" } nu-test-support = { path = "./crates/nu-test-support", version = "0.87.2" }
nu-plugin-protocol = { path = "./crates/nu-plugin-protocol", version = "0.94.2" }
nu-plugin-core = { path = "./crates/nu-plugin-core", version = "0.94.2" }
assert_cmd = "2.0" assert_cmd = "2.0"
dirs-next = { workspace = true } criterion = "0.5"
tango-bench = "0.5" pretty_assertions = "1.4"
pretty_assertions = { workspace = true } rstest = { version = "0.18", default-features = false }
rstest = { workspace = true, default-features = false } serial_test = "2.0"
serial_test = "3.1" tempfile = "3.8"
tempfile = { workspace = true }
[features] [features]
plugin = [ plugin = [
"nu-plugin-engine", "nu-plugin",
"nu-cmd-plugin", "nu-cli/plugin",
"nu-cli/plugin", "nu-parser/plugin",
"nu-parser/plugin", "nu-command/plugin",
"nu-command/plugin", "nu-protocol/plugin",
"nu-protocol/plugin", "nu-engine/plugin",
"nu-engine/plugin",
]
default = ["default-no-clipboard", "system-clipboard"]
# Enables convenient omitting of the system-clipboard feature, as it leads to problems in ci on linux
# See https://github.com/nushell/nushell/pull/11535
default-no-clipboard = [
"plugin",
"which-support",
"trash-support",
"sqlite",
"mimalloc",
] ]
default = ["plugin", "which-support", "trash-support", "sqlite", "mimalloc"]
stable = ["default"] stable = ["default"]
wasi = ["nu-cmd-lang/wasi"]
# NOTE: individual features are also passed to `nu-cmd-lang` that uses them to generate the feature matrix in the `version` command # NOTE: individual features are also passed to `nu-cmd-lang` that uses them to generate the feature matrix in the `version` command
# Enable to statically link OpenSSL (perl is required, to build OpenSSL https://docs.rs/openssl/latest/openssl/); # Enable to statically link OpenSSL (perl is required, to build OpenSSL https://docs.rs/openssl/latest/openssl/);
@ -257,16 +124,17 @@ stable = ["default"]
static-link-openssl = ["dep:openssl", "nu-cmd-lang/static-link-openssl"] static-link-openssl = ["dep:openssl", "nu-cmd-lang/static-link-openssl"]
mimalloc = ["nu-cmd-lang/mimalloc", "dep:mimalloc"] mimalloc = ["nu-cmd-lang/mimalloc", "dep:mimalloc"]
system-clipboard = [
"reedline/system_clipboard",
"nu-cli/system-clipboard",
"nu-cmd-lang/system-clipboard",
]
# Stable (Default) # Stable (Default)
which-support = ["nu-command/which-support", "nu-cmd-lang/which-support"] which-support = ["nu-command/which-support", "nu-cmd-lang/which-support"]
trash-support = ["nu-command/trash-support", "nu-cmd-lang/trash-support"] trash-support = ["nu-command/trash-support", "nu-cmd-lang/trash-support"]
# Extra feature for nushell
extra = ["dep:nu-cmd-extra", "nu-cmd-lang/extra"]
# Dataframe feature for nushell
dataframe = ["dep:nu-cmd-dataframe", "nu-cmd-lang/dataframe"]
# SQLite commands for nushell # SQLite commands for nushell
sqlite = ["nu-command/sqlite", "nu-cmd-lang/sqlite"] sqlite = ["nu-command/sqlite", "nu-cmd-lang/sqlite"]
@ -298,11 +166,13 @@ bench = false
# To use a development version of a dependency please use a global override here # To use a development version of a dependency please use a global override here
# changing versions in each sub-crate of the workspace is tedious # changing versions in each sub-crate of the workspace is tedious
[patch.crates-io] [patch.crates-io]
# reedline = { git = "https://github.com/nushell/reedline", branch = "main" } # reedline = { git = "https://github.com/nushell/reedline.git", branch = "main" }
# nu-ansi-term = {git = "https://github.com/nushell/nu-ansi-term.git", branch = "main"} # nu-ansi-term = {git = "https://github.com/nushell/nu-ansi-term.git", branch = "main"}
# uu_cp = { git = "https://github.com/uutils/coreutils.git", branch = "main" }
# Criterion benchmarking setup
# Run all benchmarks with `cargo bench` # Run all benchmarks with `cargo bench`
# Run individual benchmarks like `cargo bench -- <regex>` e.g. `cargo bench -- parse` # Run individual benchmarks like `cargo bench -- <regex>` e.g. `cargo bench -- parse`
[[bench]] [[bench]]
name = "benchmarks" name = "benchmarks"
harness = false harness = false

View File

@ -33,8 +33,13 @@ We will try to provide builds for all of them but a standard configuration for x
We have features of Nushell behind flags that can be passed at compilation time. We have features of Nushell behind flags that can be passed at compilation time.
The design focus of Nushell is primarily expressed by everything accessible without passing additional feature flag. The design focus of Nushell is primarily expressed by everything accessible without passing additional feature flag. This provides a standard command set and receives the most attention.
This provides a standard command set and receives the most attention. Two other feature flags are actively tested but are not guaranteed to express the stable design direction of Nushell:
- `extra`
- This includes commands where we are not convinced that they are ready to be stabilized for 1.0 or popular enough
- `dataframe`
- This includes dataframe support via `polars` and `arrow2`. Introduces a significant additional compilation and binary size.
- Due to the use of SIMD extensions may not be compatible with every minimal architecture.
## Passively supported platforms ## Passively supported platforms

View File

@ -54,7 +54,7 @@ Detailed installation instructions can be found in the [installation chapter of
[![Packaging status](https://repology.org/badge/vertical-allrepos/nushell.svg)](https://repology.org/project/nushell/versions) [![Packaging status](https://repology.org/badge/vertical-allrepos/nushell.svg)](https://repology.org/project/nushell/versions)
For details about which platforms the Nushell team actively supports, see [our platform support policy](devdocs/PLATFORM_SUPPORT.md). For details about which platforms the Nushell team actively supports, see [our platform support policy](PLATFORM_SUPPORT.md).
## Configuration ## Configuration
@ -199,7 +199,7 @@ topics that have been presented.
Nu adheres closely to a set of goals that make up its design philosophy. As features are added, they are checked against these goals. Nu adheres closely to a set of goals that make up its design philosophy. As features are added, they are checked against these goals.
- First and foremost, Nu is cross-platform. Commands and techniques should work across platforms and Nu has [first-class support for Windows, macOS, and Linux](devdocs/PLATFORM_SUPPORT.md). - First and foremost, Nu is cross-platform. Commands and techniques should work across platforms and Nu has [first-class support for Windows, macOS, and Linux](PLATFORM_SUPPORT.md).
- Nu ensures compatibility with existing platform-specific executables. - Nu ensures compatibility with existing platform-specific executables.
@ -228,7 +228,7 @@ Please submit an issue or PR to be added to this list.
See [Contributing](CONTRIBUTING.md) for details. Thanks to all the people who already contributed! See [Contributing](CONTRIBUTING.md) for details. Thanks to all the people who already contributed!
<a href="https://github.com/nushell/nushell/graphs/contributors"> <a href="https://github.com/nushell/nushell/graphs/contributors">
<img src="https://contributors-img.web.app/image?repo=nushell/nushell&max=750" /> <img src="https://contributors-img.web.app/image?repo=nushell/nushell&max=600" />
</a> </a>
## License ## License

View File

@ -1,6 +1,6 @@
# Divan benchmarks # Criterion benchmarks
These are benchmarks using [Divan](https://github.com/nvzqz/divan), a microbenchmarking tool for Rust. These are benchmarks using [Criterion](https://github.com/bheisler/criterion.rs), a microbenchmarking tool for Rust.
Run all benchmarks with `cargo bench` Run all benchmarks with `cargo bench`

View File

@ -1,58 +1,115 @@
use nu_cli::{eval_source, evaluate_commands}; use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use nu_plugin_core::{Encoder, EncodingType}; use nu_cli::eval_source;
use nu_plugin_protocol::{PluginCallResponse, PluginOutput}; use nu_parser::parse;
use nu_plugin::{EncodingType, PluginResponse};
use nu_protocol::{ use nu_protocol::{engine::EngineState, PipelineData, Span, Value};
engine::{EngineState, Stack},
PipelineData, Span, Spanned, Value,
};
use nu_std::load_standard_library;
use nu_utils::{get_default_config, get_default_env}; use nu_utils::{get_default_config, get_default_env};
use std::rc::Rc;
use std::hint::black_box;
use tango_bench::{benchmark_fn, tango_benchmarks, tango_main, IntoBenchmarks};
fn load_bench_commands() -> EngineState { fn load_bench_commands() -> EngineState {
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context()) nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
} }
// FIXME: All benchmarks live in this 1 file to speed up build times when benchmarking.
// When the *_benchmarks functions were in different files, `cargo bench` would build
// an executable for every single one - incredibly slowly. Would be nice to figure out
// a way to split things up again.
fn setup_engine() -> EngineState { fn parser_benchmarks(c: &mut Criterion) {
let mut engine_state = load_bench_commands(); let mut engine_state = load_bench_commands();
let cwd = std::env::current_dir() // parsing config.nu breaks without PWD set
.unwrap() engine_state.add_env_var(
.into_os_string() "PWD".into(),
.into_string() Value::string("/some/dir".to_string(), Span::test_data()),
.unwrap(); );
// parsing config.nu breaks without PWD set, so set a valid path let default_env = get_default_env().as_bytes();
engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data())); c.bench_function("parse_default_env_file", |b| {
b.iter_batched(
|| nu_protocol::engine::StateWorkingSet::new(&engine_state),
|mut working_set| parse(&mut working_set, None, default_env, false),
BatchSize::SmallInput,
)
});
engine_state.generate_nu_constant(); let default_config = get_default_config().as_bytes();
c.bench_function("parse_default_config_file", |b| {
b.iter_batched(
|| nu_protocol::engine::StateWorkingSet::new(&engine_state),
|mut working_set| parse(&mut working_set, None, default_config, false),
BatchSize::SmallInput,
)
});
engine_state c.bench_function("eval default_env.nu", |b| {
b.iter(|| {
let mut engine_state = load_bench_commands();
let mut stack = nu_protocol::engine::Stack::new();
eval_source(
&mut engine_state,
&mut stack,
get_default_env().as_bytes(),
"default_env.nu",
PipelineData::empty(),
false,
)
})
});
c.bench_function("eval default_config.nu", |b| {
b.iter(|| {
let mut engine_state = load_bench_commands();
// parsing config.nu breaks without PWD set
engine_state.add_env_var(
"PWD".into(),
Value::string("/some/dir".to_string(), Span::test_data()),
);
let mut stack = nu_protocol::engine::Stack::new();
eval_source(
&mut engine_state,
&mut stack,
get_default_config().as_bytes(),
"default_config.nu",
PipelineData::empty(),
false,
)
})
});
} }
fn setup_stack_and_engine_from_command(command: &str) -> (Stack, EngineState) { fn eval_benchmarks(c: &mut Criterion) {
let mut engine = setup_engine(); c.bench_function("eval default_env.nu", |b| {
let commands = Spanned { b.iter(|| {
span: Span::unknown(), let mut engine_state = load_bench_commands();
item: command.to_string(), let mut stack = nu_protocol::engine::Stack::new();
}; eval_source(
&mut engine_state,
&mut stack,
get_default_env().as_bytes(),
"default_env.nu",
PipelineData::empty(),
false,
)
})
});
let mut stack = Stack::new(); c.bench_function("eval default_config.nu", |b| {
evaluate_commands( b.iter(|| {
&commands, let mut engine_state = load_bench_commands();
&mut engine, // parsing config.nu breaks without PWD set
&mut stack, engine_state.add_env_var(
PipelineData::empty(), "PWD".into(),
None, Value::string("/some/dir".to_string(), Span::test_data()),
false, );
) let mut stack = nu_protocol::engine::Stack::new();
.unwrap(); eval_source(
&mut engine_state,
(stack, engine) &mut stack,
get_default_config().as_bytes(),
"default_config.nu",
PipelineData::empty(),
false,
)
})
});
} }
// generate a new table data with `row_cnt` rows, `col_cnt` columns. // generate a new table data with `row_cnt` rows, `col_cnt` columns.
@ -66,424 +123,50 @@ fn encoding_test_data(row_cnt: usize, col_cnt: usize) -> Value {
Value::list(vec![record; row_cnt], Span::test_data()) Value::list(vec![record; row_cnt], Span::test_data())
} }
fn bench_command( fn encoding_benchmarks(c: &mut Criterion) {
name: &str, let mut group = c.benchmark_group("Encoding");
command: &str, let test_cnt_pairs = [(100, 5), (100, 15), (10000, 5), (10000, 15)];
stack: Stack, for (row_cnt, col_cnt) in test_cnt_pairs.into_iter() {
engine: EngineState, for fmt in ["json", "msgpack"] {
) -> impl IntoBenchmarks { group.bench_function(&format!("{fmt} encode {row_cnt} * {col_cnt}"), |b| {
let commands = Spanned { let mut res = vec![];
span: Span::unknown(), let test_data =
item: command.to_string(), PluginResponse::Value(Box::new(encoding_test_data(row_cnt, col_cnt)));
}; let encoder = EncodingType::try_from_bytes(fmt.as_bytes()).unwrap();
[benchmark_fn(name, move |b| { b.iter(|| encoder.encode_response(&test_data, &mut res))
let commands = commands.clone(); });
let stack = stack.clone();
let engine = engine.clone();
b.iter(move || {
let mut stack = stack.clone();
let mut engine = engine.clone();
#[allow(clippy::unit_arg)]
black_box(
evaluate_commands(
&commands,
&mut engine,
&mut stack,
PipelineData::empty(),
None,
false,
)
.unwrap(),
);
})
})]
}
fn bench_eval_source(
name: &str,
fname: String,
source: Vec<u8>,
stack: Stack,
engine: EngineState,
) -> impl IntoBenchmarks {
[benchmark_fn(name, move |b| {
let stack = stack.clone();
let engine = engine.clone();
let fname = fname.clone();
let source = source.clone();
b.iter(move || {
let mut stack = stack.clone();
let mut engine = engine.clone();
let fname: &str = &fname.clone();
let source: &[u8] = &source.clone();
black_box(eval_source(
&mut engine,
&mut stack,
source,
fname,
PipelineData::empty(),
false,
));
})
})]
}
/// Load the standard library into the engine.
fn bench_load_standard_lib() -> impl IntoBenchmarks {
[benchmark_fn("load_standard_lib", move |b| {
let engine = setup_engine();
b.iter(move || {
let mut engine = engine.clone();
load_standard_library(&mut engine)
})
})]
}
fn create_flat_record_string(n: i32) -> String {
let mut s = String::from("let record = {");
for i in 0..n {
s.push_str(&format!("col_{}: {}", i, i));
if i < n - 1 {
s.push_str(", ");
} }
} }
s.push('}'); group.finish();
s
} }
fn create_nested_record_string(depth: i32) -> String { fn decoding_benchmarks(c: &mut Criterion) {
let mut s = String::from("let record = {"); let mut group = c.benchmark_group("Decoding");
for _ in 0..depth { let test_cnt_pairs = [(100, 5), (100, 15), (10000, 5), (10000, 15)];
s.push_str("col: {"); for (row_cnt, col_cnt) in test_cnt_pairs.into_iter() {
} for fmt in ["json", "msgpack"] {
s.push_str("col_final: 0"); group.bench_function(&format!("{fmt} decode for {row_cnt} * {col_cnt}"), |b| {
for _ in 0..depth { let mut res = vec![];
s.push('}'); let test_data =
} PluginResponse::Value(Box::new(encoding_test_data(row_cnt, col_cnt)));
s.push('}'); let encoder = EncodingType::try_from_bytes(fmt.as_bytes()).unwrap();
s encoder.encode_response(&test_data, &mut res).unwrap();
} let mut binary_data = std::io::Cursor::new(res);
b.iter(|| {
fn create_example_table_nrows(n: i32) -> String { binary_data.set_position(0);
let mut s = String::from("let table = [[foo bar baz]; "); encoder.decode_response(&mut binary_data)
for i in 0..n { })
s.push_str(&format!("[0, 1, {i}]")); });
if i < n - 1 {
s.push_str(", ");
} }
} }
s.push(']'); group.finish();
s
} }
fn bench_record_create(n: i32) -> impl IntoBenchmarks { criterion_group!(
bench_command( benches,
&format!("record_create_{n}"), parser_benchmarks,
&create_flat_record_string(n), eval_benchmarks,
Stack::new(), encoding_benchmarks,
setup_engine(), decoding_benchmarks
)
}
fn bench_record_flat_access(n: i32) -> impl IntoBenchmarks {
let setup_command = create_flat_record_string(n);
let (stack, engine) = setup_stack_and_engine_from_command(&setup_command);
bench_command(
&format!("record_flat_access_{n}"),
"$record.col_0 | ignore",
stack,
engine,
)
}
fn bench_record_nested_access(n: i32) -> impl IntoBenchmarks {
let setup_command = create_nested_record_string(n);
let (stack, engine) = setup_stack_and_engine_from_command(&setup_command);
let nested_access = ".col".repeat(n as usize);
bench_command(
&format!("record_nested_access_{n}"),
&format!("$record{} | ignore", nested_access),
stack,
engine,
)
}
fn bench_table_create(n: i32) -> impl IntoBenchmarks {
bench_command(
&format!("table_create_{n}"),
&create_example_table_nrows(n),
Stack::new(),
setup_engine(),
)
}
fn bench_table_get(n: i32) -> impl IntoBenchmarks {
let setup_command = create_example_table_nrows(n);
let (stack, engine) = setup_stack_and_engine_from_command(&setup_command);
bench_command(
&format!("table_get_{n}"),
"$table | get bar | math sum | ignore",
stack,
engine,
)
}
fn bench_table_select(n: i32) -> impl IntoBenchmarks {
let setup_command = create_example_table_nrows(n);
let (stack, engine) = setup_stack_and_engine_from_command(&setup_command);
bench_command(
&format!("table_select_{n}"),
"$table | select foo baz | ignore",
stack,
engine,
)
}
fn bench_eval_interleave(n: i32) -> impl IntoBenchmarks {
let engine = setup_engine();
let stack = Stack::new();
bench_command(
&format!("eval_interleave_{n}"),
&format!("seq 1 {n} | wrap a | interleave {{ seq 1 {n} | wrap b }} | ignore"),
stack,
engine,
)
}
fn bench_eval_interleave_with_ctrlc(n: i32) -> impl IntoBenchmarks {
let mut engine = setup_engine();
engine.ctrlc = Some(std::sync::Arc::new(std::sync::atomic::AtomicBool::new(
false,
)));
let stack = Stack::new();
bench_command(
&format!("eval_interleave_with_ctrlc_{n}"),
&format!("seq 1 {n} | wrap a | interleave {{ seq 1 {n} | wrap b }} | ignore"),
stack,
engine,
)
}
fn bench_eval_for(n: i32) -> impl IntoBenchmarks {
let engine = setup_engine();
let stack = Stack::new();
bench_command(
&format!("eval_for_{n}"),
&format!("(for $x in (1..{n}) {{ 1 }}) | ignore"),
stack,
engine,
)
}
fn bench_eval_each(n: i32) -> impl IntoBenchmarks {
let engine = setup_engine();
let stack = Stack::new();
bench_command(
&format!("eval_each_{n}"),
&format!("(1..{n}) | each {{|_| 1 }} | ignore"),
stack,
engine,
)
}
fn bench_eval_par_each(n: i32) -> impl IntoBenchmarks {
let engine = setup_engine();
let stack = Stack::new();
bench_command(
&format!("eval_par_each_{n}"),
&format!("(1..{}) | par-each -t 2 {{|_| 1 }} | ignore", n),
stack,
engine,
)
}
fn bench_eval_default_config() -> impl IntoBenchmarks {
let default_env = get_default_config().as_bytes().to_vec();
let fname = "default_config.nu".to_string();
bench_eval_source(
"eval_default_config",
fname,
default_env,
Stack::new(),
setup_engine(),
)
}
fn bench_eval_default_env() -> impl IntoBenchmarks {
let default_env = get_default_env().as_bytes().to_vec();
let fname = "default_env.nu".to_string();
bench_eval_source(
"eval_default_env",
fname,
default_env,
Stack::new(),
setup_engine(),
)
}
fn encode_json(row_cnt: usize, col_cnt: usize) -> impl IntoBenchmarks {
let test_data = Rc::new(PluginOutput::CallResponse(
0,
PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
));
let encoder = Rc::new(EncodingType::try_from_bytes(b"json").unwrap());
[benchmark_fn(
format!("encode_json_{}_{}", row_cnt, col_cnt),
move |b| {
let encoder = encoder.clone();
let test_data = test_data.clone();
b.iter(move || {
let mut res = Vec::new();
encoder.encode(&*test_data, &mut res).unwrap();
})
},
)]
}
fn encode_msgpack(row_cnt: usize, col_cnt: usize) -> impl IntoBenchmarks {
let test_data = Rc::new(PluginOutput::CallResponse(
0,
PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
));
let encoder = Rc::new(EncodingType::try_from_bytes(b"msgpack").unwrap());
[benchmark_fn(
format!("encode_msgpack_{}_{}", row_cnt, col_cnt),
move |b| {
let encoder = encoder.clone();
let test_data = test_data.clone();
b.iter(move || {
let mut res = Vec::new();
encoder.encode(&*test_data, &mut res).unwrap();
})
},
)]
}
fn decode_json(row_cnt: usize, col_cnt: usize) -> impl IntoBenchmarks {
let test_data = PluginOutput::CallResponse(
0,
PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
);
let encoder = EncodingType::try_from_bytes(b"json").unwrap();
let mut res = vec![];
encoder.encode(&test_data, &mut res).unwrap();
[benchmark_fn(
format!("decode_json_{}_{}", row_cnt, col_cnt),
move |b| {
let res = res.clone();
b.iter(move || {
let mut binary_data = std::io::Cursor::new(res.clone());
binary_data.set_position(0);
let _: Result<Option<PluginOutput>, _> =
black_box(encoder.decode(&mut binary_data));
})
},
)]
}
fn decode_msgpack(row_cnt: usize, col_cnt: usize) -> impl IntoBenchmarks {
let test_data = PluginOutput::CallResponse(
0,
PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
);
let encoder = EncodingType::try_from_bytes(b"msgpack").unwrap();
let mut res = vec![];
encoder.encode(&test_data, &mut res).unwrap();
[benchmark_fn(
format!("decode_msgpack_{}_{}", row_cnt, col_cnt),
move |b| {
let res = res.clone();
b.iter(move || {
let mut binary_data = std::io::Cursor::new(res.clone());
binary_data.set_position(0);
let _: Result<Option<PluginOutput>, _> =
black_box(encoder.decode(&mut binary_data));
})
},
)]
}
tango_benchmarks!(
bench_load_standard_lib(),
// Data types
// Record
bench_record_create(1),
bench_record_create(10),
bench_record_create(100),
bench_record_create(1_000),
bench_record_flat_access(1),
bench_record_flat_access(10),
bench_record_flat_access(100),
bench_record_flat_access(1_000),
bench_record_nested_access(1),
bench_record_nested_access(2),
bench_record_nested_access(4),
bench_record_nested_access(8),
bench_record_nested_access(16),
bench_record_nested_access(32),
bench_record_nested_access(64),
bench_record_nested_access(128),
// Table
bench_table_create(1),
bench_table_create(10),
bench_table_create(100),
bench_table_create(1_000),
bench_table_get(1),
bench_table_get(10),
bench_table_get(100),
bench_table_get(1_000),
bench_table_select(1),
bench_table_select(10),
bench_table_select(100),
bench_table_select(1_000),
// Eval
// Interleave
bench_eval_interleave(100),
bench_eval_interleave(1_000),
bench_eval_interleave(10_000),
bench_eval_interleave_with_ctrlc(100),
bench_eval_interleave_with_ctrlc(1_000),
bench_eval_interleave_with_ctrlc(10_000),
// For
bench_eval_for(1),
bench_eval_for(10),
bench_eval_for(100),
bench_eval_for(1_000),
bench_eval_for(10_000),
// Each
bench_eval_each(1),
bench_eval_each(10),
bench_eval_each(100),
bench_eval_each(1_000),
bench_eval_each(10_000),
// Par-Each
bench_eval_par_each(1),
bench_eval_par_each(10),
bench_eval_par_each(100),
bench_eval_par_each(1_000),
bench_eval_par_each(10_000),
// Config
bench_eval_default_config(),
// Env
bench_eval_default_env(),
// Encode
// Json
encode_json(100, 5),
encode_json(10000, 15),
// MsgPack
encode_msgpack(100, 5),
encode_msgpack(10000, 15),
// Decode
// Json
decode_json(100, 5),
decode_json(10000, 15),
// MsgPack
decode_msgpack(100, 5),
decode_msgpack(10000, 15)
); );
criterion_main!(benches);
tango_main!();

View File

@ -5,45 +5,41 @@ 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.94.2" version = "0.87.2"
[lib] [lib]
bench = false bench = false
[dev-dependencies] [dev-dependencies]
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.94.2" } nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.87.2" }
nu-command = { path = "../nu-command", version = "0.94.2" } nu-command = { path = "../nu-command", version = "0.87.2" }
nu-test-support = { path = "../nu-test-support", version = "0.94.2" } nu-test-support = { path = "../nu-test-support", version = "0.87.2" }
rstest = { workspace = true, default-features = false } rstest = { version = "0.18.1", default-features = false }
tempfile = { workspace = true }
[dependencies] [dependencies]
nu-cmd-base = { path = "../nu-cmd-base", version = "0.94.2" } nu-cmd-base = { path = "../nu-cmd-base", version = "0.87.2" }
nu-engine = { path = "../nu-engine", version = "0.94.2" } nu-engine = { path = "../nu-engine", version = "0.87.2" }
nu-path = { path = "../nu-path", version = "0.94.2" } nu-path = { path = "../nu-path", version = "0.87.2" }
nu-parser = { path = "../nu-parser", version = "0.94.2" } nu-parser = { path = "../nu-parser", version = "0.87.2" }
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.94.2", optional = true } nu-protocol = { path = "../nu-protocol", version = "0.87.2" }
nu-protocol = { path = "../nu-protocol", version = "0.94.2" } nu-utils = { path = "../nu-utils", version = "0.87.2" }
nu-utils = { path = "../nu-utils", version = "0.94.2" } nu-color-config = { path = "../nu-color-config", version = "0.87.2" }
nu-color-config = { path = "../nu-color-config", version = "0.94.2" } nu-ansi-term = "0.49.0"
nu-ansi-term = { workspace = true } reedline = { version = "0.26.0", features = ["bashisms", "sqlite"] }
reedline = { workspace = true, features = ["bashisms", "sqlite"] }
chrono = { default-features = false, features = ["std"], workspace = true } chrono = { default-features = false, features = ["std"], version = "0.4" }
crossterm = { workspace = true } crossterm = "0.27"
fancy-regex = { workspace = true } fancy-regex = "0.11"
fuzzy-matcher = { workspace = true } fuzzy-matcher = "0.3"
is_executable = { workspace = true } is_executable = "1.0"
log = { workspace = true } log = "0.4"
miette = { workspace = true, features = ["fancy-no-backtrace"] } miette = { version = "5.10", features = ["fancy-no-backtrace"] }
lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] } once_cell = "1.18"
once_cell = { workspace = true } percent-encoding = "2"
percent-encoding = { workspace = true } pathdiff = "0.2"
sysinfo = { workspace = true } sysinfo = "0.29"
unicode-segmentation = { workspace = true } unicode-segmentation = "1.10"
uuid = { workspace = true, features = ["v4"] } uuid = { version = "1.6.0", features = ["v4"] }
which = { workspace = true }
[features] [features]
plugin = ["nu-plugin-engine"] plugin = []
system-clipboard = ["reedline/system_clipboard"]

View File

@ -0,0 +1,129 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone)]
pub struct Commandline;
impl Command for Commandline {
fn name(&self) -> &str {
"commandline"
}
fn signature(&self) -> Signature {
Signature::build("commandline")
.input_output_types(vec![
(Type::Nothing, Type::Nothing),
(Type::String, Type::String),
])
.switch(
"cursor",
"Set or get the current cursor position",
Some('c'),
)
.switch(
"cursor-end",
"Set the current cursor position to the end of the buffer",
Some('e'),
)
.switch(
"append",
"appends the string to the end of the buffer",
Some('a'),
)
.switch(
"insert",
"inserts the string into the buffer at the cursor position",
Some('i'),
)
.switch(
"replace",
"replaces the current contents of the buffer (default)",
Some('r'),
)
.optional(
"cmd",
SyntaxShape::String,
"the string to perform the operation with",
)
.category(Category::Core)
}
fn usage(&self) -> &str {
"View or modify the current command line input buffer."
}
fn search_terms(&self) -> Vec<&str> {
vec!["repl", "interactive"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
if let Some(cmd) = call.opt::<Value>(engine_state, stack, 0)? {
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
if call.has_flag("cursor") {
let cmd_str = cmd.as_string()?;
match cmd_str.parse::<i64>() {
Ok(n) => {
repl.cursor_pos = if n <= 0 {
0usize
} else {
repl.buffer
.grapheme_indices(true)
.map(|(i, _c)| i)
.nth(n as usize)
.unwrap_or(repl.buffer.len())
}
}
Err(_) => {
return Err(ShellError::CantConvert {
to_type: "int".to_string(),
from_type: "string".to_string(),
span: cmd.span(),
help: Some(format!(
r#"string "{cmd_str}" does not represent a valid int"#
)),
})
}
}
} else if call.has_flag("append") {
repl.buffer.push_str(&cmd.as_string()?);
} else if call.has_flag("insert") {
let cmd_str = cmd.as_string()?;
let cursor_pos = repl.cursor_pos;
repl.buffer.insert_str(cursor_pos, &cmd_str);
repl.cursor_pos += cmd_str.len();
} else {
repl.buffer = cmd.as_string()?;
repl.cursor_pos = repl.buffer.len();
}
Ok(Value::nothing(call.head).into_pipeline_data())
} else {
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
if call.has_flag("cursor-end") {
repl.cursor_pos = repl.buffer.graphemes(true).count();
Ok(Value::nothing(call.head).into_pipeline_data())
} else if call.has_flag("cursor") {
let char_pos = repl
.buffer
.grapheme_indices(true)
.chain(std::iter::once((repl.buffer.len(), "")))
.position(|(i, _c)| i == repl.cursor_pos)
.expect("Cursor position isn't on a grapheme boundary");
Ok(Value::string(char_pos.to_string(), call.head).into_pipeline_data())
} else {
Ok(Value::string(repl.buffer.to_string(), call.head).into_pipeline_data())
}
}
}
}

View File

@ -1,35 +0,0 @@
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Commandline;
impl Command for Commandline {
fn name(&self) -> &str {
"commandline"
}
fn signature(&self) -> Signature {
Signature::build("commandline")
.input_output_types(vec![(Type::Nothing, Type::String)])
.category(Category::Core)
}
fn usage(&self) -> &str {
"View the current command line input buffer."
}
fn search_terms(&self) -> Vec<&str> {
vec!["repl", "interactive"]
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let repl = engine_state.repl_state.lock().expect("repl state mutex");
Ok(Value::string(repl.buffer.clone(), call.head).into_pipeline_data())
}
}

View File

@ -1,66 +0,0 @@
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"commandline edit"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.switch(
"append",
"appends the string to the end of the buffer",
Some('a'),
)
.switch(
"insert",
"inserts the string into the buffer at the cursor position",
Some('i'),
)
.switch(
"replace",
"replaces the current contents of the buffer (default)",
Some('r'),
)
.required(
"str",
SyntaxShape::String,
"the string to perform the operation with",
)
.category(Category::Core)
}
fn usage(&self) -> &str {
"Modify the current command line input buffer."
}
fn search_terms(&self) -> Vec<&str> {
vec!["repl", "interactive"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let str: String = call.req(engine_state, stack, 0)?;
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
if call.has_flag(engine_state, stack, "append")? {
repl.buffer.push_str(&str);
} else if call.has_flag(engine_state, stack, "insert")? {
let cursor_pos = repl.cursor_pos;
repl.buffer.insert_str(cursor_pos, &str);
repl.cursor_pos += str.len();
} else {
repl.buffer = str;
repl.cursor_pos = repl.buffer.len();
}
Ok(Value::nothing(call.head).into_pipeline_data())
}
}

View File

@ -1,52 +0,0 @@
use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"commandline get-cursor"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_types(vec![(Type::Nothing, Type::Int)])
.allow_variants_without_examples(true)
.category(Category::Core)
}
fn usage(&self) -> &str {
"Get the current cursor position."
}
fn search_terms(&self) -> Vec<&str> {
vec!["repl", "interactive"]
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let repl = engine_state.repl_state.lock().expect("repl state mutex");
let char_pos = repl
.buffer
.grapheme_indices(true)
.chain(std::iter::once((repl.buffer.len(), "")))
.position(|(i, _c)| i == repl.cursor_pos)
.expect("Cursor position isn't on a grapheme boundary");
match i64::try_from(char_pos) {
Ok(pos) => Ok(Value::int(pos, call.head).into_pipeline_data()),
Err(e) => Err(ShellError::GenericError {
error: "Failed to convert cursor position to int".to_string(),
msg: e.to_string(),
span: None,
help: None,
inner: vec![],
}),
}
}
}

View File

@ -1,9 +0,0 @@
mod commandline_;
mod edit;
mod get_cursor;
mod set_cursor;
pub use commandline_::Commandline;
pub use edit::SubCommand as CommandlineEdit;
pub use get_cursor::SubCommand as CommandlineGetCursor;
pub use set_cursor::SubCommand as CommandlineSetCursor;

View File

@ -1,65 +0,0 @@
use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"commandline set-cursor"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.switch(
"end",
"set the current cursor position to the end of the buffer",
Some('e'),
)
.optional("pos", SyntaxShape::Int, "Cursor position to be set")
.category(Category::Core)
}
fn usage(&self) -> &str {
"Set the current cursor position."
}
fn search_terms(&self) -> Vec<&str> {
vec!["repl", "interactive"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
if let Some(pos) = call.opt::<i64>(engine_state, stack, 0)? {
repl.cursor_pos = if pos <= 0 {
0usize
} else {
repl.buffer
.grapheme_indices(true)
.map(|(i, _c)| i)
.nth(pos as usize)
.unwrap_or(repl.buffer.len())
};
Ok(Value::nothing(call.head).into_pipeline_data())
} else if call.has_flag(engine_state, stack, "end")? {
repl.cursor_pos = repl.buffer.len();
Ok(Value::nothing(call.head).into_pipeline_data())
} else {
Err(ShellError::GenericError {
error: "Required a positional argument or a flag".to_string(),
msg: "".to_string(),
span: None,
help: None,
inner: vec![],
})
}
}
}

View File

@ -1,6 +1,7 @@
use crate::commands::*;
use nu_protocol::engine::{EngineState, StateWorkingSet}; use nu_protocol::engine::{EngineState, StateWorkingSet};
use crate::commands::*;
pub fn add_cli_context(mut engine_state: EngineState) -> EngineState { pub fn add_cli_context(mut engine_state: EngineState) -> EngineState {
let delta = { let delta = {
let mut working_set = StateWorkingSet::new(&engine_state); let mut working_set = StateWorkingSet::new(&engine_state);
@ -13,9 +14,6 @@ pub fn add_cli_context(mut engine_state: EngineState) -> EngineState {
bind_command! { bind_command! {
Commandline, Commandline,
CommandlineEdit,
CommandlineGetCursor,
CommandlineSetCursor,
History, History,
HistorySession, HistorySession,
Keybindings, Keybindings,

View File

@ -1,5 +1,9 @@
use nu_engine::command_prelude::*; use nu_protocol::ast::Call;
use nu_protocol::HistoryFileFormat; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, HistoryFileFormat, IntoInterruptiblePipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
use reedline::{ use reedline::{
FileBackedHistory, History as ReedlineHistory, HistoryItem, SearchDirection, SearchQuery, FileBackedHistory, History as ReedlineHistory, HistoryItem, SearchDirection, SearchQuery,
SqliteBackedHistory, SqliteBackedHistory,
@ -19,7 +23,10 @@ impl Command for History {
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("history") Signature::build("history")
.input_output_types(vec![(Type::Nothing, Type::Any)]) .input_output_types(vec![
(Type::Nothing, Type::Table(vec![])),
(Type::Nothing, Type::Nothing),
])
.allow_variants_without_examples(true) .allow_variants_without_examples(true)
.switch("clear", "Clears out the history entries", Some('c')) .switch("clear", "Clears out the history entries", Some('c'))
.switch( .switch(
@ -27,31 +34,27 @@ impl Command for History {
"Show long listing of entries for sqlite history", "Show long listing of entries for sqlite history",
Some('l'), Some('l'),
) )
.category(Category::History) .category(Category::Misc)
} }
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 head = call.head; let head = call.head;
let Some(history) = engine_state.history_config() else {
return Ok(PipelineData::empty());
};
// todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history` // todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history`
if let Some(config_path) = nu_path::config_dir() { if let Some(config_path) = nu_path::config_dir() {
let clear = call.has_flag(engine_state, stack, "clear")?; let clear = call.has_flag("clear");
let long = call.has_flag(engine_state, stack, "long")?; let long = call.has_flag("long");
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
let mut history_path = config_path; let mut history_path = config_path;
history_path.push("nushell"); history_path.push("nushell");
match history.file_format { match engine_state.config.history_file_format {
HistoryFileFormat::Sqlite => { HistoryFileFormat::Sqlite => {
history_path.push("history.sqlite3"); history_path.push("history.sqlite3");
} }
@ -65,28 +68,29 @@ impl Command for History {
// TODO: FIXME also clear the auxiliary files when using sqlite // TODO: FIXME also clear the auxiliary files when using sqlite
Ok(PipelineData::empty()) Ok(PipelineData::empty())
} else { } else {
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format { let history_reader: Option<Box<dyn ReedlineHistory>> =
HistoryFileFormat::Sqlite => { match engine_state.config.history_file_format {
SqliteBackedHistory::with_file(history_path.clone(), None, None) HistoryFileFormat::Sqlite => {
.map(|inner| { SqliteBackedHistory::with_file(history_path, None, None)
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); .map(|inner| {
boxed let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
}) boxed
.ok() })
} .ok()
}
HistoryFileFormat::PlainText => FileBackedHistory::with_file( HistoryFileFormat::PlainText => FileBackedHistory::with_file(
history.max_size as usize, engine_state.config.max_history_size as usize,
history_path.clone(), history_path,
) )
.map(|inner| { .map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed boxed
}) })
.ok(), .ok(),
}; };
match history.file_format { match engine_state.config.history_file_format {
HistoryFileFormat::PlainText => Ok(history_reader HistoryFileFormat::PlainText => Ok(history_reader
.and_then(|h| { .and_then(|h| {
h.search(SearchQuery::everything(SearchDirection::Forward, None)) h.search(SearchQuery::everything(SearchDirection::Forward, None))
@ -103,11 +107,8 @@ impl Command for History {
) )
}) })
}) })
.ok_or(ShellError::FileNotFound { .ok_or(ShellError::FileNotFound(head))?
file: history_path.display().to_string(), .into_pipeline_data(ctrlc)),
span: head,
})?
.into_pipeline_data(head, ctrlc)),
HistoryFileFormat::Sqlite => Ok(history_reader HistoryFileFormat::Sqlite => Ok(history_reader
.and_then(|h| { .and_then(|h| {
h.search(SearchQuery::everything(SearchDirection::Forward, None)) h.search(SearchQuery::everything(SearchDirection::Forward, None))
@ -118,15 +119,12 @@ impl Command for History {
create_history_record(idx, entry, long, head) create_history_record(idx, entry, long, head)
}) })
}) })
.ok_or(ShellError::FileNotFound { .ok_or(ShellError::FileNotFound(head))?
file: history_path.display().to_string(), .into_pipeline_data(ctrlc)),
span: head,
})?
.into_pipeline_data(head, ctrlc)),
} }
} }
} else { } else {
Err(ShellError::ConfigDirNotFound { span: Some(head) }) Err(ShellError::FileNotFound(head))
} }
} }

View File

@ -1,5 +0,0 @@
mod history_;
mod history_session;
pub use history_::History;
pub use history_session::HistorySession;

View File

@ -1,4 +1,8 @@
use nu_engine::command_prelude::*; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct HistorySession; pub struct HistorySession;
@ -14,7 +18,7 @@ impl Command for HistorySession {
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("history session") Signature::build("history session")
.category(Category::History) .category(Category::Misc)
.input_output_types(vec![(Type::Nothing, Type::Int)]) .input_output_types(vec![(Type::Nothing, Type::Int)])
} }

View File

@ -1,4 +1,9 @@
use nu_engine::{command_prelude::*, get_full_help}; use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Keybindings; pub struct Keybindings;
@ -36,6 +41,16 @@ For more information on input and keybindings, check:
call: &Call, call: &Call,
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
Ok(Value::string(get_full_help(self, engine_state, stack), call.head).into_pipeline_data()) Ok(Value::string(
get_full_help(
&Keybindings.signature(),
&Keybindings.examples(),
engine_state,
stack,
self.is_parser_keyword(),
),
call.head,
)
.into_pipeline_data())
} }
} }

View File

@ -1,4 +1,8 @@
use nu_engine::command_prelude::*; use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use reedline::get_reedline_default_keybindings; use reedline::get_reedline_default_keybindings;
#[derive(Clone)] #[derive(Clone)]
@ -12,7 +16,7 @@ impl Command for KeybindingsDefault {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build(self.name()) Signature::build(self.name())
.category(Category::Platform) .category(Category::Platform)
.input_output_types(vec![(Type::Nothing, Type::table())]) .input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {

View File

@ -1,4 +1,9 @@
use nu_engine::command_prelude::*; use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
};
use reedline::{ use reedline::{
get_reedline_edit_commands, get_reedline_keybinding_modifiers, get_reedline_keycodes, get_reedline_edit_commands, get_reedline_keybinding_modifiers, get_reedline_keycodes,
get_reedline_prompt_edit_modes, get_reedline_reedline_events, get_reedline_prompt_edit_modes, get_reedline_reedline_events,
@ -14,7 +19,7 @@ impl Command for KeybindingsList {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build(self.name()) Signature::build(self.name())
.input_output_types(vec![(Type::Nothing, Type::table())]) .input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
.switch("modifiers", "list of modifiers", Some('m')) .switch("modifiers", "list of modifiers", Some('m'))
.switch("keycodes", "list of keycodes", Some('k')) .switch("keycodes", "list of keycodes", Some('k'))
.switch("modes", "list of edit modes", Some('o')) .switch("modes", "list of edit modes", Some('o'))

View File

@ -1,7 +1,12 @@
use crossterm::{ use crossterm::execute;
event::Event, event::KeyCode, event::KeyEvent, execute, terminal, QueueableCommand, use crossterm::QueueableCommand;
use crossterm::{event::Event, event::KeyCode, event::KeyEvent, terminal};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
}; };
use nu_engine::command_prelude::*;
use std::io::{stdout, Write}; use std::io::{stdout, Write};
#[derive(Clone)] #[derive(Clone)]
@ -40,13 +45,13 @@ impl Command for KeybindingsListen {
Ok(v) => Ok(v.into_pipeline_data()), Ok(v) => Ok(v.into_pipeline_data()),
Err(e) => { Err(e) => {
terminal::disable_raw_mode()?; terminal::disable_raw_mode()?;
Err(ShellError::GenericError { Err(ShellError::GenericError(
error: "Error with input".into(), "Error with input".to_string(),
msg: "".into(), "".to_string(),
span: None, None,
help: Some(e.to_string()), Some(e.to_string()),
inner: vec![], Vec::new(),
}) ))
} }
} }
} }
@ -107,7 +112,7 @@ pub fn print_events(engine_state: &EngineState) -> Result<Value, ShellError> {
let o = match v { let o = match v {
Value::Record { val, .. } => val Value::Record { val, .. } => val
.iter() .iter()
.map(|(x, y)| format!("{}: {}", x, y.to_expanded_string("", config))) .map(|(x, y)| format!("{}: {}", x, y.into_string("", config)))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "), .join(", "),

View File

@ -1,13 +1,15 @@
mod commandline; mod commandline;
mod default_context; mod default_context;
mod history; mod history;
mod history_session;
mod keybindings; mod keybindings;
mod keybindings_default; mod keybindings_default;
mod keybindings_list; mod keybindings_list;
mod keybindings_listen; mod keybindings_listen;
pub use commandline::{Commandline, CommandlineEdit, CommandlineGetCursor, CommandlineSetCursor}; pub use commandline::Commandline;
pub use history::{History, HistorySession}; pub use history::History;
pub use history_session::HistorySession;
pub use keybindings::Keybindings; pub use keybindings::Keybindings;
pub use keybindings_default::KeybindingsDefault; pub use keybindings_default::KeybindingsDefault;
pub use keybindings_list::KeybindingsList; pub use keybindings_list::KeybindingsList;

View File

@ -1,30 +1,25 @@
use crate::completions::{CompletionOptions, SortBy}; use crate::completions::{CompletionOptions, SortBy};
use nu_protocol::{ use nu_protocol::{engine::StateWorkingSet, levenshtein_distance, Span};
engine::{Stack, StateWorkingSet},
levenshtein_distance, Span,
};
use reedline::Suggestion; use reedline::Suggestion;
// Completer trait represents the three stages of the completion // Completer trait represents the three stages of the completion
// fetch, filter and sort // fetch, filter and sort
pub trait Completer { pub trait Completer {
#[allow(clippy::too_many_arguments)]
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
pos: usize, pos: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion>; ) -> Vec<Suggestion>;
fn get_sort_by(&self) -> SortBy { fn get_sort_by(&self) -> SortBy {
SortBy::Ascending SortBy::Ascending
} }
fn sort(&self, items: Vec<SemanticSuggestion>, prefix: Vec<u8>) -> Vec<SemanticSuggestion> { fn sort(&self, items: Vec<Suggestion>, prefix: Vec<u8>) -> Vec<Suggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string(); let prefix_str = String::from_utf8_lossy(&prefix).to_string();
let mut filtered_items = items; let mut filtered_items = items;
@ -32,13 +27,13 @@ pub trait Completer {
match self.get_sort_by() { match self.get_sort_by() {
SortBy::LevenshteinDistance => { SortBy::LevenshteinDistance => {
filtered_items.sort_by(|a, b| { filtered_items.sort_by(|a, b| {
let a_distance = levenshtein_distance(&prefix_str, &a.suggestion.value); let a_distance = levenshtein_distance(&prefix_str, &a.value);
let b_distance = levenshtein_distance(&prefix_str, &b.suggestion.value); let b_distance = levenshtein_distance(&prefix_str, &b.value);
a_distance.cmp(&b_distance) a_distance.cmp(&b_distance)
}); });
} }
SortBy::Ascending => { SortBy::Ascending => {
filtered_items.sort_by(|a, b| a.suggestion.value.cmp(&b.suggestion.value)); filtered_items.sort_by(|a, b| a.value.cmp(&b.value));
} }
SortBy::None => {} SortBy::None => {}
}; };
@ -46,25 +41,3 @@ pub trait Completer {
filtered_items filtered_items
} }
} }
#[derive(Debug, Default, PartialEq)]
pub struct SemanticSuggestion {
pub suggestion: Suggestion,
pub kind: Option<SuggestionKind>,
}
// TODO: think about name: maybe suggestion context?
#[derive(Clone, Debug, PartialEq)]
pub enum SuggestionKind {
Command(nu_protocol::engine::CommandType),
Type(nu_protocol::Type),
}
impl From<Suggestion> for SemanticSuggestion {
fn from(suggestion: Suggestion) -> Self {
Self {
suggestion,
..Default::default()
}
}
}

View File

@ -1,17 +1,14 @@
use crate::{ use crate::completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy};
completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy},
SuggestionKind,
};
use nu_parser::FlatShape; use nu_parser::FlatShape;
use nu_protocol::{ use nu_protocol::{
engine::{CachedFile, Stack, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
Span, Span,
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::sync::Arc;
use super::SemanticSuggestion;
pub struct CommandCompletion { pub struct CommandCompletion {
engine_state: Arc<EngineState>,
flattened: Vec<(Span, FlatShape)>, flattened: Vec<(Span, FlatShape)>,
flat_shape: FlatShape, flat_shape: FlatShape,
force_completion_after_space: bool, force_completion_after_space: bool,
@ -19,11 +16,14 @@ pub struct CommandCompletion {
impl CommandCompletion { impl CommandCompletion {
pub fn new( pub fn new(
engine_state: Arc<EngineState>,
_: &StateWorkingSet,
flattened: Vec<(Span, FlatShape)>, flattened: Vec<(Span, FlatShape)>,
flat_shape: FlatShape, flat_shape: FlatShape,
force_completion_after_space: bool, force_completion_after_space: bool,
) -> Self { ) -> Self {
Self { Self {
engine_state,
flattened, flattened,
flat_shape, flat_shape,
force_completion_after_space, force_completion_after_space,
@ -32,26 +32,22 @@ impl CommandCompletion {
fn external_command_completion( fn external_command_completion(
&self, &self,
working_set: &StateWorkingSet,
prefix: &str, prefix: &str,
match_algorithm: MatchAlgorithm, match_algorithm: MatchAlgorithm,
) -> Vec<String> { ) -> Vec<String> {
let mut executables = vec![]; let mut executables = vec![];
// os agnostic way to get the PATH env var // os agnostic way to get the PATH env var
let paths = working_set.permanent_state.get_path_env_var(); let paths = self.engine_state.get_path_env_var();
if let Some(paths) = paths { if let Some(paths) = paths {
if let Ok(paths) = paths.as_list() { if let Ok(paths) = paths.as_list() {
for path in paths { for path in paths {
let path = path.coerce_str().unwrap_or_default(); let path = path.as_string().unwrap_or_default();
if let Ok(mut contents) = std::fs::read_dir(path.as_ref()) { if let Ok(mut contents) = std::fs::read_dir(path) {
while let Some(Ok(item)) = contents.next() { while let Some(Ok(item)) = contents.next() {
if working_set if self.engine_state.config.max_external_completion_results
.permanent_state
.config
.max_external_completion_results
> executables.len() as i64 > executables.len() as i64
&& !executables.contains( && !executables.contains(
&item &item
@ -87,7 +83,7 @@ impl CommandCompletion {
offset: usize, offset: usize,
find_externals: bool, find_externals: bool,
match_algorithm: MatchAlgorithm, match_algorithm: MatchAlgorithm,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let partial = working_set.get_span_contents(span); let partial = working_set.get_span_contents(span);
let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial); let filter_predicate = |command: &[u8]| match_algorithm.matches_u8(command, partial);
@ -95,16 +91,12 @@ impl CommandCompletion {
let mut results = working_set let mut results = working_set
.find_commands_by_predicate(filter_predicate, true) .find_commands_by_predicate(filter_predicate, true)
.into_iter() .into_iter()
.map(move |x| SemanticSuggestion { .map(move |x| Suggestion {
suggestion: Suggestion { value: String::from_utf8_lossy(&x.0).to_string(),
value: String::from_utf8_lossy(&x.0).to_string(), description: x.1,
description: x.1, extra: None,
style: None, span: reedline::Span::new(span.start - offset, span.end - offset),
extra: None, append_whitespace: true,
span: reedline::Span::new(span.start - offset, span.end - offset),
append_whitespace: true,
},
kind: Some(SuggestionKind::Command(x.2)),
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -113,36 +105,27 @@ impl CommandCompletion {
if find_externals { if find_externals {
let results_external = self let results_external = self
.external_command_completion(working_set, &partial, match_algorithm) .external_command_completion(&partial, match_algorithm)
.into_iter() .into_iter()
.map(move |x| SemanticSuggestion { .map(move |x| Suggestion {
suggestion: Suggestion { value: x,
value: x, description: None,
description: None, extra: None,
style: None, span: reedline::Span::new(span.start - offset, span.end - offset),
extra: None, append_whitespace: true,
span: reedline::Span::new(span.start - offset, span.end - offset),
append_whitespace: true,
},
// TODO: is there a way to create a test?
kind: None,
}); });
let results_strings: Vec<String> = let results_strings: Vec<String> =
results.iter().map(|x| x.suggestion.value.clone()).collect(); results.clone().into_iter().map(|x| x.value).collect();
for external in results_external { for external in results_external {
if results_strings.contains(&external.suggestion.value) { if results_strings.contains(&external.value) {
results.push(SemanticSuggestion { results.push(Suggestion {
suggestion: Suggestion { value: format!("^{}", external.value),
value: format!("^{}", external.suggestion.value), description: None,
description: None, extra: None,
style: None, span: external.span,
extra: None, append_whitespace: true,
span: external.suggestion.span,
append_whitespace: true,
},
kind: external.kind,
}) })
} else { } else {
results.push(external) results.push(external)
@ -160,13 +143,12 @@ impl Completer for CommandCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
_stack: &Stack,
_prefix: Vec<u8>, _prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
pos: usize, pos: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let last = self let last = self
.flattened .flattened
.iter() .iter()
@ -244,9 +226,8 @@ pub fn find_non_whitespace_index(contents: &[u8], start: usize) -> usize {
} }
} }
pub fn is_passthrough_command(working_set_file_contents: &[CachedFile]) -> bool { pub fn is_passthrough_command(working_set_file_contents: &[(Vec<u8>, usize, usize)]) -> bool {
for cached_file in working_set_file_contents { for (contents, _, _) in working_set_file_contents {
let contents = &cached_file.content;
let last_pipe_pos_rev = contents.iter().rev().position(|x| x == &b'|'); let last_pipe_pos_rev = contents.iter().rev().position(|x| x == &b'|');
let last_pipe_pos = last_pipe_pos_rev.map(|x| contents.len() - x).unwrap_or(0); let last_pipe_pos = last_pipe_pos_rev.map(|x| contents.len() - x).unwrap_or(0);
@ -266,12 +247,10 @@ pub fn is_passthrough_command(working_set_file_contents: &[CachedFile]) -> bool
#[cfg(test)] #[cfg(test)]
mod command_completions_tests { mod command_completions_tests {
use super::*; use super::*;
use nu_protocol::engine::EngineState;
use std::sync::Arc;
#[test] #[test]
fn test_find_non_whitespace_index() { fn test_find_non_whitespace_index() {
let commands = [ let commands = vec![
(" hello", 4), (" hello", 4),
("sudo ", 0), ("sudo ", 0),
(" sudo ", 2), (" sudo ", 2),
@ -291,7 +270,7 @@ mod command_completions_tests {
#[test] #[test]
fn test_is_last_command_passthrough() { fn test_is_last_command_passthrough() {
let commands = [ let commands = vec![
(" hello", false), (" hello", false),
(" sudo ", true), (" sudo ", true),
("sudo ", true), ("sudo ", true),
@ -313,7 +292,7 @@ mod command_completions_tests {
let input = ele.0.as_bytes(); let input = ele.0.as_bytes();
let mut engine_state = EngineState::new(); let mut engine_state = EngineState::new();
engine_state.add_file("test.nu".into(), Arc::new([])); engine_state.add_file("test.nu".into(), vec![]);
let delta = { let delta = {
let mut working_set = StateWorkingSet::new(&engine_state); let mut working_set = StateWorkingSet::new(&engine_state);

View File

@ -2,18 +2,16 @@ use crate::completions::{
CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion, CommandCompletion, Completer, CompletionOptions, CustomCompletion, DirectoryCompletion,
DotNuCompletion, FileCompletion, FlagCompletion, VariableCompletion, DotNuCompletion, FileCompletion, FlagCompletion, VariableCompletion,
}; };
use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style};
use nu_engine::eval_block; use nu_engine::eval_block;
use nu_parser::{flatten_pipeline_element, parse, FlatShape}; use nu_parser::{flatten_expression, parse, FlatShape};
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, ast::PipelineElement,
engine::{Closure, EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
PipelineData, Span, Value, BlockId, PipelineData, Span, Value,
}; };
use reedline::{Completer as ReedlineCompleter, Suggestion}; use reedline::{Completer as ReedlineCompleter, Suggestion};
use std::{str, sync::Arc}; use std::str;
use std::sync::Arc;
use super::base::{SemanticSuggestion, SuggestionKind};
#[derive(Clone)] #[derive(Clone)]
pub struct NuCompleter { pub struct NuCompleter {
@ -22,17 +20,13 @@ pub struct NuCompleter {
} }
impl NuCompleter { impl NuCompleter {
pub fn new(engine_state: Arc<EngineState>, stack: Arc<Stack>) -> Self { pub fn new(engine_state: Arc<EngineState>, stack: Stack) -> Self {
Self { Self {
engine_state, engine_state,
stack: Stack::with_parent(stack).reset_out_dest().capture(), stack,
} }
} }
pub fn fetch_completions_at(&mut self, line: &str, pos: usize) -> Vec<SemanticSuggestion> {
self.completion_helper(line, pos)
}
// Process the completion for a given completer // Process the completion for a given completer
fn process_completion<T: Completer>( fn process_completion<T: Completer>(
&self, &self,
@ -42,7 +36,7 @@ impl NuCompleter {
new_span: Span, new_span: Span,
offset: usize, offset: usize,
pos: usize, pos: usize,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let config = self.engine_state.get_config(); let config = self.engine_state.get_config();
let options = CompletionOptions { let options = CompletionOptions {
@ -52,15 +46,8 @@ impl NuCompleter {
}; };
// Fetch // Fetch
let mut suggestions = completer.fetch( let mut suggestions =
working_set, completer.fetch(working_set, prefix.clone(), new_span, offset, pos, &options);
&self.stack,
prefix.clone(),
new_span,
offset,
pos,
&options,
);
// Sort // Sort
suggestions = completer.sort(suggestions, prefix); suggestions = completer.sort(suggestions, prefix);
@ -70,15 +57,14 @@ impl NuCompleter {
fn external_completion( fn external_completion(
&self, &self,
closure: &Closure, block_id: BlockId,
spans: &[String], spans: &[String],
offset: usize, offset: usize,
span: Span, span: Span,
) -> Option<Vec<SemanticSuggestion>> { ) -> Option<Vec<Suggestion>> {
let block = self.engine_state.get_block(closure.block_id); let stack = self.stack.clone();
let mut callee_stack = self let block = self.engine_state.get_block(block_id);
.stack let mut callee_stack = stack.gather_captures(&self.engine_state, &block.captures);
.captures_to_stack_preserve_out_dest(closure.captures.clone());
// Line // Line
if let Some(pos_arg) = block.signature.required_positional.first() { if let Some(pos_arg) = block.signature.required_positional.first() {
@ -96,15 +82,18 @@ impl NuCompleter {
} }
} }
let result = eval_block::<WithoutDebug>( let result = eval_block(
&self.engine_state, &self.engine_state,
&mut callee_stack, &mut callee_stack,
block, block,
PipelineData::empty(), PipelineData::empty(),
true,
true,
); );
match result.and_then(|data| data.into_value(span)) { match result {
Ok(value) => { Ok(pd) => {
let value = pd.into_value(span);
if let Value::List { vals, .. } = value { if let Value::List { vals, .. } = value {
let result = let result =
map_value_completions(vals.iter(), Span::new(span.start, span.end), offset); map_value_completions(vals.iter(), Span::new(span.start, span.end), offset);
@ -118,258 +107,268 @@ impl NuCompleter {
None None
} }
fn completion_helper(&mut self, line: &str, pos: usize) -> Vec<SemanticSuggestion> { 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();
// TODO: Callers should be trimming the line themselves
let line = if line.len() > pos { &line[..pos] } else { line };
// Adjust offset so that the spans of the suggestions will start at the right
// place even with `only_buffer_difference: true`
let fake_offset = offset + line.len() - pos;
let pos = offset + line.len();
let initial_line = line.to_string(); let initial_line = line.to_string();
let mut line = line.to_string(); let mut line = line.to_string();
line.push('a'); line.insert(pos, 'a');
let pos = offset + pos;
let config = self.engine_state.get_config(); let config = self.engine_state.get_config();
let output = parse(&mut working_set, Some("completer"), line.as_bytes(), false); let output = parse(&mut working_set, Some("completer"), line.as_bytes(), false);
for pipeline in &output.pipelines { for pipeline in output.pipelines.into_iter() {
for pipeline_element in &pipeline.elements { for pipeline_element in pipeline.elements {
let flattened = flatten_pipeline_element(&working_set, pipeline_element); match pipeline_element {
let mut spans: Vec<String> = vec![]; PipelineElement::Expression(_, expr)
| PipelineElement::Redirection(_, _, expr)
| PipelineElement::And(_, expr)
| PipelineElement::Or(_, expr)
| PipelineElement::SameTargetRedirection { cmd: (_, expr), .. }
| PipelineElement::SeparateRedirection { out: (_, expr), .. } => {
let flattened: Vec<_> = flatten_expression(&working_set, &expr);
let mut spans: Vec<String> = vec![];
for (flat_idx, flat) in flattened.iter().enumerate() { for (flat_idx, flat) in flattened.iter().enumerate() {
let is_passthrough_command = spans let is_passthrough_command = spans
.first() .first()
.filter(|content| content.as_str() == "sudo" || content.as_str() == "doas") .filter(|content| {
.is_some(); content.as_str() == "sudo" || content.as_str() == "doas"
// Read the current spam to string })
let current_span = working_set.get_span_contents(flat.0).to_vec(); .is_some();
let current_span_str = String::from_utf8_lossy(&current_span); // 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(&current_span);
let is_last_span = pos >= flat.0.start && pos < flat.0.end; // Skip the last 'a' as span item
if flat_idx == flattened.len() - 1 {
// Skip the last 'a' as span item let mut chars = current_span_str.chars();
if is_last_span { chars.next_back();
let offset = pos - flat.0.start; let current_span_str = chars.as_str().to_owned();
if offset == 0 { spans.push(current_span_str.to_string());
spans.push(String::new()) } else {
} else { spans.push(current_span_str.to_string());
let mut current_span_str = current_span_str.to_string();
current_span_str.remove(offset);
spans.push(current_span_str);
}
} else {
spans.push(current_span_str.to_string());
}
// Complete based on the last span
if is_last_span {
// Context variables
let most_left_var =
most_left_variable(flat_idx, &working_set, flattened.clone());
// Create a new span
let new_span = Span::new(flat.0.start, flat.0.end - 1);
// Parses the prefix. Completion should look up to the cursor position, not after.
let mut prefix = working_set.get_span_contents(flat.0).to_vec();
let index = pos - flat.0.start;
prefix.drain(index..);
// Variables completion
if prefix.starts_with(b"$") || most_left_var.is_some() {
let mut completer =
VariableCompletion::new(most_left_var.unwrap_or((vec![], vec![])));
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
fake_offset,
pos,
);
}
// Flags completion
if prefix.starts_with(b"-") {
// Try to complete flag internally
let mut completer = FlagCompletion::new(pipeline_element.expr.clone());
let result = self.process_completion(
&mut completer,
&working_set,
prefix.clone(),
new_span,
fake_offset,
pos,
);
if !result.is_empty() {
return result;
} }
// We got no results for internal completion // Complete based on the last span
// now we can check if external completer is set and use it if pos >= flat.0.start && pos < flat.0.end {
if let Some(closure) = config.external_completer.as_ref() { // Context variables
if let Some(external_result) = let most_left_var =
self.external_completion(closure, &spans, fake_offset, new_span) most_left_variable(flat_idx, &working_set, flattened.clone());
{
return external_result;
}
}
}
// specially check if it is currently empty - always complete commands // Create a new span
if (is_passthrough_command && flat_idx == 1) let new_span = Span::new(flat.0.start, flat.0.end - 1);
|| (flat_idx == 0 && working_set.get_span_contents(new_span).is_empty())
{
let mut completer = CommandCompletion::new(
flattened.clone(),
// flat_idx,
FlatShape::String,
true,
);
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
fake_offset,
pos,
);
}
// Completions that depends on the previous expression (e.g: use, source-env) // Parses the prefix. Completion should look up to the cursor position, not after.
if (is_passthrough_command && flat_idx > 1) || flat_idx > 0 { let mut prefix = working_set.get_span_contents(flat.0).to_vec();
if let Some(previous_expr) = flattened.get(flat_idx - 1) { let index = pos - flat.0.start;
// Read the content for the previous expression prefix.drain(index..);
let prev_expr_str =
working_set.get_span_contents(previous_expr.0).to_vec();
// Completion for .nu files // Variables completion
if prev_expr_str == b"use" if prefix.starts_with(b"$") || most_left_var.is_some() {
|| prev_expr_str == b"overlay use" let mut completer = VariableCompletion::new(
|| prev_expr_str == b"source-env" self.engine_state.clone(),
{ self.stack.clone(),
let mut completer = DotNuCompletion::new(); most_left_var.unwrap_or((vec![], vec![])),
);
return self.process_completion( return self.process_completion(
&mut completer, &mut completer,
&working_set, &working_set,
prefix, prefix,
new_span, new_span,
fake_offset, offset,
pos, pos,
); );
} else if prev_expr_str == b"ls" { }
let mut completer = FileCompletion::new();
return self.process_completion( // Flags completion
if prefix.starts_with(b"-") {
// Try to complete flag internally
let mut completer = FlagCompletion::new(expr.clone());
let result = self.process_completion(
&mut completer, &mut completer,
&working_set, &working_set,
prefix, prefix.clone(),
new_span, new_span,
fake_offset, offset,
pos, pos,
); );
}
}
}
// Match other types if !result.is_empty() {
match &flat.1 { return result;
FlatShape::Custom(decl_id) => { }
let mut completer = CustomCompletion::new(
self.stack.clone(),
*decl_id,
initial_line,
);
return self.process_completion( // We got no results for internal completion
&mut completer, // now we can check if external completer is set and use it
&working_set, if let Some(block_id) = config.external_completer {
prefix, if let Some(external_result) = self
new_span, .external_completion(block_id, &spans, offset, new_span)
fake_offset, {
pos, return external_result;
); }
}
FlatShape::Directory => {
let mut completer = DirectoryCompletion::new();
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
fake_offset,
pos,
);
}
FlatShape::Filepath | FlatShape::GlobPattern => {
let mut completer = FileCompletion::new();
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
fake_offset,
pos,
);
}
flat_shape => {
let mut completer = CommandCompletion::new(
flattened.clone(),
// flat_idx,
flat_shape.clone(),
false,
);
let mut out: Vec<_> = self.process_completion(
&mut completer,
&working_set,
prefix.clone(),
new_span,
fake_offset,
pos,
);
if !out.is_empty() {
return out;
}
// Try to complete using an external completer (if set)
if let Some(closure) = config.external_completer.as_ref() {
if let Some(external_result) = self.external_completion(
closure,
&spans,
fake_offset,
new_span,
) {
return external_result;
} }
} }
// Check for file completion // specially check if it is currently empty - always complete commands
let mut completer = FileCompletion::new(); if (is_passthrough_command && flat_idx == 1)
out = self.process_completion( || (flat_idx == 0
&mut completer, && working_set.get_span_contents(new_span).is_empty())
&working_set, {
prefix, let mut completer = CommandCompletion::new(
new_span, self.engine_state.clone(),
fake_offset, &working_set,
pos, flattened.clone(),
); // flat_idx,
FlatShape::String,
if !out.is_empty() { true,
return out; );
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
} }
// Completions that depends on the previous expression (e.g: use, source-env)
if (is_passthrough_command && flat_idx > 1) || flat_idx > 0 {
if let Some(previous_expr) = flattened.get(flat_idx - 1) {
// Read the content for the previous expression
let prev_expr_str =
working_set.get_span_contents(previous_expr.0).to_vec();
// Completion for .nu files
if prev_expr_str == b"use" || prev_expr_str == b"source-env"
{
let mut completer =
DotNuCompletion::new(self.engine_state.clone());
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
} else if prev_expr_str == b"ls" {
let mut completer =
FileCompletion::new(self.engine_state.clone());
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
}
}
}
// Match other types
match &flat.1 {
FlatShape::Custom(decl_id) => {
let mut completer = CustomCompletion::new(
self.engine_state.clone(),
self.stack.clone(),
*decl_id,
initial_line,
);
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
}
FlatShape::Directory => {
let mut completer =
DirectoryCompletion::new(self.engine_state.clone());
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
}
FlatShape::Filepath | FlatShape::GlobPattern => {
let mut completer =
FileCompletion::new(self.engine_state.clone());
return self.process_completion(
&mut completer,
&working_set,
prefix,
new_span,
offset,
pos,
);
}
flat_shape => {
let mut completer = CommandCompletion::new(
self.engine_state.clone(),
&working_set,
flattened.clone(),
// flat_idx,
flat_shape.clone(),
false,
);
let mut out: Vec<_> = self.process_completion(
&mut completer,
&working_set,
prefix.clone(),
new_span,
offset,
pos,
);
if !out.is_empty() {
return out;
}
// Try to complete using an external completer (if set)
if let Some(block_id) = config.external_completer {
if let Some(external_result) = self.external_completion(
block_id, &spans, offset, new_span,
) {
if !external_result.is_empty() {
return external_result;
}
}
}
// 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;
}
}
};
} }
}; }
} }
} }
} }
@ -382,9 +381,6 @@ impl NuCompleter {
impl ReedlineCompleter for NuCompleter { impl ReedlineCompleter for NuCompleter {
fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> { fn complete(&mut self, line: &str, pos: usize) -> Vec<Suggestion> {
self.completion_helper(line, pos) self.completion_helper(line, pos)
.into_iter()
.map(|s| s.suggestion)
.collect()
} }
} }
@ -442,23 +438,19 @@ pub fn map_value_completions<'a>(
list: impl Iterator<Item = &'a Value>, list: impl Iterator<Item = &'a Value>,
span: Span, span: Span,
offset: usize, offset: usize,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
list.filter_map(move |x| { list.filter_map(move |x| {
// Match for string values // Match for string values
if let Ok(s) = x.coerce_string() { if let Ok(s) = x.as_string() {
return Some(SemanticSuggestion { return Some(Suggestion {
suggestion: Suggestion { value: s,
value: s, description: None,
description: None, extra: None,
style: None, span: reedline::Span {
extra: None, start: span.start - offset,
span: reedline::Span { end: span.end - offset,
start: span.start - offset,
end: span.end - offset,
},
append_whitespace: false,
}, },
kind: Some(SuggestionKind::Type(x.get_type())), append_whitespace: false,
}); });
} }
@ -467,7 +459,6 @@ pub fn map_value_completions<'a>(
let mut suggestion = Suggestion { let mut suggestion = Suggestion {
value: String::from(""), // Initialize with empty string value: String::from(""), // Initialize with empty string
description: None, description: None,
style: None,
extra: None, extra: None,
span: reedline::Span { span: reedline::Span {
start: span.start - offset, start: span.start - offset,
@ -481,7 +472,7 @@ pub fn map_value_completions<'a>(
// Match `value` column // Match `value` column
if it.0 == "value" { if it.0 == "value" {
// Convert the value to string // Convert the value to string
if let Ok(val_str) = it.1.coerce_string() { if let Ok(val_str) = it.1.as_string() {
// Update the suggestion value // Update the suggestion value
suggestion.value = val_str; suggestion.value = val_str;
} }
@ -490,27 +481,14 @@ pub fn map_value_completions<'a>(
// Match `description` column // Match `description` column
if it.0 == "description" { if it.0 == "description" {
// Convert the value to string // Convert the value to string
if let Ok(desc_str) = it.1.coerce_string() { if let Ok(desc_str) = it.1.as_string() {
// Update the suggestion value // Update the suggestion value
suggestion.description = Some(desc_str); suggestion.description = Some(desc_str);
} }
} }
// Match `style` column
if it.0 == "style" {
// Convert the value to string
suggestion.style = match it.1 {
Value::String { val, .. } => Some(lookup_ansi_color_style(val)),
Value::Record { .. } => Some(color_record_to_nustyle(it.1)),
_ => None,
};
}
}); });
return Some(SemanticSuggestion { return Some(suggestion);
suggestion,
kind: Some(SuggestionKind::Type(x.get_type())),
});
} }
None None
@ -540,8 +518,8 @@ mod completer_tests {
result.err().unwrap() result.err().unwrap()
); );
let mut completer = NuCompleter::new(engine_state.into(), Arc::new(Stack::new())); let mut completer = NuCompleter::new(engine_state.into(), Stack::new());
let dataset = [ let dataset = vec![
("sudo", false, "", Vec::new()), ("sudo", false, "", Vec::new()),
("sudo l", true, "l", vec!["ls", "let", "lines", "loop"]), ("sudo l", true, "l", vec!["ls", "let", "lines", "loop"]),
(" sudo", false, "", Vec::new()), (" sudo", false, "", Vec::new()),
@ -562,13 +540,13 @@ mod completer_tests {
// Test whether the result begins with the expected value // Test whether the result begins with the expected value
result result
.iter() .iter()
.for_each(|x| assert!(x.suggestion.value.starts_with(begins_with))); .for_each(|x| assert!(x.value.starts_with(begins_with)));
// Test whether the result contains all the expected values // Test whether the result contains all the expected values
assert_eq!( assert_eq!(
result result
.iter() .iter()
.map(|x| expected_values.contains(&x.suggestion.value.as_str())) .map(|x| expected_values.contains(&x.value.as_str()))
.filter(|x| *x) .filter(|x| *x)
.count(), .count(),
expected_values.len(), expected_values.len(),

View File

@ -1,71 +1,34 @@
use crate::completions::{matches, CompletionOptions}; use crate::completions::{matches, CompletionOptions};
use nu_ansi_term::Style;
use nu_engine::env_to_string;
use nu_path::home_dir; use nu_path::home_dir;
use nu_protocol::{ use nu_protocol::{engine::StateWorkingSet, Span};
engine::{EngineState, Stack, StateWorkingSet}, use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP};
Span,
};
use nu_utils::get_ls_colors;
use std::path::{
is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR,
};
#[derive(Clone, Default)]
pub struct PathBuiltFromString {
parts: Vec<String>,
isdir: bool,
}
fn complete_rec( fn complete_rec(
partial: &[&str], partial: &[String],
built: &PathBuiltFromString,
cwd: &Path, cwd: &Path,
options: &CompletionOptions, options: &CompletionOptions,
dir: bool, dir: bool,
isdir: bool, isdir: bool,
) -> Vec<PathBuiltFromString> { ) -> Vec<PathBuf> {
let mut completions = vec![]; let mut completions = vec![];
if let Some((&base, rest)) = partial.split_first() { if let Ok(result) = cwd.read_dir() {
if (base == "." || base == "..") && (isdir || !rest.is_empty()) { for entry in result.filter_map(|e| e.ok()) {
let mut built = built.clone(); let entry_name = entry.file_name().to_string_lossy().into_owned();
built.parts.push(base.to_string()); let path = entry.path();
built.isdir = true;
return complete_rec(rest, &built, cwd, options, dir, isdir);
}
}
let mut built_path = cwd.to_path_buf(); if !dir || path.is_dir() {
for part in &built.parts { match partial.first() {
built_path.push(part); Some(base) if matches(base, &entry_name, options) => {
} let partial = &partial[1..];
if !partial.is_empty() || isdir {
let Ok(result) = built_path.read_dir() else { completions.extend(complete_rec(partial, &path, options, dir, isdir))
return completions;
};
for entry in result.filter_map(|e| e.ok()) {
let entry_name = entry.file_name().to_string_lossy().into_owned();
let entry_isdir = entry.path().is_dir();
let mut built = built.clone();
built.parts.push(entry_name.clone());
built.isdir = entry_isdir;
if !dir || entry_isdir {
match partial.split_first() {
Some((base, rest)) => {
if matches(base, &entry_name, options) {
if !rest.is_empty() || isdir {
completions
.extend(complete_rec(rest, &built, cwd, options, dir, isdir));
} else { } else {
completions.push(built); completions.push(path)
} }
} }
} None => completions.push(path),
None => { _ => {}
completions.push(built);
} }
} }
} }
@ -73,23 +36,33 @@ fn complete_rec(
completions completions
} }
#[derive(Debug)]
enum OriginalCwd { enum OriginalCwd {
None, None,
Home, Home(PathBuf),
Prefix(String), Some(PathBuf),
// referencing a single local file
Local(PathBuf),
} }
impl OriginalCwd { impl OriginalCwd {
fn apply(&self, mut p: PathBuiltFromString) -> String { fn apply(&self, p: &Path) -> String {
match self { let mut ret = match self {
Self::None => {} Self::None => p.to_string_lossy().into_owned(),
Self::Home => p.parts.insert(0, "~".to_string()), Self::Some(base) => pathdiff::diff_paths(p, base)
Self::Prefix(s) => p.parts.insert(0, s.clone()), .unwrap_or(p.to_path_buf())
.to_string_lossy()
.into_owned(),
Self::Home(home) => match p.strip_prefix(home) {
Ok(suffix) => format!("~{}{}", SEP, suffix.to_string_lossy()),
_ => p.to_string_lossy().into_owned(),
},
Self::Local(base) => Path::new(".")
.join(pathdiff::diff_paths(p, base).unwrap_or(p.to_path_buf()))
.to_string_lossy()
.into_owned(),
}; };
let mut ret = p.parts.join(MAIN_SEPARATOR_STR); if p.is_dir() {
if p.isdir {
ret.push(SEP); ret.push(SEP);
} }
ret ret
@ -116,98 +89,68 @@ pub fn complete_item(
partial: &str, partial: &str,
cwd: &str, cwd: &str,
options: &CompletionOptions, options: &CompletionOptions,
engine_state: &EngineState, ) -> Vec<(nu_protocol::Span, String)> {
stack: &Stack,
) -> Vec<(nu_protocol::Span, String, Option<Style>)> {
let partial = surround_remove(partial); let partial = surround_remove(partial);
let isdir = partial.ends_with(is_separator); let isdir = partial.ends_with(is_separator);
let cwd_pathbuf = Path::new(cwd).to_path_buf(); let cwd_pathbuf = Path::new(cwd).to_path_buf();
let ls_colors = (engine_state.config.use_ls_colors_completions
&& engine_state.config.use_ansi_coloring)
.then(|| {
let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
Some(v) => env_to_string("LS_COLORS", &v, engine_state, stack).ok(),
None => None,
};
get_ls_colors(ls_colors_env_str)
});
let mut cwd = cwd_pathbuf.clone();
let mut prefix_len = 0;
let mut original_cwd = OriginalCwd::None; let mut original_cwd = OriginalCwd::None;
let mut components = Path::new(&partial).components().peekable(); let mut components = Path::new(&partial).components().peekable();
match components.peek().cloned() { let mut cwd = match components.peek().cloned() {
Some(c @ Component::Prefix(..)) => { Some(c @ Component::Prefix(..)) => {
// windows only by definition // windows only by definition
components.next(); components.next();
if let Some(Component::RootDir) = components.peek().cloned() { if let Some(Component::RootDir) = components.peek().cloned() {
components.next(); components.next();
}; };
cwd = [c, Component::RootDir].iter().collect(); [c, Component::RootDir].iter().collect()
prefix_len = c.as_os_str().len();
original_cwd = OriginalCwd::Prefix(c.as_os_str().to_string_lossy().into_owned());
} }
Some(c @ Component::RootDir) => { Some(c @ Component::RootDir) => {
components.next(); components.next();
// This is kind of a hack. When joining an empty string with the rest, PathBuf::from(c.as_os_str())
// we add the slash automagically
cwd = PathBuf::from(c.as_os_str());
prefix_len = 1;
original_cwd = OriginalCwd::Prefix(String::new());
} }
Some(Component::Normal(home)) if home.to_string_lossy() == "~" => { Some(Component::Normal(home)) if home.to_string_lossy() == "~" => {
components.next(); components.next();
cwd = home_dir().unwrap_or(cwd_pathbuf); original_cwd = OriginalCwd::Home(home_dir().unwrap_or(cwd_pathbuf.clone()));
prefix_len = 1; home_dir().unwrap_or(cwd_pathbuf)
original_cwd = OriginalCwd::Home; }
Some(Component::CurDir) => {
components.next();
original_cwd = match components.peek().cloned() {
Some(Component::Normal(_)) | None => OriginalCwd::Local(cwd_pathbuf.clone()),
_ => OriginalCwd::Some(cwd_pathbuf.clone()),
};
cwd_pathbuf
}
_ => {
original_cwd = OriginalCwd::Some(cwd_pathbuf.clone());
cwd_pathbuf
} }
_ => {}
}; };
let after_prefix = &partial[prefix_len..]; let mut partial = vec![];
let partial: Vec<_> = after_prefix
.strip_prefix(is_separator)
.unwrap_or(after_prefix)
.split(is_separator)
.filter(|s| !s.is_empty())
.collect();
complete_rec( for component in components {
partial.as_slice(), match component {
&PathBuiltFromString::default(), Component::Prefix(..) => unreachable!(),
&cwd, Component::RootDir => unreachable!(),
options, Component::CurDir => {}
want_directory, Component::ParentDir => {
isdir, if partial.pop().is_none() {
) cwd.pop();
.into_iter() }
.map(|p| { }
let path = original_cwd.apply(p); Component::Normal(c) => partial.push(c.to_string_lossy().into_owned()),
let style = ls_colors.as_ref().map(|lsc| { }
lsc.style_for_path_with_metadata(&path, std::fs::symlink_metadata(&path).ok().as_ref()) }
.map(lscolors::Style::to_nu_ansi_term_style)
.unwrap_or_default() complete_rec(partial.as_slice(), &cwd, options, want_directory, isdir)
}); .into_iter()
(span, escape_path(path, want_directory), style) .map(|p| (span, escape_path(original_cwd.apply(&p), want_directory)))
}) .collect()
.collect()
} }
// Fix files or folders with quotes or hashes // Fix files or folders with quotes or hashes
pub fn escape_path(path: String, dir: bool) -> String { pub fn escape_path(path: String, dir: bool) -> String {
// make glob pattern have the highest priority.
let glob_contaminated = path.contains(['[', '*', ']', '?']);
if glob_contaminated {
return if path.contains('\'') {
// decide to use double quote, also need to escape `"` in path
// or else users can't do anything with completed path either.
format!("\"{}\"", path.replace('"', r#"\""#))
} else {
format!("'{path}'")
};
}
let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']); let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']);
let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']); let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']);
let maybe_flag = path.starts_with('-'); let maybe_flag = path.starts_with('-');

View File

@ -1,7 +1,8 @@
use std::fmt::Display;
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use nu_parser::trim_quotes_str; use nu_parser::trim_quotes_str;
use nu_protocol::CompletionAlgorithm; use nu_protocol::CompletionAlgorithm;
use std::fmt::Display;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum SortBy { pub enum SortBy {
@ -95,6 +96,7 @@ impl std::error::Error for InvalidMatchAlgorithm {}
pub struct CompletionOptions { pub struct CompletionOptions {
pub case_sensitive: bool, pub case_sensitive: bool,
pub positional: bool, pub positional: bool,
pub sort_by: SortBy,
pub match_algorithm: MatchAlgorithm, pub match_algorithm: MatchAlgorithm,
} }
@ -103,6 +105,7 @@ impl Default for CompletionOptions {
Self { Self {
case_sensitive: true, case_sensitive: true,
positional: true, positional: true,
sort_by: SortBy::Ascending,
match_algorithm: MatchAlgorithm::Prefix, match_algorithm: MatchAlgorithm::Prefix,
} }
} }

View File

@ -1,18 +1,19 @@
use crate::completions::{ use crate::completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy};
completer::map_value_completions, Completer, CompletionOptions, MatchAlgorithm,
SemanticSuggestion, SortBy,
};
use nu_engine::eval_call; use nu_engine::eval_call;
use nu_protocol::{ use nu_protocol::{
ast::{Argument, Call, Expr, Expression}, ast::{Argument, Call, Expr, Expression},
debugger::WithoutDebug, engine::{EngineState, Stack, StateWorkingSet},
engine::{Stack, StateWorkingSet},
PipelineData, Span, Type, Value, PipelineData, Span, Type, Value,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use reedline::Suggestion;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc;
use super::completer::map_value_completions;
pub struct CustomCompletion { pub struct CustomCompletion {
engine_state: Arc<EngineState>,
stack: Stack, stack: Stack,
decl_id: usize, decl_id: usize,
line: String, line: String,
@ -20,8 +21,9 @@ pub struct CustomCompletion {
} }
impl CustomCompletion { impl CustomCompletion {
pub fn new(stack: Stack, decl_id: usize, line: String) -> Self { pub fn new(engine_state: Arc<EngineState>, stack: Stack, decl_id: usize, line: String) -> Self {
Self { Self {
engine_state,
stack, stack,
decl_id, decl_id,
line, line,
@ -33,20 +35,19 @@ impl CustomCompletion {
impl Completer for CustomCompletion { impl Completer for CustomCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, _: &StateWorkingSet,
_stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
pos: usize, pos: usize,
completion_options: &CompletionOptions, completion_options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
// Line position // Line position
let line_pos = pos - offset; let line_pos = pos - offset;
// Call custom declaration // Call custom declaration
let result = eval_call::<WithoutDebug>( let result = eval_call(
working_set.permanent_state, &self.engine_state,
&mut self.stack, &mut self.stack,
&Call { &Call {
decl_id: self.decl_id, decl_id: self.decl_id,
@ -65,6 +66,8 @@ impl Completer for CustomCompletion {
custom_completion: None, custom_completion: None,
}), }),
], ],
redirect_stdout: true,
redirect_stderr: true,
parser_info: HashMap::new(), parser_info: HashMap::new(),
}, },
PipelineData::empty(), PipelineData::empty(),
@ -74,53 +77,60 @@ impl Completer for CustomCompletion {
// Parse result // Parse result
let suggestions = result let suggestions = result
.and_then(|data| data.into_value(span)) .map(|pd| {
.map(|value| match &value { let value = pd.into_value(span);
Value::Record { val, .. } => { match &value {
let completions = val Value::Record { val, .. } => {
.get("completions") let completions = val
.and_then(|val| { .get("completions")
val.as_list() .and_then(|val| {
.ok() val.as_list()
.map(|it| map_value_completions(it.iter(), span, offset)) .ok()
}) .map(|it| map_value_completions(it.iter(), span, offset))
.unwrap_or_default(); })
let options = val.get("options"); .unwrap_or_default();
let options = val.get("options");
if let Some(Value::Record { val: options, .. }) = &options { if let Some(Value::Record { val: options, .. }) = &options {
let should_sort = options let should_sort = options
.get("sort") .get("sort")
.and_then(|val| val.as_bool().ok()) .and_then(|val| val.as_bool().ok())
.unwrap_or(false); .unwrap_or(false);
if should_sort { if should_sort {
self.sort_by = SortBy::Ascending; self.sort_by = SortBy::Ascending;
}
custom_completion_options = Some(CompletionOptions {
case_sensitive: options
.get("case_sensitive")
.and_then(|val| val.as_bool().ok())
.unwrap_or(true),
positional: options
.get("positional")
.and_then(|val| val.as_bool().ok())
.unwrap_or(true),
sort_by: if should_sort {
SortBy::Ascending
} else {
SortBy::None
},
match_algorithm: match options.get("completion_algorithm") {
Some(option) => option
.as_string()
.ok()
.and_then(|option| option.try_into().ok())
.unwrap_or(MatchAlgorithm::Prefix),
None => completion_options.match_algorithm,
},
});
} }
custom_completion_options = Some(CompletionOptions { completions
case_sensitive: options
.get("case_sensitive")
.and_then(|val| val.as_bool().ok())
.unwrap_or(true),
positional: options
.get("positional")
.and_then(|val| val.as_bool().ok())
.unwrap_or(true),
match_algorithm: match options.get("completion_algorithm") {
Some(option) => option
.coerce_string()
.ok()
.and_then(|option| option.try_into().ok())
.unwrap_or(MatchAlgorithm::Prefix),
None => completion_options.match_algorithm,
},
});
} }
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
completions _ => vec![],
} }
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
_ => vec![],
}) })
.unwrap_or_default(); .unwrap_or_default();
@ -136,22 +146,15 @@ impl Completer for CustomCompletion {
} }
} }
fn filter( fn filter(prefix: &[u8], items: Vec<Suggestion>, options: &CompletionOptions) -> Vec<Suggestion> {
prefix: &[u8],
items: Vec<SemanticSuggestion>,
options: &CompletionOptions,
) -> Vec<SemanticSuggestion> {
items items
.into_iter() .into_iter()
.filter(|it| match options.match_algorithm { .filter(|it| match options.match_algorithm {
MatchAlgorithm::Prefix => match (options.case_sensitive, options.positional) { MatchAlgorithm::Prefix => match (options.case_sensitive, options.positional) {
(true, true) => it.suggestion.value.as_bytes().starts_with(prefix), (true, true) => it.value.as_bytes().starts_with(prefix),
(true, false) => it (true, false) => it.value.contains(std::str::from_utf8(prefix).unwrap_or("")),
.suggestion
.value
.contains(std::str::from_utf8(prefix).unwrap_or("")),
(false, positional) => { (false, positional) => {
let value = it.suggestion.value.to_folded_case(); let value = it.value.to_folded_case();
let prefix = std::str::from_utf8(prefix).unwrap_or("").to_folded_case(); let prefix = std::str::from_utf8(prefix).unwrap_or("").to_folded_case();
if positional { if positional {
value.starts_with(&prefix) value.starts_with(&prefix)
@ -162,7 +165,7 @@ fn filter(
}, },
MatchAlgorithm::Fuzzy => options MatchAlgorithm::Fuzzy => options
.match_algorithm .match_algorithm
.matches_u8(it.suggestion.value.as_bytes(), prefix), .matches_u8(it.value.as_bytes(), prefix),
}) })
.collect() .collect()
} }

View File

@ -2,22 +2,22 @@ use crate::completions::{
completion_common::{adjust_if_intermediate, complete_item, AdjustView}, completion_common::{adjust_if_intermediate, complete_item, AdjustView},
Completer, CompletionOptions, SortBy, Completer, CompletionOptions, SortBy,
}; };
use nu_ansi_term::Style;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
levenshtein_distance, Span, levenshtein_distance, Span,
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::path::{Path, MAIN_SEPARATOR as SEP}; use std::path::{Path, MAIN_SEPARATOR as SEP};
use std::sync::Arc;
use super::SemanticSuggestion; #[derive(Clone)]
pub struct DirectoryCompletion {
#[derive(Clone, Default)] engine_state: Arc<EngineState>,
pub struct DirectoryCompletion {} }
impl DirectoryCompletion { impl DirectoryCompletion {
pub fn new() -> Self { pub fn new(engine_state: Arc<EngineState>) -> Self {
Self::default() Self { engine_state }
} }
} }
@ -25,40 +25,31 @@ impl Completer for DirectoryCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
_pos: usize, _: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let AdjustView { prefix, span, .. } = adjust_if_intermediate(&prefix, working_set, span); let AdjustView { prefix, span, .. } = adjust_if_intermediate(&prefix, working_set, span);
// Filter only the folders // Filter only the folders
#[allow(deprecated)]
let output: Vec<_> = directory_completion( let output: Vec<_> = directory_completion(
span, span,
&prefix, &prefix,
&working_set.permanent_state.current_work_dir(), &self.engine_state.current_work_dir(),
options, options,
working_set.permanent_state,
stack,
) )
.into_iter() .into_iter()
.map(move |x| SemanticSuggestion { .map(move |x| Suggestion {
suggestion: Suggestion { value: x.1,
value: x.1, description: None,
description: None, extra: None,
style: x.2, span: reedline::Span {
extra: None, start: x.0.start - offset,
span: reedline::Span { end: x.0.end - offset,
start: x.0.start - offset,
end: x.0.end - offset,
},
append_whitespace: false,
}, },
// TODO???? append_whitespace: false,
kind: None,
}) })
.collect(); .collect();
@ -66,7 +57,7 @@ impl Completer for DirectoryCompletion {
} }
// Sort results prioritizing the non hidden folders // Sort results prioritizing the non hidden folders
fn sort(&self, items: Vec<SemanticSuggestion>, prefix: Vec<u8>) -> Vec<SemanticSuggestion> { fn sort(&self, items: Vec<Suggestion>, prefix: Vec<u8>) -> Vec<Suggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string(); let prefix_str = String::from_utf8_lossy(&prefix).to_string();
// Sort items // Sort items
@ -76,16 +67,15 @@ impl Completer for DirectoryCompletion {
SortBy::Ascending => { SortBy::Ascending => {
sorted_items.sort_by(|a, b| { sorted_items.sort_by(|a, b| {
// Ignore trailing slashes in folder names when sorting // Ignore trailing slashes in folder names when sorting
a.suggestion a.value
.value
.trim_end_matches(SEP) .trim_end_matches(SEP)
.cmp(b.suggestion.value.trim_end_matches(SEP)) .cmp(b.value.trim_end_matches(SEP))
}); });
} }
SortBy::LevenshteinDistance => { SortBy::LevenshteinDistance => {
sorted_items.sort_by(|a, b| { sorted_items.sort_by(|a, b| {
let a_distance = levenshtein_distance(&prefix_str, &a.suggestion.value); let a_distance = levenshtein_distance(&prefix_str, &a.value);
let b_distance = levenshtein_distance(&prefix_str, &b.suggestion.value); let b_distance = levenshtein_distance(&prefix_str, &b.value);
a_distance.cmp(&b_distance) a_distance.cmp(&b_distance)
}); });
} }
@ -93,11 +83,11 @@ impl Completer for DirectoryCompletion {
} }
// Separate the results between hidden and non hidden // Separate the results between hidden and non hidden
let mut hidden: Vec<SemanticSuggestion> = vec![]; let mut hidden: Vec<Suggestion> = vec![];
let mut non_hidden: Vec<SemanticSuggestion> = vec![]; let mut non_hidden: Vec<Suggestion> = vec![];
for item in sorted_items.into_iter() { for item in sorted_items.into_iter() {
let item_path = Path::new(&item.suggestion.value); let item_path = Path::new(&item.value);
if let Some(value) = item_path.file_name() { if let Some(value) = item_path.file_name() {
if let Some(value) = value.to_str() { if let Some(value) = value.to_str() {
@ -122,8 +112,6 @@ pub fn directory_completion(
partial: &str, partial: &str,
cwd: &str, cwd: &str,
options: &CompletionOptions, options: &CompletionOptions,
engine_state: &EngineState, ) -> Vec<(nu_protocol::Span, String)> {
stack: &Stack, complete_item(true, span, partial, cwd, options)
) -> Vec<(nu_protocol::Span, String, Option<Style>)> {
complete_item(true, span, partial, cwd, options, engine_state, stack)
} }

View File

@ -1,33 +1,35 @@
use crate::completions::{file_path_completion, Completer, CompletionOptions, SortBy}; use crate::completions::{file_path_completion, Completer, CompletionOptions, SortBy};
use nu_protocol::{ use nu_protocol::{
engine::{Stack, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
Span, Span,
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::path::{is_separator, Path, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR}; use std::{
path::{is_separator, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR},
sync::Arc,
};
use super::SemanticSuggestion; #[derive(Clone)]
pub struct DotNuCompletion {
#[derive(Clone, Default)] engine_state: Arc<EngineState>,
pub struct DotNuCompletion {} }
impl DotNuCompletion { impl DotNuCompletion {
pub fn new() -> Self { pub fn new(engine_state: Arc<EngineState>) -> Self {
Self::default() Self { engine_state }
} }
} }
impl Completer for DotNuCompletion { impl Completer for DotNuCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, _: &StateWorkingSet,
stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
_pos: usize, _: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).replace('`', ""); let prefix_str = String::from_utf8_lossy(&prefix).replace('`', "");
let mut search_dirs: Vec<String> = vec![]; let mut search_dirs: Vec<String> = vec![];
@ -41,25 +43,26 @@ impl Completer for DotNuCompletion {
let mut is_current_folder = false; let mut is_current_folder = false;
// Fetch the lib dirs // Fetch the lib dirs
let lib_dirs: Vec<String> = if let Some(lib_dirs) = working_set.get_env_var("NU_LIB_DIRS") { let lib_dirs: Vec<String> =
lib_dirs if let Some(lib_dirs) = self.engine_state.get_env_var("NU_LIB_DIRS") {
.as_list() lib_dirs
.into_iter() .as_list()
.flat_map(|it| { .into_iter()
it.iter().map(|x| { .flat_map(|it| {
x.to_path() it.iter().map(|x| {
.expect("internal error: failed to convert lib path") x.as_path()
.expect("internal error: failed to convert lib path")
})
}) })
}) .map(|it| {
.map(|it| { it.into_os_string()
it.into_os_string() .into_string()
.into_string() .expect("internal error: failed to convert OS path")
.expect("internal error: failed to convert OS path") })
}) .collect()
.collect() } else {
} else { vec![]
vec![] };
};
// Check if the base_dir is a folder // Check if the base_dir is a folder
// rsplit_once removes the separator // rsplit_once removes the separator
@ -75,8 +78,7 @@ impl Completer for DotNuCompletion {
partial = base_dir_partial; partial = base_dir_partial;
} else { } else {
// Fetch the current folder // Fetch the current folder
#[allow(deprecated)] let current_folder = self.engine_state.current_work_dir();
let current_folder = working_set.permanent_state.current_work_dir();
is_current_folder = true; is_current_folder = true;
// Add the current folder and the lib dirs into the // Add the current folder and the lib dirs into the
@ -87,46 +89,29 @@ impl Completer for DotNuCompletion {
// Fetch the files filtering the ones that ends with .nu // Fetch the files filtering the ones that ends with .nu
// and transform them into suggestions // and transform them into suggestions
let output: Vec<SemanticSuggestion> = search_dirs let output: Vec<Suggestion> = search_dirs
.into_iter() .into_iter()
.flat_map(|search_dir| { .flat_map(|it| {
let completions = file_path_completion( file_path_completion(span, &partial, &it, options)
span,
&partial,
&search_dir,
options,
working_set.permanent_state,
stack,
);
completions
.into_iter() .into_iter()
.filter(move |it| { .filter(|it| {
// Different base dir, so we list the .nu files or folders // Different base dir, so we list the .nu files or folders
if !is_current_folder { if !is_current_folder {
it.1.ends_with(".nu") || it.1.ends_with(SEP) it.1.ends_with(".nu") || it.1.ends_with(SEP)
} else { } else {
// Lib dirs, so we filter only the .nu files or directory modules // Lib dirs, so we filter only the .nu files
if it.1.ends_with(SEP) { it.1.ends_with(".nu")
Path::new(&search_dir).join(&it.1).join("mod.nu").exists()
} else {
it.1.ends_with(".nu")
}
} }
}) })
.map(move |x| SemanticSuggestion { .map(move |x| Suggestion {
suggestion: Suggestion { value: x.1,
value: x.1, description: None,
description: None, extra: None,
style: x.2, span: reedline::Span {
extra: None, start: x.0.start - offset,
span: reedline::Span { end: x.0.end - offset,
start: x.0.start - offset,
end: x.0.end - offset,
},
append_whitespace: true,
}, },
// TODO???? append_whitespace: true,
kind: None,
}) })
}) })
.collect(); .collect();

View File

@ -2,23 +2,23 @@ use crate::completions::{
completion_common::{adjust_if_intermediate, complete_item, AdjustView}, completion_common::{adjust_if_intermediate, complete_item, AdjustView},
Completer, CompletionOptions, SortBy, Completer, CompletionOptions, SortBy,
}; };
use nu_ansi_term::Style;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
levenshtein_distance, Span, levenshtein_distance, Span,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use reedline::Suggestion; use reedline::Suggestion;
use std::path::{Path, MAIN_SEPARATOR as SEP}; use std::path::{Path, MAIN_SEPARATOR as SEP};
use std::sync::Arc;
use super::SemanticSuggestion; #[derive(Clone)]
pub struct FileCompletion {
#[derive(Clone, Default)] engine_state: Arc<EngineState>,
pub struct FileCompletion {} }
impl FileCompletion { impl FileCompletion {
pub fn new() -> Self { pub fn new(engine_state: Arc<EngineState>) -> Self {
Self::default() Self { engine_state }
} }
} }
@ -26,44 +26,35 @@ impl Completer for FileCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
_pos: usize, _: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let AdjustView { let AdjustView {
prefix, prefix,
span, span,
readjusted, readjusted,
} = adjust_if_intermediate(&prefix, working_set, span); } = adjust_if_intermediate(&prefix, working_set, span);
#[allow(deprecated)]
let output: Vec<_> = complete_item( let output: Vec<_> = complete_item(
readjusted, readjusted,
span, span,
&prefix, &prefix,
&working_set.permanent_state.current_work_dir(), &self.engine_state.current_work_dir(),
options, options,
working_set.permanent_state,
stack,
) )
.into_iter() .into_iter()
.map(move |x| SemanticSuggestion { .map(move |x| Suggestion {
suggestion: Suggestion { value: x.1,
value: x.1, description: None,
description: None, extra: None,
style: x.2, span: reedline::Span {
extra: None, start: x.0.start - offset,
span: reedline::Span { end: x.0.end - offset,
start: x.0.start - offset,
end: x.0.end - offset,
},
append_whitespace: false,
}, },
// TODO???? append_whitespace: false,
kind: None,
}) })
.collect(); .collect();
@ -71,7 +62,7 @@ impl Completer for FileCompletion {
} }
// Sort results prioritizing the non hidden folders // Sort results prioritizing the non hidden folders
fn sort(&self, items: Vec<SemanticSuggestion>, prefix: Vec<u8>) -> Vec<SemanticSuggestion> { fn sort(&self, items: Vec<Suggestion>, prefix: Vec<u8>) -> Vec<Suggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string(); let prefix_str = String::from_utf8_lossy(&prefix).to_string();
// Sort items // Sort items
@ -81,16 +72,15 @@ impl Completer for FileCompletion {
SortBy::Ascending => { SortBy::Ascending => {
sorted_items.sort_by(|a, b| { sorted_items.sort_by(|a, b| {
// Ignore trailing slashes in folder names when sorting // Ignore trailing slashes in folder names when sorting
a.suggestion a.value
.value
.trim_end_matches(SEP) .trim_end_matches(SEP)
.cmp(b.suggestion.value.trim_end_matches(SEP)) .cmp(b.value.trim_end_matches(SEP))
}); });
} }
SortBy::LevenshteinDistance => { SortBy::LevenshteinDistance => {
sorted_items.sort_by(|a, b| { sorted_items.sort_by(|a, b| {
let a_distance = levenshtein_distance(&prefix_str, &a.suggestion.value); let a_distance = levenshtein_distance(&prefix_str, &a.value);
let b_distance = levenshtein_distance(&prefix_str, &b.suggestion.value); let b_distance = levenshtein_distance(&prefix_str, &b.value);
a_distance.cmp(&b_distance) a_distance.cmp(&b_distance)
}); });
} }
@ -98,11 +88,11 @@ impl Completer for FileCompletion {
} }
// Separate the results between hidden and non hidden // Separate the results between hidden and non hidden
let mut hidden: Vec<SemanticSuggestion> = vec![]; let mut hidden: Vec<Suggestion> = vec![];
let mut non_hidden: Vec<SemanticSuggestion> = vec![]; let mut non_hidden: Vec<Suggestion> = vec![];
for item in sorted_items.into_iter() { for item in sorted_items.into_iter() {
let item_path = Path::new(&item.suggestion.value); let item_path = Path::new(&item.value);
if let Some(value) = item_path.file_name() { if let Some(value) = item_path.file_name() {
if let Some(value) = value.to_str() { if let Some(value) = value.to_str() {
@ -127,10 +117,8 @@ pub fn file_path_completion(
partial: &str, partial: &str,
cwd: &str, cwd: &str,
options: &CompletionOptions, options: &CompletionOptions,
engine_state: &EngineState, ) -> Vec<(nu_protocol::Span, String)> {
stack: &Stack, complete_item(false, span, partial, cwd, options)
) -> Vec<(nu_protocol::Span, String, Option<Style>)> {
complete_item(false, span, partial, cwd, options, engine_state, stack)
} }
pub fn matches(partial: &str, from: &str, options: &CompletionOptions) -> bool { pub fn matches(partial: &str, from: &str, options: &CompletionOptions) -> bool {

View File

@ -1,12 +1,11 @@
use crate::completions::{Completer, CompletionOptions}; use crate::completions::{Completer, CompletionOptions};
use nu_protocol::{ use nu_protocol::{
ast::{Expr, Expression}, ast::{Expr, Expression},
engine::{Stack, StateWorkingSet}, engine::StateWorkingSet,
Span, Span,
}; };
use reedline::Suggestion;
use super::SemanticSuggestion; use reedline::Suggestion;
#[derive(Clone)] #[derive(Clone)]
pub struct FlagCompletion { pub struct FlagCompletion {
@ -23,13 +22,12 @@ impl Completer for FlagCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
_stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
_pos: usize, _: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
// Check if it's a flag // Check if it's a flag
if let Expr::Call(call) = &self.expression.expr { if let Expr::Call(call) = &self.expression.expr {
let decl = working_set.get_decl(call.decl_id); let decl = working_set.get_decl(call.decl_id);
@ -45,20 +43,15 @@ impl Completer for FlagCompletion {
named.insert(0, b'-'); named.insert(0, b'-');
if options.match_algorithm.matches_u8(&named, &prefix) { if options.match_algorithm.matches_u8(&named, &prefix) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: String::from_utf8_lossy(&named).to_string(),
value: String::from_utf8_lossy(&named).to_string(), description: Some(flag_desc.to_string()),
description: Some(flag_desc.to_string()), extra: None,
style: None, span: reedline::Span {
extra: None, start: span.start - offset,
span: reedline::Span { end: span.end - offset,
start: span.start - offset,
end: span.end - offset,
},
append_whitespace: true,
}, },
// TODO???? append_whitespace: true,
kind: None,
}); });
} }
} }
@ -72,20 +65,15 @@ impl Completer for FlagCompletion {
named.insert(0, b'-'); named.insert(0, b'-');
if options.match_algorithm.matches_u8(&named, &prefix) { if options.match_algorithm.matches_u8(&named, &prefix) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: String::from_utf8_lossy(&named).to_string(),
value: String::from_utf8_lossy(&named).to_string(), description: Some(flag_desc.to_string()),
description: Some(flag_desc.to_string()), extra: None,
style: None, span: reedline::Span {
extra: None, start: span.start - offset,
span: reedline::Span { end: span.end - offset,
start: span.start - offset,
end: span.end - offset,
},
append_whitespace: true,
}, },
// TODO???? append_whitespace: true,
kind: None,
}); });
} }
} }

View File

@ -10,7 +10,7 @@ mod file_completions;
mod flag_completions; mod flag_completions;
mod variable_completions; mod variable_completions;
pub use base::{Completer, SemanticSuggestion, SuggestionKind}; pub use base::Completer;
pub use command_completions::CommandCompletion; pub use command_completions::CommandCompletion;
pub use completer::NuCompleter; pub use completer::NuCompleter;
pub use completion_options::{CompletionOptions, MatchAlgorithm, SortBy}; pub use completion_options::{CompletionOptions, MatchAlgorithm, SortBy};

View File

@ -1,22 +1,34 @@
use crate::completions::{ use crate::completions::{Completer, CompletionOptions};
Completer, CompletionOptions, MatchAlgorithm, SemanticSuggestion, SuggestionKind,
};
use nu_engine::{column::get_columns, eval_variable}; use nu_engine::{column::get_columns, eval_variable};
use nu_protocol::{ use nu_protocol::{
engine::{Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
Span, Value, Span, Value,
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::str; use std::str;
use std::sync::Arc;
use super::MatchAlgorithm;
#[derive(Clone)] #[derive(Clone)]
pub struct VariableCompletion { pub struct VariableCompletion {
engine_state: Arc<EngineState>, // TODO: Is engine state necessary? It's already a part of working set in fetch()
stack: Stack,
var_context: (Vec<u8>, Vec<Vec<u8>>), // tuple with $var and the sublevels (.b.c.d) var_context: (Vec<u8>, Vec<Vec<u8>>), // tuple with $var and the sublevels (.b.c.d)
} }
impl VariableCompletion { impl VariableCompletion {
pub fn new(var_context: (Vec<u8>, Vec<Vec<u8>>)) -> Self { pub fn new(
Self { var_context } engine_state: Arc<EngineState>,
stack: Stack,
var_context: (Vec<u8>, Vec<Vec<u8>>),
) -> Self {
Self {
engine_state,
stack,
var_context,
}
} }
} }
@ -24,13 +36,12 @@ impl Completer for VariableCompletion {
fn fetch( fn fetch(
&mut self, &mut self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
stack: &Stack,
prefix: Vec<u8>, prefix: Vec<u8>,
span: Span, span: Span,
offset: usize, offset: usize,
_pos: usize, _: usize,
options: &CompletionOptions, options: &CompletionOptions,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let mut output = vec![]; let mut output = vec![];
let builtins = ["$nu", "$in", "$env"]; let builtins = ["$nu", "$in", "$env"];
let var_str = std::str::from_utf8(&self.var_context.0).unwrap_or(""); let var_str = std::str::from_utf8(&self.var_context.0).unwrap_or("");
@ -45,7 +56,7 @@ impl Completer for VariableCompletion {
if !var_str.is_empty() { if !var_str.is_empty() {
// Completion for $env.<tab> // Completion for $env.<tab>
if var_str == "$env" { if var_str == "$env" {
let env_vars = stack.get_env_vars(working_set.permanent_state); let env_vars = self.stack.get_env_vars(&self.engine_state);
// Return nested values // Return nested values
if sublevels_count > 0 { if sublevels_count > 0 {
@ -59,10 +70,12 @@ impl Completer for VariableCompletion {
self.var_context.1.clone().into_iter().skip(1).collect(); self.var_context.1.clone().into_iter().skip(1).collect();
if let Some(val) = env_vars.get(&target_var_str) { if let Some(val) = env_vars.get(&target_var_str) {
for suggestion in nested_suggestions(val, &nested_levels, current_span) { for suggestion in
nested_suggestions(val.clone(), nested_levels, current_span)
{
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
suggestion.suggestion.value.as_bytes(), suggestion.value.as_bytes(),
&prefix, &prefix,
) { ) {
output.push(suggestion); output.push(suggestion);
@ -79,16 +92,12 @@ impl Completer for VariableCompletion {
env_var.0.as_bytes(), env_var.0.as_bytes(),
&prefix, &prefix,
) { ) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: env_var.0,
value: env_var.0, description: None,
description: None, extra: None,
style: None, span: current_span,
extra: None, append_whitespace: false,
span: current_span,
append_whitespace: false,
},
kind: Some(SuggestionKind::Type(env_var.1.get_type())),
}); });
} }
} }
@ -101,16 +110,17 @@ impl Completer for VariableCompletion {
if var_str == "$nu" { if var_str == "$nu" {
// Eval nu var // Eval nu var
if let Ok(nuval) = eval_variable( if let Ok(nuval) = eval_variable(
working_set.permanent_state, &self.engine_state,
stack, &self.stack,
nu_protocol::NU_VARIABLE_ID, nu_protocol::NU_VARIABLE_ID,
nu_protocol::Span::new(current_span.start, current_span.end), nu_protocol::Span::new(current_span.start, current_span.end),
) { ) {
for suggestion in nested_suggestions(&nuval, &self.var_context.1, current_span) for suggestion in
nested_suggestions(nuval, self.var_context.1.clone(), current_span)
{ {
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
suggestion.suggestion.value.as_bytes(), suggestion.value.as_bytes(),
&prefix, &prefix,
) { ) {
output.push(suggestion); output.push(suggestion);
@ -124,15 +134,16 @@ impl Completer for VariableCompletion {
// Completion other variable types // Completion other variable types
if let Some(var_id) = var_id { if let Some(var_id) = var_id {
// Extract the variable value from the stack // Extract the variable value from the stack
let var = stack.get_var(var_id, Span::new(span.start, span.end)); let var = self.stack.get_var(var_id, Span::new(span.start, span.end));
// If the value exists and it's of type Record // If the value exists and it's of type Record
if let Ok(value) = var { if let Ok(value) = var {
for suggestion in nested_suggestions(&value, &self.var_context.1, current_span) for suggestion in
nested_suggestions(value, self.var_context.1.clone(), current_span)
{ {
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
suggestion.suggestion.value.as_bytes(), suggestion.value.as_bytes(),
&prefix, &prefix,
) { ) {
output.push(suggestion); output.push(suggestion);
@ -151,17 +162,12 @@ impl Completer for VariableCompletion {
builtin.as_bytes(), builtin.as_bytes(),
&prefix, &prefix,
) { ) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: builtin.to_string(),
value: builtin.to_string(), description: None,
description: None, extra: None,
style: None, span: current_span,
extra: None, append_whitespace: false,
span: current_span,
append_whitespace: false,
},
// TODO is there a way to get the VarId to get the type???
kind: None,
}); });
} }
} }
@ -178,18 +184,12 @@ impl Completer for VariableCompletion {
v.0, v.0,
&prefix, &prefix,
) { ) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: String::from_utf8_lossy(v.0).to_string(),
value: String::from_utf8_lossy(v.0).to_string(), description: None,
description: None, extra: None,
style: None, span: current_span,
extra: None, append_whitespace: false,
span: current_span,
append_whitespace: false,
},
kind: Some(SuggestionKind::Type(
working_set.get_variable(*v.1).ty.clone(),
)),
}); });
} }
} }
@ -198,29 +198,19 @@ impl Completer for VariableCompletion {
// Permanent state vars // Permanent state vars
// for scope in &self.engine_state.scope { // for scope in &self.engine_state.scope {
for overlay_frame in working_set for overlay_frame in self.engine_state.active_overlays(&removed_overlays).rev() {
.permanent_state
.active_overlays(&removed_overlays)
.rev()
{
for v in &overlay_frame.vars { for v in &overlay_frame.vars {
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
v.0, v.0,
&prefix, &prefix,
) { ) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: String::from_utf8_lossy(v.0).to_string(),
value: String::from_utf8_lossy(v.0).to_string(), description: None,
description: None, extra: None,
style: None, span: current_span,
extra: None, append_whitespace: false,
span: current_span,
append_whitespace: false,
},
kind: Some(SuggestionKind::Type(
working_set.get_variable(*v.1).ty.clone(),
)),
}); });
} }
} }
@ -235,28 +225,37 @@ impl Completer for VariableCompletion {
// Find recursively the values for sublevels // Find recursively the values for sublevels
// if no sublevels are set it returns the current value // if no sublevels are set it returns the current value
fn nested_suggestions( fn nested_suggestions(
val: &Value, val: Value,
sublevels: &[Vec<u8>], sublevels: Vec<Vec<u8>>,
current_span: reedline::Span, current_span: reedline::Span,
) -> Vec<SemanticSuggestion> { ) -> Vec<Suggestion> {
let mut output: Vec<SemanticSuggestion> = vec![]; let mut output: Vec<Suggestion> = vec![];
let value = recursive_value(val, sublevels).unwrap_or_else(Value::nothing); let value = recursive_value(val, sublevels);
let kind = SuggestionKind::Type(value.get_type());
match value { match value {
Value::Record { val, .. } => { Value::Record { val, .. } => {
// Add all the columns as completion // Add all the columns as completion
for col in val.columns() { for (col, _) in val.into_iter() {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: col,
value: col.clone(), description: None,
description: None, extra: None,
style: None, span: current_span,
extra: None, append_whitespace: false,
span: current_span, });
append_whitespace: false, }
},
kind: Some(kind.clone()), output
}
Value::LazyRecord { val, .. } => {
// Add all the columns as completion
for column_name in val.column_names() {
output.push(Suggestion {
value: column_name.to_string(),
description: None,
extra: None,
span: current_span,
append_whitespace: false,
}); });
} }
@ -264,16 +263,12 @@ fn nested_suggestions(
} }
Value::List { vals, .. } => { Value::List { vals, .. } => {
for column_name in get_columns(vals.as_slice()) { for column_name in get_columns(vals.as_slice()) {
output.push(SemanticSuggestion { output.push(Suggestion {
suggestion: Suggestion { value: column_name,
value: column_name, description: None,
description: None, extra: None,
style: None, span: current_span,
extra: None, append_whitespace: false,
span: current_span,
append_whitespace: false,
},
kind: Some(kind.clone()),
}); });
} }
@ -284,36 +279,56 @@ fn nested_suggestions(
} }
// Extracts the recursive value (e.g: $var.a.b.c) // Extracts the recursive value (e.g: $var.a.b.c)
fn recursive_value(val: &Value, sublevels: &[Vec<u8>]) -> Result<Value, Span> { fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value {
// Go to next sublevel // Go to next sublevel
if let Some((sublevel, next_sublevels)) = sublevels.split_first() { if let Some(next_sublevel) = sublevels.clone().into_iter().next() {
let span = val.span(); let span = val.span();
match val { match val {
Value::Record { val, .. } => { Value::Record { val, .. } => {
if let Some((_, value)) = val.iter().find(|(key, _)| key.as_bytes() == sublevel) { for item in val {
// If matches try to fetch recursively the next // Check if index matches with sublevel
recursive_value(value, next_sublevels) if item.0.as_bytes().to_vec() == next_sublevel {
} else { // If matches try to fetch recursively the next
// Current sublevel value not found return recursive_value(item.1, sublevels.into_iter().skip(1).collect());
Err(span)
}
}
Value::List { vals, .. } => {
for col in get_columns(vals.as_slice()) {
if col.as_bytes() == *sublevel {
let val = val.get_data_by_key(&col).ok_or(span)?;
return recursive_value(&val, next_sublevels);
} }
} }
// Current sublevel value not found // Current sublevel value not found
Err(span) return Value::nothing(span);
} }
_ => Ok(val.clone()), Value::LazyRecord { val, .. } => {
for col in val.column_names() {
if col.as_bytes().to_vec() == next_sublevel {
return recursive_value(
val.get_column_value(col).unwrap_or_default(),
sublevels.into_iter().skip(1).collect(),
);
}
}
// Current sublevel value not found
return Value::nothing(span);
}
Value::List { vals, .. } => {
for col in get_columns(vals.as_slice()) {
if col.as_bytes().to_vec() == next_sublevel {
return recursive_value(
Value::list(vals, span)
.get_data_by_key(&col)
.unwrap_or_default(),
sublevels.into_iter().skip(1).collect(),
);
}
}
// Current sublevel value not found
return Value::nothing(span);
}
_ => return val,
} }
} else {
Ok(val.clone())
} }
val
} }
impl MatchAlgorithm { impl MatchAlgorithm {

View File

@ -1,20 +1,17 @@
use crate::util::eval_source; use crate::util::eval_source;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::report_error;
use nu_protocol::{HistoryFileFormat, PipelineData};
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_protocol::{engine::StateWorkingSet, report_error, ParseError, PluginRegistryFile, Spanned}; use nu_protocol::{ParseError, Spanned};
use nu_protocol::{
engine::{EngineState, Stack},
report_error_new, HistoryFileFormat, PipelineData,
};
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_utils::utils::perf; use nu_utils::utils::perf;
use std::path::PathBuf; use std::path::PathBuf;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
const PLUGIN_FILE: &str = "plugin.msgpackz"; const PLUGIN_FILE: &str = "plugin.nu";
#[cfg(feature = "plugin")]
const OLD_PLUGIN_FILE: &str = "plugin.nu";
const HISTORY_FILE_TXT: &str = "history.txt"; const HISTORY_FILE_TXT: &str = "history.txt";
const HISTORY_FILE_SQLITE: &str = "history.sqlite3"; const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
@ -22,149 +19,40 @@ const HISTORY_FILE_SQLITE: &str = "history.sqlite3";
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub fn read_plugin_file( pub fn read_plugin_file(
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: &mut Stack,
plugin_file: Option<Spanned<String>>, plugin_file: Option<Spanned<String>>,
storage_path: &str, storage_path: &str,
) { ) {
use nu_protocol::ShellError; let start_time = std::time::Instant::now();
use std::path::Path; let mut plug_path = String::new();
// Reading signatures from signature file
// The plugin.nu file stores the parsed signature collected from each registered plugin
add_plugin_file(engine_state, plugin_file, storage_path);
let span = plugin_file.as_ref().map(|s| s.span); let plugin_path = engine_state.plugin_signatures.clone();
if let Some(plugin_path) = plugin_path {
// Check and warn + abort if this is a .nu plugin file let plugin_filename = plugin_path.to_string_lossy();
if plugin_file plug_path = plugin_filename.to_string();
.as_ref() if let Ok(contents) = std::fs::read(&plugin_path) {
.and_then(|p| Path::new(&p.item).extension()) eval_source(
.is_some_and(|ext| ext == "nu") engine_state,
{ stack,
report_error_new( &contents,
engine_state, &plugin_filename,
&ShellError::GenericError { PipelineData::empty(),
error: "Wrong plugin file format".into(), false,
msg: ".nu plugin files are no longer supported".into(), );
span, }
help: Some("please recreate this file in the new .msgpackz format".into()),
inner: vec![],
},
);
return;
} }
let mut start_time = std::time::Instant::now();
// Reading signatures from plugin registry file
// The plugin.msgpackz file stores the parsed signature collected from each registered plugin
add_plugin_file(engine_state, plugin_file.clone(), storage_path);
perf( perf(
"add plugin file to engine_state", &format!("read_plugin_file {}", &plug_path),
start_time, start_time,
file!(), file!(),
line!(), line!(),
column!(), column!(),
engine_state.get_config().use_ansi_coloring, engine_state.get_config().use_ansi_coloring,
); );
start_time = std::time::Instant::now();
let plugin_path = engine_state.plugin_path.clone();
if let Some(plugin_path) = plugin_path {
// Open the plugin file
let mut file = match std::fs::File::open(&plugin_path) {
Ok(file) => file,
Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound {
log::warn!("Plugin file not found: {}", plugin_path.display());
// Try migration of an old plugin file if this wasn't a custom plugin file
if plugin_file.is_none() && migrate_old_plugin_file(engine_state, storage_path)
{
let Ok(file) = std::fs::File::open(&plugin_path) else {
log::warn!("Failed to load newly migrated plugin file");
return;
};
file
} else {
return;
}
} else {
report_error_new(
engine_state,
&ShellError::GenericError {
error: format!(
"Error while opening plugin registry file: {}",
plugin_path.display()
),
msg: "plugin path defined here".into(),
span,
help: None,
inner: vec![err.into()],
},
);
return;
}
}
};
// Abort if the file is empty.
if file.metadata().is_ok_and(|m| m.len() == 0) {
log::warn!(
"Not reading plugin file because it's empty: {}",
plugin_path.display()
);
return;
}
// Read the contents of the plugin file
let contents = match PluginRegistryFile::read_from(&mut file, span) {
Ok(contents) => contents,
Err(err) => {
log::warn!("Failed to read plugin registry file: {err:?}");
report_error_new(
engine_state,
&ShellError::GenericError {
error: format!(
"Error while reading plugin registry file: {}",
plugin_path.display()
),
msg: "plugin path defined here".into(),
span,
help: Some(
"you might try deleting the file and registering all of your \
plugins again"
.into(),
),
inner: vec![],
},
);
return;
}
};
perf(
&format!("read plugin file {}", plugin_path.display()),
start_time,
file!(),
line!(),
column!(),
engine_state.get_config().use_ansi_coloring,
);
start_time = std::time::Instant::now();
let mut working_set = StateWorkingSet::new(engine_state);
nu_plugin_engine::load_plugin_file(&mut working_set, &contents, span);
if let Err(err) = engine_state.merge_delta(working_set.render()) {
report_error_new(engine_state, &err);
return;
}
perf(
&format!("load plugin file {}", plugin_path.display()),
start_time,
file!(),
line!(),
column!(),
engine_state.get_config().use_ansi_coloring,
);
}
} }
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
@ -173,39 +61,21 @@ pub fn add_plugin_file(
plugin_file: Option<Spanned<String>>, plugin_file: Option<Spanned<String>>,
storage_path: &str, storage_path: &str,
) { ) {
use std::path::Path; if let Some(plugin_file) = plugin_file {
let working_set = StateWorkingSet::new(engine_state);
let cwd = working_set.get_cwd();
let working_set = StateWorkingSet::new(engine_state); if let Ok(path) = canonicalize_with(&plugin_file.item, cwd) {
engine_state.plugin_signatures = Some(path)
if let Ok(cwd) = engine_state.cwd_as_string(None) { } else {
if let Some(plugin_file) = plugin_file { let e = ParseError::FileNotFound(plugin_file.item, plugin_file.span);
let path = Path::new(&plugin_file.item); report_error(&working_set, &e);
let path_dir = path.parent().unwrap_or(path);
// Just try to canonicalize the directory of the plugin file first.
if let Ok(path_dir) = canonicalize_with(path_dir, &cwd) {
// Try to canonicalize the actual filename, but it's ok if that fails. The file doesn't
// have to exist.
let path = path_dir.join(path.file_name().unwrap_or(path.as_os_str()));
let path = canonicalize_with(&path, &cwd).unwrap_or(path);
engine_state.plugin_path = Some(path)
} else {
// It's an error if the directory for the plugin file doesn't exist.
report_error(
&working_set,
&ParseError::FileNotFound(
path_dir.to_string_lossy().into_owned(),
plugin_file.span,
),
);
}
} else if let Some(mut plugin_path) = nu_path::config_dir() {
// Path to store plugins signatures
plugin_path.push(storage_path);
let mut plugin_path = canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path);
plugin_path.push(PLUGIN_FILE);
let plugin_path = canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path);
engine_state.plugin_path = Some(plugin_path);
} }
} else if let Some(mut plugin_path) = nu_path::config_dir() {
// Path to store plugins signatures
plugin_path.push(storage_path);
plugin_path.push(PLUGIN_FILE);
engine_state.plugin_signatures = Some(plugin_path.clone());
} }
} }
@ -218,10 +88,6 @@ pub fn eval_config_contents(
let config_filename = config_path.to_string_lossy(); let config_filename = config_path.to_string_lossy();
if let Ok(contents) = std::fs::read(&config_path) { if let Ok(contents) = std::fs::read(&config_path) {
// Set the current active file to the config file.
let prev_file = engine_state.file.take();
engine_state.file = Some(config_path.clone());
eval_source( eval_source(
engine_state, engine_state,
stack, stack,
@ -231,18 +97,17 @@ pub fn eval_config_contents(
false, false,
); );
// Restore the current active file.
engine_state.file = prev_file;
// Merge the environment in case env vars changed in the config // Merge the environment in case env vars changed in the config
match engine_state.cwd(Some(stack)) { match nu_engine::env::current_dir(engine_state, stack) {
Ok(cwd) => { Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) { if let Err(e) = engine_state.merge_env(stack, cwd) {
report_error_new(engine_state, &e); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
} }
} }
Err(e) => { Err(e) => {
report_error_new(engine_state, &e); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
} }
} }
} }
@ -259,132 +124,3 @@ pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> O
history_path history_path
}) })
} }
#[cfg(feature = "plugin")]
pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool {
use nu_protocol::{
PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature,
ShellError,
};
use std::collections::BTreeMap;
let start_time = std::time::Instant::now();
let Ok(cwd) = engine_state.cwd_as_string(None) else {
return false;
};
let Some(config_dir) = nu_path::config_dir().and_then(|mut dir| {
dir.push(storage_path);
nu_path::canonicalize_with(dir, &cwd).ok()
}) else {
return false;
};
let Ok(old_plugin_file_path) = nu_path::canonicalize_with(OLD_PLUGIN_FILE, &config_dir) else {
return false;
};
let old_contents = match std::fs::read(&old_plugin_file_path) {
Ok(old_contents) => old_contents,
Err(err) => {
report_error_new(
engine_state,
&ShellError::GenericError {
error: "Can't read old plugin file to migrate".into(),
msg: "".into(),
span: None,
help: Some(err.to_string()),
inner: vec![],
},
);
return false;
}
};
// Make a copy of the engine state, because we'll read the newly generated file
let mut engine_state = engine_state.clone();
let mut stack = Stack::new();
if eval_source(
&mut engine_state,
&mut stack,
&old_contents,
&old_plugin_file_path.to_string_lossy(),
PipelineData::Empty,
false,
) != 0
{
return false;
}
// Now that the plugin commands are loaded, we just have to generate the file
let mut contents = PluginRegistryFile::new();
let mut groups = BTreeMap::<PluginIdentity, Vec<PluginSignature>>::new();
for decl in engine_state.plugin_decls() {
if let Some(identity) = decl.plugin_identity() {
groups
.entry(identity.clone())
.or_default()
.push(PluginSignature {
sig: decl.signature(),
examples: decl
.examples()
.into_iter()
.map(PluginExample::from)
.collect(),
})
}
}
for (identity, commands) in groups {
contents.upsert_plugin(PluginRegistryItem {
name: identity.name().to_owned(),
filename: identity.filename().to_owned(),
shell: identity.shell().map(|p| p.to_owned()),
data: PluginRegistryItemData::Valid { commands },
});
}
// Write the new file
let new_plugin_file_path = config_dir.join(PLUGIN_FILE);
if let Err(err) = std::fs::File::create(&new_plugin_file_path)
.map_err(|e| e.into())
.and_then(|file| contents.write_to(file, None))
{
report_error_new(
&engine_state,
&ShellError::GenericError {
error: "Failed to save migrated plugin file".into(),
msg: "".into(),
span: None,
help: Some("ensure `$nu.plugin-path` is writable".into()),
inner: vec![err],
},
);
return false;
}
if engine_state.is_interactive {
eprintln!(
"Your old plugin.nu file has been migrated to the new format: {}",
new_plugin_file_path.display()
);
eprintln!(
"The plugin.nu file has not been removed. If `plugin list` looks okay, \
you may do so manually."
);
}
perf(
"migrate old plugin file",
start_time,
file!(),
line!(),
column!(),
engine_state.get_config().use_ansi_coloring,
);
true
}

View File

@ -1,12 +1,13 @@
use log::info; use log::info;
use miette::Result;
use nu_engine::{convert_env_values, eval_block}; use nu_engine::{convert_env_values, eval_block};
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::engine::Stack;
use nu_protocol::report_error;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, engine::{EngineState, StateWorkingSet},
engine::{EngineState, Stack, StateWorkingSet}, PipelineData, Spanned, Value,
report_error, PipelineData, ShellError, Spanned, Value,
}; };
use std::sync::Arc;
/// Run a command (or commands) given to us by the user /// Run a command (or commands) given to us by the user
pub fn evaluate_commands( pub fn evaluate_commands(
@ -15,28 +16,28 @@ pub fn evaluate_commands(
stack: &mut Stack, stack: &mut Stack,
input: PipelineData, input: PipelineData,
table_mode: Option<Value>, table_mode: Option<Value>,
no_newline: bool, ) -> Result<Option<i64>> {
) -> Result<(), ShellError> {
// Translate environment variables from Strings to Values // Translate environment variables from Strings to Values
convert_env_values(engine_state, stack)?; if let Some(e) = convert_env_values(engine_state, stack) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
std::process::exit(1);
}
// Parse the source code // Parse the source code
let (block, delta) = { let (block, delta) = {
if let Some(ref t_mode) = table_mode { if let Some(ref t_mode) = table_mode {
let mut config = engine_state.get_config().clone(); let mut config = engine_state.get_config().clone();
config.table_mode = t_mode.coerce_str()?.parse().unwrap_or_default(); config.table_mode = t_mode.as_string()?.parse().unwrap_or_default();
engine_state.set_config(config); engine_state.set_config(config);
} }
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let output = parse(&mut working_set, None, commands.item.as_bytes(), false); let output = parse(&mut working_set, None, commands.item.as_bytes(), false);
if let Some(warning) = working_set.parse_warnings.first() {
report_error(&working_set, warning);
}
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err); report_error(&working_set, err);
std::process::exit(1); std::process::exit(1);
} }
@ -44,27 +45,29 @@ pub fn evaluate_commands(
}; };
// Update permanent state // Update permanent state
engine_state.merge_delta(delta)?; if let Err(err) = engine_state.merge_delta(delta) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
}
// Run the block // Run the block
let pipeline = eval_block::<WithoutDebug>(engine_state, stack, &block, input)?; let exit_code = match eval_block(engine_state, stack, &block, input, false, false) {
Ok(pipeline_data) => {
if let PipelineData::Value(Value::Error { error, .. }, ..) = pipeline { let mut config = engine_state.get_config().clone();
return Err(*error); if let Some(t_mode) = table_mode {
} config.table_mode = t_mode.as_string()?.parse().unwrap_or_default();
}
if let Some(t_mode) = table_mode { crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config)
Arc::make_mut(&mut engine_state.config).table_mode =
t_mode.coerce_str()?.parse().unwrap_or_default();
}
if let Some(status) = pipeline.print(engine_state, stack, no_newline, false)? {
if status.code() != 0 {
std::process::exit(status.code())
} }
} Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
std::process::exit(1);
}
};
info!("evaluate {}:{}:{}", file!(), line!(), column!()); info!("evaluate {}:{}:{}", file!(), line!(), column!());
Ok(()) Ok(exit_code)
} }

View File

@ -1,59 +1,94 @@
use crate::util::eval_source; use crate::util::eval_source;
use log::{info, trace}; use log::info;
use nu_engine::{convert_env_values, eval_block}; use log::trace;
use miette::{IntoDiagnostic, Result};
use nu_engine::eval_block;
use nu_engine::{convert_env_values, current_dir};
use nu_parser::parse; use nu_parser::parse;
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::report_error;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, ast::Call,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, PipelineData, ShellError, Span, Value, Config, PipelineData, ShellError, Span, Value,
}; };
use std::sync::Arc; use nu_utils::stdout_write_all_and_flush;
/// Entry point for evaluating a file. /// Main function used when a file path is found as argument for nu
///
/// If the file contains a main command, it is invoked with `args` and the pipeline data from `input`;
/// otherwise, the pipeline data is forwarded to the first command in the file, and `args` are ignored.
pub fn evaluate_file( pub fn evaluate_file(
path: String, path: String,
args: &[String], args: &[String],
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: &mut Stack, stack: &mut Stack,
input: PipelineData, input: PipelineData,
) -> Result<(), ShellError> { ) -> Result<()> {
// Convert environment variables from Strings to Values and store them in the engine state. // Translate environment variables from Strings to Values
convert_env_values(engine_state, stack)?; if let Some(e) = convert_env_values(engine_state, stack) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
std::process::exit(1);
}
let cwd = engine_state.cwd_as_string(Some(stack))?; let cwd = current_dir(engine_state, stack)?;
let file_path = let file_path = canonicalize_with(&path, cwd).unwrap_or_else(|e| {
canonicalize_with(&path, cwd).map_err(|err| ShellError::FileNotFoundCustom { let working_set = StateWorkingSet::new(engine_state);
msg: format!("Could not access file '{path}': {err}"), report_error(
span: Span::unknown(), &working_set,
})?; &ShellError::FileNotFoundCustom(
format!("Could not access file '{}': {:?}", path, e.to_string()),
let file_path_str = file_path Span::unknown(),
.to_str()
.ok_or_else(|| ShellError::NonUtf8Custom {
msg: format!(
"Input file name '{}' is not valid UTF8",
file_path.to_string_lossy()
), ),
span: Span::unknown(), );
})?; std::process::exit(1);
});
let file = std::fs::read(&file_path).map_err(|err| ShellError::FileNotFoundCustom { let file_path_str = file_path.to_str().unwrap_or_else(|| {
msg: format!("Could not read file '{file_path_str}': {err}"), let working_set = StateWorkingSet::new(engine_state);
span: Span::unknown(), report_error(
})?; &working_set,
engine_state.file = Some(file_path.clone()); &ShellError::NonUtf8Custom(
format!(
"Input file name '{}' is not valid UTF8",
file_path.to_string_lossy()
),
Span::unknown(),
),
);
std::process::exit(1);
});
let parent = file_path let file = std::fs::read(&file_path)
.parent() .into_diagnostic()
.ok_or_else(|| ShellError::FileNotFoundCustom { .unwrap_or_else(|e| {
msg: format!("The file path '{file_path_str}' does not have a parent"), let working_set = StateWorkingSet::new(engine_state);
span: Span::unknown(), report_error(
})?; &working_set,
&ShellError::FileNotFoundCustom(
format!(
"Could not read file '{}': {:?}",
file_path_str,
e.to_string()
),
Span::unknown(),
),
);
std::process::exit(1);
});
engine_state.start_in_file(Some(file_path_str));
let parent = file_path.parent().unwrap_or_else(|| {
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom(
format!("The file path '{file_path_str}' does not have a parent"),
Span::unknown(),
),
);
std::process::exit(1);
});
stack.add_env_var( stack.add_env_var(
"FILE_PWD".to_string(), "FILE_PWD".to_string(),
@ -63,27 +98,21 @@ pub fn evaluate_file(
"CURRENT_FILE".to_string(), "CURRENT_FILE".to_string(),
Value::string(file_path.to_string_lossy(), Span::unknown()), Value::string(file_path.to_string_lossy(), Span::unknown()),
); );
stack.add_env_var(
"PROCESS_PATH".to_string(),
Value::string(path, Span::unknown()),
);
let source_filename = file_path let source_filename = file_path
.file_name() .file_name()
.expect("internal error: missing filename"); .expect("internal error: script missing filename");
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
trace!("parsing file: {}", file_path_str); trace!("parsing file: {}", file_path_str);
let block = parse(&mut working_set, Some(file_path_str), &file, false); let block = parse(&mut working_set, Some(file_path_str), &file, false);
// If any parse errors were found, report the first error and exit.
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err); report_error(&working_set, err);
std::process::exit(1); std::process::exit(1);
} }
// Look for blocks whose name starts with "main" and replace it with the filename. for block in &mut working_set.delta.blocks {
for block in working_set.delta.blocks.iter_mut().map(Arc::make_mut) {
if block.signature.name == "main" { if block.signature.name == "main" {
block.signature.name = source_filename.to_string_lossy().to_string(); block.signature.name = source_filename.to_string_lossy().to_string();
} else if block.signature.name.starts_with("main ") { } else if block.signature.name.starts_with("main ") {
@ -92,49 +121,138 @@ pub fn evaluate_file(
} }
} }
// Merge the changes into the engine state. let _ = engine_state.merge_delta(working_set.delta);
engine_state.merge_delta(working_set.delta)?;
// Check if the file contains a main command. if engine_state.find_decl(b"main", &[]).is_some() {
let exit_code = if engine_state.find_decl(b"main", &[]).is_some() { let args = format!("main {}", args.join(" "));
// Evaluate the file, but don't run main yet.
let pipeline = let pipeline_data = eval_block(
match eval_block::<WithoutDebug>(engine_state, stack, &block, PipelineData::empty()) { engine_state,
Ok(data) => data, stack,
Err(ShellError::Return { .. }) => { &block,
// Allow early return before main is run. PipelineData::empty(),
return Ok(()); false,
false,
);
let pipeline_data = match pipeline_data {
Err(ShellError::Return(_, _)) => {
// allows early exists before `main` is run.
return Ok(());
}
x => x,
}
.unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
std::process::exit(1);
});
let result = pipeline_data.print(engine_state, stack, true, false);
match result {
Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
std::process::exit(1);
}
Ok(exit_code) => {
if exit_code != 0 {
std::process::exit(exit_code as i32);
} }
Err(err) => return Err(err),
};
// Print the pipeline output of the last command of the file.
if let Some(status) = pipeline.print(engine_state, stack, true, false)? {
if status.code() != 0 {
std::process::exit(status.code())
} }
} }
// Invoke the main command with arguments. if !eval_source(
// Arguments with whitespaces are quoted, thus can be safely concatenated by whitespace.
let args = format!("main {}", args.join(" "));
eval_source(
engine_state, engine_state,
stack, stack,
args.as_bytes(), args.as_bytes(),
"<commandline>", "<commandline>",
input, input,
true, true,
) ) {
} else { std::process::exit(1);
eval_source(engine_state, stack, &file, file_path_str, input, true) }
}; } else if !eval_source(engine_state, stack, &file, file_path_str, input, true) {
std::process::exit(1);
if exit_code != 0 {
std::process::exit(exit_code)
} }
info!("evaluate {}:{}:{}", file!(), line!(), column!()); info!("evaluate {}:{}:{}", file!(), line!(), column!());
Ok(()) Ok(())
} }
pub(crate) fn print_table_or_error(
engine_state: &mut EngineState,
stack: &mut Stack,
mut pipeline_data: PipelineData,
config: &mut Config,
) -> Option<i64> {
let exit_code = match &mut pipeline_data {
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
_ => None,
};
// Change the engine_state config to use the passed in configuration
engine_state.set_config(config.clone());
if let PipelineData::Value(Value::Error { error, .. }, ..) = &pipeline_data {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &**error);
std::process::exit(1);
}
if let Some(decl_id) = engine_state.find_decl("table".as_bytes(), &[]) {
let command = engine_state.get_decl(decl_id);
if command.get_block_id().is_some() {
print_or_exit(pipeline_data, engine_state, config);
} else {
// The final call on table command, it's ok to set redirect_output to false.
let mut call = Call::new(Span::new(0, 0));
call.redirect_stdout = false;
let table = command.run(engine_state, stack, &call, pipeline_data);
match table {
Ok(table) => {
print_or_exit(table, engine_state, config);
}
Err(error) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &error);
std::process::exit(1);
}
}
}
} else {
print_or_exit(pipeline_data, engine_state, config);
}
// Make sure everything has finished
if let Some(exit_code) = exit_code {
let mut exit_code: Vec<_> = exit_code.into_iter().collect();
exit_code
.pop()
.and_then(|last_exit_code| match last_exit_code {
Value::Int { val: code, .. } => Some(code),
_ => None,
})
} else {
None
}
}
fn print_or_exit(pipeline_data: PipelineData, engine_state: &mut EngineState, config: &Config) {
for item in pipeline_data {
if let Value::Error { error, .. } = item {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &*error);
std::process::exit(1);
}
let out = item.into_string("\n", config) + "\n";
let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{err}"));
}
}

View File

@ -15,11 +15,11 @@ mod util;
mod validation; mod validation;
pub use commands::add_cli_context; pub use commands::add_cli_context;
pub use completions::{FileCompletion, NuCompleter, SemanticSuggestion, SuggestionKind}; pub use completions::{FileCompletion, NuCompleter};
pub use config_files::eval_config_contents; pub use config_files::eval_config_contents;
pub use eval_cmds::evaluate_commands; pub use eval_cmds::evaluate_commands;
pub use eval_file::evaluate_file; pub use eval_file::evaluate_file;
pub use menus::NuHelpCompleter; pub use menus::{DescriptionMenu, NuHelpCompleter};
pub use nu_cmd_base::util::get_init_cwd; pub use nu_cmd_base::util::get_init_cwd;
pub use nu_highlight::NuHighlight; pub use nu_highlight::NuHighlight;
pub use print::Print; pub use print::Print;
@ -32,6 +32,4 @@ pub use validation::NuValidator;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub use config_files::add_plugin_file; pub use config_files::add_plugin_file;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub use config_files::migrate_old_plugin_file;
#[cfg(feature = "plugin")]
pub use config_files::read_plugin_file; pub use config_files::read_plugin_file;

View File

@ -0,0 +1,730 @@
use {
nu_ansi_term::{ansi::RESET, Style},
reedline::{
menu_functions::string_difference, Completer, Editor, Menu, MenuEvent, MenuTextStyle,
Painter, Suggestion, UndoBehavior,
},
};
/// Default values used as reference for the menu. These values are set during
/// the initial declaration of the menu and are always kept as reference for the
/// changeable [`WorkingDetails`]
struct DefaultMenuDetails {
/// Number of columns that the menu will have
pub columns: u16,
/// Column width
pub col_width: Option<usize>,
/// Column padding
pub col_padding: usize,
/// Number of rows for commands
pub selection_rows: u16,
/// Number of rows allowed to display the description
pub description_rows: usize,
}
impl Default for DefaultMenuDetails {
fn default() -> Self {
Self {
columns: 4,
col_width: None,
col_padding: 2,
selection_rows: 4,
description_rows: 10,
}
}
}
/// Represents the actual column conditions of the menu. These conditions change
/// since they need to accommodate possible different line sizes for the column values
#[derive(Default)]
struct WorkingDetails {
/// Number of columns that the menu will have
pub columns: u16,
/// Column width
pub col_width: usize,
/// Number of rows for description
pub description_rows: usize,
}
/// Completion menu definition
pub struct DescriptionMenu {
/// Menu name
name: String,
/// Menu status
active: bool,
/// Menu coloring
color: MenuTextStyle,
/// Default column details that are set when creating the menu
/// These values are the reference for the working details
default_details: DefaultMenuDetails,
/// Number of minimum rows that are displayed when
/// the required lines is larger than the available lines
min_rows: u16,
/// Working column details keep changing based on the collected values
working_details: WorkingDetails,
/// Menu cached values
values: Vec<Suggestion>,
/// column position of the cursor. Starts from 0
col_pos: u16,
/// row position in the menu. Starts from 0
row_pos: u16,
/// Menu marker when active
marker: String,
/// Event sent to the menu
event: Option<MenuEvent>,
/// String collected after the menu is activated
input: Option<String>,
/// Examples to select
examples: Vec<String>,
/// Example index
example_index: Option<usize>,
/// Examples may not be shown if there is not enough space in the screen
show_examples: bool,
/// Skipped description rows
skipped_rows: usize,
/// Calls the completer using only the line buffer difference difference
/// after the menu was activated
only_buffer_difference: bool,
}
impl Default for DescriptionMenu {
fn default() -> Self {
Self {
name: "description_menu".to_string(),
active: false,
color: MenuTextStyle::default(),
default_details: DefaultMenuDetails::default(),
min_rows: 3,
working_details: WorkingDetails::default(),
values: Vec::new(),
col_pos: 0,
row_pos: 0,
marker: "? ".to_string(),
event: None,
input: None,
examples: Vec::new(),
example_index: None,
show_examples: true,
skipped_rows: 0,
only_buffer_difference: true,
}
}
}
// Menu configuration
impl DescriptionMenu {
/// Menu builder with new name
pub fn with_name(mut self, name: &str) -> Self {
self.name = name.into();
self
}
/// Menu builder with new value for text style
pub fn with_text_style(mut self, text_style: Style) -> Self {
self.color.text_style = text_style;
self
}
/// Menu builder with new value for text style
pub fn with_selected_text_style(mut self, selected_text_style: Style) -> Self {
self.color.selected_text_style = selected_text_style;
self
}
/// Menu builder with new value for text style
pub fn with_description_text_style(mut self, description_text_style: Style) -> Self {
self.color.description_style = description_text_style;
self
}
/// Menu builder with new columns value
pub fn with_columns(mut self, columns: u16) -> Self {
self.default_details.columns = columns;
self
}
/// Menu builder with new column width value
pub fn with_column_width(mut self, col_width: Option<usize>) -> Self {
self.default_details.col_width = col_width;
self
}
/// Menu builder with new column width value
pub fn with_column_padding(mut self, col_padding: usize) -> Self {
self.default_details.col_padding = col_padding;
self
}
/// Menu builder with new selection rows value
pub fn with_selection_rows(mut self, selection_rows: u16) -> Self {
self.default_details.selection_rows = selection_rows;
self
}
/// Menu builder with new description rows value
pub fn with_description_rows(mut self, description_rows: usize) -> Self {
self.default_details.description_rows = description_rows;
self
}
/// Menu builder with marker
pub fn with_marker(mut self, marker: String) -> Self {
self.marker = marker;
self
}
/// Menu builder with new only buffer difference
pub fn with_only_buffer_difference(mut self, only_buffer_difference: bool) -> Self {
self.only_buffer_difference = only_buffer_difference;
self
}
}
// Menu functionality
impl DescriptionMenu {
/// Move menu cursor to the next element
fn move_next(&mut self) {
let mut new_col = self.col_pos + 1;
let mut new_row = self.row_pos;
if new_col >= self.get_cols() {
new_row += 1;
new_col = 0;
}
if new_row >= self.get_rows() {
new_row = 0;
new_col = 0;
}
let position = new_row * self.get_cols() + new_col;
if position >= self.get_values().len() as u16 {
self.reset_position();
} else {
self.col_pos = new_col;
self.row_pos = new_row;
}
}
/// Move menu cursor to the previous element
fn move_previous(&mut self) {
let new_col = self.col_pos.checked_sub(1);
let (new_col, new_row) = match new_col {
Some(col) => (col, self.row_pos),
None => match self.row_pos.checked_sub(1) {
Some(row) => (self.get_cols().saturating_sub(1), row),
None => (
self.get_cols().saturating_sub(1),
self.get_rows().saturating_sub(1),
),
},
};
let position = new_row * self.get_cols() + new_col;
if position >= self.get_values().len() as u16 {
self.col_pos = (self.get_values().len() as u16 % self.get_cols()).saturating_sub(1);
self.row_pos = self.get_rows().saturating_sub(1);
} else {
self.col_pos = new_col;
self.row_pos = new_row;
}
}
/// Menu index based on column and row position
fn index(&self) -> usize {
let index = self.row_pos * self.get_cols() + self.col_pos;
index as usize
}
/// Get selected value from the menu
fn get_value(&self) -> Option<Suggestion> {
self.get_values().get(self.index()).cloned()
}
/// Calculates how many rows the Menu will use
fn get_rows(&self) -> u16 {
let values = self.get_values().len() as u16;
if values == 0 {
// When the values are empty the no_records_msg is shown, taking 1 line
return 1;
}
let rows = values / self.get_cols();
if values % self.get_cols() != 0 {
rows + 1
} else {
rows
}
}
/// Returns working details col width
fn get_width(&self) -> usize {
self.working_details.col_width
}
/// Reset menu position
fn reset_position(&mut self) {
self.col_pos = 0;
self.row_pos = 0;
self.skipped_rows = 0;
}
fn no_records_msg(&self, use_ansi_coloring: bool) -> String {
let msg = "TYPE TO START SEARCH";
if use_ansi_coloring {
format!(
"{}{}{}",
self.color.selected_text_style.prefix(),
msg,
RESET
)
} else {
msg.to_string()
}
}
/// Returns working details columns
fn get_cols(&self) -> u16 {
self.working_details.columns.max(1)
}
/// End of line for menu
fn end_of_line(&self, column: u16, index: usize) -> &str {
let is_last = index == self.values.len().saturating_sub(1);
if column == self.get_cols().saturating_sub(1) || is_last {
"\r\n"
} else {
""
}
}
/// Update list of examples from the actual value
fn update_examples(&mut self) {
self.examples = self
.get_value()
.and_then(|suggestion| suggestion.extra)
.unwrap_or_default();
self.example_index = None;
}
/// Creates default string that represents one suggestion from the menu
fn create_entry_string(
&self,
suggestion: &Suggestion,
index: usize,
column: u16,
empty_space: usize,
use_ansi_coloring: bool,
) -> String {
if use_ansi_coloring {
if index == self.index() {
format!(
"{}{}{}{:>empty$}{}",
self.color.selected_text_style.prefix(),
&suggestion.value,
RESET,
"",
self.end_of_line(column, index),
empty = empty_space,
)
} else {
format!(
"{}{}{}{:>empty$}{}",
self.color.text_style.prefix(),
&suggestion.value,
RESET,
"",
self.end_of_line(column, index),
empty = empty_space,
)
}
} else {
// If no ansi coloring is found, then the selection word is
// the line in uppercase
let (marker, empty_space) = if index == self.index() {
(">", empty_space.saturating_sub(1))
} else {
("", empty_space)
};
let line = format!(
"{}{}{:>empty$}{}",
marker,
&suggestion.value,
"",
self.end_of_line(column, index),
empty = empty_space,
);
if index == self.index() {
line.to_uppercase()
} else {
line
}
}
}
/// Description string with color
fn create_description_string(&self, use_ansi_coloring: bool) -> String {
let description = self
.get_value()
.and_then(|suggestion| suggestion.description)
.unwrap_or_default()
.lines()
.skip(self.skipped_rows)
.take(self.working_details.description_rows)
.collect::<Vec<&str>>()
.join("\r\n");
if use_ansi_coloring && !description.is_empty() {
format!(
"{}{}{}",
self.color.description_style.prefix(),
description,
RESET,
)
} else {
description
}
}
/// Selectable list of examples from the actual value
fn create_example_string(&self, use_ansi_coloring: bool) -> String {
if !self.show_examples {
return "".into();
}
let examples: String = self
.examples
.iter()
.enumerate()
.map(|(index, example)| {
if let Some(example_index) = self.example_index {
if index == example_index {
format!(
" {}{}{}\r\n",
self.color.selected_text_style.prefix(),
example,
RESET
)
} else {
format!(" {example}\r\n")
}
} else {
format!(" {example}\r\n")
}
})
.collect();
if examples.is_empty() {
"".into()
} else if use_ansi_coloring {
format!(
"{}\r\n\r\nExamples:\r\n{}{}",
self.color.description_style.prefix(),
RESET,
examples,
)
} else {
format!("\r\n\r\nExamples:\r\n{examples}",)
}
}
}
impl Menu for DescriptionMenu {
/// Menu name
fn name(&self) -> &str {
self.name.as_str()
}
/// Menu indicator
fn indicator(&self) -> &str {
self.marker.as_str()
}
/// Deactivates context menu
fn is_active(&self) -> bool {
self.active
}
/// The menu stays active even with one record
fn can_quick_complete(&self) -> bool {
false
}
/// The menu does not need to partially complete
fn can_partially_complete(
&mut self,
_values_updated: bool,
_editor: &mut Editor,
_completer: &mut dyn Completer,
) -> bool {
false
}
/// Selects what type of event happened with the menu
fn menu_event(&mut self, event: MenuEvent) {
match &event {
MenuEvent::Activate(_) => self.active = true,
MenuEvent::Deactivate => {
self.active = false;
self.input = None;
self.values = Vec::new();
}
_ => {}
};
self.event = Some(event);
}
/// Updates menu values
fn update_values(&mut self, editor: &mut Editor, completer: &mut dyn Completer) {
if self.only_buffer_difference {
if let Some(old_string) = &self.input {
let (start, input) = string_difference(editor.get_buffer(), old_string);
if !input.is_empty() {
self.reset_position();
self.values = completer.complete(input, start);
}
}
} else {
let trimmed_buffer = editor.get_buffer().replace('\n', " ");
self.values = completer.complete(
trimmed_buffer.as_str(),
editor.line_buffer().insertion_point(),
);
self.reset_position();
}
}
/// The working details for the menu changes based on the size of the lines
/// collected from the completer
fn update_working_details(
&mut self,
editor: &mut Editor,
completer: &mut dyn Completer,
painter: &Painter,
) {
if let Some(event) = self.event.take() {
// Updating all working parameters from the menu before executing any of the
// possible event
let max_width = self.get_values().iter().fold(0, |acc, suggestion| {
let str_len = suggestion.value.len() + self.default_details.col_padding;
if str_len > acc {
str_len
} else {
acc
}
});
// If no default width is found, then the total screen width is used to estimate
// the column width based on the default number of columns
let default_width = if let Some(col_width) = self.default_details.col_width {
col_width
} else {
let col_width = painter.screen_width() / self.default_details.columns;
col_width as usize
};
// Adjusting the working width of the column based the max line width found
// in the menu values
if max_width > default_width {
self.working_details.col_width = max_width;
} else {
self.working_details.col_width = default_width;
};
// The working columns is adjusted based on possible number of columns
// that could be fitted in the screen with the calculated column width
let possible_cols = painter.screen_width() / self.working_details.col_width as u16;
if possible_cols > self.default_details.columns {
self.working_details.columns = self.default_details.columns.max(1);
} else {
self.working_details.columns = possible_cols;
}
// Updating the working rows to display the description
if self.menu_required_lines(painter.screen_width()) <= painter.remaining_lines() {
self.working_details.description_rows = self.default_details.description_rows;
self.show_examples = true;
} else {
self.working_details.description_rows = painter
.remaining_lines()
.saturating_sub(self.default_details.selection_rows + 1)
as usize;
self.show_examples = false;
}
match event {
MenuEvent::Activate(_) => {
self.reset_position();
self.input = Some(editor.get_buffer().to_string());
self.update_values(editor, completer);
}
MenuEvent::Deactivate => self.active = false,
MenuEvent::Edit(_) => {
self.reset_position();
self.update_values(editor, completer);
self.update_examples()
}
MenuEvent::NextElement => {
self.skipped_rows = 0;
self.move_next();
self.update_examples();
}
MenuEvent::PreviousElement => {
self.skipped_rows = 0;
self.move_previous();
self.update_examples();
}
MenuEvent::MoveUp => {
if let Some(example_index) = self.example_index {
if let Some(index) = example_index.checked_sub(1) {
self.example_index = Some(index);
} else {
self.example_index = Some(self.examples.len().saturating_sub(1));
}
} else if !self.examples.is_empty() {
self.example_index = Some(0);
}
}
MenuEvent::MoveDown => {
if let Some(example_index) = self.example_index {
let index = example_index + 1;
if index < self.examples.len() {
self.example_index = Some(index);
} else {
self.example_index = Some(0);
}
} else if !self.examples.is_empty() {
self.example_index = Some(0);
}
}
MenuEvent::MoveLeft => self.skipped_rows = self.skipped_rows.saturating_sub(1),
MenuEvent::MoveRight => {
let skipped = self.skipped_rows + 1;
let description_rows = self
.get_value()
.and_then(|suggestion| suggestion.description)
.unwrap_or_default()
.lines()
.count();
let allowed_skips =
description_rows.saturating_sub(self.working_details.description_rows);
if skipped < allowed_skips {
self.skipped_rows = skipped;
} else {
self.skipped_rows = allowed_skips;
}
}
MenuEvent::PreviousPage | MenuEvent::NextPage => {}
}
}
}
/// The buffer gets replaced in the Span location
fn replace_in_buffer(&self, editor: &mut Editor) {
if let Some(Suggestion { value, span, .. }) = self.get_value() {
let start = span.start.min(editor.line_buffer().len());
let end = span.end.min(editor.line_buffer().len());
let replacement = if let Some(example_index) = self.example_index {
self.examples
.get(example_index)
.expect("the example index is always checked")
} else {
&value
};
editor.edit_buffer(
|lb| {
lb.replace_range(start..end, replacement);
let mut offset = lb.insertion_point();
offset += lb
.len()
.saturating_sub(end.saturating_sub(start))
.saturating_sub(start);
lb.set_insertion_point(offset);
},
UndoBehavior::CreateUndoPoint,
);
}
}
/// Minimum rows that should be displayed by the menu
fn min_rows(&self) -> u16 {
self.get_rows().min(self.min_rows)
}
/// Gets values from filler that will be displayed in the menu
fn get_values(&self) -> &[Suggestion] {
&self.values
}
fn menu_required_lines(&self, _terminal_columns: u16) -> u16 {
let example_lines = self
.examples
.iter()
.fold(0, |acc, example| example.lines().count() + acc);
self.default_details.selection_rows
+ self.default_details.description_rows as u16
+ example_lines as u16
+ 3
}
fn menu_string(&self, _available_lines: u16, use_ansi_coloring: bool) -> String {
if self.get_values().is_empty() {
self.no_records_msg(use_ansi_coloring)
} else {
// The skip values represent the number of lines that should be skipped
// while printing the menu
let available_lines = self.default_details.selection_rows;
let skip_values = if self.row_pos >= available_lines {
let skip_lines = self.row_pos.saturating_sub(available_lines) + 1;
(skip_lines * self.get_cols()) as usize
} else {
0
};
// It seems that crossterm prefers to have a complete string ready to be printed
// rather than looping through the values and printing multiple things
// This reduces the flickering when printing the menu
let available_values = (available_lines * self.get_cols()) as usize;
let selection_values: String = self
.get_values()
.iter()
.skip(skip_values)
.take(available_values)
.enumerate()
.map(|(index, suggestion)| {
// Correcting the enumerate index based on the number of skipped values
let index = index + skip_values;
let column = index as u16 % self.get_cols();
let empty_space = self.get_width().saturating_sub(suggestion.value.len());
self.create_entry_string(
suggestion,
index,
column,
empty_space,
use_ansi_coloring,
)
})
.collect();
format!(
"{}{}{}",
selection_values,
self.create_description_string(use_ansi_coloring),
self.create_example_string(use_ansi_coloring)
)
}
}
}

View File

@ -2,7 +2,8 @@ use nu_engine::documentation::get_flags_section;
use nu_protocol::{engine::EngineState, levenshtein_distance}; use nu_protocol::{engine::EngineState, levenshtein_distance};
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use reedline::{Completer, Suggestion}; use reedline::{Completer, Suggestion};
use std::{fmt::Write, sync::Arc}; use std::fmt::Write;
use std::sync::Arc;
pub struct NuHelpCompleter(Arc<EngineState>); pub struct NuHelpCompleter(Arc<EngineState>);
@ -12,50 +13,51 @@ impl NuHelpCompleter {
} }
fn completion_helper(&self, line: &str, pos: usize) -> Vec<Suggestion> { fn completion_helper(&self, line: &str, pos: usize) -> Vec<Suggestion> {
let full_commands = self.0.get_signatures_with_examples(false);
let folded_line = line.to_folded_case(); let folded_line = line.to_folded_case();
let mut commands = self //Vec<(Signature, Vec<Example>, bool, bool)> {
.0 let mut commands = full_commands
.get_decls_sorted(false) .iter()
.into_iter() .filter(|(sig, _, _, _, _)| {
.filter_map(|(_, decl_id)| { sig.name.to_folded_case().contains(&folded_line)
let decl = self.0.get_decl(decl_id); || sig.usage.to_folded_case().contains(&folded_line)
(decl.name().to_folded_case().contains(&folded_line) || sig
|| decl.usage().to_folded_case().contains(&folded_line) .search_terms
|| decl .iter()
.search_terms()
.into_iter()
.any(|term| term.to_folded_case().contains(&folded_line)) .any(|term| term.to_folded_case().contains(&folded_line))
|| decl.extra_usage().to_folded_case().contains(&folded_line)) || sig.extra_usage.to_folded_case().contains(&folded_line)
.then_some(decl)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
commands.sort_by_cached_key(|decl| levenshtein_distance(line, decl.name())); commands.sort_by(|(a, _, _, _, _), (b, _, _, _, _)| {
let a_distance = levenshtein_distance(line, &a.name);
let b_distance = levenshtein_distance(line, &b.name);
a_distance.cmp(&b_distance)
});
commands commands
.into_iter() .into_iter()
.map(|decl| { .map(|(sig, examples, _, _, _)| {
let mut long_desc = String::new(); let mut long_desc = String::new();
let usage = decl.usage(); let usage = &sig.usage;
if !usage.is_empty() { if !usage.is_empty() {
long_desc.push_str(usage); long_desc.push_str(usage);
long_desc.push_str("\r\n\r\n"); long_desc.push_str("\r\n\r\n");
} }
let extra_usage = decl.extra_usage(); let extra_usage = &sig.extra_usage;
if !extra_usage.is_empty() { if !extra_usage.is_empty() {
long_desc.push_str(extra_usage); long_desc.push_str(extra_usage);
long_desc.push_str("\r\n\r\n"); long_desc.push_str("\r\n\r\n");
} }
let sig = decl.signature();
let _ = write!(long_desc, "Usage:\r\n > {}\r\n", sig.call_signature()); let _ = write!(long_desc, "Usage:\r\n > {}\r\n", sig.call_signature());
if !sig.named.is_empty() { if !sig.named.is_empty() {
long_desc.push_str(&get_flags_section(Some(&*self.0.clone()), &sig, |v| { long_desc.push_str(&get_flags_section(Some(&*self.0.clone()), sig, |v| {
v.to_parsable_string(", ", &self.0.config) v.into_string_parsable(", ", &self.0.config)
})) }))
} }
@ -71,7 +73,7 @@ impl NuHelpCompleter {
let opt_suffix = if let Some(value) = &positional.default_value { let opt_suffix = if let Some(value) = &positional.default_value {
format!( format!(
" (optional, default: {})", " (optional, default: {})",
&value.to_parsable_string(", ", &self.0.config), &value.into_string_parsable(", ", &self.0.config),
) )
} else { } else {
(" (optional)").to_string() (" (optional)").to_string()
@ -92,20 +94,18 @@ impl NuHelpCompleter {
} }
} }
let extra: Vec<String> = decl let extra: Vec<String> = examples
.examples()
.iter() .iter()
.map(|example| example.example.replace('\n', "\r\n")) .map(|example| example.example.replace('\n', "\r\n"))
.collect(); .collect();
Suggestion { Suggestion {
value: decl.name().into(), value: sig.name.clone(),
description: Some(long_desc), description: Some(long_desc),
style: None,
extra: Some(extra), extra: Some(extra),
span: reedline::Span { span: reedline::Span {
start: pos - line.len(), start: pos,
end: pos, end: pos + line.len(),
}, },
append_whitespace: false, append_whitespace: false,
} }
@ -119,42 +119,3 @@ impl Completer for NuHelpCompleter {
self.completion_helper(line, pos) self.completion_helper(line, pos)
} }
} }
#[cfg(test)]
mod test {
use super::*;
use rstest::rstest;
#[rstest]
#[case("who", 5, 8, &["whoami"])]
#[case("hash", 1, 5, &["hash", "hash md5", "hash sha256"])]
#[case("into f", 0, 6, &["into float", "into filesize"])]
#[case("into nonexistent", 0, 16, &[])]
fn test_help_completer(
#[case] line: &str,
#[case] start: usize,
#[case] end: usize,
#[case] expected: &[&str],
) {
let engine_state =
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context());
let mut completer = NuHelpCompleter::new(engine_state.into());
let suggestions = completer.complete(line, end);
assert_eq!(
expected.len(),
suggestions.len(),
"expected {:?}, got {:?}",
expected,
suggestions
.iter()
.map(|s| s.value.clone())
.collect::<Vec<_>>()
);
for (exp, actual) in expected.iter().zip(suggestions) {
assert_eq!(exp, &actual.value);
assert_eq!(reedline::Span::new(start, end), actual.span);
}
}
}

View File

@ -1,6 +1,5 @@
use nu_engine::eval_block; use nu_engine::eval_block;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug,
engine::{EngineState, Stack}, engine::{EngineState, Stack},
IntoPipelineData, Span, Value, IntoPipelineData, Span, Value,
}; };
@ -28,7 +27,7 @@ impl NuMenuCompleter {
Self { Self {
block_id, block_id,
span, span,
stack: stack.reset_out_dest().capture(), stack,
engine_state, engine_state,
only_buffer_difference, only_buffer_difference,
} }
@ -56,10 +55,17 @@ impl Completer for NuMenuCompleter {
} }
let input = Value::nothing(self.span).into_pipeline_data(); let input = Value::nothing(self.span).into_pipeline_data();
let res = eval_block(
&self.engine_state,
&mut self.stack,
block,
input,
false,
false,
);
let res = eval_block::<WithoutDebug>(&self.engine_state, &mut self.stack, block, input); if let Ok(values) = res {
let values = values.into_value(self.span);
if let Ok(values) = res.and_then(|data| data.into_value(self.span)) {
convert_to_suggestions(values, line, pos, self.only_buffer_difference) convert_to_suggestions(values, line, pos, self.only_buffer_difference)
} else { } else {
Vec::new() Vec::new()
@ -77,12 +83,10 @@ fn convert_to_suggestions(
Value::Record { val, .. } => { Value::Record { val, .. } => {
let text = val let text = val
.get("value") .get("value")
.and_then(|val| val.coerce_string().ok()) .and_then(|val| val.as_string().ok())
.unwrap_or_else(|| "No value key".to_string()); .unwrap_or_else(|| "No value key".to_string());
let description = val let description = val.get("description").and_then(|val| val.as_string().ok());
.get("description")
.and_then(|val| val.coerce_string().ok());
let span = match val.get("span") { let span = match val.get("span") {
Some(Value::Record { val: span, .. }) => { Some(Value::Record { val: span, .. }) => {
@ -97,13 +101,9 @@ fn convert_to_suggestions(
} }
} }
_ => reedline::Span { _ => reedline::Span {
start: if only_buffer_difference { start: if only_buffer_difference { pos } else { 0 },
pos - line.len()
} else {
0
},
end: if only_buffer_difference { end: if only_buffer_difference {
pos pos + line.len()
} else { } else {
line.len() line.len()
}, },
@ -111,13 +111,9 @@ fn convert_to_suggestions(
} }
} }
_ => reedline::Span { _ => reedline::Span {
start: if only_buffer_difference { start: if only_buffer_difference { pos } else { 0 },
pos - line.len()
} else {
0
},
end: if only_buffer_difference { end: if only_buffer_difference {
pos pos + line.len()
} else { } else {
line.len() line.len()
}, },
@ -142,7 +138,6 @@ fn convert_to_suggestions(
vec![Suggestion { vec![Suggestion {
value: text, value: text,
description, description,
style: None,
extra, extra,
span, span,
append_whitespace: false, append_whitespace: false,
@ -155,19 +150,10 @@ fn convert_to_suggestions(
_ => vec![Suggestion { _ => vec![Suggestion {
value: format!("Not a record: {value:?}"), value: format!("Not a record: {value:?}"),
description: None, description: None,
style: None,
extra: None, extra: None,
span: reedline::Span { span: reedline::Span {
start: if only_buffer_difference { start: 0,
pos - line.len() end: line.len(),
} else {
0
},
end: if only_buffer_difference {
pos
} else {
line.len()
},
}, },
append_whitespace: false, append_whitespace: false,
}], }],

View File

@ -1,5 +1,7 @@
mod description_menu;
mod help_completions; mod help_completions;
mod menu_completions; mod menu_completions;
pub use description_menu::DescriptionMenu;
pub use help_completions::NuHelpCompleter; pub use help_completions::NuHelpCompleter;
pub use menu_completions::NuMenuCompleter; pub use menu_completions::NuMenuCompleter;

View File

@ -1,5 +1,7 @@
use nu_engine::command_prelude::*; use nu_protocol::ast::Call;
use reedline::{Highlighter, StyledText}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Type, Value};
use reedline::Highlighter;
#[derive(Clone)] #[derive(Clone)]
pub struct NuHighlight; pub struct NuHighlight;
@ -26,7 +28,7 @@ impl Command for NuHighlight {
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> {
@ -38,14 +40,14 @@ impl Command for NuHighlight {
let highlighter = crate::NuHighlighter { let highlighter = crate::NuHighlighter {
engine_state, engine_state,
stack: std::sync::Arc::new(stack.clone()),
config, config,
}; };
input.map( input.map(
move |x| match x.coerce_into_string() { move |x| match x.as_string() {
Ok(line) => { Ok(line) => {
let highlights = highlighter.highlight(&line, line.len()); let highlights = highlighter.highlight(&line, line.len());
Value::string(highlights.render_simple(), head) Value::string(highlights.render_simple(), head)
} }
Err(err) => Value::error(err, head), Err(err) => Value::error(err, head),
@ -62,16 +64,3 @@ impl Command for NuHighlight {
}] }]
} }
} }
/// A highlighter that does nothing
///
/// Used to remove highlighting from a reedline instance
/// (letting NuHighlighter structs be dropped)
#[derive(Default)]
pub struct NoOpHighlighter {}
impl Highlighter for NoOpHighlighter {
fn highlight(&self, _line: &str, _cursor: usize) -> reedline::StyledText {
StyledText::new()
}
}

View File

@ -1,4 +1,10 @@
use nu_engine::command_prelude::*; use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Print; pub struct Print;
@ -48,8 +54,8 @@ Since this command has no output, there is no point in piping it with other comm
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let args: Vec<Value> = call.rest(engine_state, stack, 0)?; let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
let no_newline = call.has_flag(engine_state, stack, "no-newline")?; let no_newline = call.has_flag("no-newline");
let to_stderr = call.has_flag(engine_state, stack, "stderr")?; let to_stderr = call.has_flag("stderr");
// This will allow for easy printing of pipelines as well // This will allow for easy printing of pipelines as well
if !args.is_empty() { if !args.is_empty() {

View File

@ -1,23 +1,16 @@
use crate::prompt_update::{
POST_PROMPT_MARKER, PRE_PROMPT_MARKER, VSCODE_POST_PROMPT_MARKER, VSCODE_PRE_PROMPT_MARKER,
};
use nu_protocol::{
engine::{EngineState, Stack},
Value,
};
#[cfg(windows)] #[cfg(windows)]
use nu_utils::enable_vt_processing; use nu_utils::enable_vt_processing;
use reedline::{ use reedline::DefaultPrompt;
DefaultPrompt, Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, use {
PromptViMode, reedline::{
Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, PromptViMode,
},
std::borrow::Cow,
}; };
use std::borrow::Cow;
/// Nushell prompt definition /// Nushell prompt definition
#[derive(Clone)] #[derive(Clone)]
pub struct NushellPrompt { pub struct NushellPrompt {
shell_integration_osc133: bool,
shell_integration_osc633: bool,
left_prompt_string: Option<String>, left_prompt_string: Option<String>,
right_prompt_string: Option<String>, right_prompt_string: Option<String>,
default_prompt_indicator: Option<String>, default_prompt_indicator: Option<String>,
@ -25,20 +18,17 @@ pub struct NushellPrompt {
default_vi_normal_prompt_indicator: Option<String>, default_vi_normal_prompt_indicator: Option<String>,
default_multiline_indicator: Option<String>, default_multiline_indicator: Option<String>,
render_right_prompt_on_last_line: bool, render_right_prompt_on_last_line: bool,
engine_state: EngineState, }
stack: Stack,
impl Default for NushellPrompt {
fn default() -> Self {
NushellPrompt::new()
}
} }
impl NushellPrompt { impl NushellPrompt {
pub fn new( pub fn new() -> NushellPrompt {
shell_integration_osc133: bool,
shell_integration_osc633: bool,
engine_state: EngineState,
stack: Stack,
) -> NushellPrompt {
NushellPrompt { NushellPrompt {
shell_integration_osc133,
shell_integration_osc633,
left_prompt_string: None, left_prompt_string: None,
right_prompt_string: None, right_prompt_string: None,
default_prompt_indicator: None, default_prompt_indicator: None,
@ -46,8 +36,6 @@ impl NushellPrompt {
default_vi_normal_prompt_indicator: None, default_vi_normal_prompt_indicator: None,
default_multiline_indicator: None, default_multiline_indicator: None,
render_right_prompt_on_last_line: false, render_right_prompt_on_last_line: false,
engine_state,
stack,
} }
} }
@ -123,23 +111,7 @@ impl Prompt for NushellPrompt {
.to_string() .to_string()
.replace('\n', "\r\n"); .replace('\n', "\r\n");
if self.shell_integration_osc633 { prompt.into()
if self.stack.get_env_var(&self.engine_state, "TERM_PROGRAM")
== Some(Value::test_string("vscode"))
{
// We're in vscode and we have osc633 enabled
format!("{VSCODE_PRE_PROMPT_MARKER}{prompt}{VSCODE_POST_PROMPT_MARKER}").into()
} else if self.shell_integration_osc133 {
// If we're in VSCode but we don't find the env var, but we have osc133 set, then use it
format!("{PRE_PROMPT_MARKER}{prompt}{POST_PROMPT_MARKER}").into()
} else {
prompt.into()
}
} else if self.shell_integration_osc133 {
format!("{PRE_PROMPT_MARKER}{prompt}{POST_PROMPT_MARKER}").into()
} else {
prompt.into()
}
} }
} }

View File

@ -1,11 +1,14 @@
use crate::NushellPrompt; use crate::NushellPrompt;
use log::trace; use log::trace;
use nu_engine::ClosureEvalOnce; use nu_engine::eval_subexpression;
use nu_protocol::report_error;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{EngineState, Stack, StateWorkingSet},
report_error_new, Config, PipelineData, Value, Config, PipelineData, Value,
}; };
use reedline::Prompt; use reedline::Prompt;
use std::borrow::Cow;
use std::sync::Arc;
// Name of environment variable where the prompt could be stored // Name of environment variable where the prompt could be stored
pub(crate) const PROMPT_COMMAND: &str = "PROMPT_COMMAND"; pub(crate) const PROMPT_COMMAND: &str = "PROMPT_COMMAND";
@ -23,37 +26,10 @@ pub(crate) const TRANSIENT_PROMPT_INDICATOR_VI_NORMAL: &str =
"TRANSIENT_PROMPT_INDICATOR_VI_NORMAL"; "TRANSIENT_PROMPT_INDICATOR_VI_NORMAL";
pub(crate) const TRANSIENT_PROMPT_MULTILINE_INDICATOR: &str = pub(crate) const TRANSIENT_PROMPT_MULTILINE_INDICATOR: &str =
"TRANSIENT_PROMPT_MULTILINE_INDICATOR"; "TRANSIENT_PROMPT_MULTILINE_INDICATOR";
// Store all these Ansi Escape Markers here so they can be reused easily
// According to Daniel Imms @Tyriar, we need to do these this way: // According to Daniel Imms @Tyriar, we need to do these this way:
// <133 A><prompt><133 B><command><133 C><command output> // <133 A><prompt><133 B><command><133 C><command output>
pub(crate) const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\"; const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
pub(crate) const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\"; const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
pub(crate) const PRE_EXECUTION_MARKER: &str = "\x1b]133;C\x1b\\";
#[allow(dead_code)]
pub(crate) const POST_EXECUTION_MARKER_PREFIX: &str = "\x1b]133;D;";
#[allow(dead_code)]
pub(crate) const POST_EXECUTION_MARKER_SUFFIX: &str = "\x1b\\";
// OSC633 is the same as OSC133 but specifically for VSCode
pub(crate) const VSCODE_PRE_PROMPT_MARKER: &str = "\x1b]633;A\x1b\\";
pub(crate) const VSCODE_POST_PROMPT_MARKER: &str = "\x1b]633;B\x1b\\";
#[allow(dead_code)]
pub(crate) const VSCODE_PRE_EXECUTION_MARKER: &str = "\x1b]633;C\x1b\\";
#[allow(dead_code)]
//"\x1b]633;D;{}\x1b\\"
pub(crate) const VSCODE_POST_EXECUTION_MARKER_PREFIX: &str = "\x1b]633;D;";
#[allow(dead_code)]
pub(crate) const VSCODE_POST_EXECUTION_MARKER_SUFFIX: &str = "\x1b\\";
#[allow(dead_code)]
pub(crate) const VSCODE_COMMANDLINE_MARKER: &str = "\x1b]633;E\x1b\\";
#[allow(dead_code)]
// "\x1b]633;P;Cwd={}\x1b\\"
pub(crate) const VSCODE_CWD_PROPERTY_MARKER_PREFIX: &str = "\x1b]633;P;Cwd=";
#[allow(dead_code)]
pub(crate) const VSCODE_CWD_PROPERTY_MARKER_SUFFIX: &str = "\x1b\\";
pub(crate) const RESET_APPLICATION_MODE: &str = "\x1b[?1l";
fn get_prompt_string( fn get_prompt_string(
prompt: &str, prompt: &str,
@ -65,9 +41,11 @@ fn get_prompt_string(
.get_env_var(engine_state, prompt) .get_env_var(engine_state, prompt)
.and_then(|v| match v { .and_then(|v| match v {
Value::Closure { val, .. } => { Value::Closure { val, .. } => {
let result = ClosureEvalOnce::new(engine_state, stack, *val) let block = engine_state.get_block(val.block_id);
.run_with_input(PipelineData::Empty); let mut stack = stack.captures_to_stack(val.captures);
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
let ret_val =
eval_subexpression(engine_state, &mut stack, block, PipelineData::empty());
trace!( trace!(
"get_prompt_string (block) {}:{}:{}", "get_prompt_string (block) {}:{}:{}",
file!(), file!(),
@ -75,9 +53,28 @@ fn get_prompt_string(
column!() column!()
); );
result ret_val
.map_err(|err| { .map_err(|err| {
report_error_new(engine_state, &err); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
})
.ok()
}
Value::Block { val: block_id, .. } => {
let block = engine_state.get_block(block_id);
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
let ret_val = eval_subexpression(engine_state, stack, block, PipelineData::empty());
trace!(
"get_prompt_string (block) {}:{}:{}",
file!(),
line!(),
column!()
);
ret_val
.map_err(|err| {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
}) })
.ok() .ok()
} }
@ -101,54 +98,44 @@ fn get_prompt_string(
}) })
} }
pub(crate) fn update_prompt( pub(crate) fn update_prompt<'prompt>(
config: &Config, config: &Config,
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &Stack,
nu_prompt: &mut NushellPrompt, nu_prompt: &'prompt mut NushellPrompt,
) { ) -> &'prompt dyn Prompt {
let configured_left_prompt_string = let mut stack = stack.clone();
match get_prompt_string(PROMPT_COMMAND, config, engine_state, stack) {
Some(s) => s, let left_prompt_string = get_prompt_string(PROMPT_COMMAND, config, engine_state, &mut stack);
None => "".to_string(),
};
// Now that we have the prompt string lets ansify it. // Now that we have the prompt string lets ansify it.
// <133 A><prompt><133 B><command><133 C><command output> // <133 A><prompt><133 B><command><133 C><command output>
let left_prompt_string = if config.shell_integration_osc633 { let left_prompt_string = if config.shell_integration {
if stack.get_env_var(engine_state, "TERM_PROGRAM") == Some(Value::test_string("vscode")) { if let Some(prompt_string) = left_prompt_string {
// We're in vscode and we have osc633 enabled
Some(format!( Some(format!(
"{VSCODE_PRE_PROMPT_MARKER}{configured_left_prompt_string}{VSCODE_POST_PROMPT_MARKER}" "{PRE_PROMPT_MARKER}{prompt_string}{POST_PROMPT_MARKER}"
))
} else if config.shell_integration_osc133 {
// If we're in VSCode but we don't find the env var, but we have osc133 set, then use it
Some(format!(
"{PRE_PROMPT_MARKER}{configured_left_prompt_string}{POST_PROMPT_MARKER}"
)) ))
} else { } else {
configured_left_prompt_string.into() left_prompt_string
} }
} else if config.shell_integration_osc133 {
Some(format!(
"{PRE_PROMPT_MARKER}{configured_left_prompt_string}{POST_PROMPT_MARKER}"
))
} else { } else {
configured_left_prompt_string.into() left_prompt_string
}; };
let right_prompt_string = get_prompt_string(PROMPT_COMMAND_RIGHT, config, engine_state, stack); let right_prompt_string =
get_prompt_string(PROMPT_COMMAND_RIGHT, config, engine_state, &mut stack);
let prompt_indicator_string = get_prompt_string(PROMPT_INDICATOR, config, engine_state, stack); let prompt_indicator_string =
get_prompt_string(PROMPT_INDICATOR, config, engine_state, &mut stack);
let prompt_multiline_string = let prompt_multiline_string =
get_prompt_string(PROMPT_MULTILINE_INDICATOR, config, engine_state, stack); get_prompt_string(PROMPT_MULTILINE_INDICATOR, config, engine_state, &mut stack);
let prompt_vi_insert_string = let prompt_vi_insert_string =
get_prompt_string(PROMPT_INDICATOR_VI_INSERT, config, engine_state, stack); get_prompt_string(PROMPT_INDICATOR_VI_INSERT, config, engine_state, &mut stack);
let prompt_vi_normal_string = let prompt_vi_normal_string =
get_prompt_string(PROMPT_INDICATOR_VI_NORMAL, config, engine_state, stack); get_prompt_string(PROMPT_INDICATOR_VI_NORMAL, config, engine_state, &mut stack);
// apply the other indicators // apply the other indicators
nu_prompt.update_all_prompt_strings( nu_prompt.update_all_prompt_strings(
@ -159,55 +146,125 @@ pub(crate) fn update_prompt(
(prompt_vi_insert_string, prompt_vi_normal_string), (prompt_vi_insert_string, prompt_vi_normal_string),
config.render_right_prompt_on_last_line, config.render_right_prompt_on_last_line,
); );
let ret_val = nu_prompt as &dyn Prompt;
trace!("update_prompt {}:{}:{}", file!(), line!(), column!()); trace!("update_prompt {}:{}:{}", file!(), line!(), column!());
ret_val
} }
/// Construct the transient prompt based on the normal nu_prompt struct TransientPrompt {
pub(crate) fn make_transient_prompt( engine_state: Arc<EngineState>,
stack: Stack,
}
/// Try getting `$env.TRANSIENT_PROMPT_<X>`, and get `$env.PROMPT_<X>` if that fails
fn get_transient_prompt_string(
transient_prompt: &str,
prompt: &str,
config: &Config, config: &Config,
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
nu_prompt: &NushellPrompt, ) -> Option<String> {
) -> Box<dyn Prompt> { get_prompt_string(transient_prompt, config, engine_state, stack)
let mut nu_prompt = nu_prompt.clone(); .or_else(|| get_prompt_string(prompt, config, engine_state, stack))
}
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_COMMAND, config, engine_state, stack) {
nu_prompt.update_prompt_left(Some(s)) impl Prompt for TransientPrompt {
} fn render_prompt_left(&self) -> Cow<str> {
let mut nu_prompt = NushellPrompt::new();
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_COMMAND_RIGHT, config, engine_state, stack) let config = &self.engine_state.get_config().clone();
{ let mut stack = self.stack.clone();
nu_prompt.update_prompt_right(Some(s), config.render_right_prompt_on_last_line) nu_prompt.update_prompt_left(get_transient_prompt_string(
} TRANSIENT_PROMPT_COMMAND,
PROMPT_COMMAND,
if let Some(s) = get_prompt_string(TRANSIENT_PROMPT_INDICATOR, config, engine_state, stack) { config,
nu_prompt.update_prompt_indicator(Some(s)) &self.engine_state,
} &mut stack,
if let Some(s) = get_prompt_string( ));
TRANSIENT_PROMPT_INDICATOR_VI_INSERT, nu_prompt.render_prompt_left().to_string().into()
config, }
engine_state,
stack, fn render_prompt_right(&self) -> Cow<str> {
) { let mut nu_prompt = NushellPrompt::new();
nu_prompt.update_prompt_vi_insert(Some(s)) let config = &self.engine_state.get_config().clone();
} let mut stack = self.stack.clone();
if let Some(s) = get_prompt_string( nu_prompt.update_prompt_right(
TRANSIENT_PROMPT_INDICATOR_VI_NORMAL, get_transient_prompt_string(
config, TRANSIENT_PROMPT_COMMAND_RIGHT,
engine_state, PROMPT_COMMAND_RIGHT,
stack, config,
) { &self.engine_state,
nu_prompt.update_prompt_vi_normal(Some(s)) &mut stack,
} ),
config.render_right_prompt_on_last_line,
if let Some(s) = get_prompt_string( );
TRANSIENT_PROMPT_MULTILINE_INDICATOR, nu_prompt.render_prompt_right().to_string().into()
config, }
engine_state,
stack, fn render_prompt_indicator(&self, prompt_mode: reedline::PromptEditMode) -> Cow<str> {
) { let mut nu_prompt = NushellPrompt::new();
nu_prompt.update_prompt_multiline(Some(s)) let config = &self.engine_state.get_config().clone();
} let mut stack = self.stack.clone();
nu_prompt.update_prompt_indicator(get_transient_prompt_string(
Box::new(nu_prompt) TRANSIENT_PROMPT_INDICATOR,
PROMPT_INDICATOR,
config,
&self.engine_state,
&mut stack,
));
nu_prompt.update_prompt_vi_insert(get_transient_prompt_string(
TRANSIENT_PROMPT_INDICATOR_VI_INSERT,
PROMPT_INDICATOR_VI_INSERT,
config,
&self.engine_state,
&mut stack,
));
nu_prompt.update_prompt_vi_normal(get_transient_prompt_string(
TRANSIENT_PROMPT_INDICATOR_VI_NORMAL,
PROMPT_INDICATOR_VI_NORMAL,
config,
&self.engine_state,
&mut stack,
));
nu_prompt
.render_prompt_indicator(prompt_mode)
.to_string()
.into()
}
fn render_prompt_multiline_indicator(&self) -> Cow<str> {
let mut nu_prompt = NushellPrompt::new();
let config = &self.engine_state.get_config().clone();
let mut stack = self.stack.clone();
nu_prompt.update_prompt_multiline(get_transient_prompt_string(
TRANSIENT_PROMPT_MULTILINE_INDICATOR,
PROMPT_MULTILINE_INDICATOR,
config,
&self.engine_state,
&mut stack,
));
nu_prompt
.render_prompt_multiline_indicator()
.to_string()
.into()
}
fn render_prompt_history_search_indicator(
&self,
history_search: reedline::PromptHistorySearch,
) -> Cow<str> {
NushellPrompt::new()
.render_prompt_history_search_indicator(history_search)
.to_string()
.into()
}
}
/// Construct the transient prompt
pub(crate) fn transient_prompt(engine_state: Arc<EngineState>, stack: &Stack) -> Box<dyn Prompt> {
Box::new(TransientPrompt {
engine_state,
stack: stack.clone(),
})
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,15 @@
use log::trace; use log::trace;
use nu_ansi_term::Style; use nu_ansi_term::Style;
use nu_color_config::{get_matching_brackets_style, get_shape_color}; use nu_color_config::{get_matching_brackets_style, get_shape_color};
use nu_engine::env;
use nu_parser::{flatten_block, parse, FlatShape}; use nu_parser::{flatten_block, parse, FlatShape};
use nu_protocol::{ use nu_protocol::ast::{Argument, Block, Expr, Expression, PipelineElement};
ast::{Block, Expr, Expression, PipelineRedirection, RecordItem}, use nu_protocol::engine::{EngineState, StateWorkingSet};
engine::{EngineState, Stack, StateWorkingSet}, use nu_protocol::{Config, Span};
Config, Span,
};
use reedline::{Highlighter, StyledText}; use reedline::{Highlighter, StyledText};
use std::sync::Arc; use std::sync::Arc;
pub struct NuHighlighter { pub struct NuHighlighter {
pub engine_state: Arc<EngineState>, pub engine_state: Arc<EngineState>,
pub stack: Arc<Stack>,
pub config: Config, pub config: Config,
} }
@ -21,38 +17,10 @@ impl Highlighter for NuHighlighter {
fn highlight(&self, line: &str, _cursor: usize) -> StyledText { fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
trace!("highlighting: {}", line); trace!("highlighting: {}", line);
let highlight_resolved_externals =
self.engine_state.get_config().highlight_resolved_externals;
let mut working_set = StateWorkingSet::new(&self.engine_state); let mut working_set = StateWorkingSet::new(&self.engine_state);
let block = parse(&mut working_set, None, line.as_bytes(), false); let block = parse(&mut working_set, None, line.as_bytes(), false);
let (shapes, global_span_offset) = { let (shapes, global_span_offset) = {
let mut shapes = flatten_block(&working_set, &block); let shapes = flatten_block(&working_set, &block);
// Highlighting externals has a config point because of concerns that using which to resolve
// externals may slow down things too much.
if highlight_resolved_externals {
for (span, shape) in shapes.iter_mut() {
if *shape == FlatShape::External {
let str_contents =
working_set.get_span_contents(Span::new(span.start, span.end));
let str_word = String::from_utf8_lossy(str_contents).to_string();
let paths = env::path_str(&self.engine_state, &self.stack, *span).ok();
#[allow(deprecated)]
let res = if let Ok(cwd) =
env::current_dir_str(&self.engine_state, &self.stack)
{
which::which_in(str_word, paths.as_ref(), cwd).ok()
} else {
which::which_in_global(str_word, paths.as_ref())
.ok()
.and_then(|mut i| i.next())
};
if res.is_some() {
*shape = FlatShape::ExternalResolved;
}
}
}
}
(shapes, self.engine_state.next_span_start()) (shapes, self.engine_state.next_span_start())
}; };
@ -87,8 +55,29 @@ impl Highlighter for NuHighlighter {
[(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)] [(shape.0.start - global_span_offset)..(shape.0.end - global_span_offset)]
.to_string(); .to_string();
macro_rules! add_colored_token_with_bracket_highlight {
($shape:expr, $span:expr, $text:expr) => {{
let spans = split_span_by_highlight_positions(
line,
$span,
&matching_brackets_pos,
global_span_offset,
);
spans.iter().for_each(|(part, highlight)| {
let start = part.start - $span.start;
let end = part.end - $span.start;
let text = (&next_token[start..end]).to_string();
let mut style = get_shape_color($shape.to_string(), &self.config);
if *highlight {
style = get_matching_brackets_style(style, &self.config);
}
output.push((style, text));
});
}};
}
let mut add_colored_token = |shape: &FlatShape, text: String| { let mut add_colored_token = |shape: &FlatShape, text: String| {
output.push((get_shape_color(shape.as_str(), &self.config), text)); output.push((get_shape_color(shape.to_string(), &self.config), text));
}; };
match shape.1 { match shape.1 {
@ -102,38 +91,28 @@ impl Highlighter for NuHighlighter {
FlatShape::InternalCall(_) => add_colored_token(&shape.1, next_token), FlatShape::InternalCall(_) => add_colored_token(&shape.1, next_token),
FlatShape::External => add_colored_token(&shape.1, next_token), FlatShape::External => add_colored_token(&shape.1, next_token),
FlatShape::ExternalArg => add_colored_token(&shape.1, next_token), FlatShape::ExternalArg => add_colored_token(&shape.1, next_token),
FlatShape::ExternalResolved => add_colored_token(&shape.1, next_token),
FlatShape::Keyword => add_colored_token(&shape.1, next_token), FlatShape::Keyword => add_colored_token(&shape.1, next_token),
FlatShape::Literal => add_colored_token(&shape.1, next_token), FlatShape::Literal => add_colored_token(&shape.1, next_token),
FlatShape::Operator => add_colored_token(&shape.1, next_token), FlatShape::Operator => add_colored_token(&shape.1, next_token),
FlatShape::Signature => add_colored_token(&shape.1, next_token), FlatShape::Signature => add_colored_token(&shape.1, next_token),
FlatShape::String => add_colored_token(&shape.1, next_token), FlatShape::String => add_colored_token(&shape.1, next_token),
FlatShape::RawString => add_colored_token(&shape.1, next_token),
FlatShape::StringInterpolation => add_colored_token(&shape.1, next_token), FlatShape::StringInterpolation => add_colored_token(&shape.1, next_token),
FlatShape::DateTime => add_colored_token(&shape.1, next_token), FlatShape::DateTime => add_colored_token(&shape.1, next_token),
FlatShape::List FlatShape::List => {
| FlatShape::Table add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
| FlatShape::Record }
| FlatShape::Block FlatShape::Table => {
| FlatShape::Closure => { add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
let span = shape.0; }
let shape = &shape.1; FlatShape::Record => {
let spans = split_span_by_highlight_positions( add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
line, }
span,
&matching_brackets_pos, FlatShape::Block => {
global_span_offset, add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
); }
for (part, highlight) in spans { FlatShape::Closure => {
let start = part.start - span.start; add_colored_token_with_bracket_highlight!(shape.1, shape.0, next_token)
let end = part.end - span.start;
let text = next_token[start..end].to_string();
let mut style = get_shape_color(shape.as_str(), &self.config);
if highlight {
style = get_matching_brackets_style(style, &self.config);
}
output.push((style, text));
}
} }
FlatShape::Filepath => add_colored_token(&shape.1, next_token), FlatShape::Filepath => add_colored_token(&shape.1, next_token),
@ -253,38 +232,24 @@ fn find_matching_block_end_in_block(
) -> Option<usize> { ) -> Option<usize> {
for p in &block.pipelines { for p in &block.pipelines {
for e in &p.elements { for e in &p.elements {
if e.expr.span.contains(global_cursor_offset) { match e {
if let Some(pos) = find_matching_block_end_in_expr( PipelineElement::Expression(_, e)
line, | PipelineElement::Redirection(_, _, e)
working_set, | PipelineElement::And(_, e)
&e.expr, | PipelineElement::Or(_, e)
global_span_offset, | PipelineElement::SameTargetRedirection { cmd: (_, e), .. }
global_cursor_offset, | PipelineElement::SeparateRedirection { out: (_, e), .. } => {
) { if e.span.contains(global_cursor_offset) {
return Some(pos); if let Some(pos) = find_matching_block_end_in_expr(
} line,
} working_set,
e,
if let Some(redirection) = e.redirection.as_ref() { global_span_offset,
match redirection { global_cursor_offset,
PipelineRedirection::Single { target, .. } ) {
| PipelineRedirection::Separate { out: target, .. }
| PipelineRedirection::Separate { err: target, .. }
if target.span().contains(global_cursor_offset) =>
{
if let Some(pos) = target.expr().and_then(|expr| {
find_matching_block_end_in_expr(
line,
working_set,
expr,
global_span_offset,
global_cursor_offset,
)
}) {
return Some(pos); return Some(pos);
} }
} }
_ => {}
} }
} }
} }
@ -299,6 +264,20 @@ fn find_matching_block_end_in_expr(
global_span_offset: usize, global_span_offset: usize,
global_cursor_offset: usize, global_cursor_offset: usize,
) -> Option<usize> { ) -> Option<usize> {
macro_rules! find_in_expr_or_continue {
($inner_expr:ident) => {
if let Some(pos) = find_matching_block_end_in_expr(
line,
working_set,
$inner_expr,
global_span_offset,
global_cursor_offset,
) {
return Some(pos);
}
};
}
if expression.span.contains(global_cursor_offset) && expression.span.start >= global_span_offset if expression.span.contains(global_cursor_offset) && expression.span.start >= global_span_offset
{ {
let expr_first = expression.span.start; let expr_first = expression.span.start;
@ -324,20 +303,20 @@ fn find_matching_block_end_in_expr(
Expr::Keyword(..) => None, Expr::Keyword(..) => None,
Expr::ValueWithUnit(..) => None, Expr::ValueWithUnit(..) => None,
Expr::DateTime(_) => None, Expr::DateTime(_) => None,
Expr::Filepath(_, _) => None, Expr::Filepath(_) => None,
Expr::Directory(_, _) => None, Expr::Directory(_) => None,
Expr::GlobPattern(_, _) => None, Expr::GlobPattern(_) => None,
Expr::String(_) => None, Expr::String(_) => None,
Expr::RawString(_) => None,
Expr::CellPath(_) => None, Expr::CellPath(_) => None,
Expr::ImportPattern(_) => None, Expr::ImportPattern(_) => None,
Expr::Overlay(_) => None, Expr::Overlay(_) => None,
Expr::Signature(_) => None, Expr::Signature(_) => None,
Expr::MatchPattern(_) => None,
Expr::MatchBlock(_) => None, Expr::MatchBlock(_) => None,
Expr::Nothing => None, Expr::Nothing => None,
Expr::Garbage => None, Expr::Garbage => None,
Expr::Table(table) => { Expr::Table(hdr, rows) => {
if expr_last == global_cursor_offset { if expr_last == global_cursor_offset {
// cursor is at table end // cursor is at table end
Some(expr_first) Some(expr_first)
@ -346,19 +325,15 @@ fn find_matching_block_end_in_expr(
Some(expr_last) Some(expr_last)
} else { } else {
// cursor is inside table // cursor is inside table
table for inner_expr in hdr {
.columns find_in_expr_or_continue!(inner_expr);
.iter() }
.chain(table.rows.iter().flat_map(AsRef::as_ref)) for row in rows {
.find_map(|expr| { for inner_expr in row {
find_matching_block_end_in_expr( find_in_expr_or_continue!(inner_expr);
line, }
working_set, }
expr, None
global_span_offset,
global_cursor_offset,
)
})
} }
} }
@ -371,45 +346,28 @@ fn find_matching_block_end_in_expr(
Some(expr_last) Some(expr_last)
} else { } else {
// cursor is inside record // cursor is inside record
exprs.iter().find_map(|expr| match expr { for (k, v) in exprs {
RecordItem::Pair(k, v) => find_matching_block_end_in_expr( find_in_expr_or_continue!(k);
line, find_in_expr_or_continue!(v);
working_set, }
k, None
global_span_offset,
global_cursor_offset,
)
.or_else(|| {
find_matching_block_end_in_expr(
line,
working_set,
v,
global_span_offset,
global_cursor_offset,
)
}),
RecordItem::Spread(_, record) => find_matching_block_end_in_expr(
line,
working_set,
record,
global_span_offset,
global_cursor_offset,
),
})
} }
} }
Expr::Call(call) => call.arguments.iter().find_map(|arg| { Expr::Call(call) => {
arg.expr().and_then(|expr| { for arg in &call.arguments {
find_matching_block_end_in_expr( let opt_expr = match arg {
line, Argument::Named((_, _, opt_expr)) => opt_expr.as_ref(),
working_set, Argument::Positional(inner_expr) => Some(inner_expr),
expr, Argument::Unknown(inner_expr) => Some(inner_expr),
global_span_offset, };
global_cursor_offset,
) if let Some(inner_expr) = opt_expr {
}) find_in_expr_or_continue!(inner_expr);
}), }
}
None
}
Expr::FullCellPath(b) => find_matching_block_end_in_expr( Expr::FullCellPath(b) => find_matching_block_end_in_expr(
line, line,
@ -419,15 +377,12 @@ fn find_matching_block_end_in_expr(
global_cursor_offset, global_cursor_offset,
), ),
Expr::BinaryOp(lhs, op, rhs) => [lhs, op, rhs].into_iter().find_map(|expr| { Expr::BinaryOp(lhs, op, rhs) => {
find_matching_block_end_in_expr( find_in_expr_or_continue!(lhs);
line, find_in_expr_or_continue!(op);
working_set, find_in_expr_or_continue!(rhs);
expr, None
global_span_offset, }
global_cursor_offset,
)
}),
Expr::Block(block_id) Expr::Block(block_id)
| Expr::Closure(block_id) | Expr::Closure(block_id)
@ -452,17 +407,14 @@ fn find_matching_block_end_in_expr(
} }
} }
Expr::StringInterpolation(exprs) => exprs.iter().find_map(|expr| { Expr::StringInterpolation(inner_expr) => {
find_matching_block_end_in_expr( for inner_expr in inner_expr {
line, find_in_expr_or_continue!(inner_expr);
working_set, }
expr, None
global_span_offset, }
global_cursor_offset,
)
}),
Expr::List(list) => { Expr::List(inner_expr) => {
if expr_last == global_cursor_offset { if expr_last == global_cursor_offset {
// cursor is at list end // cursor is at list end
Some(expr_first) Some(expr_first)
@ -470,15 +422,11 @@ fn find_matching_block_end_in_expr(
// cursor is at list start // cursor is at list start
Some(expr_last) Some(expr_last)
} else { } else {
list.iter().find_map(|item| { // cursor is inside list
find_matching_block_end_in_expr( for inner_expr in inner_expr {
line, find_in_expr_or_continue!(inner_expr);
working_set, }
item.expr(), None
global_span_offset,
global_cursor_offset,
)
})
} }
} }
}; };

View File

@ -1,11 +1,12 @@
use nu_cmd_base::hook::eval_hook; use nu_cmd_base::hook::eval_hook;
use nu_engine::{eval_block, eval_block_with_early_return}; use nu_engine::{eval_block, eval_block_with_early_return};
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents}; use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
use nu_protocol::engine::StateWorkingSet;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, engine::{EngineState, Stack},
engine::{EngineState, Stack, StateWorkingSet}, print_if_stream, PipelineData, ShellError, Span, Value,
report_error, report_error_new, PipelineData, ShellError, Span, Value,
}; };
use nu_protocol::{report_error, report_error_new};
#[cfg(windows)] #[cfg(windows)]
use nu_utils::enable_vt_processing; use nu_utils::enable_vt_processing;
use nu_utils::utils::perf; use nu_utils::utils::perf;
@ -39,15 +40,16 @@ fn gather_env_vars(
init_cwd: &Path, init_cwd: &Path,
) { ) {
fn report_capture_error(engine_state: &EngineState, env_str: &str, msg: &str) { fn report_capture_error(engine_state: &EngineState, env_str: &str, msg: &str) {
report_error_new( let working_set = StateWorkingSet::new(engine_state);
engine_state, report_error(
&ShellError::GenericError { &working_set,
error: format!("Environment variable was not captured: {env_str}"), &ShellError::GenericError(
msg: "".into(), format!("Environment variable was not captured: {env_str}"),
span: None, "".to_string(),
help: Some(msg.into()), None,
inner: vec![], Some(msg.into()),
}, Vec::new(),
),
); );
} }
@ -70,17 +72,18 @@ fn gather_env_vars(
} }
None => { None => {
// Could not capture current working directory // Could not capture current working directory
report_error_new( let working_set = StateWorkingSet::new(engine_state);
engine_state, report_error(
&ShellError::GenericError { &working_set,
error: "Current directory is not a valid utf-8 path".into(), &ShellError::GenericError(
msg: "".into(), "Current directory is not a valid utf-8 path".to_string(),
span: None, "".to_string(),
help: Some(format!( None,
Some(format!(
"Retrieving current directory failed: {init_cwd:?} not a valid utf-8 path" "Retrieving current directory failed: {init_cwd:?} not a valid utf-8 path"
)), )),
inner: vec![], Vec::new(),
}, ),
); );
} }
} }
@ -90,8 +93,8 @@ fn gather_env_vars(
let span_offset = engine_state.next_span_start(); let span_offset = engine_state.next_span_start();
engine_state.add_file( engine_state.add_file(
"Host Environment Variables".into(), "Host Environment Variables".to_string(),
fake_env_file.as_bytes().into(), fake_env_file.as_bytes().to_vec(),
); );
let (tokens, _) = lex(fake_env_file.as_bytes(), span_offset, &[], &[], true); let (tokens, _) = lex(fake_env_file.as_bytes(), span_offset, &[], &[], true);
@ -206,28 +209,99 @@ pub fn eval_source(
fname: &str, fname: &str,
input: PipelineData, input: PipelineData,
allow_return: bool, allow_return: bool,
) -> i32 { ) -> bool {
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let exit_code = match evaluate_source(engine_state, stack, source, fname, input, allow_return) { let (block, delta) = {
Ok(code) => code.unwrap_or(0), let mut working_set = StateWorkingSet::new(engine_state);
Err(err) => { let output = parse(
report_error_new(engine_state, &err); &mut working_set,
1 Some(fname), // format!("entry #{}", entry_num)
source,
false,
);
if let Some(err) = working_set.parse_errors.first() {
set_last_exit_code(stack, 1);
report_error(&working_set, err);
return false;
} }
(output, working_set.render())
}; };
stack.add_env_var( if let Err(err) = engine_state.merge_delta(delta) {
"LAST_EXIT_CODE".to_string(), set_last_exit_code(stack, 1);
Value::int(exit_code.into(), Span::unknown()), report_error_new(engine_state, &err);
); return false;
// reset vt processing, aka ansi because illbehaved externals can break it
#[cfg(windows)]
{
let _ = enable_vt_processing();
} }
let b = if allow_return {
eval_block_with_early_return(engine_state, stack, &block, input, false, false)
} else {
eval_block(engine_state, stack, &block, input, false, false)
};
match b {
Ok(pipeline_data) => {
let config = engine_state.get_config();
let result;
if let PipelineData::ExternalStream {
stdout: stream,
stderr: stderr_stream,
exit_code,
..
} = pipeline_data
{
result = print_if_stream(stream, stderr_stream, false, exit_code);
} else if let Some(hook) = config.hooks.display_output.clone() {
match eval_hook(
engine_state,
stack,
Some(pipeline_data),
vec![],
&hook,
"display_output",
) {
Err(err) => {
result = Err(err);
}
Ok(val) => {
result = val.print(engine_state, stack, false, false);
}
}
} else {
result = pipeline_data.print(engine_state, stack, true, false);
}
match result {
Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
return false;
}
Ok(exit_code) => {
set_last_exit_code(stack, exit_code);
}
}
// reset vt processing, aka ansi because illbehaved externals can break it
#[cfg(windows)]
{
let _ = enable_vt_processing();
}
}
Err(err) => {
set_last_exit_code(stack, 1);
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
return false;
}
}
perf( perf(
&format!("eval_source {}", &fname), &format!("eval_source {}", &fname),
start_time, start_time,
@ -237,64 +311,14 @@ pub fn eval_source(
engine_state.get_config().use_ansi_coloring, engine_state.get_config().use_ansi_coloring,
); );
exit_code true
} }
fn evaluate_source( fn set_last_exit_code(stack: &mut Stack, exit_code: i64) {
engine_state: &mut EngineState, stack.add_env_var(
stack: &mut Stack, "LAST_EXIT_CODE".to_string(),
source: &[u8], Value::int(exit_code, Span::unknown()),
fname: &str, );
input: PipelineData,
allow_return: bool,
) -> Result<Option<i32>, ShellError> {
let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state);
let output = parse(
&mut working_set,
Some(fname), // format!("entry #{}", entry_num)
source,
false,
);
if let Some(warning) = working_set.parse_warnings.first() {
report_error(&working_set, warning);
}
if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
return Ok(Some(1));
}
(output, working_set.render())
};
engine_state.merge_delta(delta)?;
let pipeline = if allow_return {
eval_block_with_early_return::<WithoutDebug>(engine_state, stack, &block, input)
} else {
eval_block::<WithoutDebug>(engine_state, stack, &block, input)
}?;
let status = if let PipelineData::ByteStream(..) = pipeline {
pipeline.print(engine_state, stack, false, false)?
} else {
if let Some(hook) = engine_state.get_config().hooks.display_output.clone() {
let pipeline = eval_hook(
engine_state,
stack,
Some(pipeline),
vec![],
&hook,
"display_output",
)?;
pipeline.print(engine_state, stack, false, false)
} else {
pipeline.print(engine_state, stack, true, false)
}?
};
Ok(status.map(|status| status.code()))
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1 +0,0 @@
mod nu_highlight;

View File

@ -1,7 +0,0 @@
use nu_test_support::nu;
#[test]
fn nu_highlight_not_expr() {
let actual = nu!("'not false' | nu-highlight | ansi strip");
assert_eq!(actual.out, "not false");
}

View File

@ -1,15 +1,12 @@
pub mod support; pub mod support;
use std::path::PathBuf;
use nu_cli::NuCompleter; use nu_cli::NuCompleter;
use nu_engine::eval_block;
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::{debugger::WithoutDebug, engine::StateWorkingSet, PipelineData}; use nu_protocol::engine::StateWorkingSet;
use reedline::{Completer, Suggestion}; use reedline::{Completer, Suggestion};
use rstest::{fixture, rstest}; use rstest::{fixture, rstest};
use std::{
path::{PathBuf, MAIN_SEPARATOR},
sync::Arc,
};
use support::{ use support::{
completions_helpers::{new_partial_engine, new_quote_engine}, completions_helpers::{new_partial_engine, new_quote_engine},
file, folder, match_suggestions, new_engine, file, folder, match_suggestions, new_engine,
@ -25,7 +22,7 @@ fn completer() -> NuCompleter {
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(std::sync::Arc::new(engine), stack)
} }
#[fixture] #[fixture]
@ -39,7 +36,7 @@ fn completer_strings() -> NuCompleter {
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(std::sync::Arc::new(engine), stack)
} }
#[fixture] #[fixture]
@ -59,42 +56,19 @@ fn extern_completer() -> NuCompleter {
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack)) NuCompleter::new(std::sync::Arc::new(engine), stack)
}
#[fixture]
fn custom_completer() -> NuCompleter {
// Create a new engine
let (dir, _, mut engine, mut stack) = new_engine();
// Add record value as example
let record = r#"
let external_completer = {|spans|
$spans
}
$env.config.completions.external = {
enable: true
max_results: 100
completer: $external_completer
}
"#;
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer
NuCompleter::new(Arc::new(engine), Arc::new(stack))
} }
#[test] #[test]
fn variables_dollar_sign_with_varialblecompletion() { fn variables_dollar_sign_with_varialblecompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "$ "; let target_dir = "$ ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
assert_eq!(8, suggestions.len()); assert_eq!(7, suggestions.len());
} }
#[rstest] #[rstest]
@ -141,47 +115,28 @@ fn dotnu_completions() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test source completion // Test source completion
let completion_str = "source-env ".to_string(); let completion_str = "source-env ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len()); let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(2, suggestions.len()); assert_eq!(1, suggestions.len());
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value); assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
#[cfg(windows)]
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
#[cfg(not(windows))]
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
// Test use completion // Test use completion
let completion_str = "use ".to_string(); let completion_str = "use ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len()); let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(2, suggestions.len()); assert_eq!(1, suggestions.len());
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value); assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
#[cfg(windows)]
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
#[cfg(not(windows))]
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
// Test overlay use completion
let completion_str = "overlay use ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(2, suggestions.len());
assert_eq!("custom_completion.nu", suggestions.first().unwrap().value);
#[cfg(windows)]
assert_eq!("directory_completion\\", suggestions.get(1).unwrap().value);
#[cfg(not(windows))]
assert_eq!("directory_completion/", suggestions.get(1).unwrap().value);
} }
#[test] #[test]
#[ignore] #[ignore]
fn external_completer_trailing_space() { fn external_completer_trailing_space() {
// https://github.com/nushell/nushell/issues/6378 // https://github.com/nushell/nushell/issues/6378
let block = "{|spans| $spans}"; let block = "let external_completer = {|spans| $spans}";
let input = "gh alias ".to_string(); let input = "gh alias ".to_string();
let suggestions = run_external_completion(block, &input); let suggestions = run_external_completion(block, &input);
@ -220,17 +175,16 @@ fn file_completions() {
let (dir, dir_str, engine, stack) = new_engine(); let (dir, dir_str, engine, stack) = new_engine();
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test completions for the current folder // Test completions for the current folder
let target_dir = format!("cp {dir_str}{MAIN_SEPARATOR}"); let target_dir = format!("cp {dir_str}");
let suggestions = completer.complete(&target_dir, target_dir.len()); let suggestions = completer.complete(&target_dir, target_dir.len());
// Create the expected values // Create the expected values
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
folder(dir.join("another")), folder(dir.join("another")),
file(dir.join("custom_completion.nu")), file(dir.join("custom_completion.nu")),
folder(dir.join("directory_completion")),
file(dir.join("nushell")), file(dir.join("nushell")),
folder(dir.join("test_a")), folder(dir.join("test_a")),
folder(dir.join("test_b")), folder(dir.join("test_b")),
@ -250,16 +204,6 @@ fn file_completions() {
// Match the results // Match the results
match_suggestions(expected_paths, suggestions); match_suggestions(expected_paths, suggestions);
// Test completions for hidden files
let target_dir = format!("ls {}/.", folder(dir.join(".hidden_folder")));
let suggestions = completer.complete(&target_dir, target_dir.len());
let expected_paths: Vec<String> =
vec![file(dir.join(".hidden_folder").join(".hidden_subfile"))];
// Match the results
match_suggestions(expected_paths, suggestions);
} }
#[test] #[test]
@ -268,7 +212,7 @@ fn partial_completions() {
let (dir, _, engine, stack) = new_partial_engine(); let (dir, _, engine, stack) = new_partial_engine();
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test completions for a folder's name // Test completions for a folder's name
let target_dir = format!("cd {}", file(dir.join("pa"))); let target_dir = format!("cd {}", file(dir.join("pa")));
@ -292,8 +236,6 @@ fn partial_completions() {
// Create the expected values // Create the expected values
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
file(dir.join("partial_a").join("have_ext.exe")),
file(dir.join("partial_a").join("have_ext.txt")),
file(dir.join("partial_a").join("hello")), file(dir.join("partial_a").join("hello")),
file(dir.join("partial_a").join("hola")), file(dir.join("partial_a").join("hola")),
file(dir.join("partial_b").join("hello_b")), file(dir.join("partial_b").join("hello_b")),
@ -312,8 +254,6 @@ fn partial_completions() {
// Create the expected values // Create the expected values
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
file(dir.join("partial_a").join("anotherfile")), file(dir.join("partial_a").join("anotherfile")),
file(dir.join("partial_a").join("have_ext.exe")),
file(dir.join("partial_a").join("have_ext.txt")),
file(dir.join("partial_a").join("hello")), file(dir.join("partial_a").join("hello")),
file(dir.join("partial_a").join("hola")), file(dir.join("partial_a").join("hola")),
file(dir.join("partial_b").join("hello_b")), file(dir.join("partial_b").join("hello_b")),
@ -341,54 +281,7 @@ fn partial_completions() {
let suggestions = completer.complete(&target_dir, target_dir.len()); let suggestions = completer.complete(&target_dir, target_dir.len());
// Create the expected values // Create the expected values
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![file(dir.join("final_partial").join("somefile"))];
file(
dir.join("partial_a")
.join("..")
.join("final_partial")
.join("somefile"),
),
file(
dir.join("partial_b")
.join("..")
.join("final_partial")
.join("somefile"),
),
file(
dir.join("partial_c")
.join("..")
.join("final_partial")
.join("somefile"),
),
];
// Match the results
match_suggestions(expected_paths, suggestions);
// Test completion for all files under directories whose names begin with "pa"
let file_str = file(dir.join("partial_a").join("have"));
let target_file = format!("rm {file_str}");
let suggestions = completer.complete(&target_file, target_file.len());
// Create the expected values
let expected_paths: Vec<String> = vec![
file(dir.join("partial_a").join("have_ext.exe")),
file(dir.join("partial_a").join("have_ext.txt")),
];
// Match the results
match_suggestions(expected_paths, suggestions);
// Test completion for all files under directories whose names begin with "pa"
let file_str = file(dir.join("partial_a").join("have_ext."));
let file_dir = format!("rm {file_str}");
let suggestions = completer.complete(&file_dir, file_dir.len());
// Create the expected values
let expected_paths: Vec<String> = vec![
file(dir.join("partial_a").join("have_ext.exe")),
file(dir.join("partial_a").join("have_ext.txt")),
];
// Match the results // Match the results
match_suggestions(expected_paths, suggestions); match_suggestions(expected_paths, suggestions);
@ -398,7 +291,7 @@ fn partial_completions() {
fn command_ls_with_filecompletion() { fn command_ls_with_filecompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "ls "; let target_dir = "ls ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -407,7 +300,6 @@ fn command_ls_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -418,7 +310,6 @@ fn command_ls_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -426,20 +317,13 @@ fn command_ls_with_filecompletion() {
".hidden_folder/".to_string(), ".hidden_folder/".to_string(),
]; ];
match_suggestions(expected_paths, suggestions);
let target_dir = "ls custom_completion.";
let suggestions = completer.complete(target_dir, target_dir.len());
let expected_paths: Vec<String> = vec!["custom_completion.nu".to_string()];
match_suggestions(expected_paths, suggestions) match_suggestions(expected_paths, suggestions)
} }
#[test] #[test]
fn command_open_with_filecompletion() { fn command_open_with_filecompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "open "; let target_dir = "open ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -448,7 +332,6 @@ fn command_open_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -459,7 +342,6 @@ fn command_open_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -467,13 +349,6 @@ fn command_open_with_filecompletion() {
".hidden_folder/".to_string(), ".hidden_folder/".to_string(),
]; ];
match_suggestions(expected_paths, suggestions);
let target_dir = "open custom_completion.";
let suggestions = completer.complete(target_dir, target_dir.len());
let expected_paths: Vec<String> = vec!["custom_completion.nu".to_string()];
match_suggestions(expected_paths, suggestions) match_suggestions(expected_paths, suggestions)
} }
@ -481,7 +356,7 @@ fn command_open_with_filecompletion() {
fn command_rm_with_globcompletion() { fn command_rm_with_globcompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "rm "; let target_dir = "rm ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -490,7 +365,6 @@ fn command_rm_with_globcompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -501,7 +375,6 @@ fn command_rm_with_globcompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -516,7 +389,7 @@ fn command_rm_with_globcompletion() {
fn command_cp_with_globcompletion() { fn command_cp_with_globcompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "cp "; let target_dir = "cp ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -525,7 +398,6 @@ fn command_cp_with_globcompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -536,7 +408,6 @@ fn command_cp_with_globcompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -551,7 +422,7 @@ fn command_cp_with_globcompletion() {
fn command_save_with_filecompletion() { fn command_save_with_filecompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "save "; let target_dir = "save ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -560,7 +431,6 @@ fn command_save_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -571,7 +441,6 @@ fn command_save_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -586,7 +455,7 @@ fn command_save_with_filecompletion() {
fn command_touch_with_filecompletion() { fn command_touch_with_filecompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "touch "; let target_dir = "touch ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -595,7 +464,6 @@ fn command_touch_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -606,7 +474,6 @@ fn command_touch_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -621,7 +488,7 @@ fn command_touch_with_filecompletion() {
fn command_watch_with_filecompletion() { fn command_watch_with_filecompletion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "watch "; let target_dir = "watch ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -630,7 +497,6 @@ fn command_watch_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -641,7 +507,6 @@ fn command_watch_with_filecompletion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -656,13 +521,12 @@ fn command_watch_with_filecompletion() {
fn file_completion_quoted() { fn file_completion_quoted() {
let (_, _, engine, stack) = new_quote_engine(); let (_, _, engine, stack) = new_quote_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "open "; let target_dir = "open ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"\'[a] bc.txt\'".to_string(),
"`--help`".to_string(), "`--help`".to_string(),
"`-42`".to_string(), "`-42`".to_string(),
"`-inf`".to_string(), "`-inf`".to_string(),
@ -694,7 +558,7 @@ fn flag_completions() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test completions for the 'ls' flags // Test completions for the 'ls' flags
let suggestions = completer.complete("ls -", 4); let suggestions = completer.complete("ls -", 4);
@ -729,16 +593,15 @@ fn folder_with_directorycompletions() {
let (dir, dir_str, engine, stack) = new_engine(); let (dir, dir_str, engine, stack) = new_engine();
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test completions for the current folder // Test completions for the current folder
let target_dir = format!("cd {dir_str}{MAIN_SEPARATOR}"); let target_dir = format!("cd {dir_str}");
let suggestions = completer.complete(&target_dir, target_dir.len()); let suggestions = completer.complete(&target_dir, target_dir.len());
// Create the expected values // Create the expected values
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
folder(dir.join("another")), folder(dir.join("another")),
folder(dir.join("directory_completion")),
folder(dir.join("test_a")), folder(dir.join("test_a")),
folder(dir.join("test_b")), folder(dir.join("test_b")),
folder(dir.join(".hidden_folder")), folder(dir.join(".hidden_folder")),
@ -758,19 +621,18 @@ fn variables_completions() {
assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok());
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test completions for $nu // Test completions for $nu
let suggestions = completer.complete("$nu.", 4); let suggestions = completer.complete("$nu.", 4);
assert_eq!(15, suggestions.len()); assert_eq!(14, suggestions.len());
let expected: Vec<String> = vec![ let expected: Vec<String> = vec![
"config-path".into(), "config-path".into(),
"current-exe".into(), "current-exe".into(),
"default-config-dir".into(), "default-config-dir".into(),
"env-path".into(), "env-path".into(),
"history-enabled".into(),
"history-path".into(), "history-path".into(),
"home-path".into(), "home-path".into(),
"is-interactive".into(), "is-interactive".into(),
@ -789,13 +651,9 @@ fn variables_completions() {
// Test completions for $nu.h (filter) // Test completions for $nu.h (filter)
let suggestions = completer.complete("$nu.h", 5); let suggestions = completer.complete("$nu.h", 5);
assert_eq!(3, suggestions.len()); assert_eq!(2, suggestions.len());
let expected: Vec<String> = vec![ let expected: Vec<String> = vec!["history-path".into(), "home-path".into()];
"history-enabled".into(),
"history-path".into(),
"home-path".into(),
];
// Match results // Match results
match_suggestions(expected, suggestions); match_suggestions(expected, suggestions);
@ -864,7 +722,7 @@ fn alias_of_command_and_flags() {
let alias = r#"alias ll = ls -l"#; let alias = r#"alias ll = ls -l"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let suggestions = completer.complete("ll t", 4); let suggestions = completer.complete("ll t", 4);
#[cfg(windows)] #[cfg(windows)]
@ -883,7 +741,7 @@ fn alias_of_basic_command() {
let alias = r#"alias ll = ls "#; let alias = r#"alias ll = ls "#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let suggestions = completer.complete("ll t", 4); let suggestions = completer.complete("ll t", 4);
#[cfg(windows)] #[cfg(windows)]
@ -905,7 +763,7 @@ fn alias_of_another_alias() {
let alias = r#"alias lf = ll -f"#; let alias = r#"alias lf = ll -f"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let suggestions = completer.complete("lf t", 4); let suggestions = completer.complete("lf t", 4);
#[cfg(windows)] #[cfg(windows)]
@ -916,14 +774,12 @@ fn alias_of_another_alias() {
match_suggestions(expected_paths, suggestions) match_suggestions(expected_paths, suggestions)
} }
fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> { fn run_external_completion(block: &str, input: &str) -> Vec<Suggestion> {
let completer = format!("$env.config.completions.external.completer = {completer}");
// Create a new engine // Create a new engine
let (dir, _, mut engine_state, mut stack) = new_engine(); let (dir, _, mut engine_state, mut stack) = new_engine();
let (block, delta) = { let (_, delta) = {
let mut working_set = StateWorkingSet::new(&engine_state); let mut working_set = StateWorkingSet::new(&engine_state);
let block = parse(&mut working_set, None, completer.as_bytes(), false); let block = parse(&mut working_set, None, block.as_bytes(), false);
assert!(working_set.parse_errors.is_empty()); assert!(working_set.parse_errors.is_empty());
(block, working_set.render()) (block, working_set.render())
@ -931,15 +787,18 @@ fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> {
assert!(engine_state.merge_delta(delta).is_ok()); assert!(engine_state.merge_delta(delta).is_ok());
assert!(
eval_block::<WithoutDebug>(&engine_state, &mut stack, &block, PipelineData::Empty).is_ok()
);
// Merge environment into the permanent state // Merge environment into the permanent state
assert!(engine_state.merge_env(&mut stack, &dir).is_ok()); 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);
// Instantiate a new completer // Instantiate a new completer
let mut completer = NuCompleter::new(Arc::new(engine_state), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine_state), stack);
completer.complete(input, input.len()) completer.complete(input, input.len())
} }
@ -948,7 +807,7 @@ fn run_external_completion(completer: &str, input: &str) -> Vec<Suggestion> {
fn unknown_command_completion() { fn unknown_command_completion() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let target_dir = "thiscommanddoesnotexist "; let target_dir = "thiscommanddoesnotexist ";
let suggestions = completer.complete(target_dir, target_dir.len()); let suggestions = completer.complete(target_dir, target_dir.len());
@ -957,7 +816,6 @@ fn unknown_command_completion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -968,7 +826,6 @@ fn unknown_command_completion() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -1011,7 +868,7 @@ fn flagcompletion_triggers_after_cursor_piped(mut completer: NuCompleter) {
fn filecompletions_triggers_after_cursor() { fn filecompletions_triggers_after_cursor() {
let (_, _, engine, stack) = new_engine(); let (_, _, engine, stack) = new_engine();
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
let suggestions = completer.complete("cp test_c", 3); let suggestions = completer.complete("cp test_c", 3);
@ -1019,7 +876,6 @@ fn filecompletions_triggers_after_cursor() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another\\".to_string(), "another\\".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion\\".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a\\".to_string(), "test_a\\".to_string(),
"test_b\\".to_string(), "test_b\\".to_string(),
@ -1030,7 +886,6 @@ fn filecompletions_triggers_after_cursor() {
let expected_paths: Vec<String> = vec![ let expected_paths: Vec<String> = vec![
"another/".to_string(), "another/".to_string(),
"custom_completion.nu".to_string(), "custom_completion.nu".to_string(),
"directory_completion/".to_string(),
"nushell".to_string(), "nushell".to_string(),
"test_a/".to_string(), "test_a/".to_string(),
"test_b/".to_string(), "test_b/".to_string(),
@ -1083,34 +938,6 @@ fn extern_complete_flags(mut extern_completer: NuCompleter) {
match_suggestions(expected, suggestions); match_suggestions(expected, suggestions);
} }
#[rstest]
fn custom_completer_triggers_cursor_before_word(mut custom_completer: NuCompleter) {
let suggestions = custom_completer.complete("cmd foo bar", 8);
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "".into()];
match_suggestions(expected, suggestions);
}
#[rstest]
fn custom_completer_triggers_cursor_on_word_left_boundary(mut custom_completer: NuCompleter) {
let suggestions = custom_completer.complete("cmd foo bar", 8);
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "".into()];
match_suggestions(expected, suggestions);
}
#[rstest]
fn custom_completer_triggers_cursor_next_to_word(mut custom_completer: NuCompleter) {
let suggestions = custom_completer.complete("cmd foo bar", 11);
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "bar".into()];
match_suggestions(expected, suggestions);
}
#[rstest]
fn custom_completer_triggers_cursor_after_word(mut custom_completer: NuCompleter) {
let suggestions = custom_completer.complete("cmd foo bar ", 12);
let expected: Vec<String> = vec!["cmd".into(), "foo".into(), "bar".into(), "".into()];
match_suggestions(expected, suggestions);
}
#[ignore = "was reverted, still needs fixing"] #[ignore = "was reverted, still needs fixing"]
#[rstest] #[rstest]
fn alias_offset_bug_7648() { fn alias_offset_bug_7648() {
@ -1120,7 +947,7 @@ fn alias_offset_bug_7648() {
let alias = r#"alias ea = ^$env.EDITOR /tmp/test.s"#; let alias = r#"alias ea = ^$env.EDITOR /tmp/test.s"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Issue #7648 // Issue #7648
// Nushell crashes when an alias name is shorter than the alias command // Nushell crashes when an alias name is shorter than the alias command
@ -1139,7 +966,7 @@ fn alias_offset_bug_7754() {
let alias = r#"alias ll = ls -l"#; let alias = r#"alias ll = ls -l"#;
assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok()); assert!(support::merge_input(alias.as_bytes(), &mut engine, &mut stack, dir).is_ok());
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Issue #7754 // Issue #7754
// Nushell crashes when an alias name is shorter than the alias command // Nushell crashes when an alias name is shorter than the alias command

View File

@ -1,2 +0,0 @@
mod commands;
mod completions;

View File

@ -1,13 +1,15 @@
use std::path::PathBuf;
use nu_engine::eval_block; use nu_engine::eval_block;
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
PipelineData, ShellError, Span, Value, eval_const::create_nu_constant,
PipelineData, ShellError, Span, Value, NU_VARIABLE_ID,
}; };
use nu_test_support::fs; use nu_test_support::fs;
use reedline::Suggestion; use reedline::Suggestion;
use std::path::{PathBuf, MAIN_SEPARATOR}; const SEP: char = std::path::MAIN_SEPARATOR;
fn create_default_context() -> EngineState { fn create_default_context() -> EngineState {
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context()) nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
@ -17,17 +19,20 @@ fn create_default_context() -> EngineState {
pub fn new_engine() -> (PathBuf, String, EngineState, Stack) { pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
// Target folder inside assets // Target folder inside assets
let dir = fs::fixtures().join("completions"); let dir = fs::fixtures().join("completions");
let dir_str = dir let mut dir_str = dir
.clone() .clone()
.into_os_string() .into_os_string()
.into_string() .into_string()
.unwrap_or_default(); .unwrap_or_default();
dir_str.push(SEP);
// Create a new engine with default context // Create a new engine with default context
let mut engine_state = create_default_context(); let mut engine_state = create_default_context();
// Add $nu // Add $nu
engine_state.generate_nu_constant(); let nu_const =
create_nu_constant(&engine_state, Span::test_data()).expect("Failed creating $nu");
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
// New stack // New stack
let mut stack = Stack::new(); let mut stack = Stack::new();
@ -71,11 +76,12 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) { pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
// Target folder inside assets // Target folder inside assets
let dir = fs::fixtures().join("quoted_completions"); let dir = fs::fixtures().join("quoted_completions");
let dir_str = dir let mut dir_str = dir
.clone() .clone()
.into_os_string() .into_os_string()
.into_string() .into_string()
.unwrap_or_default(); .unwrap_or_default();
dir_str.push(SEP);
// Create a new engine with default context // Create a new engine with default context
let mut engine_state = create_default_context(); let mut engine_state = create_default_context();
@ -106,11 +112,12 @@ pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
pub fn new_partial_engine() -> (PathBuf, String, EngineState, Stack) { pub fn new_partial_engine() -> (PathBuf, String, EngineState, Stack) {
// Target folder inside assets // Target folder inside assets
let dir = fs::fixtures().join("partial_completions"); let dir = fs::fixtures().join("partial_completions");
let dir_str = dir let mut dir_str = dir
.clone() .clone()
.into_os_string() .into_os_string()
.into_string() .into_string()
.unwrap_or_default(); .unwrap_or_default();
dir_str.push(SEP);
// Create a new engine with default context // Create a new engine with default context
let mut engine_state = create_default_context(); let mut engine_state = create_default_context();
@ -157,7 +164,7 @@ pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
// append the separator to the converted path // append the separator to the converted path
pub fn folder(path: PathBuf) -> String { pub fn folder(path: PathBuf) -> String {
let mut converted_path = file(path); let mut converted_path = file(path);
converted_path.push(MAIN_SEPARATOR); converted_path.push(SEP);
converted_path converted_path
} }
@ -187,11 +194,13 @@ pub fn merge_input(
engine_state.merge_delta(delta)?; engine_state.merge_delta(delta)?;
assert!(eval_block::<WithoutDebug>( assert!(eval_block(
engine_state, engine_state,
stack, stack,
&block, &block,
PipelineData::Value(Value::nothing(Span::unknown()), None), PipelineData::Value(Value::nothing(Span::unknown(),), None),
false,
false
) )
.is_ok()); .is_ok());

View File

@ -5,17 +5,21 @@ edition = "2021"
license = "MIT" license = "MIT"
name = "nu-cmd-base" name = "nu-cmd-base"
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-base" repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-base"
version = "0.94.2" version = "0.87.2"
# 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-engine = { path = "../nu-engine", version = "0.94.2" } nu-engine = { path = "../nu-engine", version = "0.87.2" }
nu-parser = { path = "../nu-parser", version = "0.94.2" } nu-glob = { path = "../nu-glob", version = "0.87.2" }
nu-path = { path = "../nu-path", version = "0.94.2" } nu-parser = { path = "../nu-parser", version = "0.87.2" }
nu-protocol = { path = "../nu-protocol", version = "0.94.2" } nu-path = { path = "../nu-path", version = "0.87.2" }
nu-protocol = { path = "../nu-protocol", version = "0.87.2" }
nu-utils = { path = "../nu-utils", version = "0.87.2" }
indexmap = { workspace = true } indexmap = "2.1"
miette = { workspace = true } miette = "5.10.0"
[dev-dependencies] [dev-dependencies]
nu-test-support = { path = "../nu-test-support", version = "0.87.2" }
rstest = "0.18.2"

View File

@ -0,0 +1,207 @@
// utilities for expanding globs in command arguments
use nu_glob::{glob_with_parent, MatchOptions, Paths};
use nu_protocol::{ShellError, Spanned};
use std::fs;
use std::path::{Path, PathBuf};
// standard glob options to use for filesystem command arguments
const GLOB_PARAMS: MatchOptions = MatchOptions {
case_sensitive: true,
require_literal_separator: false,
require_literal_leading_dot: false,
recursive_match_hidden_dir: true,
};
// handle an argument that could be a literal path or a glob.
// if literal path, return just that (whether user can access it or not).
// if glob, expand into matching paths, using GLOB_PARAMS options.
pub fn arg_glob(
pattern: &Spanned<String>, // alleged path or glob
cwd: &Path, // current working directory
) -> Result<Paths, ShellError> {
arg_glob_opt(pattern, cwd, GLOB_PARAMS)
}
// variant of [arg_glob] that requires literal dot prefix in pattern to match dot-prefixed path.
pub fn arg_glob_leading_dot(pattern: &Spanned<String>, cwd: &Path) -> Result<Paths, ShellError> {
arg_glob_opt(
pattern,
cwd,
MatchOptions {
require_literal_leading_dot: true,
..GLOB_PARAMS
},
)
}
fn arg_glob_opt(
pattern: &Spanned<String>,
cwd: &Path,
options: MatchOptions,
) -> Result<Paths, ShellError> {
// remove ansi coloring (?)
let pattern = {
Spanned {
item: nu_utils::strip_ansi_string_unlikely(pattern.item.clone()),
span: pattern.span,
}
};
// if there's a file with same path as the pattern, just return that.
let pp = cwd.join(&pattern.item);
let md = fs::metadata(pp);
#[allow(clippy::single_match)]
match md {
Ok(_metadata) => {
return Ok(Paths::single(&PathBuf::from(pattern.item), cwd));
}
// file not found, but also "invalid chars in file" (e.g * on Windows). Fall through and glob
Err(_) => {}
}
// user wasn't referring to a specific thing in filesystem, try to glob it.
match glob_with_parent(&pattern.item, options, cwd) {
Ok(p) => Ok(p),
Err(pat_err) => {
Err(ShellError::InvalidGlobPattern(
pat_err.msg.into(),
pattern.span, // improve specificity
))
}
}
}
#[cfg(test)]
mod test {
use super::*;
use nu_glob::GlobResult;
use nu_protocol::{Span, Spanned};
use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground;
use rstest::rstest;
fn spanned_string(str: &str) -> Spanned<String> {
Spanned {
item: str.to_string(),
span: Span::test_data(),
}
}
#[test]
fn does_something() {
let act = arg_glob(&spanned_string("*"), &PathBuf::from("."));
assert!(act.is_ok());
for f in act.expect("checked ok") {
match f {
Ok(p) => {
assert!(!p.to_str().unwrap().is_empty());
}
Err(e) => panic!("unexpected error {:?}", e),
};
}
}
#[test]
fn glob_format_error() {
let act = arg_glob(&spanned_string(r#"ab]c[def"#), &PathBuf::from("."));
assert!(act.is_err());
}
#[rstest]
#[case("*", 4, "no dirs")]
#[case("**/*", 7, "incl dirs")]
fn glob_subdirs(#[case] pat: &str, #[case] exp_count: usize, #[case] case: &str) {
Playground::setup("glob_subdirs", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
sandbox.mkdir(".children");
sandbox.within(".children").with_files(vec![
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let p: Vec<GlobResult> = arg_glob(&spanned_string(pat), &dirs.test)
.expect("no error")
.collect();
assert_eq!(
exp_count,
p.iter().filter(|i| i.is_ok()).count(),
" case: {case} ",
);
// expected behavior -- that directories are included in results (if name matches pattern)
let t = p
.iter()
.any(|i| i.as_ref().unwrap().to_string_lossy().contains(".children"));
assert!(t, "check for dir, case {case}");
})
}
#[rstest]
#[case("yehuda.txt", true, 1, "matches literal path")]
#[case("*", false, 3, "matches glob")]
#[case(r#"bad[glob.foo"#, true, 1, "matches literal, would be bad glob pat")]
fn exact_vs_glob(
#[case] pat: &str,
#[case] exp_matches_input: bool,
#[case] exp_count: usize,
#[case] case: &str,
) {
Playground::setup("exact_vs_glob", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("bad[glob.foo"),
]);
let res = arg_glob(&spanned_string(pat), &dirs.test)
.expect("no error")
.collect::<Vec<GlobResult>>();
eprintln!("res: {:?}", res);
if exp_matches_input {
assert_eq!(
exp_count,
res.len(),
" case {case}: matches input, but count not 1? "
);
assert_eq!(
&res[0].as_ref().unwrap().to_string_lossy(),
pat, // todo: is it OK for glob to return relative paths (not to current cwd, but to arg cwd of arg_glob)?
);
} else {
assert_eq!(exp_count, res.len(), " case: {}: matched glob", case);
}
})
}
#[rstest]
#[case(r#"realbad[glob.foo"#, true, 1, "error, bad glob")]
fn exact_vs_bad_glob(
// if path doesn't exist but pattern is not valid glob, should get error.
#[case] pat: &str,
#[case] _exp_matches_input: bool,
#[case] _exp_count: usize,
#[case] _tag: &str,
) {
Playground::setup("exact_vs_bad_glob", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("bad[glob.foo"),
]);
let res = arg_glob(&spanned_string(pat), &dirs.test);
assert!(res
.expect_err("expected error")
.to_string()
.contains("Invalid glob pattern"));
})
}
}

View File

@ -2,13 +2,9 @@ use crate::util::get_guaranteed_cwd;
use miette::Result; use miette::Result;
use nu_engine::{eval_block, eval_block_with_early_return}; use nu_engine::{eval_block, eval_block_with_early_return};
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::{ use nu_protocol::cli_error::{report_error, report_error_new};
cli_error::{report_error, report_error_new}, use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
debugger::WithoutDebug, use nu_protocol::{BlockId, PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId};
engine::{Closure, EngineState, Stack, StateWorkingSet},
PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId,
};
use std::sync::Arc;
pub fn eval_env_change_hook( pub fn eval_env_change_hook(
env_change_hook: Option<Value>, env_change_hook: Option<Value>,
@ -18,7 +14,7 @@ pub fn eval_env_change_hook(
if let Some(hook) = env_change_hook { if let Some(hook) = env_change_hook {
match hook { match hook {
Value::Record { val, .. } => { Value::Record { val, .. } => {
for (env_name, hook_value) in &*val { for (env_name, hook_value) in &val {
let before = engine_state let before = engine_state
.previous_env_vars .previous_env_vars
.get(env_name) .get(env_name)
@ -39,7 +35,8 @@ pub fn eval_env_change_hook(
"env_change", "env_change",
)?; )?;
Arc::make_mut(&mut engine_state.previous_env_vars) engine_state
.previous_env_vars
.insert(env_name.to_string(), after); .insert(env_name.to_string(), after);
} }
} }
@ -93,11 +90,11 @@ pub fn eval_hook(
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err); report_error(&working_set, err);
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue(
expected: "valid source code".into(), "valid source code".into(),
value: "source code with syntax errors".into(), "source code with syntax errors".into(),
span, span,
}); ));
} }
(output, working_set.render(), vars) (output, working_set.render(), vars)
@ -118,7 +115,7 @@ pub fn eval_hook(
}) })
.collect(); .collect();
match eval_block::<WithoutDebug>(engine_state, stack, &block, input) { match eval_block(engine_state, stack, &block, input, false, false) {
Ok(pipeline_data) => { Ok(pipeline_data) => {
output = pipeline_data; output = pipeline_data;
} }
@ -153,11 +150,11 @@ pub fn eval_hook(
// If it returns true (the default if a condition block is not specified), the hook should be run. // If it returns true (the default if a condition block is not specified), the hook should be run.
let do_run_hook = if let Some(condition) = val.get("condition") { let do_run_hook = if let Some(condition) = val.get("condition") {
let other_span = condition.span(); let other_span = condition.span();
if let Ok(closure) = condition.as_closure() { if let Ok(block_id) = condition.as_block() {
match run_hook( match run_hook_block(
engine_state, engine_state,
stack, stack,
closure, block_id,
None, None,
arguments.clone(), arguments.clone(),
other_span, other_span,
@ -167,11 +164,11 @@ pub fn eval_hook(
{ {
val val
} else { } else {
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue(
expected: "boolean output".to_string(), "boolean output".to_string(),
value: "other PipelineData variant".to_string(), "other PipelineData variant".to_string(),
span: other_span, other_span,
}); ));
} }
} }
Err(err) => { Err(err) => {
@ -179,11 +176,11 @@ pub fn eval_hook(
} }
} }
} else { } else {
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue(
expected: "block".to_string(), "block".to_string(),
value: format!("{}", condition.get_type()), format!("{}", condition.get_type()),
span: other_span, other_span,
}); ));
} }
} else { } else {
// always run the hook // always run the hook
@ -225,11 +222,11 @@ pub fn eval_hook(
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err); report_error(&working_set, err);
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue(
expected: "valid source code".into(), "valid source code".into(),
value: "source code with syntax errors".into(), "source code with syntax errors".into(),
span: source_span, source_span,
}); ));
} }
(output, working_set.render(), vars) (output, working_set.render(), vars)
@ -246,7 +243,7 @@ pub fn eval_hook(
}) })
.collect(); .collect();
match eval_block::<WithoutDebug>(engine_state, stack, &block, input) { match eval_block(engine_state, stack, &block, input, false, false) {
Ok(pipeline_data) => { Ok(pipeline_data) => {
output = pipeline_data; output = pipeline_data;
} }
@ -259,28 +256,48 @@ pub fn eval_hook(
stack.remove_var(*var_id); stack.remove_var(*var_id);
} }
} }
Value::Block { val: block_id, .. } => {
run_hook_block(
engine_state,
stack,
*block_id,
input,
arguments,
source_span,
)?;
}
Value::Closure { val, .. } => { Value::Closure { val, .. } => {
run_hook(engine_state, stack, val, input, arguments, source_span)?; run_hook_block(
engine_state,
stack,
val.block_id,
input,
arguments,
source_span,
)?;
} }
other => { other => {
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue(
expected: "block or string".to_string(), "block or string".to_string(),
value: format!("{}", other.get_type()), format!("{}", other.get_type()),
span: source_span, source_span,
}); ));
} }
} }
} }
} }
Value::Block { val: block_id, .. } => {
output = run_hook_block(engine_state, stack, *block_id, input, arguments, span)?;
}
Value::Closure { val, .. } => { Value::Closure { val, .. } => {
output = run_hook(engine_state, stack, val, input, arguments, span)?; output = run_hook_block(engine_state, stack, val.block_id, input, arguments, span)?;
} }
other => { other => {
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue(
expected: "string, block, record, or list of commands".into(), "string, block, record, or list of commands".into(),
value: format!("{}", other.get_type()), format!("{}", other.get_type()),
span: other.span(), other.span(),
}); ));
} }
} }
@ -290,21 +307,19 @@ pub fn eval_hook(
Ok(output) Ok(output)
} }
fn run_hook( fn run_hook_block(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
closure: &Closure, block_id: BlockId,
optional_input: Option<PipelineData>, optional_input: Option<PipelineData>,
arguments: Vec<(String, Value)>, arguments: Vec<(String, Value)>,
span: Span, span: Span,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let block = engine_state.get_block(closure.block_id); let block = engine_state.get_block(block_id);
let input = optional_input.unwrap_or_else(PipelineData::empty); let input = optional_input.unwrap_or_else(PipelineData::empty);
let mut callee_stack = stack let mut callee_stack = stack.gather_captures(engine_state, &block.captures);
.captures_to_stack_preserve_out_dest(closure.captures.clone())
.reset_pipes();
for (idx, PositionalArg { var_id, .. }) in for (idx, PositionalArg { var_id, .. }) in
block.signature.required_positional.iter().enumerate() block.signature.required_positional.iter().enumerate()
@ -321,12 +336,8 @@ fn run_hook(
} }
} }
let pipeline_data = eval_block_with_early_return::<WithoutDebug>( let pipeline_data =
engine_state, eval_block_with_early_return(engine_state, &mut callee_stack, block, input, false, false)?;
&mut callee_stack,
block,
input,
)?;
if let PipelineData::Value(Value::Error { error, .. }, _) = pipeline_data { if let PipelineData::Value(Value::Error { error, .. }, _) = pipeline_data {
return Err(*error); return Err(*error);

View File

@ -1,5 +1,7 @@
use nu_protocol::{ast::CellPath, PipelineData, ShellError, Span, Value}; use nu_protocol::ast::CellPath;
use std::sync::{atomic::AtomicBool, Arc}; use nu_protocol::{PipelineData, ShellError, Span, Value};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
pub trait CmdArgument { pub trait CmdArgument {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>>; fn take_cell_paths(&mut self) -> Option<Vec<CellPath>>;

View File

@ -1,4 +1,7 @@
mod arg_glob;
pub mod formats; pub mod formats;
pub mod hook; pub mod hook;
pub mod input_handler; pub mod input_handler;
pub mod util; pub mod util;
pub use arg_glob::arg_glob;
pub use arg_glob::arg_glob_leading_dot;

View File

@ -1,8 +1,10 @@
use nu_protocol::report_error;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, ast::RangeInclusion,
engine::{EngineState, Stack, StateWorkingSet},
Range, ShellError, Span, Value, Range, ShellError, Span, Value,
}; };
use std::{ops::Bound, path::PathBuf}; use std::path::PathBuf;
pub fn get_init_cwd() -> PathBuf { pub fn get_init_cwd() -> PathBuf {
std::env::current_dir().unwrap_or_else(|_| { std::env::current_dir().unwrap_or_else(|_| {
@ -13,29 +15,45 @@ pub fn get_init_cwd() -> PathBuf {
} }
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf { pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf {
engine_state nu_engine::env::current_dir(engine_state, stack).unwrap_or_else(|e| {
.cwd(Some(stack)) let working_set = StateWorkingSet::new(engine_state);
.unwrap_or(crate::util::get_init_cwd()) report_error(&working_set, &e);
crate::util::get_init_cwd()
})
} }
type MakeRangeError = fn(&str, Span) -> ShellError; type MakeRangeError = fn(&str, Span) -> ShellError;
pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> { pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {
match range { let start = match &range.from {
Range::IntRange(range) => { Value::Int { val, .. } => isize::try_from(*val).unwrap_or_default(),
let start = range.start().try_into().unwrap_or(0); Value::Nothing { .. } => 0,
let end = match range.end() { _ => {
Bound::Included(v) => (v + 1) as isize, return Err(|msg, span| ShellError::TypeMismatch {
Bound::Excluded(v) => v as isize, err_message: msg.to_string(),
Bound::Unbounded => isize::MAX, span,
}; })
Ok((start, end))
} }
Range::FloatRange(_) => Err(|msg, span| ShellError::TypeMismatch { };
err_message: msg.to_string(),
span, let end = match &range.to {
}), Value::Int { val, .. } => {
} if matches!(range.inclusion, RangeInclusion::Inclusive) {
isize::try_from(*val).unwrap_or(isize::max_value())
} else {
isize::try_from(*val).unwrap_or(isize::max_value()) - 1
}
}
Value::Nothing { .. } => isize::max_value(),
_ => {
return Err(|msg, span| ShellError::TypeMismatch {
err_message: msg.to_string(),
span,
})
}
};
Ok((start, end))
} }
const HELP_MSG: &str = "Nushell's config file can be found with the command: $nu.config-path. \ const HELP_MSG: &str = "Nushell's config file can be found with the command: $nu.config-path. \
@ -48,28 +66,28 @@ fn get_editor_commandline(
match value { match value {
Value::String { val, .. } if !val.is_empty() => Ok((val.to_string(), Vec::new())), Value::String { val, .. } if !val.is_empty() => Ok((val.to_string(), Vec::new())),
Value::List { vals, .. } if !vals.is_empty() => { Value::List { vals, .. } if !vals.is_empty() => {
let mut editor_cmd = vals.iter().map(|l| l.coerce_string()); let mut editor_cmd = vals.iter().map(|l| l.as_string());
match editor_cmd.next().transpose()? { match editor_cmd.next().transpose()? {
Some(editor) if !editor.is_empty() => { Some(editor) if !editor.is_empty() => {
let params = editor_cmd.collect::<Result<_, ShellError>>()?; let params = editor_cmd.collect::<Result<_, ShellError>>()?;
Ok((editor, params)) Ok((editor, params))
} }
_ => Err(ShellError::GenericError { _ => Err(ShellError::GenericError(
error: "Editor executable is missing".into(), "Editor executable is missing".into(),
msg: "Set the first element to an executable".into(), "Set the first element to an executable".into(),
span: Some(value.span()), Some(value.span()),
help: Some(HELP_MSG.into()), Some(HELP_MSG.into()),
inner: vec![], vec![],
}), )),
} }
} }
Value::String { .. } | Value::List { .. } => Err(ShellError::GenericError { Value::String { .. } | Value::List { .. } => Err(ShellError::GenericError(
error: format!("{var_name} should be a non-empty string or list<String>"), format!("{var_name} should be a non-empty string or list<String>"),
msg: "Specify an executable here".into(), "Specify an executable here".into(),
span: Some(value.span()), Some(value.span()),
help: Some(HELP_MSG.into()), Some(HELP_MSG.into()),
inner: vec![], vec![],
}), )),
x => Err(ShellError::CantConvert { x => Err(ShellError::CantConvert {
to_type: "string or list<string>".into(), to_type: "string or list<string>".into(),
from_type: x.get_type().to_string(), from_type: x.get_type().to_string(),
@ -81,7 +99,7 @@ fn get_editor_commandline(
pub fn get_editor( pub fn get_editor(
engine_state: &EngineState, engine_state: &EngineState,
stack: &Stack, stack: &mut Stack,
span: Span, span: Span,
) -> Result<(String, Vec<String>), ShellError> { ) -> Result<(String, Vec<String>), ShellError> {
let config = engine_state.get_config(); let config = engine_state.get_config();
@ -96,14 +114,13 @@ pub fn get_editor(
} else if let Some(value) = env_vars.get("VISUAL") { } else if let Some(value) = env_vars.get("VISUAL") {
get_editor_commandline(value, "$env.VISUAL") get_editor_commandline(value, "$env.VISUAL")
} else { } else {
Err(ShellError::GenericError { Err(ShellError::GenericError(
error: "No editor configured".into(), "No editor configured".into(),
msg: "Please specify one via `$env.config.buffer_editor` or `$env.EDITOR`/`$env.VISUAL`"
"Please specify one via `$env.config.buffer_editor` or `$env.EDITOR`/`$env.VISUAL`" .into(),
.into(), Some(span),
span: Some(span), Some(HELP_MSG.into()),
help: Some(HELP_MSG.into()), vec![],
inner: vec![], ))
})
} }
} }

View File

@ -0,0 +1,70 @@
[package]
authors = ["The Nushell Project Developers"]
description = "Nushell's dataframe commands based on polars."
edition = "2021"
license = "MIT"
name = "nu-cmd-dataframe"
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-cmd-dataframe"
version = "0.87.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
bench = false
[dependencies]
nu-engine = { path = "../nu-engine", version = "0.87.2" }
nu-parser = { path = "../nu-parser", version = "0.87.2" }
nu-protocol = { path = "../nu-protocol", version = "0.87.2" }
# Potential dependencies for extras
chrono = { version = "0.4", features = ["std", "unstable-locales"], default-features = false }
chrono-tz = "0.8"
fancy-regex = "0.11"
indexmap = { version = "2.1" }
num = { version = "0.4", optional = true }
serde = { version = "1.0", features = ["derive"] }
sqlparser = { version = "0.36.1", optional = true }
polars-io = { version = "0.33", features = ["avro"], optional = true }
[dependencies.polars]
features = [
"arg_where",
"checked_arithmetic",
"concat_str",
"cross_join",
"csv",
"cum_agg",
"default",
"dtype-categorical",
"dtype-datetime",
"dtype-struct",
"dtype-i8",
"dtype-i16",
"dtype-u8",
"dtype-u16",
"dynamic_group_by",
"ipc",
"is_in",
"json",
"lazy",
"object",
"parquet",
"random",
"rolling_window",
"rows",
"serde",
"serde-lazy",
"strings",
"to_dummies",
]
optional = true
version = "0.33"
[features]
dataframe = ["num", "polars", "polars-io", "sqlparser"]
default = []
[dev-dependencies]
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.87.2" }
nu-test-support = { path = "../nu-test-support", version = "0.87.2" }

View File

@ -0,0 +1,134 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::super::values::{Axis, Column, NuDataFrame};
#[derive(Clone)]
pub struct AppendDF;
impl Command for AppendDF {
fn name(&self) -> &str {
"dfr append"
}
fn usage(&self) -> &str {
"Appends a new dataframe."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("other", SyntaxShape::Any, "dataframe to be appended")
.switch("col", "appends in col orientation", Some('c'))
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Appends a dataframe as new columns",
example: r#"let a = ([[a b]; [1 2] [3 4]] | dfr into-df);
$a | dfr append $a"#,
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
),
Column::new(
"b".to_string(),
vec![Value::test_int(2), Value::test_int(4)],
),
Column::new(
"a_x".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
),
Column::new(
"b_x".to_string(),
vec![Value::test_int(2), Value::test_int(4)],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Appends a dataframe merging at the end of columns",
example: r#"let a = ([[a b]; [1 2] [3 4]] | dfr into-df);
$a | dfr append $a --col"#,
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a".to_string(),
vec![
Value::test_int(1),
Value::test_int(3),
Value::test_int(1),
Value::test_int(3),
],
),
Column::new(
"b".to_string(),
vec![
Value::test_int(2),
Value::test_int(4),
Value::test_int(2),
Value::test_int(4),
],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let other: Value = call.req(engine_state, stack, 0)?;
let axis = if call.has_flag("col") {
Axis::Column
} else {
Axis::Row
};
let df_other = NuDataFrame::try_from_value(other)?;
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
df.append_df(&df_other, axis, call.head)
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(AppendDF {})])
}
}

View File

@ -1,19 +1,16 @@
use crate::PolarsPlugin;
use super::super::values::NuDataFrame; use super::super::values::NuDataFrame;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{ use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value, ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
}; };
#[derive(Clone)] #[derive(Clone)]
pub struct ColumnsDF; pub struct ColumnsDF;
impl PluginCommand for ColumnsDF { impl Command for ColumnsDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars columns" "dfr columns"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -29,7 +26,7 @@ impl PluginCommand for ColumnsDF {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Dataframe columns", description: "Dataframe columns",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars columns", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr columns",
result: Some(Value::list( result: Some(Value::list(
vec![Value::test_string("a"), Value::test_string("b")], vec![Value::test_string("a"), Value::test_string("b")],
Span::test_data(), Span::test_data(),
@ -39,21 +36,22 @@ impl PluginCommand for ColumnsDF {
fn run( fn run(
&self, &self,
plugin: &Self::Plugin, engine_state: &EngineState,
_engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, LabeledError> { ) -> Result<PipelineData, ShellError> {
command(plugin, call, input).map_err(|e| e.into()) command(engine_state, stack, call, input)
} }
} }
fn command( fn command(
plugin: &PolarsPlugin, _engine_state: &EngineState,
call: &EvaluatedCall, _stack: &mut Stack,
call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let names: Vec<Value> = df let names: Vec<Value> = df
.as_ref() .as_ref()
@ -69,11 +67,11 @@ fn command(
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*; use super::*;
use crate::test::test_polars_plugin_command;
#[test] #[test]
fn test_examples() -> Result<(), ShellError> { fn test_examples() {
test_polars_plugin_command(&ColumnsDF) test_dataframe(vec![Box::new(ColumnsDF {})])
} }
} }

View File

@ -0,0 +1,121 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::super::values::utils::convert_columns;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)]
pub struct DropDF;
impl Command for DropDF {
fn name(&self) -> &str {
"dfr drop"
}
fn usage(&self) -> &str {
"Creates a new dataframe by dropping the selected columns."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.rest("rest", SyntaxShape::Any, "column names to be dropped")
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "drop column a",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr drop a",
result: Some(
NuDataFrame::try_from_columns(vec![Column::new(
"b".to_string(),
vec![Value::test_int(2), Value::test_int(4)],
)])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
let (col_string, col_span) = convert_columns(columns, call.head)?;
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let new_df = col_string
.first()
.ok_or_else(|| {
ShellError::GenericError(
"Empty names list".into(),
"No column names were found".into(),
Some(col_span),
None,
Vec::new(),
)
})
.and_then(|col| {
df.as_ref().drop(&col.item).map_err(|e| {
ShellError::GenericError(
"Error dropping column".into(),
e.to_string(),
Some(col.span),
None,
Vec::new(),
)
})
})?;
// If there are more columns in the drop selection list, these
// are added from the resulting dataframe
col_string
.iter()
.skip(1)
.try_fold(new_df, |new_df, col| {
new_df.drop(&col.item).map_err(|e| {
ShellError::GenericError(
"Error dropping column".into(),
e.to_string(),
Some(col.span),
None,
Vec::new(),
)
})
})
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(DropDF {})])
}
}

View File

@ -0,0 +1,124 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use polars::prelude::UniqueKeepStrategy;
use super::super::values::utils::convert_columns_string;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)]
pub struct DropDuplicates;
impl Command for DropDuplicates {
fn name(&self) -> &str {
"dfr drop-duplicates"
}
fn usage(&self) -> &str {
"Drops duplicate values in dataframe."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.optional(
"subset",
SyntaxShape::Table(vec![]),
"subset of columns to drop duplicates",
)
.switch("maintain", "maintain order", Some('m'))
.switch(
"last",
"keeps last duplicate value (by default keeps first)",
Some('l'),
)
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "drop duplicates",
example: "[[a b]; [1 2] [3 4] [1 2]] | dfr into-df | dfr drop-duplicates",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a".to_string(),
vec![Value::test_int(3), Value::test_int(1)],
),
Column::new(
"b".to_string(),
vec![Value::test_int(4), Value::test_int(2)],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let columns: Option<Vec<Value>> = call.opt(engine_state, stack, 0)?;
let (subset, col_span) = match columns {
Some(cols) => {
let (agg_string, col_span) = convert_columns_string(cols, call.head)?;
(Some(agg_string), col_span)
}
None => (None, call.head),
};
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let subset_slice = subset.as_ref().map(|cols| &cols[..]);
let keep_strategy = if call.has_flag("last") {
UniqueKeepStrategy::Last
} else {
UniqueKeepStrategy::First
};
df.as_ref()
.unique(subset_slice, keep_strategy, None)
.map_err(|e| {
ShellError::GenericError(
"Error dropping duplicates".into(),
e.to_string(),
Some(col_span),
None,
Vec::new(),
)
})
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(DropDuplicates {})])
}
}

View File

@ -0,0 +1,140 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::super::values::utils::convert_columns_string;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)]
pub struct DropNulls;
impl Command for DropNulls {
fn name(&self) -> &str {
"dfr drop-nulls"
}
fn usage(&self) -> &str {
"Drops null values in dataframe."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.optional(
"subset",
SyntaxShape::Table(vec![]),
"subset of columns to drop nulls",
)
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "drop null values in dataframe",
example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | dfr into-df);
let res = ($df.b / $df.b);
let a = ($df | dfr with-column $res --name res);
$a | dfr drop-nulls"#,
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a".to_string(),
vec![Value::test_int(1), Value::test_int(1)],
),
Column::new(
"b".to_string(),
vec![Value::test_int(2), Value::test_int(2)],
),
Column::new(
"res".to_string(),
vec![Value::test_int(1), Value::test_int(1)],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "drop null values in dataframe",
example: r#"let s = ([1 2 0 0 3 4] | dfr into-df);
($s / $s) | dfr drop-nulls"#,
result: Some(
NuDataFrame::try_from_columns(vec![Column::new(
"div_0_0".to_string(),
vec![
Value::test_int(1),
Value::test_int(1),
Value::test_int(1),
Value::test_int(1),
],
)])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let columns: Option<Vec<Value>> = call.opt(engine_state, stack, 0)?;
let (subset, col_span) = match columns {
Some(cols) => {
let (agg_string, col_span) = convert_columns_string(cols, call.head)?;
(Some(agg_string), col_span)
}
None => (None, call.head),
};
let subset_slice = subset.as_ref().map(|cols| &cols[..]);
df.as_ref()
.drop_nulls(subset_slice)
.map_err(|e| {
ShellError::GenericError(
"Error dropping nulls".into(),
e.to_string(),
Some(col_span),
None,
Vec::new(),
)
})
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::super::WithColumn;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(DropNulls {}), Box::new(WithColumn {})])
}
}

View File

@ -0,0 +1,104 @@
use super::super::values::{Column, NuDataFrame};
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
pub struct DataTypes;
impl Command for DataTypes {
fn name(&self) -> &str {
"dfr dtypes"
}
fn usage(&self) -> &str {
"Show dataframe data types."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Dataframe dtypes",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr dtypes",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"column".to_string(),
vec![Value::test_string("a"), Value::test_string("b")],
),
Column::new(
"dtype".to_string(),
vec![Value::test_string("i64"), Value::test_string("i64")],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let mut dtypes: Vec<Value> = Vec::new();
let names: Vec<Value> = df
.as_ref()
.get_column_names()
.iter()
.map(|v| {
let dtype = df
.as_ref()
.column(v)
.expect("using name from list of names from dataframe")
.dtype();
let dtype_str = dtype.to_string();
dtypes.push(Value::string(dtype_str, call.head));
Value::string(*v, call.head)
})
.collect();
let names_col = Column::new("column".to_string(), names);
let dtypes_col = Column::new("dtype".to_string(), dtypes);
NuDataFrame::try_from_columns(vec![names_col, dtypes_col])
.map(|df| PipelineData::Value(df.into_value(call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(DataTypes {})])
}
}

View File

@ -1,19 +1,17 @@
use super::super::values::NuDataFrame; use super::super::values::NuDataFrame;
use crate::{values::CustomValueSupport, PolarsPlugin};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{ use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Type,
}; };
use polars::{prelude::*, series::Series}; use polars::{prelude::*, series::Series};
#[derive(Clone)] #[derive(Clone)]
pub struct Dummies; pub struct Dummies;
impl PluginCommand for Dummies { impl Command for Dummies {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars dummies" "dfr dummies"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -34,9 +32,9 @@ impl PluginCommand for Dummies {
vec![ vec![
Example { Example {
description: "Create new dataframe with dummy variables from a dataframe", description: "Create new dataframe with dummy variables from a dataframe",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars dummies", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr dummies",
result: Some( result: Some(
NuDataFrame::try_from_series_vec( NuDataFrame::try_from_series(
vec![ vec![
Series::new("a_1", &[1_u8, 0]), Series::new("a_1", &[1_u8, 0]),
Series::new("a_3", &[0_u8, 1]), Series::new("a_3", &[0_u8, 1]),
@ -51,9 +49,9 @@ impl PluginCommand for Dummies {
}, },
Example { Example {
description: "Create new dataframe with dummy variables from a series", description: "Create new dataframe with dummy variables from a series",
example: "[1 2 2 3 3] | polars into-df | polars dummies", example: "[1 2 2 3 3] | dfr into-df | dfr dummies",
result: Some( result: Some(
NuDataFrame::try_from_series_vec( NuDataFrame::try_from_series(
vec![ vec![
Series::new("0_1", &[1_u8, 0, 0, 0, 0]), Series::new("0_1", &[1_u8, 0, 0, 0, 0]),
Series::new("0_2", &[0_u8, 1, 1, 0, 0]), Series::new("0_2", &[0_u8, 1, 1, 0, 0]),
@ -70,47 +68,45 @@ impl PluginCommand for Dummies {
fn run( fn run(
&self, &self,
plugin: &Self::Plugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, LabeledError> { ) -> Result<PipelineData, ShellError> {
command(plugin, engine, call, input).map_err(LabeledError::from) command(engine_state, stack, call, input)
} }
} }
fn command( fn command(
plugin: &PolarsPlugin, _engine_state: &EngineState,
engine: &EngineInterface, _stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let drop_first: bool = call.has_flag("drop-first")?; let drop_first: bool = call.has_flag("drop-first");
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let polars_df = df.as_ref()
df.as_ref() .to_dummies(None, drop_first)
.to_dummies(None, drop_first) .map_err(|e| {
.map_err(|e| ShellError::GenericError { ShellError::GenericError(
error: "Error calculating dummies".into(), "Error calculating dummies".into(),
msg: e.to_string(), e.to_string(),
span: Some(call.head), Some(call.head),
help: Some("The only allowed column types for dummies are String or Int".into()), Some("The only allowed column types for dummies are String or Int".into()),
inner: vec![], Vec::new(),
})?; )
})
let df: NuDataFrame = polars_df.into(); .map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
df.to_pipeline_data(plugin, engine, call.head)
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::test::test_polars_plugin_command; use super::super::super::test_dataframe::test_dataframe;
use super::*; use super::*;
#[test] #[test]
fn test_examples() -> Result<(), ShellError> { fn test_examples() {
test_polars_plugin_command(&Dummies) test_dataframe(vec![Box::new(Dummies {})])
} }
} }

View File

@ -0,0 +1,160 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use polars::prelude::LazyFrame;
use crate::dataframe::values::{NuExpression, NuLazyFrame};
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)]
pub struct FilterWith;
impl Command for FilterWith {
fn name(&self) -> &str {
"dfr filter-with"
}
fn usage(&self) -> &str {
"Filters dataframe using a mask or expression as reference."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
"mask or expression",
SyntaxShape::Any,
"boolean mask used to filter data",
)
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe or lazyframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Filter dataframe using a bool mask",
example: r#"let mask = ([true false] | dfr into-df);
[[a b]; [1 2] [3 4]] | dfr into-df | dfr filter-with $mask"#,
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new("a".to_string(), vec![Value::test_int(1)]),
Column::new("b".to_string(), vec![Value::test_int(2)]),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Filter dataframe using an expression",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr filter-with ((dfr col a) > 1)",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new("a".to_string(), vec![Value::test_int(3)]),
Column::new("b".to_string(), vec![Value::test_int(4)]),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let value = input.into_value(call.head);
if NuLazyFrame::can_downcast(&value) {
let df = NuLazyFrame::try_from_value(value)?;
command_lazy(engine_state, stack, call, df)
} else {
let df = NuDataFrame::try_from_value(value)?;
command_eager(engine_state, stack, call, df)
}
}
}
fn command_eager(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
df: NuDataFrame,
) -> Result<PipelineData, ShellError> {
let mask_value: Value = call.req(engine_state, stack, 0)?;
let mask_span = mask_value.span();
if NuExpression::can_downcast(&mask_value) {
let expression = NuExpression::try_from_value(mask_value)?;
let lazy = NuLazyFrame::new(true, df.lazy());
let lazy = lazy.apply_with_expr(expression, LazyFrame::filter);
Ok(PipelineData::Value(
NuLazyFrame::into_value(lazy, call.head)?,
None,
))
} else {
let mask = NuDataFrame::try_from_value(mask_value)?.as_series(mask_span)?;
let mask = mask.bool().map_err(|e| {
ShellError::GenericError(
"Error casting to bool".into(),
e.to_string(),
Some(mask_span),
Some("Perhaps you want to use a series with booleans as mask".into()),
Vec::new(),
)
})?;
df.as_ref()
.filter(mask)
.map_err(|e| {
ShellError::GenericError(
"Error filtering dataframe".into(),
e.to_string(),
Some(call.head),
Some("The only allowed column types for dummies are String or Int".into()),
Vec::new(),
)
})
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}
}
fn command_lazy(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> {
let expr: Value = call.req(engine_state, stack, 0)?;
let expr = NuExpression::try_from_value(expr)?;
let lazy = lazy.apply_with_expr(expr, LazyFrame::filter);
Ok(PipelineData::Value(
NuLazyFrame::into_value(lazy, call.head)?,
None,
))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
use crate::dataframe::expressions::ExprCol;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(FilterWith {}), Box::new(ExprCol {})])
}
}

View File

@ -0,0 +1,143 @@
use super::super::values::{Column, NuDataFrame, NuExpression};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct FirstDF;
impl Command for FirstDF {
fn name(&self) -> &str {
"dfr first"
}
fn usage(&self) -> &str {
"Show only the first number of rows or create a first expression"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.optional(
"rows",
SyntaxShape::Int,
"starting from the front, the number of rows to return",
)
.input_output_types(vec![
(
Type::Custom("expression".into()),
Type::Custom("expression".into()),
),
(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
),
])
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Return the first row of a dataframe",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr first",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new("a".to_string(), vec![Value::test_int(1)]),
Column::new("b".to_string(), vec![Value::test_int(2)]),
])
.expect("should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Return the first two rows of a dataframe",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr first 2",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
),
Column::new(
"b".to_string(),
vec![Value::test_int(2), Value::test_int(4)],
),
])
.expect("should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Creates a first expression from a column",
example: "dfr col a | dfr first",
result: None,
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let value = input.into_value(call.head);
if NuDataFrame::can_downcast(&value) {
let df = NuDataFrame::try_from_value(value)?;
command(engine_state, stack, call, df)
} else {
let expr = NuExpression::try_from_value(value)?;
let expr: NuExpression = expr.into_polars().first().into();
Ok(PipelineData::Value(
NuExpression::into_value(expr, call.head),
None,
))
}
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
df: NuDataFrame,
) -> Result<PipelineData, ShellError> {
let rows: Option<usize> = call.opt(engine_state, stack, 0)?;
let rows = rows.unwrap_or(1);
let res = df.as_ref().head(Some(rows));
Ok(PipelineData::Value(
NuDataFrame::dataframe_into_value(res, call.head),
None,
))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::{build_test_engine_state, test_dataframe_example};
use super::*;
use crate::dataframe::lazy::aggregate::LazyAggregate;
use crate::dataframe::lazy::groupby::ToLazyGroupBy;
#[test]
fn test_examples_dataframe() {
let mut engine_state = build_test_engine_state(vec![Box::new(FirstDF {})]);
test_dataframe_example(&mut engine_state, &FirstDF.examples()[0]);
test_dataframe_example(&mut engine_state, &FirstDF.examples()[1]);
}
#[test]
fn test_examples_expression() {
let mut engine_state = build_test_engine_state(vec![
Box::new(FirstDF {}),
Box::new(LazyAggregate {}),
Box::new(ToLazyGroupBy {}),
]);
test_dataframe_example(&mut engine_state, &FirstDF.examples()[2]);
}
}

View File

@ -0,0 +1,94 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::dataframe::values::utils::convert_columns_string;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)]
pub struct GetDF;
impl Command for GetDF {
fn name(&self) -> &str {
"dfr get"
}
fn usage(&self) -> &str {
"Creates dataframe with the selected columns."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.rest("rest", SyntaxShape::Any, "column names to sort dataframe")
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Returns the selected column",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr get a",
result: Some(
NuDataFrame::try_from_columns(vec![Column::new(
"a".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
)])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
let (col_string, col_span) = convert_columns_string(columns, call.head)?;
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
df.as_ref()
.select(col_string)
.map_err(|e| {
ShellError::GenericError(
"Error selecting columns".into(),
e.to_string(),
Some(col_span),
None,
Vec::new(),
)
})
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(GetDF {})])
}
}

View File

@ -0,0 +1,120 @@
use super::super::values::{utils::DEFAULT_ROWS, Column, NuDataFrame, NuExpression};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct LastDF;
impl Command for LastDF {
fn name(&self) -> &str {
"dfr last"
}
fn usage(&self) -> &str {
"Creates new dataframe with tail rows or creates a last expression."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.optional("rows", SyntaxShape::Int, "Number of rows for tail")
.input_output_types(vec![
(
Type::Custom("expression".into()),
Type::Custom("expression".into()),
),
(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
),
])
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Create new dataframe with last rows",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr last 1",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new("a".to_string(), vec![Value::test_int(3)]),
Column::new("b".to_string(), vec![Value::test_int(4)]),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Creates a last expression from a column",
example: "dfr col a | dfr last",
result: None,
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let value = input.into_value(call.head);
if NuDataFrame::can_downcast(&value) {
let df = NuDataFrame::try_from_value(value)?;
command(engine_state, stack, call, df)
} else {
let expr = NuExpression::try_from_value(value)?;
let expr: NuExpression = expr.into_polars().last().into();
Ok(PipelineData::Value(
NuExpression::into_value(expr, call.head),
None,
))
}
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
df: NuDataFrame,
) -> Result<PipelineData, ShellError> {
let rows: Option<usize> = call.opt(engine_state, stack, 0)?;
let rows = rows.unwrap_or(DEFAULT_ROWS);
let res = df.as_ref().tail(Some(rows));
Ok(PipelineData::Value(
NuDataFrame::dataframe_into_value(res, call.head),
None,
))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::{build_test_engine_state, test_dataframe_example};
use super::*;
use crate::dataframe::lazy::aggregate::LazyAggregate;
use crate::dataframe::lazy::groupby::ToLazyGroupBy;
#[test]
fn test_examples_dataframe() {
let mut engine_state = build_test_engine_state(vec![Box::new(LastDF {})]);
test_dataframe_example(&mut engine_state, &LastDF.examples()[0]);
}
#[test]
fn test_examples_expression() {
let mut engine_state = build_test_engine_state(vec![
Box::new(LastDF {}),
Box::new(LazyAggregate {}),
Box::new(ToLazyGroupBy {}),
]);
test_dataframe_example(&mut engine_state, &LastDF.examples()[1]);
}
}

View File

@ -0,0 +1,73 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
};
use crate::dataframe::values::NuDataFrame;
#[derive(Clone)]
pub struct ListDF;
impl Command for ListDF {
fn name(&self) -> &str {
"dfr ls"
}
fn usage(&self) -> &str {
"Lists stored dataframes."
}
fn signature(&self) -> Signature {
Signature::build(self.name()).category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Creates a new dataframe and shows it in the dataframe list",
example: r#"let test = ([[a b];[1 2] [3 4]] | dfr into-df);
ls"#,
result: None,
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let mut vals: Vec<(String, Value)> = vec![];
for overlay_frame in engine_state.active_overlays(&[]) {
for var in &overlay_frame.vars {
if let Ok(value) = stack.get_var(*var.1, call.head) {
let name = String::from_utf8_lossy(var.0).to_string();
vals.push((name, value));
}
}
}
let vals = vals
.into_iter()
.filter_map(|(name, value)| {
NuDataFrame::try_from_value(value).ok().map(|df| (name, df))
})
.map(|(name, df)| {
Value::record(
record! {
"name" => Value::string(name, call.head),
"columns" => Value::int(df.as_ref().width() as i64, call.head),
"rows" => Value::int(df.as_ref().height() as i64, call.head),
},
call.head,
)
})
.collect::<Vec<Value>>();
let list = Value::list(vals, call.head);
Ok(list.into_pipeline_data())
}
}

View File

@ -1,23 +1,21 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Spanned, ast::Call,
SyntaxShape, Type, Value, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
}; };
use crate::{ use crate::dataframe::values::utils::convert_columns_string;
dataframe::values::utils::convert_columns_string, values::CustomValueSupport, PolarsPlugin,
};
use super::super::values::{Column, NuDataFrame}; use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct MeltDF; pub struct MeltDF;
impl PluginCommand for MeltDF { impl Command for MeltDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars melt" "dfr melt"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -61,7 +59,7 @@ impl PluginCommand for MeltDF {
vec![Example { vec![Example {
description: "melt dataframe", description: "melt dataframe",
example: example:
"[[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | polars into-df | polars melt -c [b c] -v [a d]", "[[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | dfr into-df | dfr melt -c [b c] -v [a d]",
result: Some( result: Some(
NuDataFrame::try_from_columns(vec![ NuDataFrame::try_from_columns(vec![
Column::new( Column::new(
@ -108,7 +106,7 @@ impl PluginCommand for MeltDF {
Value::test_string("c"), Value::test_string("c"),
], ],
), ),
], None) ])
.expect("simple df for test should not fail") .expect("simple df for test should not fail")
.into_value(Span::test_data()), .into_value(Span::test_data()),
), ),
@ -117,31 +115,36 @@ impl PluginCommand for MeltDF {
fn run( fn run(
&self, &self,
plugin: &Self::Plugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, LabeledError> { ) -> Result<PipelineData, ShellError> {
command(plugin, engine, call, input).map_err(LabeledError::from) command(engine_state, stack, call, input)
} }
} }
fn command( fn command(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let id_col: Vec<Value> = call.get_flag("columns")?.expect("required value"); let id_col: Vec<Value> = call
let val_col: Vec<Value> = call.get_flag("values")?.expect("required value"); .get_flag(engine_state, stack, "columns")?
.expect("required value");
let val_col: Vec<Value> = call
.get_flag(engine_state, stack, "values")?
.expect("required value");
let value_name: Option<Spanned<String>> = call.get_flag("value-name")?; let value_name: Option<Spanned<String>> = call.get_flag(engine_state, stack, "value-name")?;
let variable_name: Option<Spanned<String>> = call.get_flag("variable-name")?; let variable_name: Option<Spanned<String>> =
call.get_flag(engine_state, stack, "variable-name")?;
let (id_col_string, id_col_span) = convert_columns_string(id_col, call.head)?; let (id_col_string, id_col_span) = convert_columns_string(id_col, call.head)?;
let (val_col_string, val_col_span) = convert_columns_string(val_col, call.head)?; let (val_col_string, val_col_span) = convert_columns_string(val_col, call.head)?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
check_column_datatypes(df.as_ref(), &id_col_string, id_col_span)?; check_column_datatypes(df.as_ref(), &id_col_string, id_col_span)?;
check_column_datatypes(df.as_ref(), &val_col_string, val_col_span)?; check_column_datatypes(df.as_ref(), &val_col_string, val_col_span)?;
@ -149,38 +152,44 @@ fn command(
let mut res = df let mut res = df
.as_ref() .as_ref()
.melt(&id_col_string, &val_col_string) .melt(&id_col_string, &val_col_string)
.map_err(|e| ShellError::GenericError { .map_err(|e| {
error: "Error calculating melt".into(), ShellError::GenericError(
msg: e.to_string(), "Error calculating melt".into(),
span: Some(call.head), e.to_string(),
help: None, Some(call.head),
inner: vec![], None,
Vec::new(),
)
})?; })?;
if let Some(name) = &variable_name { if let Some(name) = &variable_name {
res.rename("variable", &name.item) res.rename("variable", &name.item).map_err(|e| {
.map_err(|e| ShellError::GenericError { ShellError::GenericError(
error: "Error renaming column".into(), "Error renaming column".into(),
msg: e.to_string(), e.to_string(),
span: Some(name.span), Some(name.span),
help: None, None,
inner: vec![], Vec::new(),
})?; )
})?;
} }
if let Some(name) = &value_name { if let Some(name) = &value_name {
res.rename("value", &name.item) res.rename("value", &name.item).map_err(|e| {
.map_err(|e| ShellError::GenericError { ShellError::GenericError(
error: "Error renaming column".into(), "Error renaming column".into(),
msg: e.to_string(), e.to_string(),
span: Some(name.span), Some(name.span),
help: None, None,
inner: vec![], Vec::new(),
})?; )
})?;
} }
let res = NuDataFrame::new(false, res); Ok(PipelineData::Value(
res.to_pipeline_data(plugin, engine, call.head) NuDataFrame::dataframe_into_value(res, call.head),
None,
))
} }
fn check_column_datatypes<T: AsRef<str>>( fn check_column_datatypes<T: AsRef<str>>(
@ -189,50 +198,50 @@ fn check_column_datatypes<T: AsRef<str>>(
col_span: Span, col_span: Span,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
if cols.is_empty() { if cols.is_empty() {
return Err(ShellError::GenericError { return Err(ShellError::GenericError(
error: "Merge error".into(), "Merge error".into(),
msg: "empty column list".into(), "empty column list".into(),
span: Some(col_span), Some(col_span),
help: None, None,
inner: vec![], Vec::new(),
}); ));
} }
// Checking if they are same type // Checking if they are same type
if cols.len() > 1 { if cols.len() > 1 {
for w in cols.windows(2) { for w in cols.windows(2) {
let l_series = df let l_series = df.column(w[0].as_ref()).map_err(|e| {
.column(w[0].as_ref()) ShellError::GenericError(
.map_err(|e| ShellError::GenericError { "Error selecting columns".into(),
error: "Error selecting columns".into(), e.to_string(),
msg: e.to_string(), Some(col_span),
span: Some(col_span), None,
help: None, Vec::new(),
inner: vec![], )
})?; })?;
let r_series = df let r_series = df.column(w[1].as_ref()).map_err(|e| {
.column(w[1].as_ref()) ShellError::GenericError(
.map_err(|e| ShellError::GenericError { "Error selecting columns".into(),
error: "Error selecting columns".into(), e.to_string(),
msg: e.to_string(), Some(col_span),
span: Some(col_span), None,
help: None, Vec::new(),
inner: vec![], )
})?; })?;
if l_series.dtype() != r_series.dtype() { if l_series.dtype() != r_series.dtype() {
return Err(ShellError::GenericError { return Err(ShellError::GenericError(
error: "Merge error".into(), "Merge error".into(),
msg: "found different column types in list".into(), "found different column types in list".into(),
span: Some(col_span), Some(col_span),
help: Some(format!( Some(format!(
"datatypes {} and {} are incompatible", "datatypes {} and {} are incompatible",
l_series.dtype(), l_series.dtype(),
r_series.dtype() r_series.dtype()
)), )),
inner: vec![], Vec::new(),
}); ));
} }
} }
} }
@ -242,12 +251,11 @@ fn check_column_datatypes<T: AsRef<str>>(
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::test::test_polars_plugin_command; use super::super::super::test_dataframe::test_dataframe;
use super::*; use super::*;
#[test] #[test]
fn test_examples() -> Result<(), ShellError> { fn test_examples() {
test_polars_plugin_command(&MeltDF) test_dataframe(vec![Box::new(MeltDF {})])
} }
} }

View File

@ -1,20 +1,20 @@
mod append; mod append;
mod cast;
mod columns; mod columns;
mod drop; mod drop;
mod drop_duplicates; mod drop_duplicates;
mod drop_nulls; mod drop_nulls;
mod dtypes;
mod dummies; mod dummies;
mod filter_with; mod filter_with;
mod first; mod first;
mod get; mod get;
mod last; mod last;
mod list;
mod melt; mod melt;
mod open; mod open;
mod query_df; mod query_df;
mod rename; mod rename;
mod sample; mod sample;
mod schema;
mod shape; mod shape;
mod slice; mod slice;
mod sql_context; mod sql_context;
@ -30,29 +30,29 @@ mod to_nu;
mod to_parquet; mod to_parquet;
mod with_column; mod with_column;
use crate::PolarsPlugin; use nu_protocol::engine::StateWorkingSet;
pub use self::open::OpenDataFrame; pub use self::open::OpenDataFrame;
pub use append::AppendDF; pub use append::AppendDF;
pub use cast::CastDF;
pub use columns::ColumnsDF; pub use columns::ColumnsDF;
pub use drop::DropDF; pub use drop::DropDF;
pub use drop_duplicates::DropDuplicates; pub use drop_duplicates::DropDuplicates;
pub use drop_nulls::DropNulls; pub use drop_nulls::DropNulls;
pub use dtypes::DataTypes;
pub use dummies::Dummies; pub use dummies::Dummies;
pub use filter_with::FilterWith; pub use filter_with::FilterWith;
pub use first::FirstDF; pub use first::FirstDF;
pub use get::GetDF; pub use get::GetDF;
pub use last::LastDF; pub use last::LastDF;
pub use list::ListDF;
pub use melt::MeltDF; pub use melt::MeltDF;
use nu_plugin::PluginCommand;
pub use query_df::QueryDf; pub use query_df::QueryDf;
pub use rename::RenameDF; pub use rename::RenameDF;
pub use sample::SampleDF; pub use sample::SampleDF;
pub use schema::SchemaCmd;
pub use shape::ShapeDF; pub use shape::ShapeDF;
pub use slice::SliceDF; pub use slice::SliceDF;
pub use sql_context::SQLContext; pub use sql_context::SQLContext;
pub use sql_expr::parse_sql_expr;
pub use summary::Summary; pub use summary::Summary;
pub use take::TakeDF; pub use take::TakeDF;
pub use to_arrow::ToArrow; pub use to_arrow::ToArrow;
@ -64,36 +64,46 @@ pub use to_nu::ToNu;
pub use to_parquet::ToParquet; pub use to_parquet::ToParquet;
pub use with_column::WithColumn; pub use with_column::WithColumn;
pub(crate) fn eager_commands() -> Vec<Box<dyn PluginCommand<Plugin = PolarsPlugin>>> { pub fn add_eager_decls(working_set: &mut StateWorkingSet) {
vec![ macro_rules! bind_command {
Box::new(AppendDF), ( $command:expr ) => {
Box::new(CastDF), working_set.add_decl(Box::new($command));
Box::new(ColumnsDF), };
Box::new(DropDF), ( $( $command:expr ),* ) => {
Box::new(DropDuplicates), $( working_set.add_decl(Box::new($command)); )*
Box::new(DropNulls), };
Box::new(Dummies), }
Box::new(FilterWith),
Box::new(GetDF), // Dataframe commands
Box::new(OpenDataFrame), bind_command!(
Box::new(MeltDF), AppendDF,
Box::new(Summary), ColumnsDF,
Box::new(FirstDF), DataTypes,
Box::new(LastDF), Summary,
Box::new(RenameDF), DropDF,
Box::new(SampleDF), DropDuplicates,
Box::new(ShapeDF), DropNulls,
Box::new(SliceDF), Dummies,
Box::new(SchemaCmd), FilterWith,
Box::new(TakeDF), FirstDF,
Box::new(ToNu), GetDF,
Box::new(ToArrow), LastDF,
Box::new(ToAvro), ListDF,
Box::new(ToDataFrame), MeltDF,
Box::new(ToCSV), OpenDataFrame,
Box::new(ToJsonLines), QueryDf,
Box::new(ToParquet), RenameDF,
Box::new(QueryDf), SampleDF,
Box::new(WithColumn), ShapeDF,
] SliceDF,
TakeDF,
ToArrow,
ToAvro,
ToCSV,
ToDataFrame,
ToNu,
ToParquet,
ToJsonLines,
WithColumn
);
} }

View File

@ -0,0 +1,512 @@
use super::super::values::{NuDataFrame, NuLazyFrame};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use std::{fs::File, io::BufReader, path::PathBuf};
use polars::prelude::{
CsvEncoding, CsvReader, IpcReader, JsonFormat, JsonReader, LazyCsvReader, LazyFileListReader,
LazyFrame, ParallelStrategy, ParquetReader, ScanArgsIpc, ScanArgsParquet, SerReader,
};
use polars_io::avro::AvroReader;
#[derive(Clone)]
pub struct OpenDataFrame;
impl Command for OpenDataFrame {
fn name(&self) -> &str {
"dfr open"
}
fn usage(&self) -> &str {
"Opens CSV, JSON, JSON lines, arrow, avro, or parquet file to create dataframe."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
"file",
SyntaxShape::Filepath,
"file path to load values from",
)
.switch("lazy", "creates a lazy dataframe", Some('l'))
.named(
"type",
SyntaxShape::String,
"File type: csv, tsv, json, parquet, arrow, avro. If omitted, derive from file extension",
Some('t'),
)
.named(
"delimiter",
SyntaxShape::String,
"file delimiter character. CSV file",
Some('d'),
)
.switch(
"no-header",
"Indicates if file doesn't have header. CSV file",
None,
)
.named(
"infer-schema",
SyntaxShape::Number,
"Number of rows to infer the schema of the file. CSV file",
None,
)
.named(
"skip-rows",
SyntaxShape::Number,
"Number of rows to skip from file. CSV file",
None,
)
.named(
"columns",
SyntaxShape::List(Box::new(SyntaxShape::String)),
"Columns to be selected from csv file. CSV and Parquet file",
None,
)
.input_output_type(Type::Any, Type::Custom("dataframe".into()))
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Takes a file name and creates a dataframe",
example: "dfr open test.csv",
result: None,
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let type_option: Option<Spanned<String>> = call.get_flag(engine_state, stack, "type")?;
let type_id = match &type_option {
Some(ref t) => Some((t.item.to_owned(), "Invalid type", t.span)),
None => file.item.extension().map(|e| {
(
e.to_string_lossy().into_owned(),
"Invalid extension",
file.span,
)
}),
};
match type_id {
Some((e, msg, blamed)) => match e.as_str() {
"csv" | "tsv" => from_csv(engine_state, stack, call),
"parquet" | "parq" => from_parquet(engine_state, stack, call),
"ipc" | "arrow" => from_ipc(engine_state, stack, call),
"json" => from_json(engine_state, stack, call),
"jsonl" => from_jsonl(engine_state, stack, call),
"avro" => from_avro(engine_state, stack, call),
_ => Err(ShellError::FileNotFoundCustom(
format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"),
blamed,
)),
},
None => Err(ShellError::FileNotFoundCustom(
"File without extension".into(),
file.span,
)),
}
.map(|value| PipelineData::Value(value, None))
}
fn from_parquet(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Value, ShellError> {
if call.has_flag("lazy") {
let file: String = call.req(engine_state, stack, 0)?;
let args = ScanArgsParquet {
n_rows: None,
cache: true,
parallel: ParallelStrategy::Auto,
rechunk: false,
row_count: None,
low_memory: false,
cloud_options: None,
use_statistics: false,
};
let df: NuLazyFrame = LazyFrame::scan_parquet(file, args)
.map_err(|e| {
ShellError::GenericError(
"Parquet reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
df.into_value(call.head)
} else {
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
let r = File::open(&file.item).map_err(|e| {
ShellError::GenericError(
"Error opening file".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let reader = ParquetReader::new(r);
let reader = match columns {
None => reader,
Some(columns) => reader.with_columns(Some(columns)),
};
let df: NuDataFrame = reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"Parquet reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
Ok(df.into_value(call.head))
}
}
fn from_avro(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Value, ShellError> {
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
let r = File::open(&file.item).map_err(|e| {
ShellError::GenericError(
"Error opening file".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let reader = AvroReader::new(r);
let reader = match columns {
None => reader,
Some(columns) => reader.with_columns(Some(columns)),
};
let df: NuDataFrame = reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"Avro reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
Ok(df.into_value(call.head))
}
fn from_ipc(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Value, ShellError> {
if call.has_flag("lazy") {
let file: String = call.req(engine_state, stack, 0)?;
let args = ScanArgsIpc {
n_rows: None,
cache: true,
rechunk: false,
row_count: None,
memmap: true,
};
let df: NuLazyFrame = LazyFrame::scan_ipc(file, args)
.map_err(|e| {
ShellError::GenericError(
"IPC reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
df.into_value(call.head)
} else {
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
let r = File::open(&file.item).map_err(|e| {
ShellError::GenericError(
"Error opening file".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let reader = IpcReader::new(r);
let reader = match columns {
None => reader,
Some(columns) => reader.with_columns(Some(columns)),
};
let df: NuDataFrame = reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"IPC reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
Ok(df.into_value(call.head))
}
}
fn from_json(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Value, ShellError> {
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let file = File::open(&file.item).map_err(|e| {
ShellError::GenericError(
"Error opening file".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let buf_reader = BufReader::new(file);
let reader = JsonReader::new(buf_reader);
let df: NuDataFrame = reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"Json reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
Ok(df.into_value(call.head))
}
fn from_jsonl(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Value, ShellError> {
let infer_schema: Option<usize> = call.get_flag(engine_state, stack, "infer-schema")?;
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let file = File::open(&file.item).map_err(|e| {
ShellError::GenericError(
"Error opening file".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?;
let buf_reader = BufReader::new(file);
let reader = JsonReader::new(buf_reader)
.with_json_format(JsonFormat::JsonLines)
.infer_schema_len(infer_schema);
let df: NuDataFrame = reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"Json lines reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
Ok(df.into_value(call.head))
}
fn from_csv(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<Value, ShellError> {
let delimiter: Option<Spanned<String>> = call.get_flag(engine_state, stack, "delimiter")?;
let no_header: bool = call.has_flag("no-header");
let infer_schema: Option<usize> = call.get_flag(engine_state, stack, "infer-schema")?;
let skip_rows: Option<usize> = call.get_flag(engine_state, stack, "skip-rows")?;
let columns: Option<Vec<String>> = call.get_flag(engine_state, stack, "columns")?;
if call.has_flag("lazy") {
let file: String = call.req(engine_state, stack, 0)?;
let csv_reader = LazyCsvReader::new(file);
let csv_reader = match delimiter {
None => csv_reader,
Some(d) => {
if d.item.len() != 1 {
return Err(ShellError::GenericError(
"Incorrect delimiter".into(),
"Delimiter has to be one character".into(),
Some(d.span),
None,
Vec::new(),
));
} else {
let delimiter = match d.item.chars().next() {
Some(d) => d as u8,
None => unreachable!(),
};
csv_reader.with_delimiter(delimiter)
}
}
};
let csv_reader = csv_reader.has_header(!no_header);
let csv_reader = match infer_schema {
None => csv_reader,
Some(r) => csv_reader.with_infer_schema_length(Some(r)),
};
let csv_reader = match skip_rows {
None => csv_reader,
Some(r) => csv_reader.with_skip_rows(r),
};
let df: NuLazyFrame = csv_reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"Parquet reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
df.into_value(call.head)
} else {
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let csv_reader = CsvReader::from_path(&file.item)
.map_err(|e| {
ShellError::GenericError(
"Error creating CSV reader".into(),
e.to_string(),
Some(file.span),
None,
Vec::new(),
)
})?
.with_encoding(CsvEncoding::LossyUtf8);
let csv_reader = match delimiter {
None => csv_reader,
Some(d) => {
if d.item.len() != 1 {
return Err(ShellError::GenericError(
"Incorrect delimiter".into(),
"Delimiter has to be one character".into(),
Some(d.span),
None,
Vec::new(),
));
} else {
let delimiter = match d.item.chars().next() {
Some(d) => d as u8,
None => unreachable!(),
};
csv_reader.with_delimiter(delimiter)
}
}
};
let csv_reader = csv_reader.has_header(!no_header);
let csv_reader = match infer_schema {
None => csv_reader,
Some(r) => csv_reader.infer_schema(Some(r)),
};
let csv_reader = match skip_rows {
None => csv_reader,
Some(r) => csv_reader.with_skip_rows(r),
};
let csv_reader = match columns {
None => csv_reader,
Some(columns) => csv_reader.with_columns(Some(columns)),
};
let df: NuDataFrame = csv_reader
.finish()
.map_err(|e| {
ShellError::GenericError(
"Parquet reader error".into(),
format!("{e:?}"),
Some(call.head),
None,
Vec::new(),
)
})?
.into();
Ok(df.into_value(call.head))
}
}

View File

@ -0,0 +1,105 @@
use super::super::values::NuDataFrame;
use crate::dataframe::values::Column;
use crate::dataframe::{eager::SQLContext, values::NuLazyFrame};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
// attribution:
// sql_context.rs, and sql_expr.rs were copied from polars-sql. thank you.
// maybe we should just use the crate at some point but it's not published yet.
// https://github.com/pola-rs/polars/tree/master/polars-sql
#[derive(Clone)]
pub struct QueryDf;
impl Command for QueryDf {
fn name(&self) -> &str {
"dfr query"
}
fn usage(&self) -> &str {
"Query dataframe using SQL. Note: The dataframe is always named 'df' in your query's from clause."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("sql", SyntaxShape::String, "sql query")
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn search_terms(&self) -> Vec<&str> {
vec!["dataframe", "sql", "search"]
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Query dataframe using SQL",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr query 'select a from df'",
result: Some(
NuDataFrame::try_from_columns(vec![Column::new(
"a".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
)])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let sql_query: String = call.req(engine_state, stack, 0)?;
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let mut ctx = SQLContext::new();
ctx.register("df", &df.df);
let df_sql = ctx.execute(&sql_query).map_err(|e| {
ShellError::GenericError(
"Dataframe Error".into(),
e.to_string(),
Some(call.head),
None,
Vec::new(),
)
})?;
let lazy = NuLazyFrame::new(false, df_sql);
let eager = lazy.collect(call.head)?;
let value = Value::custom_value(Box::new(eager), call.head);
Ok(PipelineData::Value(value, None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(QueryDf {})])
}
}

View File

@ -0,0 +1,182 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::dataframe::{utils::extract_strings, values::NuLazyFrame};
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)]
pub struct RenameDF;
impl Command for RenameDF {
fn name(&self) -> &str {
"dfr rename"
}
fn usage(&self) -> &str {
"Rename a dataframe column."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
"columns",
SyntaxShape::Any,
"Column(s) to be renamed. A string or list of strings",
)
.required(
"new names",
SyntaxShape::Any,
"New names for the selected column(s). A string or list of strings",
)
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe or lazyframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Renames a series",
example: "[5 6 7 8] | dfr into-df | dfr rename '0' new_name",
result: Some(
NuDataFrame::try_from_columns(vec![Column::new(
"new_name".to_string(),
vec![
Value::test_int(5),
Value::test_int(6),
Value::test_int(7),
Value::test_int(8),
],
)])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Renames a dataframe column",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr rename a a_new",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a_new".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
),
Column::new(
"b".to_string(),
vec![Value::test_int(2), Value::test_int(4)],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Renames two dataframe columns",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr rename [a b] [a_new b_new]",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"a_new".to_string(),
vec![Value::test_int(1), Value::test_int(3)],
),
Column::new(
"b_new".to_string(),
vec![Value::test_int(2), Value::test_int(4)],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let value = input.into_value(call.head);
if NuLazyFrame::can_downcast(&value) {
let df = NuLazyFrame::try_from_value(value)?;
command_lazy(engine_state, stack, call, df)
} else {
let df = NuDataFrame::try_from_value(value)?;
command_eager(engine_state, stack, call, df)
}
}
}
fn command_eager(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
mut df: NuDataFrame,
) -> Result<PipelineData, ShellError> {
let columns: Value = call.req(engine_state, stack, 0)?;
let columns = extract_strings(columns)?;
let new_names: Value = call.req(engine_state, stack, 1)?;
let new_names = extract_strings(new_names)?;
for (from, to) in columns.iter().zip(new_names.iter()) {
df.as_mut().rename(from, to).map_err(|e| {
ShellError::GenericError(
"Error renaming".into(),
e.to_string(),
Some(call.head),
None,
Vec::new(),
)
})?;
}
Ok(PipelineData::Value(df.into_value(call.head), None))
}
fn command_lazy(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> {
let columns: Value = call.req(engine_state, stack, 0)?;
let columns = extract_strings(columns)?;
let new_names: Value = call.req(engine_state, stack, 1)?;
let new_names = extract_strings(new_names)?;
if columns.len() != new_names.len() {
let value: Value = call.req(engine_state, stack, 1)?;
return Err(ShellError::IncompatibleParametersSingle {
msg: "New name list has different size to column list".into(),
span: value.span(),
});
}
let lazy = lazy.into_polars();
let lazy: NuLazyFrame = lazy.rename(&columns, &new_names).into();
Ok(PipelineData::Value(lazy.into_value(call.head)?, None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(RenameDF {})])
}
}

View File

@ -0,0 +1,135 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
};
use super::super::values::NuDataFrame;
#[derive(Clone)]
pub struct SampleDF;
impl Command for SampleDF {
fn name(&self) -> &str {
"dfr sample"
}
fn usage(&self) -> &str {
"Create sample dataframe."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.named(
"n-rows",
SyntaxShape::Int,
"number of rows to be taken from dataframe",
Some('n'),
)
.named(
"fraction",
SyntaxShape::Number,
"fraction of dataframe to be taken",
Some('f'),
)
.named(
"seed",
SyntaxShape::Number,
"seed for the selection",
Some('s'),
)
.switch("replace", "sample with replace", Some('e'))
.switch("shuffle", "shuffle sample", Some('u'))
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Sample rows from dataframe",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr sample --n-rows 1",
result: None, // No expected value because sampling is random
},
Example {
description: "Shows sample row using fraction and replace",
example:
"[[a b]; [1 2] [3 4] [5 6]] | dfr into-df | dfr sample --fraction 0.5 --replace",
result: None, // No expected value because sampling is random
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let rows: Option<Spanned<usize>> = call.get_flag(engine_state, stack, "n-rows")?;
let fraction: Option<Spanned<f64>> = call.get_flag(engine_state, stack, "fraction")?;
let seed: Option<u64> = call
.get_flag::<i64>(engine_state, stack, "seed")?
.map(|val| val as u64);
let replace: bool = call.has_flag("replace");
let shuffle: bool = call.has_flag("shuffle");
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
match (rows, fraction) {
(Some(rows), None) => df
.as_ref()
.sample_n(rows.item, replace, shuffle, seed)
.map_err(|e| {
ShellError::GenericError(
"Error creating sample".into(),
e.to_string(),
Some(rows.span),
None,
Vec::new(),
)
}),
(None, Some(frac)) => df
.as_ref()
.sample_frac(frac.item, replace, shuffle, seed)
.map_err(|e| {
ShellError::GenericError(
"Error creating sample".into(),
e.to_string(),
Some(frac.span),
None,
Vec::new(),
)
}),
(Some(_), Some(_)) => Err(ShellError::GenericError(
"Incompatible flags".into(),
"Only one selection criterion allowed".into(),
Some(call.head),
None,
Vec::new(),
)),
(None, None) => Err(ShellError::GenericError(
"No selection".into(),
"No selection criterion was found".into(),
Some(call.head),
Some("Perhaps you want to use the flag -n or -f".into()),
Vec::new(),
)),
}
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}

View File

@ -0,0 +1,86 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
use crate::dataframe::values::Column;
use super::super::values::NuDataFrame;
#[derive(Clone)]
pub struct ShapeDF;
impl Command for ShapeDF {
fn name(&self) -> &str {
"dfr shape"
}
fn usage(&self) -> &str {
"Shows column and row size for a dataframe."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Shows row and column shape",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr shape",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new("rows".to_string(), vec![Value::test_int(2)]),
Column::new("columns".to_string(), vec![Value::test_int(2)]),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let rows = Value::int(df.as_ref().height() as i64, call.head);
let cols = Value::int(df.as_ref().width() as i64, call.head);
let rows_col = Column::new("rows".to_string(), vec![rows]);
let cols_col = Column::new("columns".to_string(), vec![cols]);
NuDataFrame::try_from_columns(vec![rows_col, cols_col])
.map(|df| PipelineData::Value(df.into_value(call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(ShapeDF {})])
}
}

View File

@ -0,0 +1,89 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::dataframe::values::Column;
use super::super::values::NuDataFrame;
#[derive(Clone)]
pub struct SliceDF;
impl Command for SliceDF {
fn name(&self) -> &str {
"dfr slice"
}
fn usage(&self) -> &str {
"Creates new dataframe from a slice of rows."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("offset", SyntaxShape::Int, "start of slice")
.required("size", SyntaxShape::Int, "size of slice")
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Create new dataframe from a slice of the rows",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr slice 0 1",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new("a".to_string(), vec![Value::test_int(1)]),
Column::new("b".to_string(), vec![Value::test_int(2)]),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let offset: i64 = call.req(engine_state, stack, 0)?;
let size: usize = call.req(engine_state, stack, 1)?;
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let res = df.as_ref().slice(offset, size);
Ok(PipelineData::Value(
NuDataFrame::dataframe_into_value(res, call.head),
None,
))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(SliceDF {})])
}
}

View File

@ -2,8 +2,7 @@ use crate::dataframe::eager::sql_expr::parse_sql_expr;
use polars::error::{ErrString, PolarsError}; use polars::error::{ErrString, PolarsError};
use polars::prelude::{col, DataFrame, DataType, IntoLazy, LazyFrame}; use polars::prelude::{col, DataFrame, DataType, IntoLazy, LazyFrame};
use sqlparser::ast::{ use sqlparser::ast::{
Expr as SqlExpr, GroupByExpr, Select, SelectItem, SetExpr, Statement, TableFactor, Expr as SqlExpr, Select, SelectItem, SetExpr, Statement, TableFactor, Value as SQLValue,
Value as SQLValue,
}; };
use sqlparser::dialect::GenericDialect; use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser; use sqlparser::parser::Parser;
@ -97,13 +96,8 @@ impl SQLContext {
.collect::<Result<Vec<_>, PolarsError>>()?; .collect::<Result<Vec<_>, PolarsError>>()?;
// Check for group by // Check for group by
// After projection since there might be number. // After projection since there might be number.
let group_by = match &select_stmt.group_by { let group_by = select_stmt
GroupByExpr::All => .group_by
Err(
PolarsError::ComputeError("Group-By Error: Only positive number or expression are supported, not all".into())
)?,
GroupByExpr::Expressions(expressions) => expressions
}
.iter() .iter()
.map( .map(
|e|match e { |e|match e {

View File

@ -2,8 +2,8 @@ use polars::error::PolarsError;
use polars::prelude::{col, lit, DataType, Expr, LiteralValue, PolarsResult as Result, TimeUnit}; use polars::prelude::{col, lit, DataType, Expr, LiteralValue, PolarsResult as Result, TimeUnit};
use sqlparser::ast::{ use sqlparser::ast::{
ArrayElemTypeDef, BinaryOperator as SQLBinaryOperator, DataType as SQLDataType, BinaryOperator as SQLBinaryOperator, DataType as SQLDataType, Expr as SqlExpr,
Expr as SqlExpr, Function as SQLFunction, Value as SqlValue, WindowType, Function as SQLFunction, Value as SqlValue, WindowType,
}; };
fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> { fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
@ -13,7 +13,7 @@ fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
| SQLDataType::Uuid | SQLDataType::Uuid
| SQLDataType::Clob(_) | SQLDataType::Clob(_)
| SQLDataType::Text | SQLDataType::Text
| SQLDataType::String(_) => DataType::String, | SQLDataType::String => DataType::Utf8,
SQLDataType::Float(_) => DataType::Float32, SQLDataType::Float(_) => DataType::Float32,
SQLDataType::Real => DataType::Float32, SQLDataType::Real => DataType::Float32,
SQLDataType::Double => DataType::Float64, SQLDataType::Double => DataType::Float64,
@ -31,12 +31,9 @@ fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
SQLDataType::Time(_, _) => DataType::Time, SQLDataType::Time(_, _) => DataType::Time,
SQLDataType::Timestamp(_, _) => DataType::Datetime(TimeUnit::Microseconds, None), SQLDataType::Timestamp(_, _) => DataType::Datetime(TimeUnit::Microseconds, None),
SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds), SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds),
SQLDataType::Array(array_type_def) => match array_type_def { SQLDataType::Array(inner_type) => match inner_type {
ArrayElemTypeDef::AngleBracket(inner_type) Some(inner_type) => DataType::List(Box::new(map_sql_polars_datatype(inner_type)?)),
| ArrayElemTypeDef::SquareBracket(inner_type) => { None => {
DataType::List(Box::new(map_sql_polars_datatype(inner_type)?))
}
_ => {
return Err(PolarsError::ComputeError( return Err(PolarsError::ComputeError(
"SQL Datatype Array(None) was not supported in polars-sql yet!".into(), "SQL Datatype Array(None) was not supported in polars-sql yet!".into(),
)) ))
@ -62,9 +59,7 @@ fn binary_op_(left: Expr, right: Expr, op: &SQLBinaryOperator) -> Result<Expr> {
SQLBinaryOperator::Multiply => left * right, SQLBinaryOperator::Multiply => left * right,
SQLBinaryOperator::Divide => left / right, SQLBinaryOperator::Divide => left / right,
SQLBinaryOperator::Modulo => left % right, SQLBinaryOperator::Modulo => left % right,
SQLBinaryOperator::StringConcat => { SQLBinaryOperator::StringConcat => left.cast(DataType::Utf8) + right.cast(DataType::Utf8),
left.cast(DataType::String) + right.cast(DataType::String)
}
SQLBinaryOperator::Gt => left.gt(right), SQLBinaryOperator::Gt => left.gt(right),
SQLBinaryOperator::Lt => left.lt(right), SQLBinaryOperator::Lt => left.lt(right),
SQLBinaryOperator::GtEq => left.gt_eq(right), SQLBinaryOperator::GtEq => left.gt_eq(right),
@ -119,11 +114,7 @@ pub fn parse_sql_expr(expr: &SqlExpr) -> Result<Expr> {
binary_op_(left, right, op)? binary_op_(left, right, op)?
} }
SqlExpr::Function(sql_function) => parse_sql_function(sql_function)?, SqlExpr::Function(sql_function) => parse_sql_function(sql_function)?,
SqlExpr::Cast { SqlExpr::Cast { expr, data_type } => cast_(parse_sql_expr(expr)?, data_type)?,
expr,
data_type,
format: _,
} => cast_(parse_sql_expr(expr)?, data_type)?,
SqlExpr::Nested(expr) => parse_sql_expr(expr)?, SqlExpr::Nested(expr) => parse_sql_expr(expr)?,
SqlExpr::Value(value) => literal_expr(value)?, SqlExpr::Value(value) => literal_expr(value)?,
_ => { _ => {

View File

@ -0,0 +1,274 @@
use super::super::values::{Column, NuDataFrame};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use polars::{
chunked_array::ChunkedArray,
prelude::{
AnyValue, DataFrame, DataType, Float64Type, IntoSeries, NewChunkedArray,
QuantileInterpolOptions, Series, Utf8Type,
},
};
#[derive(Clone)]
pub struct Summary;
impl Command for Summary {
fn name(&self) -> &str {
"dfr summary"
}
fn usage(&self) -> &str {
"For a dataframe, produces descriptive statistics (summary statistics) for its numeric columns."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.category(Category::Custom("dataframe".into()))
.input_output_type(
Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()),
)
.named(
"quantiles",
SyntaxShape::Table(vec![]),
"provide optional quantiles",
Some('q'),
)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "list dataframe descriptives",
example: "[[a b]; [1 1] [1 1]] | dfr into-df | dfr summary",
result: Some(
NuDataFrame::try_from_columns(vec![
Column::new(
"descriptor".to_string(),
vec![
Value::test_string("count"),
Value::test_string("sum"),
Value::test_string("mean"),
Value::test_string("median"),
Value::test_string("std"),
Value::test_string("min"),
Value::test_string("25%"),
Value::test_string("50%"),
Value::test_string("75%"),
Value::test_string("max"),
],
),
Column::new(
"a (i64)".to_string(),
vec![
Value::test_float(2.0),
Value::test_float(2.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(0.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(1.0),
],
),
Column::new(
"b (i64)".to_string(),
vec![
Value::test_float(2.0),
Value::test_float(2.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(0.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(1.0),
Value::test_float(1.0),
],
),
])
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let quantiles: Option<Vec<Value>> = call.get_flag(engine_state, stack, "quantiles")?;
let quantiles = quantiles.map(|values| {
values
.iter()
.map(|value| {
let span = value.span();
match value {
Value::Float { val, .. } => {
if (&0.0..=&1.0).contains(&val) {
Ok(*val)
} else {
Err(ShellError::GenericError(
"Incorrect value for quantile".to_string(),
"value should be between 0 and 1".to_string(),
Some(span),
None,
Vec::new(),
))
}
}
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(ShellError::GenericError(
"Incorrect value for quantile".to_string(),
"value should be a float".to_string(),
Some(span),
None,
Vec::new(),
)),
}
})
.collect::<Result<Vec<f64>, ShellError>>()
});
let quantiles = match quantiles {
Some(quantiles) => quantiles?,
None => vec![0.25, 0.50, 0.75],
};
let mut quantiles_labels = quantiles
.iter()
.map(|q| Some(format!("{}%", q * 100.0)))
.collect::<Vec<Option<String>>>();
let mut labels = vec![
Some("count".to_string()),
Some("sum".to_string()),
Some("mean".to_string()),
Some("median".to_string()),
Some("std".to_string()),
Some("min".to_string()),
];
labels.append(&mut quantiles_labels);
labels.push(Some("max".to_string()));
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let names = ChunkedArray::<Utf8Type>::from_slice_options("descriptor", &labels).into_series();
let head = std::iter::once(names);
let tail = df
.as_ref()
.get_columns()
.iter()
.filter(|col| col.dtype() != &DataType::Object("object"))
.map(|col| {
let count = col.len() as f64;
let sum = col
.sum_as_series()
.cast(&DataType::Float64)
.ok()
.and_then(|ca| match ca.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
});
let mean = match col.mean_as_series().get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
};
let median = match col.median_as_series().get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
};
let std = match col.std_as_series(0).get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
};
let min = col
.min_as_series()
.cast(&DataType::Float64)
.ok()
.and_then(|ca| match ca.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
});
let mut quantiles = quantiles
.clone()
.into_iter()
.map(|q| {
col.quantile_as_series(q, QuantileInterpolOptions::default())
.ok()
.and_then(|ca| ca.cast(&DataType::Float64).ok())
.and_then(|ca| match ca.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
})
})
.collect::<Vec<Option<f64>>>();
let max = col
.max_as_series()
.cast(&DataType::Float64)
.ok()
.and_then(|ca| match ca.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
});
let mut descriptors = vec![Some(count), sum, mean, median, std, min];
descriptors.append(&mut quantiles);
descriptors.push(max);
let name = format!("{} ({})", col.name(), col.dtype());
ChunkedArray::<Float64Type>::from_slice_options(&name, &descriptors).into_series()
});
let res = head.chain(tail).collect::<Vec<Series>>();
DataFrame::new(res)
.map_err(|e| {
ShellError::GenericError(
"Dataframe Error".into(),
e.to_string(),
Some(call.head),
None,
Vec::new(),
)
})
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(Summary {})])
}
}

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