Compare commits

..

4 Commits

Author SHA1 Message Date
Stefan Holderbach
2a08a18b26 Bump version to 0.92.2 (#12402) 2024-04-10 23:15:51 +02:00
Stefan Holderbach
d5aad7a4ef Tweak release workflow after 0.92.1 lessons (#12401)
Encountered repeated build failure that vanished after clearing the
existing build caches.

- Don't `fail-fast` (this is highly annoying, if a severe problem is
detected, there is still the possibility to intervene manually)
- Don't cache the build process, we do it rarely so any potential
speed-up is offset by the uncertainty if this affects the artefact
2024-04-10 23:00:59 +02:00
sholderbach
5bc21fbb0a Bump our Rust version to stable
This was prompted by CVE-2024-24576

- https://nvd.nist.gov/vuln/detail/CVE-2024-24576
- https://blog.rust-lang.org/2024/04/09/cve-2024-24576.html
- https://flatt.tech/research/posts/batbadbut-you-cant-securely-execute-commands-on-windows/

Affected is launching commands on Windows with arbitrary arguments,
which is the case for Nushell's external invocation on Windows

Rust has fixed this quoting vulnerability in 1.77.2 (latest stable at
time of commit)

We will thus use this version for our builds and recommend all our
packaging/distribution maintainers to use this version of Rust when
building Nushell.
2024-04-10 22:53:07 +02:00
Stefan Holderbach
f136e0601d Update MSRV following rust-toolchain.toml (#12455)
Also update the `rust-version` in `Cargo.toml` following the update to
`rust-toolchain.toml` in #12258

Added a CI check to verify any future PRs trying to update one will also
have to update the other. (using `std-lib-and-python-virtualenv` job as
this already includes a fresh `nu` binary for a little toml munching
script)
2024-04-10 22:51:03 +02:00
1299 changed files with 40252 additions and 73731 deletions

View File

@@ -18,21 +18,6 @@ updates:
ignore: ignore:
- dependency-name: "*" - dependency-name: "*"
update-types: ["version-update:semver-patch"] update-types: ["version-update:semver-patch"]
groups:
# Only update polars as a whole as there are many subcrates that need to
# be updated at once. We explicitly depend on some of them, so batch their
# updates to not take up dependabot PR slots with dysfunctional PRs
polars:
patterns:
- "polars"
- "polars-*"
# uutils/coreutils also versions all their workspace crates the same at the moment
# Most of them have bleeding edge version requirements (some not)
# see: https://github.com/uutils/coreutils/blob/main/Cargo.toml
uutils:
patterns:
- "uucore"
- "uu_*"
- package-ecosystem: "github-actions" - package-ecosystem: "github-actions"
directory: "/" directory: "/"
schedule: schedule:

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.7 - uses: actions/checkout@v4.1.2
- 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

@@ -10,7 +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: concurrency:
group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }} group: ${{ github.workflow }}-${{ github.head_ref && github.ref || github.run_id }}
@@ -24,55 +24,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]
# 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"
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.7 - uses: actions/checkout@v4.1.2
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 uses: actions-rust-lang/setup-rust-toolchain@v1.8.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 - name: Clippy of benchmarks
run: cargo clippy --benches --workspace --exclude nu_plugin_* -- -D warnings run: cargo clippy --benches --workspace ${{ matrix.flags }} --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]
include: include:
- default-flags: ""
# linux CI cannot handle clipboard feature # linux CI cannot handle clipboard feature
- platform: ubuntu-20.04 - default-flags: ""
- platform: ubuntu-20.04
default-flags: "--no-default-features --features=default-no-clipboard" default-flags: "--no-default-features --features=default-no-clipboard"
- feature: default
flags: ""
- feature: dataframe
flags: "--features=dataframe"
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.7 - uses: actions/checkout@v4.1.2
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 uses: actions-rust-lang/setup-rust-toolchain@v1.8.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.default-flags }} ${{ matrix.flags }}
- name: Check for clean repo - name: Check for clean repo
shell: bash shell: bash
run: | run: |
@@ -95,10 +117,12 @@ jobs:
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- uses: actions/checkout@v4.1.7 - uses: actions/checkout@v4.1.2
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 uses: actions-rust-lang/setup-rust-toolchain@v1.8.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
@@ -137,19 +161,17 @@ jobs:
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.7 - uses: actions/checkout@v4.1.2
- name: Setup Rust toolchain and cache - name: Setup Rust toolchain and cache
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
with:
rustflags: ""
- name: Clippy - name: Clippy
run: cargo clippy --package nu_plugin_* -- $CLIPPY_OPTIONS run: cargo clippy --package nu_plugin_* -- $CLIPPY_OPTIONS

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.7 uses: actions/checkout@v4.1.2
if: github.repository == 'nushell/nightly' if: github.repository == 'nushell/nightly'
with: with:
ref: main ref: main
@@ -36,10 +36,10 @@ jobs:
token: ${{ secrets.WORKFLOW_TOKEN }} token: ${{ secrets.WORKFLOW_TOKEN }}
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.12 uses: hustcer/setup-nu@v3.9
if: github.repository == 'nushell/nightly' if: github.repository == 'nushell/nightly'
with: with:
version: 0.95.0 version: 0.91.0
# 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 +84,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-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: x86_64-unknown-linux-musl - target: x86_64-unknown-linux-musl
os: ubuntu-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: aarch64-unknown-linux-gnu - target: aarch64-unknown-linux-gnu
os: ubuntu-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: armv7-unknown-linux-gnueabihf - target: armv7-unknown-linux-gnueabihf
os: ubuntu-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: riscv64gc-unknown-linux-gnu - target: riscv64gc-unknown-linux-gnu
os: ubuntu-latest os: ubuntu-latest
target_rustflags: ''
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
steps: steps:
- uses: actions/checkout@v4.1.7 - uses: actions/checkout@v4.1.2
with: with:
ref: main ref: main
fetch-depth: 0 fetch-depth: 0
@@ -122,24 +133,26 @@ 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.9.0 uses: actions-rust-lang/setup-rust-toolchain@v1.8.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.12 uses: hustcer/setup-nu@v3.9
with: with:
version: 0.95.0 version: 0.91.0
- 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() }}
@@ -161,7 +174,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.8 uses: softprops/action-gh-release@v2.0.4
if: ${{ startsWith(github.repository, 'nushell/nightly') }} if: ${{ startsWith(github.repository, 'nushell/nightly') }}
with: with:
prerelease: true prerelease: true
@@ -171,6 +184,122 @@ 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'
- target: x86_64-apple-darwin
os: macos-latest
target_rustflags: '--features=dataframe'
- target: x86_64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe'
- target: x86_64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe'
- target: aarch64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe'
- target: aarch64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe'
- target: x86_64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe'
- target: x86_64-unknown-linux-musl
os: ubuntu-20.04
target_rustflags: '--features=dataframe'
- target: aarch64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe'
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4.1.2
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.8.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.9
with:
version: 0.91.0
- 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.2
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@v2.0.4
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 +310,14 @@ jobs:
- name: Waiting for Release - name: Waiting for Release
run: sleep 1800 run: sleep 1800
- uses: actions/checkout@v4.1.7 - uses: actions/checkout@v4.1.2
with: with:
ref: main ref: main
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.12 uses: hustcer/setup-nu@v3.9
with: with:
version: 0.95.0 version: 0.91.0
# 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 = $os starts-with ubuntu
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 }
@@ -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 $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
} }
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
@@ -115,15 +134,9 @@ print $'(char nl)All executable files:'; hr-line
print (ls -f ($executable | into glob)); sleep 1sec print (ls -f ($executable | into glob)); 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
For more information, refer to https://www.nushell.sh/book/plugins.html
" | save $'($dist)/README.txt' -f
[LICENSE ...(glob $executable)] | each {|it| cp -rv $it $dist } | flatten [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
@@ -143,7 +156,7 @@ cd $dist; print $'(char nl)Creating release archive...'; hr-line
if $os in ['macos-latest'] or $USE_UBUNTU { if $os in ['macos-latest'] or $USE_UBUNTU {
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
@@ -158,15 +171,11 @@ 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
# todo: less-v661 is out but is released as a zip file. maybe we should switch to that and extract it?
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
# the below was renamed because it was failing to download for darren. it should work but it wasn't aria2c https://raw.githubusercontent.com/jftuga/less-Windows/master/LICENSE -o LICENSE-for-less.txt
# todo: maybe we should get rid of this aria2c dependency and just use http get?
#aria2c https://raw.githubusercontent.com/jftuga/less-Windows/master/LICENSE -o LICENSE-for-less.txt
aria2c https://github.com/jftuga/less-Windows/blob/master/LICENSE -o LICENSE-for-less.txt
# Create Windows msi release package # Create Windows msi release package
if (get-env _EXTRA_) == 'msi' { if (get-env _EXTRA_) == 'msi' {
@@ -177,7 +186,7 @@ if $os in ['macos-latest'] or $USE_UBUNTU {
# 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)/*' | into glob) target/release/
ls target/release/* | print ls target/release/* | print
cargo install cargo-wix --version 0.3.8 cargo install cargo-wix --version 0.3.4
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 '\' '/')
@@ -199,11 +208,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

@@ -34,64 +34,167 @@ 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-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: x86_64-unknown-linux-musl - target: x86_64-unknown-linux-musl
os: ubuntu-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: aarch64-unknown-linux-gnu - target: aarch64-unknown-linux-gnu
os: ubuntu-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: armv7-unknown-linux-gnueabihf - target: armv7-unknown-linux-gnueabihf
os: ubuntu-22.04 os: ubuntu-20.04
target_rustflags: ''
- target: riscv64gc-unknown-linux-gnu - target: riscv64gc-unknown-linux-gnu
os: ubuntu-latest os: ubuntu-latest
target_rustflags: ''
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
steps: steps:
- uses: actions/checkout@v4.1.7 - uses: actions/checkout@v4.1.2
- 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
uses: actions-rust-lang/setup-rust-toolchain@v1.9.0 uses: actions-rust-lang/setup-rust-toolchain@v1.8.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 cache: false
rustflags: '' rustflags: ''
- name: Setup Nushell - name: Setup Nushell
uses: hustcer/setup-nu@v3.12 uses: hustcer/setup-nu@v3.9
with: with:
version: 0.95.0 version: 0.91.0
- 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.8 uses: softprops/action-gh-release@v2.0.4
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'
- target: x86_64-apple-darwin
os: macos-latest
target_rustflags: '--features=dataframe'
- target: x86_64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe'
- target: x86_64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe'
- target: aarch64-pc-windows-msvc
extra: 'bin'
os: windows-latest
target_rustflags: '--features=dataframe'
- target: aarch64-pc-windows-msvc
extra: msi
os: windows-latest
target_rustflags: '--features=dataframe'
- target: x86_64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe'
- target: x86_64-unknown-linux-musl
os: ubuntu-20.04
target_rustflags: '--features=dataframe'
- target: aarch64-unknown-linux-gnu
os: ubuntu-20.04
target_rustflags: '--features=dataframe'
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4.1.2
- name: Update Rust Toolchain Target
run: |
echo "targets = ['${{matrix.target}}']" >> rust-toolchain.toml
- name: Setup Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1.8.0
# WARN: Keep the rustflags to prevent from the winget submission error: `CAQuietExec: Error 0xc0000135`
with:
cache: false
rustflags: ''
- name: Setup Nushell
uses: hustcer/setup-nu@v3.9
with:
version: 0.91.0
- 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@v2.0.4
if: ${{ startsWith(github.ref, 'refs/tags/') }} if: ${{ startsWith(github.ref, 'refs/tags/') }}
with: with:
draft: true draft: true

View File

@@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout Actions Repository - name: Checkout Actions Repository
uses: actions/checkout@v4.1.7 uses: actions/checkout@v4.1.2
- name: Check spelling - name: Check spelling
uses: crate-ci/typos@v1.23.6 uses: crate-ci/typos@v1.20.3

View File

@@ -1,26 +0,0 @@
cff-version: 1.2.0
title: 'Nushell'
message: >-
If you use this software and wish to cite it,
you can use the metadata from this file.
type: software
authors:
- name: "The Nushell Project Team"
identifiers:
- type: url
value: 'https://github.com/nushell/nushell'
description: Repository
repository-code: 'https://github.com/nushell/nushell'
url: 'https://www.nushell.sh/'
abstract: >-
The goal of the Nushell project is to take the Unix
philosophy of shells, where pipes connect simple commands
together, and bring it to the modern style of development.
Thus, rather than being either a shell, or a programming
language, Nushell connects both by bringing a rich
programming language and a full-featured shell together
into one package.
keywords:
- nushell
- shell
license: MIT

View File

@@ -55,6 +55,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
@@ -71,6 +72,11 @@ Read cargo's documentation for more details: https://doc.rust-lang.org/cargo/ref
cargo run cargo run
``` ```
- Build and run with dataframe support.
```nushell
cargo run --features=dataframe
```
- Run Clippy on Nushell: - Run Clippy on Nushell:
```nushell ```nushell
@@ -88,6 +94,11 @@ Read cargo's documentation for more details: https://doc.rust-lang.org/cargo/ref
cargo test --workspace cargo test --workspace
``` ```
along with dataframe tests
```nushell
cargo test --workspace --features=dataframe
```
or via the `toolkit.nu` command: or via the `toolkit.nu` command:
```nushell ```nushell
use toolkit.nu test use toolkit.nu test
@@ -230,7 +241,7 @@ 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.)
## License ## License
We use the [MIT License](https://github.com/nushell/nushell/blob/main/LICENSE) in all of our Nushell projects. If you are including or referencing a crate that uses the [GPL License](https://www.gnu.org/licenses/gpl-3.0.en.html#license-text) unfortunately we will not be able to accept your PR. We use the [MIT License](https://github.com/nushell/nushell/blob/main/LICENSE) in all of our Nushell projects. If you are including or referencing a crate that uses the [GPL License](https://www.gnu.org/licenses/gpl-3.0.en.html#license-text) unfortunately we will not be able to accept your PR.

2190
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.78.0" rust-version = "1.77.2"
version = "0.97.1" version = "0.92.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
@@ -31,7 +31,7 @@ members = [
"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-color-config",
"crates/nu-explore", "crates/nu-explore",
@@ -39,11 +39,7 @@ members = [
"crates/nu-lsp", "crates/nu-lsp",
"crates/nu-pretty-hex", "crates/nu-pretty-hex",
"crates/nu-protocol", "crates/nu-protocol",
"crates/nu-derive-value",
"crates/nu-plugin", "crates/nu-plugin",
"crates/nu-plugin-core",
"crates/nu-plugin-engine",
"crates/nu-plugin-protocol",
"crates/nu-plugin-test-support", "crates/nu-plugin-test-support",
"crates/nu_plugin_inc", "crates/nu_plugin_inc",
"crates/nu_plugin_gstat", "crates/nu_plugin_gstat",
@@ -51,51 +47,44 @@ members = [
"crates/nu_plugin_query", "crates/nu_plugin_query",
"crates/nu_plugin_custom_values", "crates/nu_plugin_custom_values",
"crates/nu_plugin_formats", "crates/nu_plugin_formats",
"crates/nu_plugin_polars",
"crates/nu_plugin_stress_internals",
"crates/nu-std", "crates/nu-std",
"crates/nu-table", "crates/nu-table",
"crates/nu-term-grid", "crates/nu-term-grid",
"crates/nu-test-support", "crates/nu-test-support",
"crates/nu-utils", "crates/nu-utils",
"crates/nuon",
] ]
[workspace.dependencies] [workspace.dependencies]
alphanumeric-sort = "1.5" alphanumeric-sort = "1.5"
ansi-str = "0.8" ansi-str = "0.8"
anyhow = "1.0.82" base64 = "0.22"
base64 = "0.22.1"
bracoxide = "0.1.2" bracoxide = "0.1.2"
brotli = "5.0"
byteorder = "1.5" byteorder = "1.5"
bytesize = "1.3" bytesize = "1.3"
calamine = "0.24.0" calamine = "0.24.0"
chardetng = "0.1.17" chardetng = "0.1.17"
chrono = { default-features = false, version = "0.4.34" } chrono = { default-features = false, version = "0.4" }
chrono-humanize = "0.2.3" chrono-humanize = "0.2.3"
chrono-tz = "0.8" chrono-tz = "0.8"
convert_case = "0.6"
crossbeam-channel = "0.5.8" crossbeam-channel = "0.5.8"
crossterm = "0.27" crossterm = "0.27"
csv = "1.3" csv = "1.3"
ctrlc = "3.4" ctrlc = "3.4"
deunicode = "1.6.0"
dialoguer = { default-features = false, version = "0.11" } dialoguer = { default-features = false, version = "0.11" }
digest = { default-features = false, version = "0.10" } digest = { default-features = false, version = "0.10" }
dirs = "5.0" dirs-next = "2.0"
dirs-sys = "0.4"
dtparse = "2.0" dtparse = "2.0"
encoding_rs = "0.8" encoding_rs = "0.8"
fancy-regex = "0.13" fancy-regex = "0.13"
filesize = "0.2" filesize = "0.2"
filetime = "0.2" filetime = "0.2"
fs_extra = "1.3"
fuzzy-matcher = "0.3" fuzzy-matcher = "0.3"
hamcrest2 = "0.3"
heck = "0.5.0" heck = "0.5.0"
human-date-parser = "0.1.1" human-date-parser = "0.1.1"
indexmap = "2.4" indexmap = "2.2"
indicatif = "0.17" indicatif = "0.17"
interprocess = "2.2.0"
is_executable = "1.0" is_executable = "1.0"
itertools = "0.12" itertools = "0.12"
libc = "0.2" libc = "0.2"
@@ -106,46 +95,39 @@ lscolors = { version = "0.17", default-features = false }
lsp-server = "0.7.5" lsp-server = "0.7.5"
lsp-types = "0.95.0" lsp-types = "0.95.0"
mach2 = "0.4" mach2 = "0.4"
md5 = { version = "0.10", package = "md-5" } md5 = { version = "0.10", package = "md-5"}
miette = "7.2" miette = "7.2"
mime = "0.3.17" mime = "0.3"
mime_guess = "2.0" mime_guess = "2.0"
mockito = { version = "1.5", default-features = false } mockito = { version = "1.4", default-features = false }
multipart-rs = "0.1.11"
native-tls = "0.2" native-tls = "0.2"
nix = { version = "0.28", default-features = false } nix = { version = "0.28", default-features = false }
notify-debouncer-full = { version = "0.3", default-features = false } notify-debouncer-full = { version = "0.3", default-features = false }
nu-ansi-term = "0.50.1" nu-ansi-term = "0.50.0"
num-format = "0.4" num-format = "0.4"
num-traits = "0.2" num-traits = "0.2"
omnipath = "0.1" omnipath = "0.1"
once_cell = "1.18" once_cell = "1.18"
open = "5.3" open = "5.1"
os_pipe = { version = "1.2", features = ["io_safety"] } os_pipe = "1.1"
pathdiff = "0.2" pathdiff = "0.2"
percent-encoding = "2" percent-encoding = "2"
pretty_assertions = "1.4"
print-positions = "0.6" print-positions = "0.6"
proc-macro-error = { version = "1.0", default-features = false }
proc-macro2 = "1.0"
procfs = "0.16.0" procfs = "0.16.0"
pwd = "1.3" pwd = "1.3"
quick-xml = "0.32.0" quick-xml = "0.31.0"
quickcheck = "1.0" quickcheck = "1.0"
quickcheck_macros = "1.0" quickcheck_macros = "1.0"
quote = "1.0"
rand = "0.8" rand = "0.8"
ratatui = "0.26" ratatui = "0.26"
rayon = "1.10" rayon = "1.10"
reedline = "0.34.0" reedline = "0.31.0"
regex = "1.9.5" regex = "1.9.5"
rmp = "0.8"
rmp-serde = "1.3"
ropey = "1.6.1" ropey = "1.6.1"
roxmltree = "0.19" roxmltree = "0.19"
rstest = { version = "0.18", default-features = false } rstest = { version = "0.18", default-features = false }
rusqlite = "0.31" rusqlite = "0.31"
rust-embed = "8.5.0" rust-embed = "8.2.0"
same-file = "1.0" same-file = "1.0"
serde = { version = "1.0", default-features = false } serde = { version = "1.0", default-features = false }
serde_json = "1.0" serde_json = "1.0"
@@ -153,7 +135,6 @@ serde_urlencoded = "0.7.1"
serde_yaml = "0.9" serde_yaml = "0.9"
sha2 = "0.10" sha2 = "0.10"
strip-ansi-escapes = "0.2.0" strip-ansi-escapes = "0.2.0"
syn = "2.0"
sysinfo = "0.30" sysinfo = "0.30"
tabled = { version = "0.14.0", default-features = false } tabled = { version = "0.14.0", default-features = false }
tempfile = "3.10" tempfile = "3.10"
@@ -164,49 +145,49 @@ trash = "3.3"
umask = "2.1" umask = "2.1"
unicode-segmentation = "1.11" unicode-segmentation = "1.11"
unicode-width = "0.1" unicode-width = "0.1"
ureq = { version = "2.10", default-features = false } ureq = { version = "2.9", default-features = false }
url = "2.2" url = "2.2"
uu_cp = "0.0.27" uu_cp = "0.0.25"
uu_mkdir = "0.0.27" uu_mkdir = "0.0.25"
uu_mktemp = "0.0.27" uu_mktemp = "0.0.25"
uu_mv = "0.0.27" uu_mv = "0.0.25"
uu_whoami = "0.0.27" uu_whoami = "0.0.25"
uu_uname = "0.0.27" uu_uname = "0.0.25"
uucore = "0.0.27" uucore = "0.0.25"
uuid = "1.10.0" uuid = "1.8.0"
v_htmlescape = "0.15.0" v_htmlescape = "0.15.0"
wax = "0.6" wax = "0.6"
which = "6.0.0" which = "6.0.0"
windows = "0.54" windows = "0.54"
windows-sys = "0.48"
winreg = "0.52" winreg = "0.52"
[dependencies] [dependencies]
nu-cli = { path = "./crates/nu-cli", version = "0.97.1" } nu-cli = { path = "./crates/nu-cli", version = "0.92.2" }
nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.97.1" } nu-cmd-base = { path = "./crates/nu-cmd-base", version = "0.92.2" }
nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.97.1" } nu-cmd-lang = { path = "./crates/nu-cmd-lang", version = "0.92.2" }
nu-cmd-plugin = { path = "./crates/nu-cmd-plugin", version = "0.97.1", optional = true } nu-cmd-dataframe = { path = "./crates/nu-cmd-dataframe", version = "0.92.2", features = [
nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.97.1" } "dataframe",
nu-command = { path = "./crates/nu-command", version = "0.97.1" } ], optional = true }
nu-engine = { path = "./crates/nu-engine", version = "0.97.1" } nu-cmd-extra = { path = "./crates/nu-cmd-extra", version = "0.92.2" }
nu-explore = { path = "./crates/nu-explore", version = "0.97.1" } nu-command = { path = "./crates/nu-command", version = "0.92.2" }
nu-lsp = { path = "./crates/nu-lsp/", version = "0.97.1" } nu-engine = { path = "./crates/nu-engine", version = "0.92.2" }
nu-parser = { path = "./crates/nu-parser", version = "0.97.1" } nu-explore = { path = "./crates/nu-explore", version = "0.92.2" }
nu-path = { path = "./crates/nu-path", version = "0.97.1" } nu-lsp = { path = "./crates/nu-lsp/", version = "0.92.2" }
nu-plugin-engine = { path = "./crates/nu-plugin-engine", optional = true, version = "0.97.1" } nu-parser = { path = "./crates/nu-parser", version = "0.92.2" }
nu-protocol = { path = "./crates/nu-protocol", version = "0.97.1" } nu-path = { path = "./crates/nu-path", version = "0.92.2" }
nu-std = { path = "./crates/nu-std", version = "0.97.1" } nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.92.2" }
nu-system = { path = "./crates/nu-system", version = "0.97.1" } nu-protocol = { path = "./crates/nu-protocol", version = "0.92.2" }
nu-utils = { path = "./crates/nu-utils", version = "0.97.1" } nu-std = { path = "./crates/nu-std", version = "0.92.2" }
nu-system = { path = "./crates/nu-system", version = "0.92.2" }
nu-utils = { path = "./crates/nu-utils", version = "0.92.2" }
reedline = { workspace = true, features = ["bashisms", "sqlite"] } reedline = { workspace = true, features = ["bashisms", "sqlite"] }
crossterm = { workspace = true } crossterm = { workspace = true }
ctrlc = { workspace = true } ctrlc = { workspace = true }
dirs = { workspace = true }
log = { workspace = true } log = { workspace = true }
miette = { workspace = true, features = ["fancy-no-backtrace", "fancy"] } miette = { workspace = true, features = ["fancy-no-backtrace", "fancy"] }
mimalloc = { version = "0.1.42", default-features = false, optional = true } mimalloc = { version = "0.1.37", default-features = false, optional = true }
multipart-rs = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
simplelog = "0.12" simplelog = "0.12"
time = "0.3" time = "0.3"
@@ -227,22 +208,18 @@ nix = { workspace = true, default-features = false, features = [
] } ] }
[dev-dependencies] [dev-dependencies]
nu-test-support = { path = "./crates/nu-test-support", version = "0.97.1" } nu-test-support = { path = "./crates/nu-test-support", version = "0.92.2" }
nu-plugin-protocol = { path = "./crates/nu-plugin-protocol", version = "0.97.1" }
nu-plugin-core = { path = "./crates/nu-plugin-core", version = "0.97.1" }
assert_cmd = "2.0" assert_cmd = "2.0"
dirs = { workspace = true } dirs-next = { workspace = true }
tango-bench = "0.5" divan = "0.1.14"
pretty_assertions = { workspace = true } pretty_assertions = "1.4"
regex = { workspace = true }
rstest = { workspace = true, default-features = false } rstest = { workspace = true, default-features = false }
serial_test = "3.1" serial_test = "3.0"
tempfile = { workspace = true } 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",
@@ -254,11 +231,13 @@ default = ["default-no-clipboard", "system-clipboard"]
# See https://github.com/nushell/nushell/pull/11535 # See https://github.com/nushell/nushell/pull/11535
default-no-clipboard = [ default-no-clipboard = [
"plugin", "plugin",
"which-support",
"trash-support", "trash-support",
"sqlite", "sqlite",
"mimalloc", "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/);
@@ -266,15 +245,15 @@ 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 = [ system-clipboard = ["reedline/system_clipboard", "nu-cli/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"]
trash-support = ["nu-command/trash-support", "nu-cmd-lang/trash-support"] trash-support = ["nu-command/trash-support", "nu-cmd-lang/trash-support"]
# 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"]

View File

@@ -52,7 +52,7 @@ To use `Nu` in GitHub Action, check [setup-nu](https://github.com/marketplace/ac
Detailed installation instructions can be found in the [installation chapter of the book](https://www.nushell.sh/book/installation.html). Nu is available via many package managers: Detailed installation instructions can be found in the [installation chapter of the book](https://www.nushell.sh/book/installation.html). Nu is available via many package managers:
[![Packaging status](https://repology.org/badge/vertical-allrepos/nushell.svg?columns=3)](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](devdocs/PLATFORM_SUPPORT.md).
@@ -222,7 +222,6 @@ Please submit an issue or PR to be added to this list.
- [clap](https://github.com/clap-rs/clap/tree/master/clap_complete_nushell) - [clap](https://github.com/clap-rs/clap/tree/master/clap_complete_nushell)
- [Dorothy](http://github.com/bevry/dorothy) - [Dorothy](http://github.com/bevry/dorothy)
- [Direnv](https://github.com/direnv/direnv/blob/master/docs/hook.md#nushell) - [Direnv](https://github.com/direnv/direnv/blob/master/docs/hook.md#nushell)
- [x-cmd](https://x-cmd.com/mod/nu)
## Contributing ## Contributing

View File

@@ -1,29 +0,0 @@
# Security Policy
As a shell and programming language Nushell provides you with great powers and the potential to do dangerous things to your computer and data. Whenever there is a risk that a malicious actor can abuse a bug or a violation of documented behavior/assumptions in Nushell to harm you this is a *security* risk.
We want to fix those issues without exposing our users to unnecessary risk. Thus we want to explain our security policy.
Additional issues may be part of *safety* where the behavior of Nushell as designed and implemented can cause unintended harm or a bug causes damage without the involvement of a third party.
## Supported Versions
As Nushell is still under very active pre-stable development, the only version the core team prioritizes for security and safety fixes is the [most recent version as published on GitHub](https://github.com/nushell/nushell/releases/latest).
Only if you provide a strong reasoning and the necessary resources, will we consider blessing a backported fix with an official patch release for a previous version.
## Reporting a Vulnerability
If you suspect that a bug or behavior of Nushell can affect security or may be potentially exploitable, please report the issue to us in private.
Either reach out to the core team on [our Discord server](https://discord.gg/NtAbbGn) to arrange a private channel or use the [GitHub vulnerability reporting form](https://github.com/nushell/nushell/security/advisories/new).
Please try to answer the following questions:
- How can we reach you for further questions?
- What is the bug? Which system of Nushell may be affected?
- Do you have proof-of-concept for a potential exploit or have you observed an exploit in the wild?
- What is your assessment of the severity based on what could be impacted should the bug be exploited?
- Are additional people aware of the issue or deserve credit for identifying the issue?
We will try to get back to you within a week with:
- acknowledging the receipt of the report
- an initial plan of how we want to address this including the primary points of contact for further communication
- our preliminary assessment of how severe we judge the issue
- a proposal for how we can coordinate responsible disclosure (e.g. how we ship the bugfix, if we need to coordinate with distribution maintainers, when you can release a blog post if you want to etc.)
For purely *safety* related issues where the impact is severe by direct user action instead of malicious input or third parties, feel free to open a regular issue. If we deem that there may be an additional *security* risk on a *safety* issue we may continue discussions in a restricted forum.

View File

@@ -1,42 +1,95 @@
use nu_cli::{eval_source, evaluate_commands}; use nu_cli::{eval_source, evaluate_commands};
use nu_plugin_core::{Encoder, EncodingType}; use nu_parser::parse;
use nu_plugin_protocol::{PluginCallResponse, PluginOutput}; use nu_plugin::{Encoder, EncodingType, PluginCallResponse, PluginOutput};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{EngineState, Stack},
PipelineData, Signals, Span, Spanned, Value, eval_const::create_nu_constant,
PipelineData, Span, Spanned, Value, NU_VARIABLE_ID,
}; };
use nu_std::load_standard_library; 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::{ use std::path::{Path, PathBuf};
rc::Rc,
sync::{atomic::AtomicBool, Arc},
};
use std::hint::black_box; fn main() {
// Run registered benchmarks.
use tango_bench::{benchmark_fn, tango_benchmarks, tango_main, IntoBenchmarks}; divan::main();
}
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())
} }
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
let cwd = engine_state.current_work_dir();
if path.exists() {
match nu_path::canonicalize_with(path, cwd) {
Ok(canon_path) => canon_path,
Err(_) => path.to_owned(),
}
} else {
path.to_owned()
}
}
fn get_home_path(engine_state: &EngineState) -> PathBuf {
nu_path::home_dir()
.map(|path| canonicalize_path(engine_state, &path))
.unwrap_or_default()
}
fn setup_engine() -> EngineState { fn setup_engine() -> EngineState {
let mut engine_state = load_bench_commands(); let mut engine_state = load_bench_commands();
let cwd = std::env::current_dir() let home_path = get_home_path(&engine_state);
.unwrap()
.into_os_string()
.into_string()
.unwrap();
// parsing config.nu breaks without PWD set, so set a valid path // parsing config.nu breaks without PWD set, so set a valid path
engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data())); engine_state.add_env_var(
"PWD".into(),
Value::string(home_path.to_string_lossy(), Span::test_data()),
);
engine_state.generate_nu_constant(); let nu_const = create_nu_constant(&engine_state, Span::unknown())
.expect("Failed to create nushell constant.");
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
engine_state engine_state
} }
fn bench_command(bencher: divan::Bencher, scaled_command: String) {
bench_command_with_custom_stack_and_engine(
bencher,
scaled_command,
Stack::new(),
setup_engine(),
)
}
fn bench_command_with_custom_stack_and_engine(
bencher: divan::Bencher,
scaled_command: String,
stack: nu_protocol::engine::Stack,
mut engine: EngineState,
) {
load_standard_library(&mut engine).unwrap();
let commands = Spanned {
span: Span::unknown(),
item: scaled_command,
};
bencher
.with_inputs(|| engine.clone())
.bench_values(|mut engine| {
evaluate_commands(
&commands,
&mut engine,
&mut stack.clone(),
PipelineData::empty(),
None,
)
.unwrap();
})
}
fn setup_stack_and_engine_from_command(command: &str) -> (Stack, EngineState) { fn setup_stack_and_engine_from_command(command: &str) -> (Stack, EngineState) {
let mut engine = setup_engine(); let mut engine = setup_engine();
let commands = Spanned { let commands = Spanned {
@@ -45,22 +98,272 @@ fn setup_stack_and_engine_from_command(command: &str) -> (Stack, EngineState) {
}; };
let mut stack = Stack::new(); let mut stack = Stack::new();
// Support running benchmarks with IR mode
stack.use_ir = std::env::var_os("NU_USE_IR").is_some();
evaluate_commands( evaluate_commands(
&commands, &commands,
&mut engine, &mut engine,
&mut stack, &mut stack,
PipelineData::empty(), PipelineData::empty(),
Default::default(), None,
) )
.unwrap(); .unwrap();
(stack, engine) (stack, engine)
} }
// 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.
#[divan::bench]
fn load_standard_lib(bencher: divan::Bencher) {
let engine = setup_engine();
bencher
.with_inputs(|| engine.clone())
.bench_values(|mut engine| {
load_standard_library(&mut engine).unwrap();
})
}
#[divan::bench_group]
mod record {
use super::*;
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('}');
s
}
fn create_nested_record_string(depth: i32) -> String {
let mut s = String::from("let record = {");
for _ in 0..depth {
s.push_str("col: {");
}
s.push_str("col_final: 0");
for _ in 0..depth {
s.push('}');
}
s.push('}');
s
}
#[divan::bench(args = [1, 10, 100, 1000])]
fn create(bencher: divan::Bencher, n: i32) {
bench_command(bencher, create_flat_record_string(n));
}
#[divan::bench(args = [1, 10, 100, 1000])]
fn flat_access(bencher: divan::Bencher, n: i32) {
let (stack, engine) = setup_stack_and_engine_from_command(&create_flat_record_string(n));
bench_command_with_custom_stack_and_engine(
bencher,
"$record.col_0 | ignore".to_string(),
stack,
engine,
);
}
#[divan::bench(args = [1, 2, 4, 8, 16, 32, 64, 128])]
fn nest_access(bencher: divan::Bencher, depth: i32) {
let (stack, engine) =
setup_stack_and_engine_from_command(&create_nested_record_string(depth));
let nested_access = ".col".repeat(depth as usize);
bench_command_with_custom_stack_and_engine(
bencher,
format!("$record{} | ignore", nested_access),
stack,
engine,
);
}
}
#[divan::bench_group]
mod table {
use super::*;
fn create_example_table_nrows(n: i32) -> String {
let mut s = String::from("let table = [[foo bar baz]; ");
for i in 0..n {
s.push_str(&format!("[0, 1, {i}]"));
if i < n - 1 {
s.push_str(", ");
}
}
s.push(']');
s
}
#[divan::bench(args = [1, 10, 100, 1000])]
fn create(bencher: divan::Bencher, n: i32) {
bench_command(bencher, create_example_table_nrows(n));
}
#[divan::bench(args = [1, 10, 100, 1000])]
fn get(bencher: divan::Bencher, n: i32) {
let (stack, engine) = setup_stack_and_engine_from_command(&create_example_table_nrows(n));
bench_command_with_custom_stack_and_engine(
bencher,
"$table | get bar | math sum | ignore".to_string(),
stack,
engine,
);
}
#[divan::bench(args = [1, 10, 100, 1000])]
fn select(bencher: divan::Bencher, n: i32) {
let (stack, engine) = setup_stack_and_engine_from_command(&create_example_table_nrows(n));
bench_command_with_custom_stack_and_engine(
bencher,
"$table | select foo baz | ignore".to_string(),
stack,
engine,
);
}
}
#[divan::bench_group]
mod eval_commands {
use super::*;
#[divan::bench(args = [100, 1_000, 10_000])]
fn interleave(bencher: divan::Bencher, n: i32) {
bench_command(
bencher,
format!("seq 1 {n} | wrap a | interleave {{ seq 1 {n} | wrap b }} | ignore"),
)
}
#[divan::bench(args = [100, 1_000, 10_000])]
fn interleave_with_ctrlc(bencher: divan::Bencher, n: i32) {
let mut engine = setup_engine();
engine.ctrlc = Some(std::sync::Arc::new(std::sync::atomic::AtomicBool::new(
false,
)));
load_standard_library(&mut engine).unwrap();
let commands = Spanned {
span: Span::unknown(),
item: format!("seq 1 {n} | wrap a | interleave {{ seq 1 {n} | wrap b }} | ignore"),
};
bencher
.with_inputs(|| engine.clone())
.bench_values(|mut engine| {
evaluate_commands(
&commands,
&mut engine,
&mut nu_protocol::engine::Stack::new(),
PipelineData::empty(),
None,
)
.unwrap();
})
}
#[divan::bench(args = [1, 5, 10, 100, 1_000])]
fn for_range(bencher: divan::Bencher, n: i32) {
bench_command(bencher, format!("(for $x in (1..{}) {{ sleep 50ns }})", n))
}
#[divan::bench(args = [1, 5, 10, 100, 1_000])]
fn each(bencher: divan::Bencher, n: i32) {
bench_command(
bencher,
format!("(1..{}) | each {{|_| sleep 50ns }} | ignore", n),
)
}
#[divan::bench(args = [1, 5, 10, 100, 1_000])]
fn par_each_1t(bencher: divan::Bencher, n: i32) {
bench_command(
bencher,
format!("(1..{}) | par-each -t 1 {{|_| sleep 50ns }} | ignore", n),
)
}
#[divan::bench(args = [1, 5, 10, 100, 1_000])]
fn par_each_2t(bencher: divan::Bencher, n: i32) {
bench_command(
bencher,
format!("(1..{}) | par-each -t 2 {{|_| sleep 50ns }} | ignore", n),
)
}
}
#[divan::bench_group()]
mod parser_benchmarks {
use super::*;
#[divan::bench()]
fn parse_default_config_file(bencher: divan::Bencher) {
let engine_state = setup_engine();
let default_env = get_default_config().as_bytes();
bencher
.with_inputs(|| nu_protocol::engine::StateWorkingSet::new(&engine_state))
.bench_refs(|working_set| parse(working_set, None, default_env, false))
}
#[divan::bench()]
fn parse_default_env_file(bencher: divan::Bencher) {
let engine_state = setup_engine();
let default_env = get_default_env().as_bytes();
bencher
.with_inputs(|| nu_protocol::engine::StateWorkingSet::new(&engine_state))
.bench_refs(|working_set| parse(working_set, None, default_env, false))
}
}
#[divan::bench_group()]
mod eval_benchmarks {
use super::*;
#[divan::bench()]
fn eval_default_env(bencher: divan::Bencher) {
let default_env = get_default_env().as_bytes();
let fname = "default_env.nu";
bencher
.with_inputs(|| (setup_engine(), nu_protocol::engine::Stack::new()))
.bench_values(|(mut engine_state, mut stack)| {
eval_source(
&mut engine_state,
&mut stack,
default_env,
fname,
PipelineData::empty(),
false,
)
})
}
#[divan::bench()]
fn eval_default_config(bencher: divan::Bencher) {
let default_env = get_default_config().as_bytes();
let fname = "default_config.nu";
bencher
.with_inputs(|| (setup_engine(), nu_protocol::engine::Stack::new()))
.bench_values(|(mut engine_state, mut stack)| {
eval_source(
&mut engine_state,
&mut stack,
default_env,
fname,
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.
fn encoding_test_data(row_cnt: usize, col_cnt: usize) -> Value { fn encoding_test_data(row_cnt: usize, col_cnt: usize) -> Value {
let record = Value::test_record( let record = Value::test_record(
@@ -72,421 +375,76 @@ 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( #[divan::bench_group()]
name: &str, mod encoding_benchmarks {
command: &str, use super::*;
stack: Stack,
engine: EngineState,
) -> impl IntoBenchmarks {
let commands = Spanned {
span: Span::unknown(),
item: command.to_string(),
};
[benchmark_fn(name, move |b| {
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(),
Default::default(),
)
.unwrap(),
);
})
})]
}
fn bench_eval_source( #[divan::bench(args = [(100, 5), (10000, 15)])]
name: &str, fn json_encode(bencher: divan::Bencher, (row_cnt, col_cnt): (usize, usize)) {
fname: String, let test_data = PluginOutput::CallResponse(
source: Vec<u8>, 0,
stack: Stack, PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
engine: EngineState, );
) -> impl IntoBenchmarks { let encoder = EncodingType::try_from_bytes(b"json").unwrap();
[benchmark_fn(name, move |b| { bencher
let stack = stack.clone(); .with_inputs(Vec::new)
let engine = engine.clone(); .bench_values(|mut res| encoder.encode(&test_data, &mut res))
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('}');
s
}
fn create_nested_record_string(depth: i32) -> String { #[divan::bench(args = [(100, 5), (10000, 15)])]
let mut s = String::from("let record = {"); fn msgpack_encode(bencher: divan::Bencher, (row_cnt, col_cnt): (usize, usize)) {
for _ in 0..depth { let test_data = PluginOutput::CallResponse(
s.push_str("col: {"); 0,
PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
);
let encoder = EncodingType::try_from_bytes(b"msgpack").unwrap();
bencher
.with_inputs(Vec::new)
.bench_values(|mut res| encoder.encode(&test_data, &mut res))
} }
s.push_str("col_final: 0");
for _ in 0..depth {
s.push('}');
}
s.push('}');
s
} }
fn create_example_table_nrows(n: i32) -> String { #[divan::bench_group()]
let mut s = String::from("let table = [[foo bar baz]; "); mod decoding_benchmarks {
for i in 0..n { use super::*;
s.push_str(&format!("[0, 1, {i}]"));
if i < n - 1 {
s.push_str(", ");
}
}
s.push(']');
s
}
fn bench_record_create(n: i32) -> impl IntoBenchmarks { #[divan::bench(args = [(100, 5), (10000, 15)])]
bench_command( fn json_decode(bencher: divan::Bencher, (row_cnt, col_cnt): (usize, usize)) {
&format!("record_create_{n}"), let test_data = PluginOutput::CallResponse(
&create_flat_record_string(n), 0,
Stack::new(), PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
setup_engine(), );
) let encoder = EncodingType::try_from_bytes(b"json").unwrap();
} let mut res = vec![];
encoder.encode(&test_data, &mut res).unwrap();
fn bench_record_flat_access(n: i32) -> impl IntoBenchmarks { bencher
let setup_command = create_flat_record_string(n); .with_inputs(|| {
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_interrupt(n: i32) -> impl IntoBenchmarks {
let mut engine = setup_engine();
engine.set_signals(Signals::new(Arc::new(AtomicBool::new(false))));
let stack = Stack::new();
bench_command(
&format!("eval_interleave_with_interrupt_{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()); let mut binary_data = std::io::Cursor::new(res.clone());
binary_data.set_position(0); binary_data.set_position(0);
let _: Result<Option<PluginOutput>, _> = binary_data
black_box(encoder.decode(&mut binary_data));
}) })
}, .bench_values(|mut binary_data| -> Result<Option<PluginOutput>, _> {
)] encoder.decode(&mut binary_data)
} })
}
fn decode_msgpack(row_cnt: usize, col_cnt: usize) -> impl IntoBenchmarks { #[divan::bench(args = [(100, 5), (10000, 15)])]
let test_data = PluginOutput::CallResponse( fn msgpack_decode(bencher: divan::Bencher, (row_cnt, col_cnt): (usize, usize)) {
0, let test_data = PluginOutput::CallResponse(
PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)), 0,
); PluginCallResponse::value(encoding_test_data(row_cnt, col_cnt)),
let encoder = EncodingType::try_from_bytes(b"msgpack").unwrap(); );
let mut res = vec![]; let encoder = EncodingType::try_from_bytes(b"msgpack").unwrap();
encoder.encode(&test_data, &mut res).unwrap(); let mut res = vec![];
encoder.encode(&test_data, &mut res).unwrap();
[benchmark_fn( bencher
format!("decode_msgpack_{}_{}", row_cnt, col_cnt), .with_inputs(|| {
move |b| {
let res = res.clone();
b.iter(move || {
let mut binary_data = std::io::Cursor::new(res.clone()); let mut binary_data = std::io::Cursor::new(res.clone());
binary_data.set_position(0); binary_data.set_position(0);
let _: Result<Option<PluginOutput>, _> = binary_data
black_box(encoder.decode(&mut binary_data));
}) })
}, .bench_values(|mut binary_data| -> Result<Option<PluginOutput>, _> {
)] 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_interrupt(100),
bench_eval_interleave_with_interrupt(1_000),
bench_eval_interleave_with_interrupt(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)
);
tango_main!();

View File

@@ -5,27 +5,25 @@ 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.97.1" version = "0.92.2"
[lib] [lib]
bench = false bench = false
[dev-dependencies] [dev-dependencies]
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.97.1" } nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.92.2" }
nu-command = { path = "../nu-command", version = "0.97.1" } nu-command = { path = "../nu-command", version = "0.92.2" }
nu-test-support = { path = "../nu-test-support", version = "0.97.1" } nu-test-support = { path = "../nu-test-support", version = "0.92.2" }
rstest = { workspace = true, default-features = false } rstest = { workspace = true, default-features = false }
tempfile = { workspace = true }
[dependencies] [dependencies]
nu-cmd-base = { path = "../nu-cmd-base", version = "0.97.1" } nu-cmd-base = { path = "../nu-cmd-base", version = "0.92.2" }
nu-engine = { path = "../nu-engine", version = "0.97.1" } nu-engine = { path = "../nu-engine", version = "0.92.2" }
nu-path = { path = "../nu-path", version = "0.97.1" } nu-path = { path = "../nu-path", version = "0.92.2" }
nu-parser = { path = "../nu-parser", version = "0.97.1" } nu-parser = { path = "../nu-parser", version = "0.92.2" }
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.97.1", optional = true } nu-protocol = { path = "../nu-protocol", version = "0.92.2" }
nu-protocol = { path = "../nu-protocol", version = "0.97.1" } nu-utils = { path = "../nu-utils", version = "0.92.2" }
nu-utils = { path = "../nu-utils", version = "0.97.1" } nu-color-config = { path = "../nu-color-config", version = "0.92.2" }
nu-color-config = { path = "../nu-color-config", version = "0.97.1" }
nu-ansi-term = { workspace = true } nu-ansi-term = { workspace = true }
reedline = { workspace = true, features = ["bashisms", "sqlite"] } reedline = { workspace = true, features = ["bashisms", "sqlite"] }
@@ -39,11 +37,12 @@ miette = { workspace = true, features = ["fancy-no-backtrace"] }
lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] } lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] }
once_cell = { workspace = true } once_cell = { workspace = true }
percent-encoding = { workspace = true } percent-encoding = { workspace = true }
pathdiff = { workspace = true }
sysinfo = { workspace = true } sysinfo = { workspace = true }
unicode-segmentation = { workspace = true } unicode-segmentation = { workspace = true }
uuid = { workspace = true, features = ["v4"] } uuid = { workspace = true, features = ["v4"] }
which = { workspace = true } which = { workspace = true }
[features] [features]
plugin = ["nu-plugin-engine"] plugin = []
system-clipboard = ["reedline/system_clipboard"] system-clipboard = ["reedline/system_clipboard"]

View File

@@ -1,7 +0,0 @@
This crate implements the core functionality of the interactive Nushell REPL and interfaces with `reedline`.
Currently implements the syntax highlighting and completions logic.
Furthermore includes a few commands that are specific to `reedline`
## Internal Nushell crate
This crate implements components of Nushell and is not designed to support plugin authors or other users directly.

View File

@@ -1,4 +1,5 @@
use nu_engine::command_prelude::*; use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone)] #[derive(Clone)]
pub struct Commandline; pub struct Commandline;
@@ -10,12 +11,45 @@ impl Command for Commandline {
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("commandline") Signature::build("commandline")
.input_output_types(vec![(Type::Nothing, Type::String)]) .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) .category(Category::Core)
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"View the current command line input buffer." "View or modify the current command line input buffer."
} }
fn search_terms(&self) -> Vec<&str> { fn search_terms(&self) -> Vec<&str> {
@@ -25,11 +59,126 @@ impl Command for Commandline {
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 repl = engine_state.repl_state.lock().expect("repl state mutex"); if let Some(cmd) = call.opt::<Value>(engine_state, stack, 0)? {
Ok(Value::string(repl.buffer.clone(), call.head).into_pipeline_data()) let span = cmd.span();
let cmd = cmd.coerce_into_string()?;
let mut repl = engine_state.repl_state.lock().expect("repl state mutex");
if call.has_flag(engine_state, stack, "cursor")? {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "`--cursor (-c)` is deprecated".into(),
msg: "Setting the current cursor position by `--cursor (-c)` is deprecated"
.into(),
span: Some(call.arguments_span()),
help: Some("Use `commandline set-cursor`".into()),
inner: vec![],
},
);
match cmd.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,
help: Some(format!(r#"string "{cmd}" does not represent a valid int"#)),
})
}
}
} else if call.has_flag(engine_state, stack, "append")? {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "`--append (-a)` is deprecated".into(),
msg: "Appending the string to the end of the buffer by `--append (-a)` is deprecated".into(),
span: Some(call.arguments_span()),
help: Some("Use `commandline edit --append (-a)`".into()),
inner: vec![],
},
);
repl.buffer.push_str(&cmd);
} else if call.has_flag(engine_state, stack, "insert")? {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "`--insert (-i)` is deprecated".into(),
msg: "Inserts the string into the buffer at the cursor position by `--insert (-i)` is deprecated".into(),
span: Some(call.arguments_span()),
help: Some("Use `commandline edit --insert (-i)`".into()),
inner: vec![],
},
);
let cursor_pos = repl.cursor_pos;
repl.buffer.insert_str(cursor_pos, &cmd);
repl.cursor_pos += cmd.len();
} else {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "`--replace (-r)` is deprecated".into(),
msg: "Replacing the current contents of the buffer by `--replace (-p)` or positional argument is deprecated".into(),
span: Some(call.arguments_span()),
help: Some("Use `commandline edit --replace (-r)`".into()),
inner: vec![],
},
);
repl.buffer = cmd;
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(engine_state, stack, "cursor-end")? {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "`--cursor-end (-e)` is deprecated".into(),
msg: "Setting the current cursor position to the end of the buffer by `--cursor-end (-e)` is deprecated".into(),
span: Some(call.arguments_span()),
help: Some("Use `commandline set-cursor --end (-e)`".into()),
inner: vec![],
},
);
repl.cursor_pos = repl.buffer.len();
Ok(Value::nothing(call.head).into_pipeline_data())
} else if call.has_flag(engine_state, stack, "cursor")? {
nu_protocol::report_error_new(
engine_state,
&ShellError::GenericError {
error: "`--cursor (-c)` is deprecated".into(),
msg: "Getting the current cursor position by `--cursor (-c)` is deprecated"
.into(),
span: Some(call.arguments_span()),
help: Some("Use `commandline get-cursor`".into()),
inner: vec![],
},
);
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

@@ -47,7 +47,7 @@ impl Command for 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(engine_state, stack, "clear")?;
let long = call.has_flag(engine_state, stack, "long")?; let long = call.has_flag(engine_state, stack, "long")?;
let signals = engine_state.signals().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");
@@ -67,7 +67,7 @@ impl Command for History {
} else { } else {
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format { let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
HistoryFileFormat::Sqlite => { HistoryFileFormat::Sqlite => {
SqliteBackedHistory::with_file(history_path.clone().into(), None, None) SqliteBackedHistory::with_file(history_path.clone(), None, None)
.map(|inner| { .map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
boxed boxed
@@ -77,7 +77,7 @@ impl Command for History {
HistoryFileFormat::PlainText => FileBackedHistory::with_file( HistoryFileFormat::PlainText => FileBackedHistory::with_file(
history.max_size as usize, history.max_size as usize,
history_path.clone().into(), history_path.clone(),
) )
.map(|inner| { .map(|inner| {
let boxed: Box<dyn ReedlineHistory> = Box::new(inner); let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
@@ -107,7 +107,7 @@ impl Command for History {
file: history_path.display().to_string(), file: history_path.display().to_string(),
span: head, span: head,
})? })?
.into_pipeline_data(head, signals)), .into_pipeline_data(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))
@@ -122,7 +122,7 @@ impl Command for History {
file: history_path.display().to_string(), file: history_path.display().to_string(),
span: head, span: head,
})? })?
.into_pipeline_data(head, signals)), .into_pipeline_data(ctrlc)),
} }
} }
} else { } else {
@@ -156,34 +156,58 @@ fn create_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span)
//2. Create a record of either short or long columns and values //2. Create a record of either short or long columns and values
let item_id_value = Value::int( let item_id_value = Value::int(
entry match entry.id {
.id Some(id) => {
.and_then(|id| id.to_string().parse::<i64>().ok()) let ids = id.to_string();
.unwrap_or_default(), match ids.parse::<i64>() {
Ok(i) => i,
_ => 0i64,
}
}
None => 0i64,
},
head, head,
); );
let start_timestamp_value = Value::string( let start_timestamp_value = Value::string(
entry match entry.start_timestamp {
.start_timestamp Some(time) => time.to_string(),
.map(|time| time.to_string()) None => "".into(),
.unwrap_or_default(), },
head, head,
); );
let command_value = Value::string(entry.command_line, head); let command_value = Value::string(entry.command_line, head);
let session_id_value = Value::int( let session_id_value = Value::int(
entry match entry.session_id {
.session_id Some(sid) => {
.and_then(|id| id.to_string().parse::<i64>().ok()) let sids = sid.to_string();
.unwrap_or_default(), match sids.parse::<i64>() {
Ok(i) => i,
_ => 0i64,
}
}
None => 0i64,
},
head,
);
let hostname_value = Value::string(
match entry.hostname {
Some(host) => host,
None => "".into(),
},
head,
);
let cwd_value = Value::string(
match entry.cwd {
Some(cwd) => cwd,
None => "".into(),
},
head, head,
); );
let hostname_value = Value::string(entry.hostname.unwrap_or_default(), head);
let cwd_value = Value::string(entry.cwd.unwrap_or_default(), head);
let duration_value = Value::duration( let duration_value = Value::duration(
entry match entry.duration {
.duration Some(d) => d.as_nanos().try_into().unwrap_or(0),
.and_then(|d| d.as_nanos().try_into().ok()) None => 0,
.unwrap_or(0), },
head, head,
); );
let exit_status_value = Value::int(entry.exit_status.unwrap_or(0), head); let exit_status_value = Value::int(entry.exit_status.unwrap_or(0), head);

View File

@@ -36,6 +36,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

@@ -12,7 +12,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

@@ -14,7 +14,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'))
@@ -49,26 +49,22 @@ impl Command for KeybindingsList {
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 all_options = ["modifiers", "keycodes", "edits", "modes", "events"]; let records = if call.named_len() == 0 {
let all_options = ["modifiers", "keycodes", "edits", "modes", "events"];
let presence = all_options all_options
.iter() .iter()
.map(|option| call.has_flag(engine_state, stack, option)) .flat_map(|argument| get_records(argument, call.head))
.collect::<Result<Vec<_>, ShellError>>()?; .collect()
} else {
let no_option_specified = presence.iter().all(|present| !*present); call.named_iter()
.flat_map(|(argument, _, _)| get_records(argument.item.as_str(), call.head))
let records = all_options .collect()
.iter() };
.zip(presence)
.filter(|(_, present)| no_option_specified || *present)
.flat_map(|(option, _)| get_records(option, call.head))
.collect();
Ok(Value::list(records, call.head).into_pipeline_data()) Ok(Value::list(records, call.head).into_pipeline_data())
} }

View File

@@ -1,23 +1,45 @@
use crate::completions::CompletionOptions; use crate::completions::{CompletionOptions, SortBy};
use nu_protocol::{ use nu_protocol::{engine::StateWorkingSet, levenshtein_distance, Span};
engine::{Stack, StateWorkingSet},
Span,
};
use reedline::Suggestion; use reedline::Suggestion;
// Completer trait represents the three stages of the completion
// fetch, filter and sort
pub trait Completer { pub trait Completer {
/// Fetch, filter, and sort completions
#[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<SemanticSuggestion>;
fn get_sort_by(&self) -> SortBy {
SortBy::Ascending
}
fn sort(&self, items: Vec<SemanticSuggestion>, prefix: Vec<u8>) -> Vec<SemanticSuggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string();
let mut filtered_items = items;
// Sort items
match self.get_sort_by() {
SortBy::LevenshteinDistance => {
filtered_items.sort_by(|a, b| {
let a_distance = levenshtein_distance(&prefix_str, &a.suggestion.value);
let b_distance = levenshtein_distance(&prefix_str, &b.suggestion.value);
a_distance.cmp(&b_distance)
});
}
SortBy::Ascending => {
filtered_items.sort_by(|a, b| a.suggestion.value.cmp(&b.suggestion.value));
}
SortBy::None => {}
};
filtered_items
}
} }
#[derive(Debug, Default, PartialEq)] #[derive(Debug, Default, PartialEq)]

View File

@@ -1,17 +1,19 @@
use crate::{ use crate::{
completions::{Completer, CompletionOptions, MatchAlgorithm}, completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy},
SuggestionKind, SuggestionKind,
}; };
use nu_parser::FlatShape; use nu_parser::FlatShape;
use nu_protocol::{ use nu_protocol::{
engine::{CachedFile, Stack, StateWorkingSet}, engine::{CachedFile, EngineState, StateWorkingSet},
Span, Span,
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::sync::Arc;
use super::{completion_common::sort_suggestions, SemanticSuggestion}; 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 +21,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,14 +37,13 @@ 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() {
@@ -48,10 +52,7 @@ impl CommandCompletion {
if let Ok(mut contents) = std::fs::read_dir(path.as_ref()) { if let Ok(mut contents) = std::fs::read_dir(path.as_ref()) {
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
@@ -99,9 +100,10 @@ impl CommandCompletion {
suggestion: 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,
style: None,
extra: None,
span: reedline::Span::new(span.start - offset, span.end - offset), span: reedline::Span::new(span.start - offset, span.end - offset),
append_whitespace: true, append_whitespace: true,
..Suggestion::default()
}, },
kind: Some(SuggestionKind::Command(x.2)), kind: Some(SuggestionKind::Command(x.2)),
}) })
@@ -112,14 +114,16 @@ 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| SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: x, value: x,
description: None,
style: None,
extra: None,
span: reedline::Span::new(span.start - offset, span.end - offset), span: reedline::Span::new(span.start - offset, span.end - offset),
append_whitespace: true, append_whitespace: true,
..Suggestion::default()
}, },
// TODO: is there a way to create a test? // TODO: is there a way to create a test?
kind: None, kind: None,
@@ -133,9 +137,11 @@ impl CommandCompletion {
results.push(SemanticSuggestion { results.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: format!("^{}", external.suggestion.value), value: format!("^{}", external.suggestion.value),
description: None,
style: None,
extra: None,
span: external.suggestion.span, span: external.suggestion.span,
append_whitespace: true, append_whitespace: true,
..Suggestion::default()
}, },
kind: external.kind, kind: external.kind,
}) })
@@ -155,8 +161,7 @@ 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,
@@ -193,7 +198,7 @@ impl Completer for CommandCompletion {
}; };
if !subcommands.is_empty() { if !subcommands.is_empty() {
return sort_suggestions(&String::from_utf8_lossy(&prefix), subcommands, options); return subcommands;
} }
let config = working_set.get_config(); let config = working_set.get_config();
@@ -218,7 +223,11 @@ impl Completer for CommandCompletion {
vec![] vec![]
}; };
sort_suggestions(&String::from_utf8_lossy(&prefix), commands, options) subcommands.into_iter().chain(commands).collect::<Vec<_>>()
}
fn get_sort_by(&self) -> SortBy {
SortBy::LevenshteinDistance
} }
} }
@@ -257,8 +266,6 @@ 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() {

View File

@@ -7,8 +7,8 @@ use nu_engine::eval_block;
use nu_parser::{flatten_pipeline_element, parse, FlatShape}; use nu_parser::{flatten_pipeline_element, parse, FlatShape};
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
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, sync::Arc};
@@ -22,10 +22,10 @@ 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: stack.reset_stdio().capture(),
} }
} }
@@ -48,32 +48,30 @@ impl NuCompleter {
let options = CompletionOptions { let options = CompletionOptions {
case_sensitive: config.case_sensitive_completions, case_sensitive: config.case_sensitive_completions,
match_algorithm: config.completion_algorithm.into(), match_algorithm: config.completion_algorithm.into(),
sort: config.completion_sort,
..Default::default() ..Default::default()
}; };
completer.fetch( // Fetch
working_set, let mut suggestions =
&self.stack, completer.fetch(working_set, prefix.clone(), new_span, offset, pos, &options);
prefix.clone(),
new_span, // Sort
offset, suggestions = completer.sort(suggestions, prefix);
pos,
&options, suggestions
)
} }
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<SemanticSuggestion>> {
let block = self.engine_state.get_block(closure.block_id); let block = self.engine_state.get_block(block_id);
let mut callee_stack = self let mut callee_stack = self
.stack .stack
.captures_to_stack_preserve_out_dest(closure.captures.clone()); .gather_captures(&self.engine_state, &block.captures);
// Line // Line
if let Some(pos_arg) = block.signature.required_positional.first() { if let Some(pos_arg) = block.signature.required_positional.first() {
@@ -98,8 +96,9 @@ impl NuCompleter {
PipelineData::empty(), PipelineData::empty(),
); );
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);
@@ -176,8 +175,11 @@ impl NuCompleter {
// Variables completion // Variables completion
if prefix.starts_with(b"$") || most_left_var.is_some() { if prefix.starts_with(b"$") || most_left_var.is_some() {
let mut completer = let mut completer = VariableCompletion::new(
VariableCompletion::new(most_left_var.unwrap_or((vec![], vec![]))); self.engine_state.clone(),
self.stack.clone(),
most_left_var.unwrap_or((vec![], vec![])),
);
return self.process_completion( return self.process_completion(
&mut completer, &mut completer,
@@ -208,10 +210,13 @@ impl NuCompleter {
// We got no results for internal completion // We got no results for internal completion
// now we can check if external completer is set and use it // now we can check if external completer is set and use it
if let Some(closure) = config.external_completer.as_ref() { if let Some(block_id) = config.external_completer {
if let Some(external_result) = if let Some(external_result) = self.external_completion(
self.external_completion(closure, &spans, fake_offset, new_span) block_id,
{ &spans,
fake_offset,
new_span,
) {
return external_result; return external_result;
} }
} }
@@ -222,6 +227,8 @@ impl NuCompleter {
|| (flat_idx == 0 && working_set.get_span_contents(new_span).is_empty()) || (flat_idx == 0 && working_set.get_span_contents(new_span).is_empty())
{ {
let mut completer = CommandCompletion::new( let mut completer = CommandCompletion::new(
self.engine_state.clone(),
&working_set,
flattened.clone(), flattened.clone(),
// flat_idx, // flat_idx,
FlatShape::String, FlatShape::String,
@@ -249,7 +256,10 @@ impl NuCompleter {
|| prev_expr_str == b"overlay use" || prev_expr_str == b"overlay use"
|| prev_expr_str == b"source-env" || prev_expr_str == b"source-env"
{ {
let mut completer = DotNuCompletion::new(); let mut completer = DotNuCompletion::new(
self.engine_state.clone(),
self.stack.clone(),
);
return self.process_completion( return self.process_completion(
&mut completer, &mut completer,
@@ -260,7 +270,10 @@ impl NuCompleter {
pos, pos,
); );
} else if prev_expr_str == b"ls" { } else if prev_expr_str == b"ls" {
let mut completer = FileCompletion::new(); let mut completer = FileCompletion::new(
self.engine_state.clone(),
self.stack.clone(),
);
return self.process_completion( return self.process_completion(
&mut completer, &mut completer,
@@ -278,6 +291,7 @@ impl NuCompleter {
match &flat.1 { match &flat.1 {
FlatShape::Custom(decl_id) => { FlatShape::Custom(decl_id) => {
let mut completer = CustomCompletion::new( let mut completer = CustomCompletion::new(
self.engine_state.clone(),
self.stack.clone(), self.stack.clone(),
*decl_id, *decl_id,
initial_line, initial_line,
@@ -293,7 +307,10 @@ impl NuCompleter {
); );
} }
FlatShape::Directory => { FlatShape::Directory => {
let mut completer = DirectoryCompletion::new(); let mut completer = DirectoryCompletion::new(
self.engine_state.clone(),
self.stack.clone(),
);
return self.process_completion( return self.process_completion(
&mut completer, &mut completer,
@@ -305,7 +322,10 @@ impl NuCompleter {
); );
} }
FlatShape::Filepath | FlatShape::GlobPattern => { FlatShape::Filepath | FlatShape::GlobPattern => {
let mut completer = FileCompletion::new(); let mut completer = FileCompletion::new(
self.engine_state.clone(),
self.stack.clone(),
);
return self.process_completion( return self.process_completion(
&mut completer, &mut completer,
@@ -318,6 +338,8 @@ impl NuCompleter {
} }
flat_shape => { flat_shape => {
let mut completer = CommandCompletion::new( let mut completer = CommandCompletion::new(
self.engine_state.clone(),
&working_set,
flattened.clone(), flattened.clone(),
// flat_idx, // flat_idx,
flat_shape.clone(), flat_shape.clone(),
@@ -338,9 +360,9 @@ impl NuCompleter {
} }
// Try to complete using an external completer (if set) // Try to complete using an external completer (if set)
if let Some(closure) = config.external_completer.as_ref() { if let Some(block_id) = config.external_completer {
if let Some(external_result) = self.external_completion( if let Some(external_result) = self.external_completion(
closure, block_id,
&spans, &spans,
fake_offset, fake_offset,
new_span, new_span,
@@ -350,7 +372,10 @@ impl NuCompleter {
} }
// Check for file completion // Check for file completion
let mut completer = FileCompletion::new(); let mut completer = FileCompletion::new(
self.engine_state.clone(),
self.stack.clone(),
);
out = self.process_completion( out = self.process_completion(
&mut completer, &mut completer,
&working_set, &working_set,
@@ -444,11 +469,14 @@ pub fn map_value_completions<'a>(
return Some(SemanticSuggestion { return Some(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: s, value: s,
description: None,
style: None,
extra: None,
span: reedline::Span { span: reedline::Span {
start: span.start - offset, start: span.start - offset,
end: span.end - offset, end: span.end - offset,
}, },
..Suggestion::default() append_whitespace: false,
}, },
kind: Some(SuggestionKind::Type(x.get_type())), kind: Some(SuggestionKind::Type(x.get_type())),
}); });
@@ -458,11 +486,14 @@ pub fn map_value_completions<'a>(
if let Ok(record) = x.as_record() { if let Ok(record) = x.as_record() {
let mut suggestion = Suggestion { let mut suggestion = Suggestion {
value: String::from(""), // Initialize with empty string value: String::from(""), // Initialize with empty string
description: None,
style: None,
extra: None,
span: reedline::Span { span: reedline::Span {
start: span.start - offset, start: span.start - offset,
end: span.end - offset, end: span.end - offset,
}, },
..Suggestion::default() append_whitespace: false,
}; };
// Iterate the cols looking for `value` and `description` // Iterate the cols looking for `value` and `description`
@@ -529,7 +560,7 @@ 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 = [
("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"]),

View File

@@ -1,133 +1,81 @@
use crate::{ use crate::completions::{matches, CompletionOptions};
completions::{matches, CompletionOptions},
SemanticSuggestion,
};
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use nu_ansi_term::Style; use nu_ansi_term::Style;
use nu_engine::env_to_string; use nu_engine::env_to_string;
use nu_path::{expand_to_real_path, home_dir}; use nu_path::home_dir;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
CompletionSort, Span, Span,
}; };
use nu_utils::get_ls_colors; use nu_utils::get_ls_colors;
use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP}; use std::{
ffi::OsStr,
path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP},
};
use super::MatchAlgorithm; fn complete_rec(
partial: &[String],
#[derive(Clone, Default)]
pub struct PathBuiltFromString {
parts: Vec<String>,
isdir: bool,
}
/// Recursively goes through paths that match a given `partial`.
/// built: State struct for a valid matching path built so far.
///
/// `isdir`: whether the current partial path has a trailing slash.
/// Parsing a path string into a pathbuf loses that bit of information.
///
/// want_directory: Whether we want only directories as completion matches.
/// Some commands like `cd` can only be run on directories whereas others
/// like `ls` can be run on regular files as well.
pub fn complete_rec(
partial: &[&str],
built: &PathBuiltFromString,
cwd: &Path, cwd: &Path,
options: &CompletionOptions, options: &CompletionOptions,
want_directory: 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, want_directory, 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; if entry_name.eq(base) {
}; break;
}
let mut entries = Vec::new(); } else {
for entry in result.filter_map(|e| e.ok()) { completions.push(path)
let entry_name = entry.file_name().to_string_lossy().into_owned(); }
let entry_isdir = entry.path().is_dir();
let mut built = built.clone();
built.parts.push(entry_name.clone());
built.isdir = entry_isdir;
if !want_directory || entry_isdir {
entries.push((entry_name, built));
}
}
let prefix = partial.first().unwrap_or(&"");
let sorted_entries = sort_completions(prefix, entries, options, |(entry, _)| entry);
for (entry_name, built) in sorted_entries {
match partial.split_first() {
Some((base, rest)) => {
if matches(base, &entry_name, options) {
// We use `isdir` to confirm that the current component has
// at least one next component or a slash.
// Serves as confirmation to ignore longer completions for
// components in between.
if !rest.is_empty() || isdir {
completions.extend(complete_rec(
rest,
&built,
cwd,
options,
want_directory,
isdir,
));
} else {
completions.push(built);
} }
None => completions.push(path),
_ => {}
} }
if entry_name.eq(base)
&& matches!(options.match_algorithm, MatchAlgorithm::Prefix)
&& isdir
{
break;
}
}
None => {
completions.push(built);
} }
} }
} }
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, path_separator: char) -> 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(&path_separator.to_string()); if p.is_dir() {
if p.isdir { ret.push(SEP);
ret.push(path_separator);
} }
ret ret
} }
@@ -158,14 +106,6 @@ pub fn complete_item(
) -> Vec<(nu_protocol::Span, String, Option<Style>)> { ) -> 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);
#[cfg(unix)]
let path_separator = SEP;
#[cfg(windows)]
let path_separator = partial
.chars()
.rfind(|c: &char| is_separator(*c))
.unwrap_or(SEP);
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 let ls_colors = (engine_state.config.use_ls_colors_completions
&& engine_state.config.use_ansi_coloring) && engine_state.config.use_ansi_coloring)
@@ -176,72 +116,79 @@ pub fn complete_item(
}; };
get_ls_colors(ls_colors_env_str) 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_vec: Vec<Component> = Path::new(&partial).components().collect();
let mut components = Path::new(&partial).components().peekable(); // Path components that end with a single "." get normalized away,
match components.peek().cloned() { // so if the partial path ends in a literal "." we must add it back in manually
if partial.ends_with('.') && partial.len() > 1 {
components_vec.push(Component::Normal(OsStr::new(".")));
};
let mut components = components_vec.into_iter().peekable();
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().map(Into::into).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, path_separator); 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(expand_to_real_path(&path)) complete_rec(partial.as_slice(), &cwd, options, want_directory, isdir)
.ok() .into_iter()
.as_ref(), .map(|p| {
) let path = original_cwd.apply(&p);
.map(lscolors::Style::to_nu_ansi_term_style) let style = ls_colors.as_ref().map(|lsc| {
.unwrap_or_default() lsc.style_for_path_with_metadata(
}); &path,
(span, escape_path(path, want_directory), style) std::fs::symlink_metadata(&path).ok().as_ref(),
}) )
.collect() .map(lscolors::Style::to_nu_ansi_term_style)
.unwrap_or_default()
});
(span, escape_path(path, want_directory), style)
})
.collect()
} }
// Fix files or folders with quotes or hashes // Fix files or folders with quotes or hashes
@@ -301,42 +248,3 @@ pub fn adjust_if_intermediate(
readjusted, readjusted,
} }
} }
/// Convenience function to sort suggestions using [`sort_completions`]
pub fn sort_suggestions(
prefix: &str,
items: Vec<SemanticSuggestion>,
options: &CompletionOptions,
) -> Vec<SemanticSuggestion> {
sort_completions(prefix, items, options, |it| &it.suggestion.value)
}
/// # Arguments
/// * `prefix` - What the user's typed, for sorting by fuzzy matcher score
pub fn sort_completions<T>(
prefix: &str,
mut items: Vec<T>,
options: &CompletionOptions,
get_value: fn(&T) -> &str,
) -> Vec<T> {
// Sort items
if options.sort == CompletionSort::Smart && options.match_algorithm == MatchAlgorithm::Fuzzy {
let mut matcher = SkimMatcherV2::default();
if options.case_sensitive {
matcher = matcher.respect_case();
} else {
matcher = matcher.ignore_case();
};
items.sort_by(|a, b| {
let a_str = get_value(a);
let b_str = get_value(b);
let a_score = matcher.fuzzy_match(a_str, prefix).unwrap_or_default();
let b_score = matcher.fuzzy_match(b_str, prefix).unwrap_or_default();
b_score.cmp(&a_score).then(a_str.cmp(b_str))
});
} else {
items.sort_by(|a, b| get_value(a).cmp(get_value(b)));
}
items
}

View File

@@ -1,10 +1,17 @@
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, CompletionSort}; use nu_protocol::CompletionAlgorithm;
use std::fmt::Display; use std::fmt::Display;
#[derive(Copy, Clone)]
pub enum SortBy {
LevenshteinDistance,
Ascending,
None,
}
/// Describes how suggestions should be matched. /// Describes how suggestions should be matched.
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug)]
pub enum MatchAlgorithm { pub enum MatchAlgorithm {
/// Only show suggestions which begin with the given input /// Only show suggestions which begin with the given input
/// ///
@@ -89,7 +96,6 @@ pub struct CompletionOptions {
pub case_sensitive: bool, pub case_sensitive: bool,
pub positional: bool, pub positional: bool,
pub match_algorithm: MatchAlgorithm, pub match_algorithm: MatchAlgorithm,
pub sort: CompletionSort,
} }
impl Default for CompletionOptions { impl Default for CompletionOptions {
@@ -98,7 +104,6 @@ impl Default for CompletionOptions {
case_sensitive: true, case_sensitive: true,
positional: true, positional: true,
match_algorithm: MatchAlgorithm::Prefix, match_algorithm: MatchAlgorithm::Prefix,
sort: Default::default(),
} }
} }
} }

View File

@@ -1,31 +1,33 @@
use crate::completions::{ use crate::completions::{
completer::map_value_completions, Completer, CompletionOptions, MatchAlgorithm, completer::map_value_completions, Completer, CompletionOptions, MatchAlgorithm,
SemanticSuggestion, 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, debugger::WithoutDebug,
engine::{Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
CompletionSort, PipelineData, Span, Type, Value, PipelineData, Span, Type, Value,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use std::collections::HashMap; use std::{collections::HashMap, sync::Arc};
use super::completion_common::sort_suggestions;
pub struct CustomCompletion { pub struct CustomCompletion {
engine_state: Arc<EngineState>,
stack: Stack, stack: Stack,
decl_id: usize, decl_id: usize,
line: String, line: String,
sort_by: SortBy,
} }
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 {
stack, engine_state,
stack: stack.reset_stdio().capture(),
decl_id, decl_id,
line, line,
sort_by: SortBy::None,
} }
} }
} }
@@ -33,8 +35,7 @@ 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,
@@ -46,22 +47,24 @@ impl Completer for CustomCompletion {
// Call custom declaration // Call custom declaration
let result = eval_call::<WithoutDebug>( let result = eval_call::<WithoutDebug>(
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,
head: span, head: span,
arguments: vec![ arguments: vec![
Argument::Positional(Expression::new_unknown( Argument::Positional(Expression {
Expr::String(self.line.clone()), span: Span::unknown(),
Span::unknown(), ty: Type::String,
Type::String, expr: Expr::String(self.line.clone()),
)), custom_completion: None,
Argument::Positional(Expression::new_unknown( }),
Expr::Int(line_pos as i64), Argument::Positional(Expression {
Span::unknown(), span: Span::unknown(),
Type::Int, ty: Type::Int,
)), expr: Expr::Int(line_pos as i64),
custom_completion: None,
}),
], ],
parser_info: HashMap::new(), parser_info: HashMap::new(),
}, },
@@ -72,62 +75,67 @@ 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))
})
.unwrap_or_default();
let options = val.get("options");
if let Some(Value::Record { val: options, .. }) = &options {
let should_sort = options
.get("sort")
.and_then(|val| val.as_bool().ok())
.unwrap_or(false);
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),
match_algorithm: match options.get("completion_algorithm") {
Some(option) => option
.coerce_string()
.ok() .ok()
.and_then(|option| option.try_into().ok()) .map(|it| map_value_completions(it.iter(), span, offset))
.unwrap_or(MatchAlgorithm::Prefix), })
None => completion_options.match_algorithm, .unwrap_or_default();
}, let options = val.get("options");
sort: if should_sort {
CompletionSort::Alphabetical
} else {
CompletionSort::Smart
},
});
}
completions if let Some(Value::Record { val: options, .. }) = &options {
let should_sort = options
.get("sort")
.and_then(|val| val.as_bool().ok())
.unwrap_or(false);
if should_sort {
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),
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,
},
});
}
completions
}
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
_ => vec![],
} }
Value::List { vals, .. } => map_value_completions(vals.iter(), span, offset),
_ => vec![],
}) })
.unwrap_or_default(); .unwrap_or_default();
let options = custom_completion_options if let Some(custom_completion_options) = custom_completion_options {
.as_ref() filter(&prefix, suggestions, &custom_completion_options)
.unwrap_or(completion_options); } else {
let suggestions = filter(&prefix, suggestions, completion_options); filter(&prefix, suggestions, completion_options)
sort_suggestions(&String::from_utf8_lossy(&prefix), suggestions, options) }
}
fn get_sort_by(&self) -> SortBy {
self.sort_by
} }
} }

View File

@@ -1,23 +1,32 @@
use crate::completions::{ use crate::completions::{
completion_common::{adjust_if_intermediate, complete_item, AdjustView}, completion_common::{adjust_if_intermediate, complete_item, AdjustView},
Completer, CompletionOptions, Completer, CompletionOptions, SortBy,
}; };
use nu_ansi_term::Style; use nu_ansi_term::Style;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
Span, levenshtein_distance, Span,
}; };
use reedline::Suggestion; use reedline::Suggestion;
use std::path::Path; use std::{
path::{Path, MAIN_SEPARATOR as SEP},
sync::Arc,
};
use super::SemanticSuggestion; use super::SemanticSuggestion;
#[derive(Clone, Default)] #[derive(Clone)]
pub struct DirectoryCompletion {} pub struct DirectoryCompletion {
engine_state: Arc<EngineState>,
stack: Stack,
}
impl DirectoryCompletion { impl DirectoryCompletion {
pub fn new() -> Self { pub fn new(engine_state: Arc<EngineState>, stack: Stack) -> Self {
Self::default() Self {
engine_state,
stack,
}
} }
} }
@@ -25,46 +34,76 @@ 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<SemanticSuggestion> {
let AdjustView { prefix, span, .. } = adjust_if_intermediate(&prefix, working_set, span); let AdjustView { prefix, span, .. } = adjust_if_intermediate(&prefix, working_set, span);
// Filter only the folders // Filter only the folders
#[allow(deprecated)] let output: Vec<_> = directory_completion(
let items: 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, self.engine_state.as_ref(),
stack, &self.stack,
) )
.into_iter() .into_iter()
.map(move |x| SemanticSuggestion { .map(move |x| SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: x.1, value: x.1,
description: None,
style: x.2, style: x.2,
extra: None,
span: reedline::Span { span: reedline::Span {
start: x.0.start - offset, start: x.0.start - offset,
end: x.0.end - offset, end: x.0.end - offset,
}, },
..Suggestion::default() append_whitespace: false,
}, },
// TODO???? // TODO????
kind: None, kind: None,
}) })
.collect(); .collect();
output
}
// Sort results prioritizing the non hidden folders
fn sort(&self, items: Vec<SemanticSuggestion>, prefix: Vec<u8>) -> Vec<SemanticSuggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string();
// Sort items
let mut sorted_items = items;
match self.get_sort_by() {
SortBy::Ascending => {
sorted_items.sort_by(|a, b| {
// Ignore trailing slashes in folder names when sorting
a.suggestion
.value
.trim_end_matches(SEP)
.cmp(b.suggestion.value.trim_end_matches(SEP))
});
}
SortBy::LevenshteinDistance => {
sorted_items.sort_by(|a, b| {
let a_distance = levenshtein_distance(&prefix_str, &a.suggestion.value);
let b_distance = levenshtein_distance(&prefix_str, &b.suggestion.value);
a_distance.cmp(&b_distance)
});
}
_ => (),
}
// 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<SemanticSuggestion> = vec![];
let mut non_hidden: Vec<SemanticSuggestion> = vec![]; let mut non_hidden: Vec<SemanticSuggestion> = vec![];
for item in items.into_iter() { for item in sorted_items.into_iter() {
let item_path = Path::new(&item.suggestion.value); let item_path = Path::new(&item.suggestion.value);
if let Some(value) = item_path.file_name() { if let Some(value) = item_path.file_name() {

View File

@@ -1,31 +1,39 @@
use crate::completions::{file_path_completion, Completer, CompletionOptions}; use crate::completions::{file_path_completion, Completer, CompletionOptions, SortBy};
use nu_protocol::{ use nu_protocol::{
engine::{Stack, StateWorkingSet}, engine::{EngineState, Stack, 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, Path, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR},
sync::Arc,
};
use super::{completion_common::sort_suggestions, SemanticSuggestion}; use super::SemanticSuggestion;
#[derive(Clone, Default)] #[derive(Clone)]
pub struct DotNuCompletion {} pub struct DotNuCompletion {
engine_state: Arc<EngineState>,
stack: Stack,
}
impl DotNuCompletion { impl DotNuCompletion {
pub fn new() -> Self { pub fn new(engine_state: Arc<EngineState>, stack: Stack) -> Self {
Self::default() Self {
engine_state,
stack,
}
} }
} }
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<SemanticSuggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).replace('`', ""); let prefix_str = String::from_utf8_lossy(&prefix).replace('`', "");
@@ -41,25 +49,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.to_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 +84,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
@@ -95,8 +103,8 @@ impl Completer for DotNuCompletion {
&partial, &partial,
&search_dir, &search_dir,
options, options,
working_set.permanent_state, self.engine_state.as_ref(),
stack, &self.stack,
); );
completions completions
.into_iter() .into_iter()
@@ -116,13 +124,14 @@ impl Completer for DotNuCompletion {
.map(move |x| SemanticSuggestion { .map(move |x| SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: x.1, value: x.1,
description: None,
style: x.2, style: x.2,
extra: None,
span: reedline::Span { span: reedline::Span {
start: x.0.start - offset, start: x.0.start - offset,
end: x.0.end - offset, end: x.0.end - offset,
}, },
append_whitespace: true, append_whitespace: true,
..Suggestion::default()
}, },
// TODO???? // TODO????
kind: None, kind: None,
@@ -130,6 +139,10 @@ impl Completer for DotNuCompletion {
}) })
.collect(); .collect();
sort_suggestions(&prefix_str, output, options) output
}
fn get_sort_by(&self) -> SortBy {
SortBy::LevenshteinDistance
} }
} }

View File

@@ -1,24 +1,33 @@
use crate::completions::{ use crate::completions::{
completion_common::{adjust_if_intermediate, complete_item, AdjustView}, completion_common::{adjust_if_intermediate, complete_item, AdjustView},
Completer, CompletionOptions, Completer, CompletionOptions, SortBy,
}; };
use nu_ansi_term::Style; use nu_ansi_term::Style;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
Span, levenshtein_distance, Span,
}; };
use nu_utils::IgnoreCaseExt; use nu_utils::IgnoreCaseExt;
use reedline::Suggestion; use reedline::Suggestion;
use std::path::Path; use std::{
path::{Path, MAIN_SEPARATOR as SEP},
sync::Arc,
};
use super::SemanticSuggestion; use super::SemanticSuggestion;
#[derive(Clone, Default)] #[derive(Clone)]
pub struct FileCompletion {} pub struct FileCompletion {
engine_state: Arc<EngineState>,
stack: Stack,
}
impl FileCompletion { impl FileCompletion {
pub fn new() -> Self { pub fn new(engine_state: Arc<EngineState>, stack: Stack) -> Self {
Self::default() Self {
engine_state,
stack,
}
} }
} }
@@ -26,11 +35,10 @@ 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<SemanticSuggestion> {
let AdjustView { let AdjustView {
@@ -39,39 +47,68 @@ impl Completer for FileCompletion {
readjusted, readjusted,
} = adjust_if_intermediate(&prefix, working_set, span); } = adjust_if_intermediate(&prefix, working_set, span);
#[allow(deprecated)] let output: Vec<_> = complete_item(
let items: 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, self.engine_state.as_ref(),
stack, &self.stack,
) )
.into_iter() .into_iter()
.map(move |x| SemanticSuggestion { .map(move |x| SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: x.1, value: x.1,
description: None,
style: x.2, style: x.2,
extra: None,
span: reedline::Span { span: reedline::Span {
start: x.0.start - offset, start: x.0.start - offset,
end: x.0.end - offset, end: x.0.end - offset,
}, },
..Suggestion::default() append_whitespace: false,
}, },
// TODO???? // TODO????
kind: None, kind: None,
}) })
.collect(); .collect();
// Sort results prioritizing the non hidden folders output
}
// Sort results prioritizing the non hidden folders
fn sort(&self, items: Vec<SemanticSuggestion>, prefix: Vec<u8>) -> Vec<SemanticSuggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string();
// Sort items
let mut sorted_items = items;
match self.get_sort_by() {
SortBy::Ascending => {
sorted_items.sort_by(|a, b| {
// Ignore trailing slashes in folder names when sorting
a.suggestion
.value
.trim_end_matches(SEP)
.cmp(b.suggestion.value.trim_end_matches(SEP))
});
}
SortBy::LevenshteinDistance => {
sorted_items.sort_by(|a, b| {
let a_distance = levenshtein_distance(&prefix_str, &a.suggestion.value);
let b_distance = levenshtein_distance(&prefix_str, &b.suggestion.value);
a_distance.cmp(&b_distance)
});
}
_ => (),
}
// 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<SemanticSuggestion> = vec![];
let mut non_hidden: Vec<SemanticSuggestion> = vec![]; let mut non_hidden: Vec<SemanticSuggestion> = vec![];
for item in items.into_iter() { for item in sorted_items.into_iter() {
let item_path = Path::new(&item.suggestion.value); let item_path = Path::new(&item.suggestion.value);
if let Some(value) = item_path.file_name() { if let Some(value) = item_path.file_name() {

View File

@@ -1,7 +1,7 @@
use crate::completions::{completion_common::sort_suggestions, 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 reedline::Suggestion;
@@ -23,11 +23,10 @@ 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<SemanticSuggestion> {
// Check if it's a flag // Check if it's a flag
@@ -49,12 +48,13 @@ impl Completer for FlagCompletion {
suggestion: 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()),
style: None,
extra: None,
span: reedline::Span { span: reedline::Span {
start: span.start - offset, start: span.start - offset,
end: span.end - offset, end: span.end - offset,
}, },
append_whitespace: true, append_whitespace: true,
..Suggestion::default()
}, },
// TODO???? // TODO????
kind: None, kind: None,
@@ -75,12 +75,13 @@ impl Completer for FlagCompletion {
suggestion: 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()),
style: None,
extra: None,
span: reedline::Span { span: reedline::Span {
start: span.start - offset, start: span.start - offset,
end: span.end - offset, end: span.end - offset,
}, },
append_whitespace: true, append_whitespace: true,
..Suggestion::default()
}, },
// TODO???? // TODO????
kind: None, kind: None,
@@ -88,7 +89,7 @@ impl Completer for FlagCompletion {
} }
} }
return sort_suggestions(&String::from_utf8_lossy(&prefix), output, options); return output;
} }
vec![] vec![]

View File

@@ -13,7 +13,7 @@ mod variable_completions;
pub use base::{Completer, SemanticSuggestion, SuggestionKind}; pub use base::{Completer, SemanticSuggestion, SuggestionKind};
pub use command_completions::CommandCompletion; pub use command_completions::CommandCompletion;
pub use completer::NuCompleter; pub use completer::NuCompleter;
pub use completion_options::{CompletionOptions, MatchAlgorithm}; pub use completion_options::{CompletionOptions, MatchAlgorithm, SortBy};
pub use custom_completions::CustomCompletion; pub use custom_completions::CustomCompletion;
pub use directory_completions::DirectoryCompletion; pub use directory_completions::DirectoryCompletion;
pub use dotnu_completions::DotNuCompletion; pub use dotnu_completions::DotNuCompletion;

View File

@@ -3,22 +3,30 @@ use crate::completions::{
}; };
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, sync::Arc};
use super::completion_common::sort_suggestions;
#[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,
}
} }
} }
@@ -26,11 +34,10 @@ 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<SemanticSuggestion> {
let mut output = vec![]; let mut output = vec![];
@@ -42,13 +49,12 @@ impl Completer for VariableCompletion {
end: span.end - offset, end: span.end - offset,
}; };
let sublevels_count = self.var_context.1.len(); let sublevels_count = self.var_context.1.len();
let prefix_str = String::from_utf8_lossy(&prefix);
// Completions for the given variable // Completions for the given variable
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 {
@@ -62,7 +68,9 @@ 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.suggestion.value.as_bytes(),
@@ -72,7 +80,7 @@ impl Completer for VariableCompletion {
} }
} }
return sort_suggestions(&prefix_str, output, options); return output;
} }
} else { } else {
// No nesting provided, return all env vars // No nesting provided, return all env vars
@@ -85,15 +93,18 @@ impl Completer for VariableCompletion {
output.push(SemanticSuggestion { output.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: env_var.0, value: env_var.0,
description: None,
style: None,
extra: None,
span: current_span, span: current_span,
..Suggestion::default() append_whitespace: false,
}, },
kind: Some(SuggestionKind::Type(env_var.1.get_type())), kind: Some(SuggestionKind::Type(env_var.1.get_type())),
}); });
} }
} }
return sort_suggestions(&prefix_str, output, options); return output;
} }
} }
@@ -101,12 +112,13 @@ 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,
@@ -117,18 +129,19 @@ impl Completer for VariableCompletion {
} }
} }
return sort_suggestions(&prefix_str, output, options); return output;
} }
} }
// 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,
@@ -139,7 +152,7 @@ impl Completer for VariableCompletion {
} }
} }
return sort_suggestions(&prefix_str, output, options); return output;
} }
} }
} }
@@ -154,8 +167,11 @@ impl Completer for VariableCompletion {
output.push(SemanticSuggestion { output.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: builtin.to_string(), value: builtin.to_string(),
description: None,
style: None,
extra: None,
span: current_span, span: current_span,
..Suggestion::default() append_whitespace: false,
}, },
// TODO is there a way to get the VarId to get the type??? // TODO is there a way to get the VarId to get the type???
kind: None, kind: None,
@@ -178,8 +194,11 @@ impl Completer for VariableCompletion {
output.push(SemanticSuggestion { output.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: String::from_utf8_lossy(v.0).to_string(), value: String::from_utf8_lossy(v.0).to_string(),
description: None,
style: None,
extra: None,
span: current_span, span: current_span,
..Suggestion::default() append_whitespace: false,
}, },
kind: Some(SuggestionKind::Type( kind: Some(SuggestionKind::Type(
working_set.get_variable(*v.1).ty.clone(), working_set.get_variable(*v.1).ty.clone(),
@@ -192,11 +211,7 @@ 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,
@@ -206,8 +221,11 @@ impl Completer for VariableCompletion {
output.push(SemanticSuggestion { output.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: String::from_utf8_lossy(v.0).to_string(), value: String::from_utf8_lossy(v.0).to_string(),
description: None,
style: None,
extra: None,
span: current_span, span: current_span,
..Suggestion::default() append_whitespace: false,
}, },
kind: Some(SuggestionKind::Type( kind: Some(SuggestionKind::Type(
working_set.get_variable(*v.1).ty.clone(), working_set.get_variable(*v.1).ty.clone(),
@@ -217,8 +235,6 @@ impl Completer for VariableCompletion {
} }
} }
output = sort_suggestions(&prefix_str, output, options);
output.dedup(); // TODO: Removes only consecutive duplicates, is it intended? output.dedup(); // TODO: Removes only consecutive duplicates, is it intended?
output output
@@ -228,23 +244,44 @@ 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<SemanticSuggestion> {
let mut output: Vec<SemanticSuggestion> = vec![]; let mut output: Vec<SemanticSuggestion> = 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()); 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(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: col.clone(), value: col,
description: None,
style: None,
extra: None,
span: current_span, span: current_span,
..Suggestion::default() 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(SemanticSuggestion {
suggestion: Suggestion {
value: column_name.to_string(),
description: None,
style: None,
extra: None,
span: current_span,
append_whitespace: false,
}, },
kind: Some(kind.clone()), kind: Some(kind.clone()),
}); });
@@ -257,8 +294,11 @@ fn nested_suggestions(
output.push(SemanticSuggestion { output.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: column_name, value: column_name,
description: None,
style: None,
extra: None,
span: current_span, span: current_span,
..Suggestion::default() append_whitespace: false,
}, },
kind: Some(kind.clone()), kind: Some(kind.clone()),
}); });
@@ -271,36 +311,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,18 @@
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;
#[cfg(feature = "plugin")]
use nu_protocol::{engine::StateWorkingSet, report_error, ParseError, PluginRegistryFile, Spanned};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{EngineState, Stack, StateWorkingSet},
report_error_new, HistoryFileFormat, PipelineData, report_error, HistoryFileFormat, PipelineData,
}; };
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_utils::perf; use nu_protocol::{ParseError, Spanned};
#[cfg(feature = "plugin")]
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,140 +20,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();
// Check and warn + abort if this is a .nu plugin file
if plugin_file
.as_ref()
.and_then(|p| Path::new(&p.item).extension())
.is_some_and(|ext| ext == "nu")
{
report_error_new(
engine_state,
&ShellError::GenericError {
error: "Wrong plugin file format".into(),
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!(
"add plugin file to engine_state",
start_time,
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 { if let Some(plugin_path) = plugin_path {
// Open the plugin file let plugin_filename = plugin_path.to_string_lossy();
let mut file = match std::fs::File::open(&plugin_path) { plug_path = plugin_filename.to_string();
Ok(file) => file, if let Ok(contents) = std::fs::read(&plugin_path) {
Err(err) => { eval_source(
if err.kind() == std::io::ErrorKind::NotFound { engine_state,
log::warn!("Plugin file not found: {}", plugin_path.display()); stack,
&contents,
// Try migration of an old plugin file if this wasn't a custom plugin file &plugin_filename,
if plugin_file.is_none() && migrate_old_plugin_file(engine_state, storage_path) PipelineData::empty(),
{ false,
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,
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,
engine_state.get_config().use_ansi_coloring
);
} }
perf(
&format!("read_plugin_file {}", &plug_path),
start_time,
file!(),
line!(),
column!(),
engine_state.get_config().use_ansi_coloring,
);
} }
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
@@ -164,40 +62,23 @@ 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;
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
let cwd = working_set.get_cwd();
if let Ok(cwd) = engine_state.cwd_as_string(None) { if let Some(plugin_file) = plugin_file {
if let Some(plugin_file) = plugin_file { if let Ok(path) = canonicalize_with(&plugin_file.item, cwd) {
let path = Path::new(&plugin_file.item); engine_state.plugin_signatures = Some(path)
let path_dir = path.parent().unwrap_or(path); } else {
// Just try to canonicalize the directory of the plugin file first. let e = ParseError::FileNotFound(plugin_file.item, plugin_file.span);
if let Ok(path_dir) = canonicalize_with(path_dir, &cwd) { report_error(&working_set, &e);
// 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.into());
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);
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_signatures = Some(plugin_path);
} }
} }
@@ -210,10 +91,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,
@@ -223,18 +100,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);
} }
} }
} }
@@ -248,135 +124,6 @@ pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> O
HistoryFileFormat::PlainText => HISTORY_FILE_TXT, HistoryFileFormat::PlainText => HISTORY_FILE_TXT,
HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE, HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE,
}); });
history_path.into() 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 {
metadata: Default::default(),
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,
engine_state.get_config().use_ansi_coloring
);
true
}

View File

@@ -1,19 +1,12 @@
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::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, PipelineData, ShellError, Spanned, Value, report_error, PipelineData, Spanned, Value,
}; };
use std::sync::Arc;
#[derive(Default)]
pub struct EvaluateCommandsOpts {
pub table_mode: Option<Value>,
pub error_style: Option<Value>,
pub no_newline: bool,
}
/// 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(
@@ -21,40 +14,21 @@ pub fn evaluate_commands(
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: &mut Stack, stack: &mut Stack,
input: PipelineData, input: PipelineData,
opts: EvaluateCommandsOpts, table_mode: Option<Value>,
) -> Result<(), ShellError> { ) -> Result<Option<i64>> {
let EvaluateCommandsOpts {
table_mode,
error_style,
no_newline,
} = opts;
// Handle the configured error style early
if let Some(e_style) = error_style {
match e_style.coerce_str()?.parse() {
Ok(e_style) => {
Arc::make_mut(&mut engine_state.config).error_style = e_style;
}
Err(err) => {
return Err(ShellError::GenericError {
error: "Invalid value for `--error-style`".into(),
msg: err.into(),
span: Some(e_style.span()),
help: None,
inner: vec![],
});
}
}
}
// 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 {
Arc::make_mut(&mut engine_state.config).table_mode = let mut config = engine_state.get_config().clone();
t_mode.coerce_str()?.parse().unwrap_or_default(); config.table_mode = t_mode.coerce_str()?.parse().unwrap_or_default();
engine_state.set_config(config);
} }
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
@@ -66,39 +40,37 @@ pub fn evaluate_commands(
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);
}
if let Some(err) = working_set.compile_errors.first() { std::process::exit(1);
report_error(&working_set, err);
// Not a fatal error, for now
} }
(output, working_set.render()) (output, working_set.render())
}; };
// 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::<WithoutDebug>(engine_state, stack, &block, input) {
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.coerce_str()?.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,93 @@
use crate::util::eval_source; use crate::util::eval_source;
use log::{info, trace}; use log::{info, trace};
use nu_engine::{convert_env_values, eval_block}; use miette::{IntoDiagnostic, Result};
use nu_engine::{convert_env_values, current_dir, eval_block};
use nu_parser::parse; use nu_parser::parse;
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::{ use nu_protocol::{
ast::Call,
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, PipelineData, ShellError, Span, Value, report_error, Config, PipelineData, ShellError, Span, Value,
}; };
use nu_utils::stdout_write_all_and_flush;
use std::sync::Arc; use std::sync::Arc;
/// 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 {
msg: format!("Could not access file '{}': {:?}", path, e.to_string()),
span: Span::unknown(),
},
);
std::process::exit(1);
});
let file_path_str = file_path let file_path_str = file_path.to_str().unwrap_or_else(|| {
.to_str() let working_set = StateWorkingSet::new(engine_state);
.ok_or_else(|| ShellError::NonUtf8Custom { report_error(
msg: format!( &working_set,
"Input file name '{}' is not valid UTF8", &ShellError::NonUtf8Custom {
file_path.to_string_lossy() msg: format!(
), "Input file name '{}' is not valid UTF8",
span: Span::unknown(), 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 = std::fs::read(&file_path)
msg: format!("Could not read file '{file_path_str}': {err}"), .into_diagnostic()
span: Span::unknown(), .unwrap_or_else(|e| {
})?; let working_set = StateWorkingSet::new(engine_state);
engine_state.file = Some(file_path.clone()); report_error(
&working_set,
&ShellError::FileNotFoundCustom {
msg: format!(
"Could not read file '{}': {:?}",
file_path_str,
e.to_string()
),
span: Span::unknown(),
},
);
std::process::exit(1);
});
let parent = file_path engine_state.start_in_file(Some(file_path_str));
.parent()
.ok_or_else(|| ShellError::FileNotFoundCustom { let parent = file_path.parent().unwrap_or_else(|| {
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 {
msg: format!("The file path '{file_path_str}' does not have a parent"),
span: Span::unknown(),
},
);
std::process::exit(1);
});
stack.add_env_var( stack.add_env_var(
"FILE_PWD".to_string(), "FILE_PWD".to_string(),
@@ -70,28 +104,17 @@ pub fn evaluate_file(
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 let Some(warning) = working_set.parse_warnings.first() {
report_error(&working_set, warning);
}
// 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);
} }
if let Some(err) = working_set.compile_errors.first() {
report_error(&working_set, err);
// Not a fatal error, for now
}
// Look for blocks whose name starts with "main" and replace it with the filename.
for block in working_set.delta.blocks.iter_mut().map(Arc::make_mut) { 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();
@@ -101,49 +124,131 @@ 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 =
match eval_block::<WithoutDebug>(engine_state, stack, &block, PipelineData::empty()) { eval_block::<WithoutDebug>(engine_state, stack, &block, PipelineData::empty());
Ok(data) => data, let pipeline_data = match pipeline_data {
Err(ShellError::Return { .. }) => { Err(ShellError::Return { .. }) => {
// Allow early return before main is run. // allows early exists before `main` is run.
return Ok(()); 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 call = Call::new(Span::new(0, 0));
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.to_expanded_string("\n", config) + "\n";
let _ = stdout_write_all_and_flush(out).map_err(|err| eprintln!("{err}"));
}
}

View File

@@ -1,4 +1,3 @@
#![doc = include_str!("../README.md")]
mod commands; mod commands;
mod completions; mod completions;
mod config_files; mod config_files;
@@ -18,7 +17,7 @@ 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, SemanticSuggestion, SuggestionKind};
pub use config_files::eval_config_contents; pub use config_files::eval_config_contents;
pub use eval_cmds::{evaluate_commands, EvaluateCommandsOpts}; 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::NuHelpCompleter;
pub use nu_cmd_base::util::get_init_cwd; pub use nu_cmd_base::util::get_init_cwd;
@@ -33,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

@@ -1,70 +1,62 @@
use nu_engine::documentation::{get_flags_section, HelpStyle}; use nu_engine::documentation::get_flags_section;
use nu_protocol::{engine::EngineState, levenshtein_distance, Config}; 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, sync::Arc};
pub struct NuHelpCompleter { pub struct NuHelpCompleter(Arc<EngineState>);
engine_state: Arc<EngineState>,
config: Arc<Config>,
}
impl NuHelpCompleter { impl NuHelpCompleter {
pub fn new(engine_state: Arc<EngineState>, config: Arc<Config>) -> Self { pub fn new(engine_state: Arc<EngineState>) -> Self {
Self { Self(engine_state)
engine_state,
config,
}
} }
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 help_style = HelpStyle::default(); //Vec<(Signature, Vec<Example>, bool, bool)> {
help_style.update_from_config(&self.engine_state, &self.config); let mut commands = full_commands
.iter()
let mut commands = self .filter(|(sig, _, _, _, _)| {
.engine_state sig.name.to_folded_case().contains(&folded_line)
.get_decls_sorted(false) || sig.usage.to_folded_case().contains(&folded_line)
.into_iter() || sig
.filter_map(|(_, decl_id)| { .search_terms
let decl = self.engine_state.get_decl(decl_id); .iter()
(decl.name().to_folded_case().contains(&folded_line)
|| decl.usage().to_folded_case().contains(&folded_line)
|| decl
.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(&sig, &help_style, |v| { long_desc.push_str(&get_flags_section(Some(&*self.0.clone()), sig, |v| {
v.to_parsable_string(", ", &self.config) v.to_parsable_string(", ", &self.0.config)
})) }))
} }
@@ -80,7 +72,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.config), &value.to_parsable_string(", ", &self.0.config),
) )
} else { } else {
(" (optional)").to_string() (" (optional)").to_string()
@@ -101,21 +93,21 @@ 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 - line.len(),
end: pos, end: pos,
}, },
..Suggestion::default() append_whitespace: false,
} }
}) })
.collect() .collect()
@@ -146,8 +138,7 @@ mod test {
) { ) {
let engine_state = let engine_state =
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context()); nu_command::add_shell_command_context(nu_cmd_lang::create_default_context());
let config = engine_state.get_config().clone(); let mut completer = NuHelpCompleter::new(engine_state.into());
let mut completer = NuHelpCompleter::new(engine_state.into(), config);
let suggestions = completer.complete(line, end); let suggestions = completer.complete(line, end);
assert_eq!( assert_eq!(

View File

@@ -28,7 +28,7 @@ impl NuMenuCompleter {
Self { Self {
block_id, block_id,
span, span,
stack: stack.reset_out_dest().capture(), stack: stack.reset_stdio().capture(),
engine_state, engine_state,
only_buffer_difference, only_buffer_difference,
} }
@@ -59,7 +59,8 @@ impl Completer for NuMenuCompleter {
let res = eval_block::<WithoutDebug>(&self.engine_state, &mut self.stack, block, input); let res = eval_block::<WithoutDebug>(&self.engine_state, &mut self.stack, block, input);
if let Ok(values) = res.and_then(|data| data.into_value(self.span)) { if let Ok(values) = res {
let values = values.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()
@@ -142,9 +143,10 @@ fn convert_to_suggestions(
vec![Suggestion { vec![Suggestion {
value: text, value: text,
description, description,
style: None,
extra, extra,
span, span,
..Suggestion::default() append_whitespace: false,
}] }]
} }
Value::List { vals, .. } => vals Value::List { vals, .. } => vals
@@ -153,6 +155,9 @@ fn convert_to_suggestions(
.collect(), .collect(),
_ => vec![Suggestion { _ => vec![Suggestion {
value: format!("Not a record: {value:?}"), value: format!("Not a record: {value:?}"),
description: None,
style: None,
extra: None,
span: reedline::Span { span: reedline::Span {
start: if only_buffer_difference { start: if only_buffer_difference {
pos - line.len() pos - line.len()
@@ -165,7 +170,7 @@ fn convert_to_suggestions(
line.len() line.len()
}, },
}, },
..Suggestion::default() append_whitespace: false,
}], }],
} }
} }

View File

@@ -1,5 +1,3 @@
use std::sync::Arc;
use nu_engine::command_prelude::*; use nu_engine::command_prelude::*;
use reedline::{Highlighter, StyledText}; use reedline::{Highlighter, StyledText};
@@ -34,11 +32,14 @@ impl Command for NuHighlight {
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let head = call.head; let head = call.head;
let signals = engine_state.signals(); let ctrlc = engine_state.ctrlc.clone();
let engine_state = std::sync::Arc::new(engine_state.clone());
let config = engine_state.get_config().clone();
let highlighter = crate::NuHighlighter { let highlighter = crate::NuHighlighter {
engine_state: Arc::new(engine_state.clone()), engine_state,
stack: Arc::new(stack.clone()), stack: std::sync::Arc::new(stack.clone()),
config,
}; };
input.map( input.map(
@@ -49,7 +50,7 @@ impl Command for NuHighlight {
} }
Err(err) => Value::error(err, head), Err(err) => Value::error(err, head),
}, },
signals, ctrlc,
) )
} }

View File

@@ -22,11 +22,6 @@ impl Command for Print {
Some('n'), Some('n'),
) )
.switch("stderr", "print to stderr instead of stdout", Some('e')) .switch("stderr", "print to stderr instead of stdout", Some('e'))
.switch(
"raw",
"print without formatting (including binary data)",
Some('r'),
)
.category(Category::Strings) .category(Category::Strings)
} }
@@ -55,25 +50,15 @@ Since this command has no output, there is no point in piping it with other comm
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(engine_state, stack, "no-newline")?;
let to_stderr = call.has_flag(engine_state, stack, "stderr")?; let to_stderr = call.has_flag(engine_state, stack, "stderr")?;
let raw = call.has_flag(engine_state, stack, "raw")?;
// 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() {
for arg in args { for arg in args {
if raw { arg.into_pipeline_data()
arg.into_pipeline_data() .print(engine_state, stack, no_newline, to_stderr)?;
.print_raw(engine_state, no_newline, to_stderr)?;
} else {
arg.into_pipeline_data()
.print(engine_state, stack, no_newline, to_stderr)?;
}
} }
} else if !input.is_nothing() { } else if !input.is_nothing() {
if raw { input.print(engine_state, stack, no_newline, to_stderr)?;
input.print_raw(engine_state, no_newline, to_stderr)?;
} else {
input.print(engine_state, stack, no_newline, to_stderr)?;
}
} }
Ok(PipelineData::empty()) Ok(PipelineData::empty())
@@ -91,11 +76,6 @@ Since this command has no output, there is no point in piping it with other comm
example: r#"print (2 + 3)"#, example: r#"print (2 + 3)"#,
result: None, result: None,
}, },
Example {
description: "Print 'ABC' from binary data",
example: r#"0x[41 42 43] | print --raw"#,
result: None,
},
] ]
} }
} }

View File

@@ -1,10 +1,4 @@
use crate::prompt_update::{ use crate::prompt_update::{POST_PROMPT_MARKER, PRE_PROMPT_MARKER};
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::{
@@ -16,8 +10,7 @@ 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: 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,12 @@ 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 NushellPrompt { impl NushellPrompt {
pub fn new( pub fn new(shell_integration: bool) -> NushellPrompt {
shell_integration_osc133: bool,
shell_integration_osc633: bool,
engine_state: EngineState,
stack: Stack,
) -> NushellPrompt {
NushellPrompt { NushellPrompt {
shell_integration_osc133, shell_integration,
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 +31,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,19 +106,7 @@ impl Prompt for NushellPrompt {
.to_string() .to_string()
.replace('\n', "\r\n"); .replace('\n', "\r\n");
if self.shell_integration_osc633 { if self.shell_integration {
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() format!("{PRE_PROMPT_MARKER}{prompt}{POST_PROMPT_MARKER}").into()
} else { } else {
prompt.into() prompt.into()

View File

@@ -1,9 +1,9 @@
use crate::NushellPrompt; use crate::NushellPrompt;
use log::trace; use log::trace;
use nu_engine::ClosureEvalOnce; use nu_engine::get_eval_subexpression;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{EngineState, Stack, StateWorkingSet},
report_error_new, Config, PipelineData, Value, report_error, Config, PipelineData, Value,
}; };
use reedline::Prompt; use reedline::Prompt;
@@ -23,40 +23,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\\"; pub(crate) const PRE_PROMPT_MARKER: &str = "\x1b]133;A\x1b\\";
pub(crate) const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\"; pub(crate) const POST_PROMPT_MARKER: &str = "\x1b]133;B\x1b\\";
pub(crate) const PRE_EXECUTION_MARKER: &str = "\x1b]133;C\x1b\\";
#[allow(dead_code)]
pub(crate) const POST_EXECUTION_MARKER_PREFIX: &str = "\x1b]133;D;";
#[allow(dead_code)]
pub(crate) const POST_EXECUTION_MARKER_SUFFIX: &str = "\x1b\\";
// OSC633 is the same as OSC133 but specifically for VSCode
pub(crate) const VSCODE_PRE_PROMPT_MARKER: &str = "\x1b]633;A\x1b\\";
pub(crate) const VSCODE_POST_PROMPT_MARKER: &str = "\x1b]633;B\x1b\\";
#[allow(dead_code)]
pub(crate) const VSCODE_PRE_EXECUTION_MARKER: &str = "\x1b]633;C\x1b\\";
#[allow(dead_code)]
//"\x1b]633;D;{}\x1b\\"
pub(crate) const VSCODE_POST_EXECUTION_MARKER_PREFIX: &str = "\x1b]633;D;";
#[allow(dead_code)]
pub(crate) const VSCODE_POST_EXECUTION_MARKER_SUFFIX: &str = "\x1b\\";
#[allow(dead_code)]
//"\x1b]633;E;{}\x1b\\"
pub(crate) const VSCODE_COMMANDLINE_MARKER_PREFIX: &str = "\x1b]633;E;";
#[allow(dead_code)]
pub(crate) const VSCODE_COMMANDLINE_MARKER_SUFFIX: &str = "\x1b\\";
#[allow(dead_code)]
// "\x1b]633;P;Cwd={}\x1b\\"
pub(crate) const VSCODE_CWD_PROPERTY_MARKER_PREFIX: &str = "\x1b]633;P;Cwd=";
#[allow(dead_code)]
pub(crate) const VSCODE_CWD_PROPERTY_MARKER_SUFFIX: &str = "\x1b\\";
pub(crate) const RESET_APPLICATION_MODE: &str = "\x1b[?1l";
fn get_prompt_string( fn get_prompt_string(
prompt: &str, prompt: &str,
@@ -64,13 +34,17 @@ fn get_prompt_string(
engine_state: &EngineState, engine_state: &EngineState,
stack: &mut Stack, stack: &mut Stack,
) -> Option<String> { ) -> Option<String> {
let eval_subexpression = get_eval_subexpression(engine_state);
stack stack
.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!(),
@@ -78,9 +52,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()
} }
@@ -110,34 +103,20 @@ pub(crate) fn update_prompt(
stack: &mut Stack, stack: &mut Stack,
nu_prompt: &mut NushellPrompt, nu_prompt: &mut NushellPrompt,
) { ) {
let configured_left_prompt_string = let left_prompt_string = get_prompt_string(PROMPT_COMMAND, config, engine_state, stack);
match get_prompt_string(PROMPT_COMMAND, config, engine_state, stack) {
Some(s) => s,
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, stack);

View File

@@ -1,6 +1,6 @@
use crate::{menus::NuMenuCompleter, NuHelpCompleter}; use crate::{menus::NuMenuCompleter, NuHelpCompleter};
use crossterm::event::{KeyCode, KeyModifiers}; use crossterm::event::{KeyCode, KeyModifiers};
use nu_ansi_term::Style; use log::trace;
use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style}; 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::parse; use nu_parser::parse;
@@ -75,21 +75,15 @@ const DEFAULT_HELP_MENU: &str = r#"
// Adds all menus to line editor // Adds all menus to line editor
pub(crate) fn add_menus( pub(crate) fn add_menus(
mut line_editor: Reedline, mut line_editor: Reedline,
engine_state_ref: Arc<EngineState>, engine_state: Arc<EngineState>,
stack: &Stack, stack: &Stack,
config: Arc<Config>, config: &Config,
) -> Result<Reedline, ShellError> { ) -> Result<Reedline, ShellError> {
//log::trace!("add_menus: config: {:#?}", &config); trace!("add_menus: config: {:#?}", &config);
line_editor = line_editor.clear_menus(); line_editor = line_editor.clear_menus();
for menu in &config.menus { for menu in &config.menus {
line_editor = add_menu( line_editor = add_menu(line_editor, menu, engine_state.clone(), stack, config)?
line_editor,
menu,
engine_state_ref.clone(),
stack,
config.clone(),
)?
} }
// Checking if the default menus have been added from the config file // Checking if the default menus have been added from the config file
@@ -99,16 +93,13 @@ pub(crate) fn add_menus(
("help_menu", DEFAULT_HELP_MENU), ("help_menu", DEFAULT_HELP_MENU),
]; ];
let mut engine_state = (*engine_state_ref).clone();
let mut menu_eval_results = vec![];
for (name, definition) in default_menus { for (name, definition) in default_menus {
if !config if !config
.menus .menus
.iter() .iter()
.any(|menu| menu.name.to_expanded_string("", &config) == name) .any(|menu| menu.name.to_expanded_string("", config) == name)
{ {
let (block, delta) = { let (block, _) = {
let mut working_set = StateWorkingSet::new(&engine_state); let mut working_set = StateWorkingSet::new(&engine_state);
let output = parse( let output = parse(
&mut working_set, &mut working_set,
@@ -120,31 +111,15 @@ pub(crate) fn add_menus(
(output, working_set.render()) (output, working_set.render())
}; };
engine_state.merge_delta(delta)?;
let mut temp_stack = Stack::new().capture(); let mut temp_stack = Stack::new().capture();
let input = PipelineData::Empty; let input = PipelineData::Empty;
menu_eval_results.push(eval_block::<WithoutDebug>( let res = eval_block::<WithoutDebug>(&engine_state, &mut temp_stack, &block, input)?;
&engine_state,
&mut temp_stack,
&block,
input,
)?);
}
}
let new_engine_state_ref = Arc::new(engine_state); if let PipelineData::Value(value, None) = res {
for menu in create_menus(&value)? {
for res in menu_eval_results.into_iter() { line_editor =
if let PipelineData::Value(value, None) = res { add_menu(line_editor, &menu, engine_state.clone(), stack, config)?;
for menu in create_menus(&value)? { }
line_editor = add_menu(
line_editor,
&menu,
new_engine_state_ref.clone(),
stack,
config.clone(),
)?;
} }
} }
} }
@@ -157,63 +132,47 @@ fn add_menu(
menu: &ParsedMenu, menu: &ParsedMenu,
engine_state: Arc<EngineState>, engine_state: Arc<EngineState>,
stack: &Stack, stack: &Stack,
config: Arc<Config>, config: &Config,
) -> Result<Reedline, ShellError> { ) -> Result<Reedline, ShellError> {
let span = menu.menu_type.span(); let span = menu.menu_type.span();
if let Value::Record { val, .. } = &menu.menu_type { if let Value::Record { val, .. } = &menu.menu_type {
let layout = extract_value("layout", val, span)?.to_expanded_string("", &config); let layout = extract_value("layout", val, span)?.to_expanded_string("", config);
match layout.as_str() { match layout.as_str() {
"columnar" => add_columnar_menu(line_editor, menu, engine_state, stack, &config), "columnar" => add_columnar_menu(line_editor, menu, engine_state, stack, config),
"list" => add_list_menu(line_editor, menu, engine_state, stack, config), "list" => add_list_menu(line_editor, menu, engine_state, stack, config),
"ide" => add_ide_menu(line_editor, menu, engine_state, stack, config), "ide" => add_ide_menu(line_editor, menu, engine_state, stack, config),
"description" => add_description_menu(line_editor, menu, engine_state, stack, config), "description" => add_description_menu(line_editor, menu, engine_state, stack, config),
_ => Err(ShellError::UnsupportedConfigValue { _ => Err(ShellError::UnsupportedConfigValue {
expected: "columnar, list, ide or description".to_string(), expected: "columnar, list, ide or description".to_string(),
value: menu.menu_type.to_abbreviated_string(&config), value: menu.menu_type.to_abbreviated_string(config),
span: menu.menu_type.span(), span: menu.menu_type.span(),
}), }),
} }
} else { } else {
Err(ShellError::UnsupportedConfigValue { Err(ShellError::UnsupportedConfigValue {
expected: "only record type".to_string(), expected: "only record type".to_string(),
value: menu.menu_type.to_abbreviated_string(&config), value: menu.menu_type.to_abbreviated_string(config),
span: menu.menu_type.span(), span: menu.menu_type.span(),
}) })
} }
} }
fn get_style(record: &Record, name: &str, span: Span) -> Option<Style> { macro_rules! add_style {
extract_value(name, record, span) // first arm match add!(1,2), add!(2,3) etc
.ok() ($name:expr, $record: expr, $span:expr, $config: expr, $menu:expr, $f:expr) => {
.map(|text| match text { $menu = match extract_value($name, $record, $span) {
Value::String { val, .. } => lookup_ansi_color_style(val), Ok(text) => {
Value::Record { .. } => color_record_to_nustyle(text), let style = match text {
_ => lookup_ansi_color_style("green"), Value::String { val, .. } => lookup_ansi_color_style(&val),
}) Value::Record { .. } => color_record_to_nustyle(&text),
} _ => lookup_ansi_color_style("green"),
};
fn set_menu_style<M: MenuBuilder>(mut menu: M, style: &Value) -> M { $f($menu, style)
let span = style.span(); }
let Value::Record { val, .. } = &style else { Err(_) => $menu,
return menu; };
}; };
if let Some(style) = get_style(val, "text", span) {
menu = menu.with_text_style(style);
}
if let Some(style) = get_style(val, "selected_text", span) {
menu = menu.with_selected_text_style(style);
}
if let Some(style) = get_style(val, "description_text", span) {
menu = menu.with_description_text_style(style);
}
if let Some(style) = get_style(val, "match_text", span) {
menu = menu.with_match_text_style(style);
}
if let Some(style) = get_style(val, "selected_match_text", span) {
menu = menu.with_selected_match_text_style(style);
}
menu
} }
// Adds a columnar menu to the editor engine // Adds a columnar menu to the editor engine
@@ -254,7 +213,49 @@ pub(crate) fn add_columnar_menu(
}; };
} }
columnar_menu = set_menu_style(columnar_menu, &menu.style); let span = menu.style.span();
if let Value::Record { val, .. } = &menu.style {
add_style!(
"text",
val,
span,
config,
columnar_menu,
ColumnarMenu::with_text_style
);
add_style!(
"selected_text",
val,
span,
config,
columnar_menu,
ColumnarMenu::with_selected_text_style
);
add_style!(
"description_text",
val,
span,
config,
columnar_menu,
ColumnarMenu::with_description_text_style
);
add_style!(
"match_text",
val,
span,
config,
columnar_menu,
ColumnarMenu::with_match_text_style
);
add_style!(
"selected_match_text",
val,
span,
config,
columnar_menu,
ColumnarMenu::with_selected_match_text_style
);
}
let marker = menu.marker.to_expanded_string("", config); let marker = menu.marker.to_expanded_string("", config);
columnar_menu = columnar_menu.with_marker(&marker); columnar_menu = columnar_menu.with_marker(&marker);
@@ -294,9 +295,9 @@ pub(crate) fn add_list_menu(
menu: &ParsedMenu, menu: &ParsedMenu,
engine_state: Arc<EngineState>, engine_state: Arc<EngineState>,
stack: &Stack, stack: &Stack,
config: Arc<Config>, config: &Config,
) -> Result<Reedline, ShellError> { ) -> Result<Reedline, ShellError> {
let name = menu.name.to_expanded_string("", &config); let name = menu.name.to_expanded_string("", config);
let mut list_menu = ListMenu::default().with_name(&name); let mut list_menu = ListMenu::default().with_name(&name);
let span = menu.menu_type.span(); let span = menu.menu_type.span();
@@ -310,9 +311,35 @@ pub(crate) fn add_list_menu(
}; };
} }
list_menu = set_menu_style(list_menu, &menu.style); let span = menu.style.span();
if let Value::Record { val, .. } = &menu.style {
add_style!(
"text",
val,
span,
config,
list_menu,
ListMenu::with_text_style
);
add_style!(
"selected_text",
val,
span,
config,
list_menu,
ListMenu::with_selected_text_style
);
add_style!(
"description_text",
val,
span,
config,
list_menu,
ListMenu::with_description_text_style
);
}
let marker = menu.marker.to_expanded_string("", &config); let marker = menu.marker.to_expanded_string("", config);
list_menu = list_menu.with_marker(&marker); list_menu = list_menu.with_marker(&marker);
let only_buffer_difference = menu.only_buffer_difference.as_bool()?; let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
@@ -338,7 +365,7 @@ pub(crate) fn add_list_menu(
} }
_ => Err(ShellError::UnsupportedConfigValue { _ => Err(ShellError::UnsupportedConfigValue {
expected: "block or omitted value".to_string(), expected: "block or omitted value".to_string(),
value: menu.source.to_abbreviated_string(&config), value: menu.source.to_abbreviated_string(config),
span: menu.source.span(), span: menu.source.span(),
}), }),
} }
@@ -350,10 +377,10 @@ pub(crate) fn add_ide_menu(
menu: &ParsedMenu, menu: &ParsedMenu,
engine_state: Arc<EngineState>, engine_state: Arc<EngineState>,
stack: &Stack, stack: &Stack,
config: Arc<Config>, config: &Config,
) -> Result<Reedline, ShellError> { ) -> Result<Reedline, ShellError> {
let span = menu.menu_type.span(); let span = menu.menu_type.span();
let name = menu.name.to_expanded_string("", &config); let name = menu.name.to_expanded_string("", config);
let mut ide_menu = IdeMenu::default().with_name(&name); let mut ide_menu = IdeMenu::default().with_name(&name);
if let Value::Record { val, .. } = &menu.menu_type { if let Value::Record { val, .. } = &menu.menu_type {
@@ -418,7 +445,7 @@ pub(crate) fn add_ide_menu(
} else { } else {
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue {
expected: "bool or record".to_string(), expected: "bool or record".to_string(),
value: border.to_abbreviated_string(&config), value: border.to_abbreviated_string(config),
span: border.span(), span: border.span(),
}); });
} }
@@ -442,7 +469,7 @@ pub(crate) fn add_ide_menu(
_ => { _ => {
return Err(ShellError::UnsupportedConfigValue { return Err(ShellError::UnsupportedConfigValue {
expected: "\"left\", \"right\" or \"prefer_right\"".to_string(), expected: "\"left\", \"right\" or \"prefer_right\"".to_string(),
value: description_mode.to_abbreviated_string(&config), value: description_mode.to_abbreviated_string(config),
span: description_mode.span(), span: description_mode.span(),
}); });
} }
@@ -491,9 +518,51 @@ pub(crate) fn add_ide_menu(
}; };
} }
ide_menu = set_menu_style(ide_menu, &menu.style); let span = menu.style.span();
if let Value::Record { val, .. } = &menu.style {
add_style!(
"text",
val,
span,
config,
ide_menu,
IdeMenu::with_text_style
);
add_style!(
"selected_text",
val,
span,
config,
ide_menu,
IdeMenu::with_selected_text_style
);
add_style!(
"description_text",
val,
span,
config,
ide_menu,
IdeMenu::with_description_text_style
);
add_style!(
"match_text",
val,
span,
config,
ide_menu,
IdeMenu::with_match_text_style
);
add_style!(
"selected_match_text",
val,
span,
config,
ide_menu,
IdeMenu::with_selected_match_text_style
);
}
let marker = menu.marker.to_expanded_string("", &config); let marker = menu.marker.to_expanded_string("", config);
ide_menu = ide_menu.with_marker(&marker); ide_menu = ide_menu.with_marker(&marker);
let only_buffer_difference = menu.only_buffer_difference.as_bool()?; let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
@@ -519,7 +588,7 @@ pub(crate) fn add_ide_menu(
} }
_ => Err(ShellError::UnsupportedConfigValue { _ => Err(ShellError::UnsupportedConfigValue {
expected: "block or omitted value".to_string(), expected: "block or omitted value".to_string(),
value: menu.source.to_abbreviated_string(&config), value: menu.source.to_abbreviated_string(config),
span, span,
}), }),
} }
@@ -531,9 +600,9 @@ pub(crate) fn add_description_menu(
menu: &ParsedMenu, menu: &ParsedMenu,
engine_state: Arc<EngineState>, engine_state: Arc<EngineState>,
stack: &Stack, stack: &Stack,
config: Arc<Config>, config: &Config,
) -> Result<Reedline, ShellError> { ) -> Result<Reedline, ShellError> {
let name = menu.name.to_expanded_string("", &config); let name = menu.name.to_expanded_string("", config);
let mut description_menu = DescriptionMenu::default().with_name(&name); let mut description_menu = DescriptionMenu::default().with_name(&name);
let span = menu.menu_type.span(); let span = menu.menu_type.span();
@@ -579,9 +648,35 @@ pub(crate) fn add_description_menu(
}; };
} }
description_menu = set_menu_style(description_menu, &menu.style); let span = menu.style.span();
if let Value::Record { val, .. } = &menu.style {
add_style!(
"text",
val,
span,
config,
description_menu,
DescriptionMenu::with_text_style
);
add_style!(
"selected_text",
val,
span,
config,
description_menu,
DescriptionMenu::with_selected_text_style
);
add_style!(
"description_text",
val,
span,
config,
description_menu,
DescriptionMenu::with_description_text_style
);
}
let marker = menu.marker.to_expanded_string("", &config); let marker = menu.marker.to_expanded_string("", config);
description_menu = description_menu.with_marker(&marker); description_menu = description_menu.with_marker(&marker);
let only_buffer_difference = menu.only_buffer_difference.as_bool()?; let only_buffer_difference = menu.only_buffer_difference.as_bool()?;
@@ -590,7 +685,7 @@ pub(crate) fn add_description_menu(
let span = menu.source.span(); let span = menu.source.span();
match &menu.source { match &menu.source {
Value::Nothing { .. } => { Value::Nothing { .. } => {
let completer = Box::new(NuHelpCompleter::new(engine_state, config)); let completer = Box::new(NuHelpCompleter::new(engine_state));
Ok(line_editor.with_menu(ReedlineMenu::WithCompleter { Ok(line_editor.with_menu(ReedlineMenu::WithCompleter {
menu: Box::new(description_menu), menu: Box::new(description_menu),
completer, completer,
@@ -611,7 +706,7 @@ pub(crate) fn add_description_menu(
} }
_ => Err(ShellError::UnsupportedConfigValue { _ => Err(ShellError::UnsupportedConfigValue {
expected: "closure or omitted value".to_string(), expected: "closure or omitted value".to_string(),
value: menu.source.to_abbreviated_string(&config), value: menu.source.to_abbreviated_string(config),
span: menu.source.span(), span: menu.source.span(),
}), }),
} }

File diff suppressed because it is too large Load Diff

View File

@@ -4,9 +4,9 @@ use nu_color_config::{get_matching_brackets_style, get_shape_color};
use nu_engine::env; 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::{Block, Expr, Expression, PipelineRedirection, RecordItem}, ast::{Argument, Block, Expr, Expression, PipelineRedirection, RecordItem},
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
Span, Config, Span,
}; };
use reedline::{Highlighter, StyledText}; use reedline::{Highlighter, StyledText};
use std::sync::Arc; use std::sync::Arc;
@@ -14,14 +14,15 @@ 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 stack: Arc<Stack>,
pub config: Config,
} }
impl Highlighter for NuHighlighter { 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 config = self.stack.get_config(&self.engine_state); let highlight_resolved_externals =
let highlight_resolved_externals = config.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) = {
@@ -36,7 +37,6 @@ impl Highlighter for NuHighlighter {
let str_word = String::from_utf8_lossy(str_contents).to_string(); let str_word = String::from_utf8_lossy(str_contents).to_string();
let paths = env::path_str(&self.engine_state, &self.stack, *span).ok(); let paths = env::path_str(&self.engine_state, &self.stack, *span).ok();
#[allow(deprecated)]
let res = if let Ok(cwd) = let res = if let Ok(cwd) =
env::current_dir_str(&self.engine_state, &self.stack) env::current_dir_str(&self.engine_state, &self.stack)
{ {
@@ -86,8 +86,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(), &config), text)); output.push((get_shape_color(shape.to_string(), &self.config), text));
}; };
match shape.1 { match shape.1 {
@@ -107,37 +128,27 @@ impl Highlighter for NuHighlighter {
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(), &config);
if highlight {
style = get_matching_brackets_style(style, &config);
}
output.push((style, text));
}
} }
FlatShape::Filepath => add_colored_token(&shape.1, next_token), FlatShape::Filepath => add_colored_token(&shape.1, next_token),
FlatShape::Directory => add_colored_token(&shape.1, next_token), FlatShape::Directory => add_colored_token(&shape.1, next_token),
FlatShape::GlobInterpolation => add_colored_token(&shape.1, next_token),
FlatShape::GlobPattern => add_colored_token(&shape.1, next_token), FlatShape::GlobPattern => add_colored_token(&shape.1, next_token),
FlatShape::Variable(_) | FlatShape::VarDecl(_) => { FlatShape::Variable(_) | FlatShape::VarDecl(_) => {
add_colored_token(&shape.1, next_token) add_colored_token(&shape.1, next_token)
@@ -299,6 +310,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;
@@ -328,7 +353,6 @@ fn find_matching_block_end_in_expr(
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,
@@ -336,8 +360,9 @@ fn find_matching_block_end_in_expr(
Expr::MatchBlock(_) => None, Expr::MatchBlock(_) => None,
Expr::Nothing => None, Expr::Nothing => None,
Expr::Garbage => None, Expr::Garbage => None,
Expr::Spread(_) => 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 +371,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 +392,36 @@ 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 expr in exprs {
RecordItem::Pair(k, v) => find_matching_block_end_in_expr( match expr {
line, RecordItem::Pair(k, v) => {
working_set, find_in_expr_or_continue!(k);
k, find_in_expr_or_continue!(v);
global_span_offset, }
global_cursor_offset, RecordItem::Spread(_, record) => {
) find_in_expr_or_continue!(record);
.or_else(|| { }
find_matching_block_end_in_expr( }
line, }
working_set, None
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, Argument::Spread(inner_expr) => Some(inner_expr),
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,23 +431,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::Collect(_, expr) => find_matching_block_end_in_expr(
line,
working_set,
expr,
global_span_offset,
global_cursor_offset,
),
Expr::Block(block_id) Expr::Block(block_id)
| Expr::Closure(block_id) | Expr::Closure(block_id)
@@ -460,19 +461,14 @@ fn find_matching_block_end_in_expr(
} }
} }
Expr::StringInterpolation(exprs) | Expr::GlobInterpolation(exprs, _) => { Expr::StringInterpolation(inner_expr) => {
exprs.iter().find_map(|expr| { for inner_expr in inner_expr {
find_matching_block_end_in_expr( find_in_expr_or_continue!(inner_expr);
line, }
working_set, None
expr,
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)
@@ -480,15 +476,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

@@ -4,11 +4,11 @@ use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token,
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, report_error_new, PipelineData, ShellError, Span, Value, print_if_stream, report_error, report_error_new, PipelineData, ShellError, Span, Value,
}; };
#[cfg(windows)] #[cfg(windows)]
use nu_utils::enable_vt_processing; use nu_utils::enable_vt_processing;
use nu_utils::perf; use nu_utils::utils::perf;
use std::path::Path; use std::path::Path;
// This will collect environment variables from std::env and adds them to a stack. // This will collect environment variables from std::env and adds them to a stack.
@@ -39,8 +39,9 @@ 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(
&working_set,
&ShellError::GenericError { &ShellError::GenericError {
error: format!("Environment variable was not captured: {env_str}"), error: format!("Environment variable was not captured: {env_str}"),
msg: "".into(), msg: "".into(),
@@ -70,8 +71,9 @@ 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(
&working_set,
&ShellError::GenericError { &ShellError::GenericError {
error: "Current directory is not a valid utf-8 path".into(), error: "Current directory is not a valid utf-8 path".into(),
msg: "".into(), msg: "".into(),
@@ -206,45 +208,9 @@ 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) {
Ok(code) => code.unwrap_or(0),
Err(err) => {
report_error_new(engine_state, &err);
1
}
};
stack.add_env_var(
"LAST_EXIT_CODE".to_string(),
Value::int(exit_code.into(), Span::unknown()),
);
// reset vt processing, aka ansi because illbehaved externals can break it
#[cfg(windows)]
{
let _ = enable_vt_processing();
}
perf!(
&format!("eval_source {}", &fname),
start_time,
engine_state.get_config().use_ansi_coloring
);
exit_code
}
fn evaluate_source(
engine_state: &mut EngineState,
stack: &mut Stack,
source: &[u8],
fname: &str,
input: PipelineData,
allow_return: bool,
) -> Result<Option<i32>, ShellError> {
let (block, delta) = { let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let output = parse( let output = parse(
@@ -258,45 +224,104 @@ fn evaluate_source(
} }
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
set_last_exit_code(stack, 1);
report_error(&working_set, err); report_error(&working_set, err);
return Ok(Some(1)); return false;
}
if let Some(err) = working_set.compile_errors.first() {
report_error(&working_set, err);
// Not a fatal error, for now
} }
(output, working_set.render()) (output, working_set.render())
}; };
engine_state.merge_delta(delta)?; if let Err(err) = engine_state.merge_delta(delta) {
set_last_exit_code(stack, 1);
report_error_new(engine_state, &err);
return false;
}
let pipeline = if allow_return { let b = if allow_return {
eval_block_with_early_return::<WithoutDebug>(engine_state, stack, &block, input) eval_block_with_early_return::<WithoutDebug>(engine_state, stack, &block, input)
} else { } else {
eval_block::<WithoutDebug>(engine_state, stack, &block, input) 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())) 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(
&format!("eval_source {}", &fname),
start_time,
file!(),
line!(),
column!(),
engine_state.get_config().use_ansi_coloring,
);
true
}
fn set_last_exit_code(stack: &mut Stack, exit_code: i64) {
stack.add_env_var(
"LAST_EXIT_CODE".to_string(),
Value::int(exit_code, Span::unknown()),
);
} }
#[cfg(test)] #[cfg(test)]
@@ -321,10 +346,16 @@ mod test {
let env = engine_state.render_env_vars(); let env = engine_state.render_env_vars();
assert!(matches!(env.get("FOO"), Some(&Value::String { val, .. }) if val == "foo")); assert!(
assert!(matches!(env.get("SYMBOLS"), Some(&Value::String { val, .. }) if val == symbols)); matches!(env.get(&"FOO".to_string()), Some(&Value::String { val, .. }) if val == "foo")
assert!(matches!(env.get(symbols), Some(&Value::String { val, .. }) if val == "symbols")); );
assert!(env.contains_key("PWD")); assert!(
matches!(env.get(&"SYMBOLS".to_string()), Some(&Value::String { val, .. }) if val == symbols)
);
assert!(
matches!(env.get(&symbols.to_string()), Some(&Value::String { val, .. }) if val == "symbols")
);
assert!(env.get(&"PWD".to_string()).is_some());
assert_eq!(env.len(), 4); assert_eq!(env.len(), 4);
} }
} }

View File

@@ -1,7 +0,0 @@
use nu_test_support::nu;
#[test]
fn not_empty() {
let result = nu!("keybindings list | is-not-empty");
assert_eq!(result.out, "true");
}

View File

@@ -1,2 +0,0 @@
mod keybindings_list;
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,2 +0,0 @@
mod commands;
mod completions;

View File

@@ -1,34 +1,39 @@
use nu_engine::eval_block; use nu_engine::eval_block;
use nu_parser::parse; use nu_parser::parse;
use nu_path::{AbsolutePathBuf, PathBuf};
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, 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::MAIN_SEPARATOR; use std::path::PathBuf;
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())
} }
// creates a new engine with the current path into the completions fixtures folder // creates a new engine with the current path into the completions fixtures folder
pub fn new_engine() -> (AbsolutePathBuf, 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();
@@ -69,60 +74,15 @@ pub fn new_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
} }
// creates a new engine with the current path into the completions fixtures folder pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
pub fn new_dotnu_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
// Target folder inside assets
let dir = fs::fixtures().join("dotnu_completions");
let dir_str = dir
.clone()
.into_os_string()
.into_string()
.unwrap_or_default();
let dir_span = nu_protocol::Span::new(0, dir_str.len());
// Create a new engine with default context
let mut engine_state = create_default_context();
// Add $nu
engine_state.generate_nu_constant();
// New stack
let mut stack = Stack::new();
// Add pwd as env var
stack.add_env_var("PWD".to_string(), Value::string(dir_str.clone(), dir_span));
stack.add_env_var(
"TEST".to_string(),
Value::string("NUSHELL".to_string(), dir_span),
);
stack.add_env_var(
"NU_LIB_DIRS".to_string(),
Value::List {
vals: vec![
Value::string(file(dir.join("lib-dir1")), dir_span),
Value::string(file(dir.join("lib-dir2")), dir_span),
Value::string(file(dir.join("lib-dir3")), dir_span),
],
internal_span: dir_span,
},
);
// Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok());
(dir, dir_str, engine_state, stack)
}
pub fn new_quote_engine() -> (AbsolutePathBuf, 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();
@@ -150,14 +110,15 @@ pub fn new_quote_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
(dir, dir_str, engine_state, stack) (dir, dir_str, engine_state, stack)
} }
pub fn new_partial_engine() -> (AbsolutePathBuf, 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();
@@ -186,7 +147,7 @@ pub fn new_partial_engine() -> (AbsolutePathBuf, String, EngineState, Stack) {
} }
// match a list of suggestions with the expected values // match a list of suggestions with the expected values
pub fn match_suggestions(expected: &Vec<String>, suggestions: &Vec<Suggestion>) { pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
let expected_len = expected.len(); let expected_len = expected.len();
let suggestions_len = suggestions.len(); let suggestions_len = suggestions.len();
if expected_len != suggestions_len { if expected_len != suggestions_len {
@@ -196,25 +157,22 @@ pub fn match_suggestions(expected: &Vec<String>, suggestions: &Vec<Suggestion>)
Expected: {expected:#?}\n" Expected: {expected:#?}\n"
) )
} }
expected.iter().zip(suggestions).for_each(|it| {
let suggestoins_str = suggestions assert_eq!(it.0, &it.1.value);
.iter() });
.map(|it| it.value.clone())
.collect::<Vec<_>>();
assert_eq!(expected, &suggestoins_str);
} }
// append the separator to the converted path // append the separator to the converted path
pub fn folder(path: impl Into<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
} }
// convert a given path to string // convert a given path to string
pub fn file(path: impl Into<PathBuf>) -> String { pub fn file(path: PathBuf) -> String {
path.into().into_os_string().into_string().unwrap() path.into_os_string().into_string().unwrap_or_default()
} }
// merge_input executes the given input into the engine // merge_input executes the given input into the engine
@@ -223,7 +181,7 @@ pub fn merge_input(
input: &[u8], input: &[u8],
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: &mut Stack, stack: &mut Stack,
dir: AbsolutePathBuf, dir: PathBuf,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
let (block, delta) = { let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);

View File

@@ -5,17 +5,17 @@ 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.97.1" version = "0.92.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.97.1" } nu-engine = { path = "../nu-engine", version = "0.92.2" }
nu-parser = { path = "../nu-parser", version = "0.97.1" } nu-parser = { path = "../nu-parser", version = "0.92.2" }
nu-path = { path = "../nu-path", version = "0.97.1" } nu-path = { path = "../nu-path", version = "0.92.2" }
nu-protocol = { path = "../nu-protocol", version = "0.97.1" } nu-protocol = { path = "../nu-protocol", version = "0.92.2" }
indexmap = { workspace = true } indexmap = { workspace = true }
miette = { workspace = true } miette = { workspace = true }
[dev-dependencies] [dev-dependencies]

View File

@@ -1,5 +0,0 @@
Utilities used by the different `nu-command`/`nu-cmd-*` crates, should not contain any full `Command` implementations.
## Internal Nushell crate
This crate implements components of Nushell and is not designed to support plugin authors or other users directly.

View File

@@ -5,8 +5,8 @@ use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
cli_error::{report_error, report_error_new}, cli_error::{report_error, report_error_new},
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{Closure, EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId, BlockId, PipelineData, PositionalArg, ShellError, Span, Type, Value, VarId,
}; };
use std::sync::Arc; use std::sync::Arc;
@@ -153,11 +153,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.coerce_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,
@@ -194,7 +194,7 @@ pub fn eval_hook(
let Some(follow) = val.get("code") else { let Some(follow) = val.get("code") else {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
col_name: "code".into(), col_name: "code".into(),
span: Some(span), span,
src_span: span, src_span: span,
}); });
}; };
@@ -259,8 +259,25 @@ 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 {
@@ -272,8 +289,11 @@ pub fn eval_hook(
} }
} }
} }
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 {
@@ -290,20 +310,20 @@ 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
.captures_to_stack_preserve_out_dest(closure.captures.clone()) .gather_captures(engine_state, &block.captures)
.reset_pipes(); .reset_pipes();
for (idx, PositionalArg { var_id, .. }) in for (idx, PositionalArg { var_id, .. }) in

View File

@@ -1,5 +1,5 @@
use nu_protocol::{ast::CellPath, PipelineData, ShellError, Signals, Span, Value}; use nu_protocol::{ast::CellPath, PipelineData, ShellError, Span, Value};
use std::sync::Arc; use std::sync::{atomic::AtomicBool, 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>>;
@@ -40,7 +40,7 @@ pub fn operate<C, A>(
mut arg: A, mut arg: A,
input: PipelineData, input: PipelineData,
span: Span, span: Span,
signals: &Signals, ctrlc: Option<Arc<AtomicBool>>,
) -> Result<PipelineData, ShellError> ) -> Result<PipelineData, ShellError>
where where
A: CmdArgument + Send + Sync + 'static, A: CmdArgument + Send + Sync + 'static,
@@ -55,7 +55,7 @@ where
_ => cmd(&v, &arg, span), _ => cmd(&v, &arg, span),
} }
}, },
signals, ctrlc,
), ),
Some(column_paths) => { Some(column_paths) => {
let arg = Arc::new(arg); let arg = Arc::new(arg);
@@ -79,7 +79,7 @@ where
} }
v v
}, },
signals, ctrlc,
) )
} }
} }

View File

@@ -1,4 +1,3 @@
#![doc = include_str!("../README.md")]
pub mod formats; pub mod formats;
pub mod hook; pub mod hook;
pub mod input_handler; pub mod input_handler;

View File

@@ -1,49 +1,58 @@
use nu_path::AbsolutePathBuf;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, ast::RangeInclusion,
Range, ShellError, Span, Value, engine::{EngineState, Stack, StateWorkingSet},
report_error, Range, ShellError, Span, Value,
}; };
use std::ops::Bound; use std::path::PathBuf;
pub fn get_init_cwd() -> AbsolutePathBuf { pub fn get_init_cwd() -> PathBuf {
std::env::current_dir() std::env::current_dir().unwrap_or_else(|_| {
.ok() std::env::var("PWD")
.and_then(|path| AbsolutePathBuf::try_from(path).ok()) .map(Into::into)
.or_else(|| { .unwrap_or_else(|_| nu_path::home_dir().unwrap_or_default())
std::env::var("PWD") })
.ok()
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
})
.or_else(nu_path::home_dir)
.expect("Failed to get current working directory")
} }
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> AbsolutePathBuf { 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);
.ok() report_error(&working_set, &e);
.unwrap_or_else(get_init_cwd) crate::util::get_init_cwd()
})
} }
type MakeRangeError = fn(&str, Span) -> ShellError; type MakeRangeError = fn(&str, Span) -> ShellError;
/// Returns a inclusive pair of boundary in given `range`.
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 as isize, return Err(|msg, span| ShellError::TypeMismatch {
Bound::Excluded(v) => (v - 1) 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. \

View File

@@ -0,0 +1,75 @@
[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.92.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.92.2" }
nu-parser = { path = "../nu-parser", version = "0.92.2" }
nu-protocol = { path = "../nu-protocol", version = "0.92.2" }
# Potential dependencies for extras
chrono = { workspace = true, features = ["std", "unstable-locales"], default-features = false }
chrono-tz = { workspace = true }
fancy-regex = { workspace = true }
indexmap = { workspace = true }
num = { version = "0.4", optional = true }
serde = { workspace = true, features = ["derive"] }
# keep sqlparser at 0.39.0 until we can update polars
sqlparser = { version = "0.39.0", optional = true }
polars-io = { version = "0.37", features = ["avro"], optional = true }
polars-arrow = { version = "0.37", optional = true }
polars-ops = { version = "0.37", optional = true }
polars-plan = { version = "0.37", features = ["regex"], optional = true }
polars-utils = { version = "0.37", optional = true }
[dependencies.polars]
features = [
"arg_where",
"checked_arithmetic",
"concat_str",
"cross_join",
"csv",
"cum_agg",
"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",
"temporal",
"to_dummies",
]
default-features = false
optional = true
version = "0.37"
[features]
dataframe = ["num", "polars", "polars-io", "polars-arrow", "polars-ops", "polars-plan", "polars-utils", "sqlparser"]
default = []
[dev-dependencies]
nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.92.2" }

View File

@@ -1,24 +1,22 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{Axis, Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use crate::{
values::{Axis, Column, CustomValueSupport, NuDataFrame},
PolarsPlugin,
};
#[derive(Clone)] #[derive(Clone)]
pub struct AppendDF; pub struct AppendDF;
impl PluginCommand for AppendDF { impl Command for AppendDF {
type Plugin = PolarsPlugin; fn name(&self) -> &str {
"dfr append"
}
fn usage(&self) -> &str {
"Appends a new dataframe."
}
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build(self.name()) Signature::build(self.name())
.required("other", SyntaxShape::Any, "other dataframe to append") .required("other", SyntaxShape::Any, "dataframe to be appended")
.switch("col", "append as new columns instead of rows", Some('c')) .switch("col", "appends in col orientation", Some('c'))
.input_output_type( .input_output_type(
Type::Custom("dataframe".into()), Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()), Type::Custom("dataframe".into()),
@@ -26,30 +24,12 @@ impl PluginCommand for AppendDF {
.category(Category::Custom("dataframe".into())) .category(Category::Custom("dataframe".into()))
} }
fn run(
&self,
plugin: &Self::Plugin,
engine: &EngineInterface,
call: &EvaluatedCall,
input: PipelineData,
) -> Result<PipelineData, LabeledError> {
command(plugin, engine, call, input).map_err(LabeledError::from)
}
fn name(&self) -> &str {
"polars append"
}
fn usage(&self) -> &str {
"Appends a new dataframe."
}
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![ vec![
Example { Example {
description: "Appends a dataframe as new columns", description: "Appends a dataframe as new columns",
example: r#"let a = ([[a b]; [1 2] [3 4]] | polars into-df); example: r#"let a = ([[a b]; [1 2] [3 4]] | dfr into-df);
$a | polars append $a"#, $a | dfr append $a"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -78,7 +58,8 @@ impl PluginCommand for AppendDF {
}, },
Example { Example {
description: "Appends a dataframe merging at the end of columns", description: "Appends a dataframe merging at the end of columns",
example: r#"let a = ([[a b]; [1 2] [3 4]] | polars into-df); $a | polars append $a --col"#, example: r#"let a = ([[a b]; [1 2] [3 4]] | dfr into-df);
$a | dfr append $a --col"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -109,36 +90,45 @@ impl PluginCommand for AppendDF {
}, },
] ]
} }
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
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 other: Value = call.req(0)?; let other: Value = call.req(engine_state, stack, 0)?;
let axis = if call.has_flag("col")? { let axis = if call.has_flag(engine_state, stack, "col")? {
Axis::Column Axis::Column
} else { } else {
Axis::Row Axis::Row
}; };
let df_other = NuDataFrame::try_from_value(other)?;
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let df_other = NuDataFrame::try_from_value_coerce(plugin, &other, call.head)?; df.append_df(&df_other, axis, call.head)
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; .map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
let df = df.append_df(&df_other, axis, call.head)?;
df.to_pipeline_data(plugin, engine, call.head)
} }
#[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(&AppendDF) test_dataframe(vec![Box::new(AppendDF {})])
} }
} }

View File

@@ -1,25 +1,14 @@
use crate::{ use crate::dataframe::values::{str_to_dtype, NuDataFrame, NuExpression, NuLazyFrame};
dataframe::values::{str_to_dtype, NuExpression, NuLazyFrame}, use nu_engine::command_prelude::*;
values::{cant_convert_err, CustomValueSupport, PolarsPluginObject, PolarsPluginType},
PolarsPlugin,
};
use super::super::values::NuDataFrame;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
record, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use polars::prelude::*; use polars::prelude::*;
#[derive(Clone)] #[derive(Clone)]
pub struct CastDF; pub struct CastDF;
impl PluginCommand for CastDF { impl Command for CastDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars cast" "dfr cast"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -55,7 +44,7 @@ impl PluginCommand for CastDF {
vec![ vec![
Example { Example {
description: "Cast a column in a dataframe to a different dtype", description: "Cast a column in a dataframe to a different dtype",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars cast u8 a | polars schema", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr cast u8 a | dfr schema",
result: Some(Value::record( result: Some(Value::record(
record! { record! {
"a" => Value::string("u8", Span::test_data()), "a" => Value::string("u8", Span::test_data()),
@@ -66,8 +55,7 @@ impl PluginCommand for CastDF {
}, },
Example { Example {
description: "Cast a column in a lazy dataframe to a different dtype", description: "Cast a column in a lazy dataframe to a different dtype",
example: example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr into-lazy | dfr cast u8 a | dfr schema",
"[[a b]; [1 2] [3 4]] | polars into-df | polars into-lazy | polars cast u8 a | polars schema",
result: Some(Value::record( result: Some(Value::record(
record! { record! {
"a" => Value::string("u8", Span::test_data()), "a" => Value::string("u8", Span::test_data()),
@@ -78,85 +66,90 @@ impl PluginCommand for CastDF {
}, },
Example { Example {
description: "Cast a column in a expression to a different dtype", description: "Cast a column in a expression to a different dtype",
example: r#"[[a b]; [1 2] [1 4]] | polars into-df | polars group-by a | polars agg [ (polars col b | polars cast u8 | polars min | polars as "b_min") ] | polars schema"#, example: r#"[[a b]; [1 2] [1 4]] | dfr into-df | dfr group-by a | dfr agg [ (dfr col b | dfr cast u8 | dfr min | dfr as "b_min") ] | dfr schema"#,
result: None, result: None
}, }
] ]
} }
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> {
let value = input.into_value(call.head)?; let value = input.into_value(call.head);
match PolarsPluginObject::try_from_value(plugin, &value)? { if NuLazyFrame::can_downcast(&value) {
PolarsPluginObject::NuLazyFrame(lazy) => { let (dtype, column_nm) = df_args(engine_state, stack, call)?;
let (dtype, column_nm) = df_args(call)?; let df = NuLazyFrame::try_from_value(value)?;
command_lazy(plugin, engine, call, column_nm, dtype, lazy) command_lazy(call, column_nm, dtype, df)
} } else if NuDataFrame::can_downcast(&value) {
PolarsPluginObject::NuDataFrame(df) => { let (dtype, column_nm) = df_args(engine_state, stack, call)?;
let (dtype, column_nm) = df_args(call)?; let df = NuDataFrame::try_from_value(value)?;
command_eager(plugin, engine, call, column_nm, dtype, df) command_eager(call, column_nm, dtype, df)
} } else {
PolarsPluginObject::NuExpression(expr) => { let dtype: String = call.req(engine_state, stack, 0)?;
let dtype: String = call.req(0)?; let dtype = str_to_dtype(&dtype, call.head)?;
let dtype = str_to_dtype(&dtype, call.head)?;
let expr: NuExpression = expr.into_polars().cast(dtype).into(); let expr = NuExpression::try_from_value(value)?;
expr.to_pipeline_data(plugin, engine, call.head) let expr: NuExpression = expr.into_polars().cast(dtype).into();
}
_ => Err(cant_convert_err( Ok(PipelineData::Value(
&value, NuExpression::into_value(expr, call.head),
&[ None,
PolarsPluginType::NuDataFrame, ))
PolarsPluginType::NuLazyFrame,
PolarsPluginType::NuExpression,
],
)),
} }
.map_err(LabeledError::from)
} }
} }
fn df_args(call: &EvaluatedCall) -> Result<(DataType, String), ShellError> { fn df_args(
let dtype = dtype_arg(call)?; engine_state: &EngineState,
let column_nm: String = call.opt(1)?.ok_or(ShellError::MissingParameter { stack: &mut Stack,
param_name: "column_name".into(), call: &Call,
span: call.head, ) -> Result<(DataType, String), ShellError> {
})?; let dtype = dtype_arg(engine_state, stack, call)?;
let column_nm: String =
call.opt(engine_state, stack, 1)?
.ok_or(ShellError::MissingParameter {
param_name: "column_name".into(),
span: call.head,
})?;
Ok((dtype, column_nm)) Ok((dtype, column_nm))
} }
fn dtype_arg(call: &EvaluatedCall) -> Result<DataType, ShellError> { fn dtype_arg(
let dtype: String = call.req(0)?; engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<DataType, ShellError> {
let dtype: String = call.req(engine_state, stack, 0)?;
str_to_dtype(&dtype, call.head) str_to_dtype(&dtype, call.head)
} }
fn command_lazy( fn command_lazy(
plugin: &PolarsPlugin, call: &Call,
engine: &EngineInterface,
call: &EvaluatedCall,
column_nm: String, column_nm: String,
dtype: DataType, dtype: DataType,
lazy: NuLazyFrame, lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let column = col(&column_nm).cast(dtype); let column = col(&column_nm).cast(dtype);
let lazy = lazy.to_polars().with_columns(&[column]); let lazy = lazy.into_polars().with_columns(&[column]);
let lazy = NuLazyFrame::new(false, lazy); let lazy = NuLazyFrame::new(false, lazy);
lazy.to_pipeline_data(plugin, engine, call.head)
Ok(PipelineData::Value(
NuLazyFrame::into_value(lazy, call.head)?,
None,
))
} }
fn command_eager( fn command_eager(
plugin: &PolarsPlugin, call: &Call,
engine: &EngineInterface,
call: &EvaluatedCall,
column_nm: String, column_nm: String,
dtype: DataType, dtype: DataType,
nu_df: NuDataFrame, nu_df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let mut df = (*nu_df.df).clone(); let mut df = nu_df.df;
let column = df let column = df
.column(&column_nm) .column(&column_nm)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
@@ -186,17 +179,17 @@ fn command_eager(
})?; })?;
let df = NuDataFrame::new(false, df); let df = NuDataFrame::new(false, df);
df.to_pipeline_data(plugin, engine, call.head) Ok(PipelineData::Value(df.into_value(call.head), None))
} }
#[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(&CastDF) test_dataframe(vec![Box::new(CastDF {})])
} }
} }

View File

@@ -1,19 +1,12 @@
use crate::PolarsPlugin; use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use super::super::values::NuDataFrame;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, 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 +22,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 +32,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 +63,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

@@ -1,23 +1,12 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{utils::convert_columns, Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use crate::values::CustomValueSupport;
use crate::PolarsPlugin;
use super::super::values::utils::convert_columns;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct DropDF; pub struct DropDF;
impl PluginCommand for DropDF { impl Command for DropDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars drop" "dfr drop"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -37,7 +26,7 @@ impl PluginCommand for DropDF {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "drop column a", description: "drop column a",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars drop a", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr drop a",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -54,25 +43,25 @@ impl PluginCommand for DropDF {
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 columns: Vec<Value> = call.rest(0)?; let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
let (col_string, col_span) = convert_columns(columns, call.head)?; let (col_string, col_span) = convert_columns(columns, call.head)?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let new_df = col_string let new_df = col_string
.first() .first()
@@ -97,30 +86,30 @@ fn command(
// If there are more columns in the drop selection list, these // If there are more columns in the drop selection list, these
// are added from the resulting dataframe // are added from the resulting dataframe
let polars_df = col_string.iter().skip(1).try_fold(new_df, |new_df, col| { col_string
new_df .iter()
.drop(&col.item) .skip(1)
.map_err(|e| ShellError::GenericError { .try_fold(new_df, |new_df, col| {
error: "Error dropping column".into(), new_df
msg: e.to_string(), .drop(&col.item)
span: Some(col.span), .map_err(|e| ShellError::GenericError {
help: None, error: "Error dropping column".into(),
inner: vec![], msg: e.to_string(),
}) span: Some(col.span),
})?; help: None,
inner: vec![],
let final_df = NuDataFrame::new(df.from_lazy, polars_df); })
})
final_df.to_pipeline_data(plugin, engine, call.head) .map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
} }
#[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(&DropDF) test_dataframe(vec![Box::new(DropDF {})])
} }
} }

View File

@@ -1,24 +1,14 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{utils::convert_columns_string, Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use polars::prelude::UniqueKeepStrategy; use polars::prelude::UniqueKeepStrategy;
use crate::values::CustomValueSupport;
use crate::PolarsPlugin;
use super::super::values::utils::convert_columns_string;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct DropDuplicates; pub struct DropDuplicates;
impl PluginCommand for DropDuplicates { impl Command for DropDuplicates {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars drop-duplicates" "dfr drop-duplicates"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -48,7 +38,7 @@ impl PluginCommand for DropDuplicates {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "drop duplicates", description: "drop duplicates",
example: "[[a b]; [1 2] [3 4] [1 2]] | polars into-df | polars drop-duplicates", example: "[[a b]; [1 2] [3 4] [1 2]] | dfr into-df | dfr drop-duplicates",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -71,22 +61,22 @@ impl PluginCommand for DropDuplicates {
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 columns: Option<Vec<Value>> = call.opt(0)?; let columns: Option<Vec<Value>> = call.opt(engine_state, stack, 0)?;
let (subset, col_span) = match columns { let (subset, col_span) = match columns {
Some(cols) => { Some(cols) => {
let (agg_string, col_span) = convert_columns_string(cols, call.head)?; let (agg_string, col_span) = convert_columns_string(cols, call.head)?;
@@ -95,18 +85,17 @@ fn command(
None => (None, call.head), None => (None, call.head),
}; };
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let subset_slice = subset.as_ref().map(|cols| &cols[..]); let subset_slice = subset.as_ref().map(|cols| &cols[..]);
let keep_strategy = if call.has_flag("last")? { let keep_strategy = if call.has_flag(engine_state, stack, "last")? {
UniqueKeepStrategy::Last UniqueKeepStrategy::Last
} else { } else {
UniqueKeepStrategy::First UniqueKeepStrategy::First
}; };
let polars_df = df df.as_ref()
.as_ref()
.unique(subset_slice, keep_strategy, None) .unique(subset_slice, keep_strategy, None)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
error: "Error dropping duplicates".into(), error: "Error dropping duplicates".into(),
@@ -114,20 +103,17 @@ fn command(
span: Some(col_span), span: Some(col_span),
help: None, help: None,
inner: vec![], inner: vec![],
})?; })
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
let df = NuDataFrame::new(df.from_lazy, polars_df);
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(&DropDuplicates) test_dataframe(vec![Box::new(DropDuplicates {})])
} }
} }

View File

@@ -1,23 +1,12 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{utils::convert_columns_string, Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use crate::values::CustomValueSupport;
use crate::PolarsPlugin;
use super::super::values::utils::convert_columns_string;
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct DropNulls; pub struct DropNulls;
impl PluginCommand for DropNulls { impl Command for DropNulls {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars drop-nulls" "dfr drop-nulls"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -42,10 +31,10 @@ impl PluginCommand for DropNulls {
vec![ vec![
Example { Example {
description: "drop null values in dataframe", description: "drop null values in dataframe",
example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | polars into-df); example: r#"let df = ([[a b]; [1 2] [3 0] [1 2]] | dfr into-df);
let res = ($df.b / $df.b); let res = ($df.b / $df.b);
let a = ($df | polars with-column $res --name res); let a = ($df | dfr with-column $res --name res);
$a | polars drop-nulls"#, $a | dfr drop-nulls"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -70,8 +59,8 @@ impl PluginCommand for DropNulls {
}, },
Example { Example {
description: "drop null values in dataframe", description: "drop null values in dataframe",
example: r#"let s = ([1 2 0 0 3 4] | polars into-df); example: r#"let s = ([1 2 0 0 3 4] | dfr into-df);
($s / $s) | polars drop-nulls"#, ($s / $s) | dfr drop-nulls"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -94,24 +83,24 @@ impl PluginCommand for DropNulls {
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 df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let columns: Option<Vec<Value>> = call.opt(0)?; let columns: Option<Vec<Value>> = call.opt(engine_state, stack, 0)?;
let (subset, col_span) = match columns { let (subset, col_span) = match columns {
Some(cols) => { Some(cols) => {
@@ -123,8 +112,7 @@ fn command(
let subset_slice = subset.as_ref().map(|cols| &cols[..]); let subset_slice = subset.as_ref().map(|cols| &cols[..]);
let polars_df = df df.as_ref()
.as_ref()
.drop_nulls(subset_slice) .drop_nulls(subset_slice)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
error: "Error dropping nulls".into(), error: "Error dropping nulls".into(),
@@ -132,18 +120,18 @@ fn command(
span: Some(col_span), span: Some(col_span),
help: None, help: None,
inner: vec![], inner: vec![],
})?; })
let df = NuDataFrame::new(df.from_lazy, polars_df); .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 super::super::super::test_dataframe::test_dataframe;
use super::super::WithColumn;
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(&DropNulls) test_dataframe(vec![Box::new(DropNulls {}), Box::new(WithColumn {})])
} }
} }

View File

@@ -0,0 +1,104 @@
use crate::dataframe::values::{Column, NuDataFrame};
use nu_engine::command_prelude::*;
#[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")],
),
],
None,
)
.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], None)
.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,14 @@
use super::super::values::NuDataFrame; use crate::dataframe::values::NuDataFrame;
use crate::{values::CustomValueSupport, PolarsPlugin}; use nu_engine::command_prelude::*;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, 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 +29,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 +46,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 +65,43 @@ 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(engine_state, stack, "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| ShellError::GenericError {
.map_err(|e| ShellError::GenericError { error: "Error calculating dummies".into(),
error: "Error calculating dummies".into(), msg: e.to_string(),
msg: e.to_string(), span: Some(call.head),
span: Some(call.head), help: Some("The only allowed column types for dummies are String or Int".into()),
help: Some("The only allowed column types for dummies are String or Int".into()), inner: vec![],
inner: vec![], })
})?; .map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
let df: NuDataFrame = polars_df.into();
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

@@ -1,26 +1,14 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use polars::prelude::LazyFrame; use polars::prelude::LazyFrame;
use crate::{
dataframe::values::{NuExpression, NuLazyFrame},
values::{cant_convert_err, CustomValueSupport, PolarsPluginObject, PolarsPluginType},
PolarsPlugin,
};
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct FilterWith; pub struct FilterWith;
impl PluginCommand for FilterWith { impl Command for FilterWith {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars filter-with" "dfr filter-with"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -45,8 +33,8 @@ impl PluginCommand for FilterWith {
vec![ vec![
Example { Example {
description: "Filter dataframe using a bool mask", description: "Filter dataframe using a bool mask",
example: r#"let mask = ([true false] | polars into-df); example: r#"let mask = ([true false] | dfr into-df);
[[a b]; [1 2] [3 4]] | polars into-df | polars filter-with $mask"#, [[a b]; [1 2] [3 4]] | dfr into-df | dfr filter-with $mask"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -61,7 +49,7 @@ impl PluginCommand for FilterWith {
}, },
Example { Example {
description: "Filter dataframe using an expression", description: "Filter dataframe using an expression",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars filter-with ((polars col a) > 1)", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr filter-with ((dfr col a) > 1)",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -79,42 +67,43 @@ impl PluginCommand for FilterWith {
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> {
let value = input.into_value(call.head)?; let value = input.into_value(call.head);
match PolarsPluginObject::try_from_value(plugin, &value)? {
PolarsPluginObject::NuDataFrame(df) => command_eager(plugin, engine, call, df), if NuLazyFrame::can_downcast(&value) {
PolarsPluginObject::NuLazyFrame(lazy) => command_lazy(plugin, engine, call, lazy), let df = NuLazyFrame::try_from_value(value)?;
_ => Err(cant_convert_err( command_lazy(engine_state, stack, call, df)
&value, } else {
&[PolarsPluginType::NuDataFrame, PolarsPluginType::NuLazyFrame], let df = NuDataFrame::try_from_value(value)?;
)), command_eager(engine_state, stack, call, df)
} }
.map_err(LabeledError::from)
} }
} }
fn command_eager( fn command_eager(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
df: NuDataFrame, df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let mask_value: Value = call.req(0)?; let mask_value: Value = call.req(engine_state, stack, 0)?;
let mask_span = mask_value.span(); let mask_span = mask_value.span();
if NuExpression::can_downcast(&mask_value) { if NuExpression::can_downcast(&mask_value) {
let expression = NuExpression::try_from_value(plugin, &mask_value)?; let expression = NuExpression::try_from_value(mask_value)?;
let lazy = df.lazy(); let lazy = NuLazyFrame::new(true, df.lazy());
let lazy = lazy.apply_with_expr(expression, LazyFrame::filter); let lazy = lazy.apply_with_expr(expression, LazyFrame::filter);
lazy.to_pipeline_data(plugin, engine, call.head) Ok(PipelineData::Value(
NuLazyFrame::into_value(lazy, call.head)?,
None,
))
} else { } else {
let mask = NuDataFrame::try_from_value_coerce(plugin, &mask_value, mask_span)? let mask = NuDataFrame::try_from_value(mask_value)?.as_series(mask_span)?;
.as_series(mask_span)?;
let mask = mask.bool().map_err(|e| ShellError::GenericError { let mask = mask.bool().map_err(|e| ShellError::GenericError {
error: "Error casting to bool".into(), error: "Error casting to bool".into(),
msg: e.to_string(), msg: e.to_string(),
@@ -123,8 +112,7 @@ fn command_eager(
inner: vec![], inner: vec![],
})?; })?;
let polars_df = df df.as_ref()
.as_ref()
.filter(mask) .filter(mask)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
error: "Error filtering dataframe".into(), error: "Error filtering dataframe".into(),
@@ -132,31 +120,36 @@ fn command_eager(
span: Some(call.head), span: Some(call.head),
help: Some("The only allowed column types for dummies are String or Int".into()), help: Some("The only allowed column types for dummies are String or Int".into()),
inner: vec![], inner: vec![],
})?; })
let df = NuDataFrame::new(df.from_lazy, polars_df); .map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
df.to_pipeline_data(plugin, engine, call.head)
} }
} }
fn command_lazy( fn command_lazy(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
lazy: NuLazyFrame, lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let expr: Value = call.req(0)?; let expr: Value = call.req(engine_state, stack, 0)?;
let expr = NuExpression::try_from_value(plugin, &expr)?; let expr = NuExpression::try_from_value(expr)?;
let lazy = lazy.apply_with_expr(expr, LazyFrame::filter); let lazy = lazy.apply_with_expr(expr, LazyFrame::filter);
lazy.to_pipeline_data(plugin, engine, call.head)
Ok(PipelineData::Value(
NuLazyFrame::into_value(lazy, call.head)?,
None,
))
} }
#[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; use crate::dataframe::expressions::ExprCol;
#[test] #[test]
fn test_examples() -> Result<(), ShellError> { fn test_examples() {
test_polars_plugin_command(&FilterWith) test_dataframe(vec![Box::new(FilterWith {}), Box::new(ExprCol {})])
} }
} }

View File

@@ -1,23 +1,12 @@
use crate::{ use crate::dataframe::values::{Column, NuDataFrame, NuExpression};
values::{Column, CustomValueSupport, NuLazyFrame, PolarsPluginObject}, use nu_engine::command_prelude::*;
PolarsPlugin,
};
use super::super::values::{NuDataFrame, NuExpression};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct FirstDF; pub struct FirstDF;
impl PluginCommand for FirstDF { impl Command for FirstDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars first" "dfr first"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -48,7 +37,7 @@ impl PluginCommand for FirstDF {
vec![ vec![
Example { Example {
description: "Return the first row of a dataframe", description: "Return the first row of a dataframe",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars first", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr first",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -63,7 +52,7 @@ impl PluginCommand for FirstDF {
}, },
Example { Example {
description: "Return the first two rows of a dataframe", description: "Return the first two rows of a dataframe",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars first 2", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr first 2",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -84,7 +73,7 @@ impl PluginCommand for FirstDF {
}, },
Example { Example {
description: "Creates a first expression from a column", description: "Creates a first expression from a column",
example: "polars col a | polars first", example: "dfr col a | dfr first",
result: None, result: None,
}, },
] ]
@@ -92,65 +81,64 @@ impl PluginCommand for FirstDF {
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> {
let value = input.into_value(call.head)?; let value = input.into_value(call.head);
match PolarsPluginObject::try_from_value(plugin, &value)? { if NuDataFrame::can_downcast(&value) {
PolarsPluginObject::NuDataFrame(df) => { let df = NuDataFrame::try_from_value(value)?;
command_eager(plugin, engine, call, df).map_err(|e| e.into()) command(engine_state, stack, call, df)
} } else {
PolarsPluginObject::NuLazyFrame(lazy) => { let expr = NuExpression::try_from_value(value)?;
command_lazy(plugin, engine, call, lazy).map_err(|e| e.into()) let expr: NuExpression = expr.into_polars().first().into();
}
_ => {
let expr = NuExpression::try_from_value(plugin, &value)?;
let expr: NuExpression = expr.into_polars().first().into();
expr.to_pipeline_data(plugin, engine, call.head) Ok(PipelineData::Value(
.map_err(LabeledError::from) NuExpression::into_value(expr, call.head),
} None,
))
} }
} }
} }
fn command_eager( fn command(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
df: NuDataFrame, df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let rows: Option<usize> = call.opt(0)?; let rows: Option<usize> = call.opt(engine_state, stack, 0)?;
let rows = rows.unwrap_or(1); let rows = rows.unwrap_or(1);
let res = df.as_ref().head(Some(rows)); let res = df.as_ref().head(Some(rows));
let res = NuDataFrame::new(false, res); Ok(PipelineData::Value(
NuDataFrame::dataframe_into_value(res, call.head),
res.to_pipeline_data(plugin, engine, call.head) None,
} ))
fn command_lazy(
plugin: &PolarsPlugin,
engine: &EngineInterface,
call: &EvaluatedCall,
lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> {
let rows: Option<u32> = call.opt(0)?;
let rows = rows.unwrap_or(1);
let res: NuLazyFrame = lazy.to_polars().limit(rows).into();
res.to_pipeline_data(plugin, engine, call.head)
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::super::super::test_dataframe::{build_test_engine_state, test_dataframe_example};
use super::*; use super::*;
use crate::test::test_polars_plugin_command; use crate::dataframe::lazy::aggregate::LazyAggregate;
use crate::dataframe::lazy::groupby::ToLazyGroupBy;
#[test] #[test]
fn test_examples() -> Result<(), ShellError> { fn test_examples_dataframe() {
test_polars_plugin_command(&FirstDF) 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

@@ -1,23 +1,12 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{utils::convert_columns_string, Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use crate::{
dataframe::values::utils::convert_columns_string, values::CustomValueSupport, PolarsPlugin,
};
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct GetDF; pub struct GetDF;
impl PluginCommand for GetDF { impl Command for GetDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars get" "dfr get"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -37,7 +26,7 @@ impl PluginCommand for GetDF {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Returns the selected column", description: "Returns the selected column",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars get a", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr get a",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -54,28 +43,27 @@ impl PluginCommand for GetDF {
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 columns: Vec<Value> = call.rest(0)?; let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
let (col_string, col_span) = convert_columns_string(columns, call.head)?; let (col_string, col_span) = convert_columns_string(columns, call.head)?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let df = df df.as_ref()
.as_ref()
.select(col_string) .select(col_string)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
error: "Error selecting columns".into(), error: "Error selecting columns".into(),
@@ -83,19 +71,17 @@ fn command(
span: Some(col_span), span: Some(col_span),
help: None, help: None,
inner: vec![], inner: vec![],
})?; })
let df = NuDataFrame::new(false, df); .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(&GetDF) test_dataframe(vec![Box::new(GetDF {})])
} }
} }

View File

@@ -0,0 +1,118 @@
use crate::dataframe::values::{utils::DEFAULT_ROWS, Column, NuDataFrame, NuExpression};
use nu_engine::command_prelude::*;
#[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)]),
],
None,
)
.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,68 @@
use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
#[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

@@ -0,0 +1,248 @@
use crate::dataframe::values::{utils::convert_columns_string, Column, NuDataFrame};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct MeltDF;
impl Command for MeltDF {
fn name(&self) -> &str {
"dfr melt"
}
fn usage(&self) -> &str {
"Unpivot a DataFrame from wide to long format."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required_named(
"columns",
SyntaxShape::Table(vec![]),
"column names for melting",
Some('c'),
)
.required_named(
"values",
SyntaxShape::Table(vec![]),
"column names used as value columns",
Some('v'),
)
.named(
"variable-name",
SyntaxShape::String,
"optional name for variable column",
Some('r'),
)
.named(
"value-name",
SyntaxShape::String,
"optional name for value column",
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: "melt dataframe",
example:
"[[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(
NuDataFrame::try_from_columns(vec![
Column::new(
"b".to_string(),
vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
],
),
Column::new(
"c".to_string(),
vec![
Value::test_int(4),
Value::test_int(5),
Value::test_int(6),
Value::test_int(4),
Value::test_int(5),
Value::test_int(6),
],
),
Column::new(
"variable".to_string(),
vec![
Value::test_string("a"),
Value::test_string("a"),
Value::test_string("a"),
Value::test_string("d"),
Value::test_string("d"),
Value::test_string("d"),
],
),
Column::new(
"value".to_string(),
vec![
Value::test_string("x"),
Value::test_string("y"),
Value::test_string("z"),
Value::test_string("a"),
Value::test_string("b"),
Value::test_string("c"),
],
),
], None)
.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 id_col: Vec<Value> = call
.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(engine_state, stack, "value-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 (val_col_string, val_col_span) = convert_columns_string(val_col, 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(), &val_col_string, val_col_span)?;
let mut res = df
.as_ref()
.melt(&id_col_string, &val_col_string)
.map_err(|e| ShellError::GenericError {
error: "Error calculating melt".into(),
msg: e.to_string(),
span: Some(call.head),
help: None,
inner: vec![],
})?;
if let Some(name) = &variable_name {
res.rename("variable", &name.item)
.map_err(|e| ShellError::GenericError {
error: "Error renaming column".into(),
msg: e.to_string(),
span: Some(name.span),
help: None,
inner: vec![],
})?;
}
if let Some(name) = &value_name {
res.rename("value", &name.item)
.map_err(|e| ShellError::GenericError {
error: "Error renaming column".into(),
msg: e.to_string(),
span: Some(name.span),
help: None,
inner: vec![],
})?;
}
Ok(PipelineData::Value(
NuDataFrame::dataframe_into_value(res, call.head),
None,
))
}
fn check_column_datatypes<T: AsRef<str>>(
df: &polars::prelude::DataFrame,
cols: &[T],
col_span: Span,
) -> Result<(), ShellError> {
if cols.is_empty() {
return Err(ShellError::GenericError {
error: "Merge error".into(),
msg: "empty column list".into(),
span: Some(col_span),
help: None,
inner: vec![],
});
}
// Checking if they are same type
if cols.len() > 1 {
for w in cols.windows(2) {
let l_series = df
.column(w[0].as_ref())
.map_err(|e| ShellError::GenericError {
error: "Error selecting columns".into(),
msg: e.to_string(),
span: Some(col_span),
help: None,
inner: vec![],
})?;
let r_series = df
.column(w[1].as_ref())
.map_err(|e| ShellError::GenericError {
error: "Error selecting columns".into(),
msg: e.to_string(),
span: Some(col_span),
help: None,
inner: vec![],
})?;
if l_series.dtype() != r_series.dtype() {
return Err(ShellError::GenericError {
error: "Merge error".into(),
msg: "found different column types in list".into(),
span: Some(col_span),
help: Some(format!(
"datatypes {} and {} are incompatible",
l_series.dtype(),
r_series.dtype()
)),
inner: vec![],
});
}
}
}
Ok(())
}
#[cfg(test)]
mod test {
use super::super::super::test_dataframe::test_dataframe;
use super::*;
#[test]
fn test_examples() {
test_dataframe(vec![Box::new(MeltDF {})])
}
}

View File

@@ -0,0 +1,114 @@
mod append;
mod cast;
mod columns;
mod drop;
mod drop_duplicates;
mod drop_nulls;
mod dtypes;
mod dummies;
mod filter_with;
mod first;
mod get;
mod last;
mod list;
mod melt;
mod open;
mod query_df;
mod rename;
mod sample;
mod schema;
mod shape;
mod slice;
mod sql_context;
mod sql_expr;
mod summary;
mod take;
mod to_arrow;
mod to_avro;
mod to_csv;
mod to_df;
mod to_json_lines;
mod to_nu;
mod to_parquet;
mod with_column;
use nu_protocol::engine::StateWorkingSet;
pub use self::open::OpenDataFrame;
pub use append::AppendDF;
pub use cast::CastDF;
pub use columns::ColumnsDF;
pub use drop::DropDF;
pub use drop_duplicates::DropDuplicates;
pub use drop_nulls::DropNulls;
pub use dtypes::DataTypes;
pub use dummies::Dummies;
pub use filter_with::FilterWith;
pub use first::FirstDF;
pub use get::GetDF;
pub use last::LastDF;
pub use list::ListDF;
pub use melt::MeltDF;
pub use query_df::QueryDf;
pub use rename::RenameDF;
pub use sample::SampleDF;
pub use schema::SchemaDF;
pub use shape::ShapeDF;
pub use slice::SliceDF;
pub use sql_context::SQLContext;
pub use summary::Summary;
pub use take::TakeDF;
pub use to_arrow::ToArrow;
pub use to_avro::ToAvro;
pub use to_csv::ToCSV;
pub use to_df::ToDataFrame;
pub use to_json_lines::ToJsonLines;
pub use to_nu::ToNu;
pub use to_parquet::ToParquet;
pub use with_column::WithColumn;
pub fn add_eager_decls(working_set: &mut StateWorkingSet) {
macro_rules! bind_command {
( $command:expr ) => {
working_set.add_decl(Box::new($command));
};
( $( $command:expr ),* ) => {
$( working_set.add_decl(Box::new($command)); )*
};
}
// Dataframe commands
bind_command!(
AppendDF,
CastDF,
ColumnsDF,
DataTypes,
Summary,
DropDF,
DropDuplicates,
DropNulls,
Dummies,
FilterWith,
FirstDF,
GetDF,
LastDF,
ListDF,
MeltDF,
OpenDataFrame,
QueryDf,
RenameDF,
SampleDF,
SchemaDF,
ShapeDF,
SliceDF,
TakeDF,
ToArrow,
ToAvro,
ToCSV,
ToDataFrame,
ToNu,
ToParquet,
ToJsonLines,
WithColumn
);
}

View File

@@ -0,0 +1,515 @@
use crate::dataframe::values::{NuDataFrame, NuLazyFrame, NuSchema};
use nu_engine::command_prelude::*;
use polars::prelude::{
CsvEncoding, CsvReader, IpcReader, JsonFormat, JsonReader, LazyCsvReader, LazyFileListReader,
LazyFrame, ParallelStrategy, ParquetReader, ScanArgsIpc, ScanArgsParquet, SerReader,
};
use polars_io::avro::AvroReader;
use std::{fs::File, io::BufReader, path::PathBuf};
#[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,
)
.named(
"schema",
SyntaxShape::Record(vec![]),
r#"Polars Schema in format [{name: str}]. CSV, JSON, and JSONL files"#,
Some('s')
)
.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 {
msg: format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"),
span: blamed,
}),
},
None => Err(ShellError::FileNotFoundCustom {
msg: "File without extension".into(),
span: 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(engine_state, stack, "lazy")? {
let file: String = call.req(engine_state, stack, 0)?;
let args = ScanArgsParquet {
n_rows: None,
cache: true,
parallel: ParallelStrategy::Auto,
rechunk: false,
row_index: None,
low_memory: false,
cloud_options: None,
use_statistics: false,
hive_partitioning: false,
};
let df: NuLazyFrame = LazyFrame::scan_parquet(file, args)
.map_err(|e| ShellError::GenericError {
error: "Parquet reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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: "Error opening file".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
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 {
error: "Parquet reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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: "Error opening file".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
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 {
error: "Avro reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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(engine_state, stack, "lazy")? {
let file: String = call.req(engine_state, stack, 0)?;
let args = ScanArgsIpc {
n_rows: None,
cache: true,
rechunk: false,
row_index: None,
memmap: true,
};
let df: NuLazyFrame = LazyFrame::scan_ipc(file, args)
.map_err(|e| ShellError::GenericError {
error: "IPC reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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: "Error opening file".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
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 {
error: "IPC reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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: "Error opening file".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
let maybe_schema = call
.get_flag(engine_state, stack, "schema")?
.map(|schema| NuSchema::try_from(&schema))
.transpose()?;
let buf_reader = BufReader::new(file);
let reader = JsonReader::new(buf_reader);
let reader = match maybe_schema {
Some(schema) => reader.with_schema(schema.into()),
None => reader,
};
let df: NuDataFrame = reader
.finish()
.map_err(|e| ShellError::GenericError {
error: "Json reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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 maybe_schema = call
.get_flag(engine_state, stack, "schema")?
.map(|schema| NuSchema::try_from(&schema))
.transpose()?;
let file: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let file = File::open(&file.item).map_err(|e| ShellError::GenericError {
error: "Error opening file".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?;
let buf_reader = BufReader::new(file);
let reader = JsonReader::new(buf_reader)
.with_json_format(JsonFormat::JsonLines)
.infer_schema_len(infer_schema);
let reader = match maybe_schema {
Some(schema) => reader.with_schema(schema.into()),
None => reader,
};
let df: NuDataFrame = reader
.finish()
.map_err(|e| ShellError::GenericError {
error: "Json lines reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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(engine_state, stack, "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")?;
let maybe_schema = call
.get_flag(engine_state, stack, "schema")?
.map(|schema| NuSchema::try_from(&schema))
.transpose()?;
if call.has_flag(engine_state, stack, "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 {
error: "Incorrect delimiter".into(),
msg: "Delimiter has to be one character".into(),
span: Some(d.span),
help: None,
inner: vec![],
});
} else {
let delimiter = match d.item.chars().next() {
Some(d) => d as u8,
None => unreachable!(),
};
csv_reader.with_separator(delimiter)
}
}
};
let csv_reader = csv_reader.has_header(!no_header);
let csv_reader = match maybe_schema {
Some(schema) => csv_reader.with_schema(Some(schema.into())),
None => csv_reader,
};
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 {
error: "Parquet reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.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: "Error creating CSV reader".into(),
msg: e.to_string(),
span: Some(file.span),
help: None,
inner: vec![],
})?
.with_encoding(CsvEncoding::LossyUtf8);
let csv_reader = match delimiter {
None => csv_reader,
Some(d) => {
if d.item.len() != 1 {
return Err(ShellError::GenericError {
error: "Incorrect delimiter".into(),
msg: "Delimiter has to be one character".into(),
span: Some(d.span),
help: None,
inner: vec![],
});
} else {
let delimiter = match d.item.chars().next() {
Some(d) => d as u8,
None => unreachable!(),
};
csv_reader.with_separator(delimiter)
}
}
};
let csv_reader = csv_reader.has_header(!no_header);
let csv_reader = match maybe_schema {
Some(schema) => csv_reader.with_schema(Some(schema.into())),
None => csv_reader,
};
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 {
error: "Parquet reader error".into(),
msg: format!("{e:?}"),
span: Some(call.head),
help: None,
inner: vec![],
})?
.into();
Ok(df.into_value(call.head))
}
}

View File

@@ -1,13 +1,8 @@
use super::super::values::NuDataFrame; use crate::dataframe::{
use crate::dataframe::values::Column; eager::SQLContext,
use crate::dataframe::{eager::SQLContext, values::NuLazyFrame}; values::{Column, NuDataFrame, NuLazyFrame},
use crate::values::CustomValueSupport;
use crate::PolarsPlugin;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
}; };
use nu_engine::command_prelude::*;
// attribution: // attribution:
// sql_context.rs, and sql_expr.rs were copied from polars-sql. thank you. // sql_context.rs, and sql_expr.rs were copied from polars-sql. thank you.
@@ -17,11 +12,9 @@ use nu_protocol::{
#[derive(Clone)] #[derive(Clone)]
pub struct QueryDf; pub struct QueryDf;
impl PluginCommand for QueryDf { impl Command for QueryDf {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars query" "dfr query"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -45,7 +38,7 @@ impl PluginCommand for QueryDf {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Query dataframe using SQL", description: "Query dataframe using SQL",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars query 'select a from df'", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr query 'select a from df'",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -62,23 +55,23 @@ impl PluginCommand for QueryDf {
fn run( fn run(
&self, &self,
plugin: &PolarsPlugin, 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 sql_query: String = call.req(0)?; let sql_query: String = call.req(engine_state, stack, 0)?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let mut ctx = SQLContext::new(); let mut ctx = SQLContext::new();
ctx.register("df", &df.df); ctx.register("df", &df.df);
@@ -91,18 +84,21 @@ fn command(
help: None, help: None,
inner: vec![], inner: vec![],
})?; })?;
let lazy = NuLazyFrame::new(!df.from_lazy, df_sql); let lazy = NuLazyFrame::new(false, df_sql);
lazy.to_pipeline_data(plugin, engine, call.head)
let eager = lazy.collect(call.head)?;
let value = Value::custom(Box::new(eager), call.head);
Ok(PipelineData::Value(value, None))
} }
#[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(&QueryDf) test_dataframe(vec![Box::new(QueryDf {})])
} }
} }

View File

@@ -1,25 +1,15 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::{
use nu_protocol::{ utils::extract_strings,
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, values::{Column, NuDataFrame, NuLazyFrame},
Value,
}; };
use nu_engine::command_prelude::*;
use crate::{
dataframe::{utils::extract_strings, values::NuLazyFrame},
values::{CustomValueSupport, PolarsPluginObject},
PolarsPlugin,
};
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct RenameDF; pub struct RenameDF;
impl PluginCommand for RenameDF { impl Command for RenameDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars rename" "dfr rename"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -49,7 +39,7 @@ impl PluginCommand for RenameDF {
vec![ vec![
Example { Example {
description: "Renames a series", description: "Renames a series",
example: "[5 6 7 8] | polars into-df | polars rename '0' new_name", example: "[5 6 7 8] | dfr into-df | dfr rename '0' new_name",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -69,7 +59,7 @@ impl PluginCommand for RenameDF {
}, },
Example { Example {
description: "Renames a dataframe column", description: "Renames a dataframe column",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars rename a a_new", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr rename a a_new",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -90,8 +80,7 @@ impl PluginCommand for RenameDF {
}, },
Example { Example {
description: "Renames two dataframe columns", description: "Renames two dataframe columns",
example: example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr rename [a b] [a_new b_new]",
"[[a b]; [1 2] [3 4]] | polars into-df | polars rename [a b] [a_new b_new]",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -115,41 +104,37 @@ impl PluginCommand for RenameDF {
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> {
let value = input.into_value(call.head)?; let value = input.into_value(call.head);
match PolarsPluginObject::try_from_value(plugin, &value).map_err(LabeledError::from)? {
PolarsPluginObject::NuDataFrame(df) => { if NuLazyFrame::can_downcast(&value) {
command_eager(plugin, engine, call, df).map_err(LabeledError::from) let df = NuLazyFrame::try_from_value(value)?;
} command_lazy(engine_state, stack, call, df)
PolarsPluginObject::NuLazyFrame(lazy) => { } else {
command_lazy(plugin, engine, call, lazy).map_err(LabeledError::from) let df = NuDataFrame::try_from_value(value)?;
} command_eager(engine_state, stack, call, df)
_ => Err(LabeledError::new(format!("Unsupported type: {value:?}"))
.with_label("Unsupported Type", call.head)),
} }
} }
} }
fn command_eager( fn command_eager(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
df: NuDataFrame, mut df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let columns: Value = call.req(0)?; let columns: Value = call.req(engine_state, stack, 0)?;
let columns = extract_strings(columns)?; let columns = extract_strings(columns)?;
let new_names: Value = call.req(1)?; let new_names: Value = call.req(engine_state, stack, 1)?;
let new_names = extract_strings(new_names)?; let new_names = extract_strings(new_names)?;
let mut polars_df = df.to_polars();
for (from, to) in columns.iter().zip(new_names.iter()) { for (from, to) in columns.iter().zip(new_names.iter()) {
polars_df df.as_mut()
.rename(from, to) .rename(from, to)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
error: "Error renaming".into(), error: "Error renaming".into(),
@@ -160,44 +145,42 @@ fn command_eager(
})?; })?;
} }
let df = NuDataFrame::new(false, polars_df); Ok(PipelineData::Value(df.into_value(call.head), None))
df.to_pipeline_data(plugin, engine, call.head)
} }
fn command_lazy( fn command_lazy(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
lazy: NuLazyFrame, lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let columns: Value = call.req(0)?; let columns: Value = call.req(engine_state, stack, 0)?;
let columns = extract_strings(columns)?; let columns = extract_strings(columns)?;
let new_names: Value = call.req(1)?; let new_names: Value = call.req(engine_state, stack, 1)?;
let new_names = extract_strings(new_names)?; let new_names = extract_strings(new_names)?;
if columns.len() != new_names.len() { if columns.len() != new_names.len() {
let value: Value = call.req(1)?; let value: Value = call.req(engine_state, stack, 1)?;
return Err(ShellError::IncompatibleParametersSingle { return Err(ShellError::IncompatibleParametersSingle {
msg: "New name list has different size to column list".into(), msg: "New name list has different size to column list".into(),
span: value.span(), span: value.span(),
}); });
} }
let lazy = lazy.to_polars(); let lazy = lazy.into_polars();
let lazy: NuLazyFrame = lazy.rename(&columns, &new_names).into(); let lazy: NuLazyFrame = lazy.rename(&columns, &new_names).into();
lazy.to_pipeline_data(plugin, engine, call.head) Ok(PipelineData::Value(lazy.into_value(call.head)?, None))
} }
#[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(&RenameDF) test_dataframe(vec![Box::new(RenameDF {})])
} }
} }

View File

@@ -1,23 +1,14 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::NuDataFrame;
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
};
use polars::prelude::NamedFrom;
use polars::series::Series;
use crate::{values::CustomValueSupport, PolarsPlugin}; use polars::{prelude::NamedFrom, series::Series};
use super::super::values::{Column, NuDataFrame};
#[derive(Clone)] #[derive(Clone)]
pub struct SampleDF; pub struct SampleDF;
impl PluginCommand for SampleDF { impl Command for SampleDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars sample" "dfr sample"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -57,61 +48,46 @@ impl PluginCommand for SampleDF {
vec![ vec![
Example { Example {
description: "Sample rows from dataframe", description: "Sample rows from dataframe",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars sample --n-rows 1", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr sample --n-rows 1",
result: None, // No expected value because sampling is random result: None, // No expected value because sampling is random
}, },
Example { Example {
description: "Shows sample row using fraction and replace", description: "Shows sample row using fraction and replace",
example: example:
"[[a b]; [1 2] [3 4] [5 6]] | polars into-df | polars sample --fraction 0.5 --replace", "[[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 result: None, // No expected value because sampling is random
}, },
Example {
description: "Shows sample row using using predefined seed 1",
example:
"[[a b]; [1 2] [3 4] [5 6]] | polars into-df | polars sample --seed 1 --n-rows 1",
result: Some(
NuDataFrame::try_from_columns(
vec![
Column::new("a".to_string(), vec![Value::test_int(5)]),
Column::new("b".to_string(), vec![Value::test_int(6)]),
],
None,
)
.expect("should not fail")
.into_value(Span::test_data()),
)
},
] ]
} }
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 rows: Option<Spanned<i64>> = call.get_flag("n-rows")?; let rows: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "n-rows")?;
let fraction: Option<Spanned<f64>> = call.get_flag("fraction")?; let fraction: Option<Spanned<f64>> = call.get_flag(engine_state, stack, "fraction")?;
let seed: Option<u64> = call.get_flag::<i64>("seed")?.map(|val| val as u64); let seed: Option<u64> = call
let replace: bool = call.has_flag("replace")?; .get_flag::<i64>(engine_state, stack, "seed")?
let shuffle: bool = call.has_flag("shuffle")?; .map(|val| val as u64);
let replace: bool = call.has_flag(engine_state, stack, "replace")?;
let shuffle: bool = call.has_flag(engine_state, stack, "shuffle")?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let df = match (rows, fraction) { match (rows, fraction) {
(Some(rows), None) => df (Some(rows), None) => df
.as_ref() .as_ref()
.sample_n(&Series::new("s", &[rows.item]), replace, shuffle, seed) .sample_n(&Series::new("s", &[rows.item]), replace, shuffle, seed)
@@ -146,18 +122,6 @@ fn command(
help: Some("Perhaps you want to use the flag -n or -f".into()), help: Some("Perhaps you want to use the flag -n or -f".into()),
inner: vec![], inner: vec![],
}), }),
};
let df = NuDataFrame::new(false, df?);
df.to_pipeline_data(plugin, engine, call.head)
}
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&SampleDF)
} }
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
} }

View File

@@ -1,18 +1,12 @@
use crate::{values::PolarsPluginObject, PolarsPlugin}; use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
record, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct SchemaCmd; pub struct SchemaDF;
impl PluginCommand for SchemaCmd {
type Plugin = PolarsPlugin;
impl Command for SchemaDF {
fn name(&self) -> &str { fn name(&self) -> &str {
"polars schema" "dfr schema"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -32,7 +26,7 @@ impl PluginCommand for SchemaCmd {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Dataframe schema", description: "Dataframe schema",
example: r#"[[a b]; [1 "foo"] [3 "bar"]] | polars into-df | polars schema"#, example: r#"[[a b]; [1 "foo"] [3 "bar"]] | dfr into-df | dfr schema"#,
result: Some(Value::record( result: Some(Value::record(
record! { record! {
"a" => Value::string("i64", Span::test_data()), "a" => Value::string("i64", Span::test_data()),
@@ -45,44 +39,29 @@ impl PluginCommand for SchemaCmd {
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> {
if call.has_flag("datatype-list")? { if call.has_flag(engine_state, stack, "datatype-list")? {
Ok(PipelineData::Value(datatype_list(Span::unknown()), None)) Ok(PipelineData::Value(datatype_list(Span::unknown()), None))
} else { } else {
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> {
match PolarsPluginObject::try_from_pipeline(plugin, input, call.head)? { let df = NuDataFrame::try_from_pipeline(input, call.head)?;
PolarsPluginObject::NuDataFrame(df) => { let schema = df.schema();
let schema = df.schema(); let value: Value = schema.into();
let value: Value = schema.into(); Ok(PipelineData::Value(value, None))
Ok(PipelineData::Value(value, None))
}
PolarsPluginObject::NuLazyFrame(mut lazy) => {
let schema = lazy.schema()?;
let value: Value = schema.into();
Ok(PipelineData::Value(value, None))
}
_ => Err(ShellError::GenericError {
error: "Must be a dataframe or lazy dataframe".into(),
msg: "".into(),
span: Some(call.head),
help: None,
inner: vec![],
}),
}
} }
fn datatype_list(span: Span) -> Value { fn datatype_list(span: Span) -> Value {
@@ -123,11 +102,11 @@ fn datatype_list(span: Span) -> Value {
#[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(&SchemaCmd) test_dataframe(vec![Box::new(SchemaDF {})])
} }
} }

View File

@@ -1,20 +1,12 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type, Value,
};
use crate::{dataframe::values::Column, values::CustomValueSupport, PolarsPlugin};
use super::super::values::NuDataFrame;
#[derive(Clone)] #[derive(Clone)]
pub struct ShapeDF; pub struct ShapeDF;
impl PluginCommand for ShapeDF { impl Command for ShapeDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars shape" "dfr shape"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -33,7 +25,7 @@ impl PluginCommand for ShapeDF {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Shows row and column shape", description: "Shows row and column shape",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars shape", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr shape",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -50,22 +42,22 @@ impl PluginCommand for ShapeDF {
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 df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let rows = Value::int(df.as_ref().height() as i64, call.head); let rows = Value::int(df.as_ref().height() as i64, call.head);
@@ -74,17 +66,17 @@ fn command(
let rows_col = Column::new("rows".to_string(), vec![rows]); let rows_col = Column::new("rows".to_string(), vec![rows]);
let cols_col = Column::new("columns".to_string(), vec![cols]); let cols_col = Column::new("columns".to_string(), vec![cols]);
let df = NuDataFrame::try_from_columns(vec![rows_col, cols_col], None)?; NuDataFrame::try_from_columns(vec![rows_col, cols_col], None)
df.to_pipeline_data(plugin, engine, call.head) .map(|df| PipelineData::Value(df.into_value(call.head), None))
} }
#[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(&ShapeDF) test_dataframe(vec![Box::new(ShapeDF {})])
} }
} }

View File

@@ -1,21 +1,12 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use crate::{dataframe::values::Column, values::CustomValueSupport, PolarsPlugin};
use super::super::values::NuDataFrame;
#[derive(Clone)] #[derive(Clone)]
pub struct SliceDF; pub struct SliceDF;
impl PluginCommand for SliceDF { impl Command for SliceDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars slice" "dfr slice"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -36,7 +27,7 @@ impl PluginCommand for SliceDF {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Create new dataframe from a slice of the rows", description: "Create new dataframe from a slice of the rows",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars slice 0 1", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr slice 0 1",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -53,39 +44,41 @@ impl PluginCommand for SliceDF {
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 offset: i64 = call.req(0)?; let offset: i64 = call.req(engine_state, stack, 0)?;
let size: usize = call.req(1)?; let size: usize = call.req(engine_state, stack, 1)?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let res = df.as_ref().slice(offset, size); let res = df.as_ref().slice(offset, size);
let res = NuDataFrame::new(false, res);
res.to_pipeline_data(plugin, engine, call.head) Ok(PipelineData::Value(
NuDataFrame::dataframe_into_value(res, call.head),
None,
))
} }
#[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(&SliceDF) test_dataframe(vec![Box::new(SliceDF {})])
} }
} }

View File

@@ -3,8 +3,7 @@ use polars::prelude::{col, lit, DataType, Expr, LiteralValue, PolarsResult as Re
use sqlparser::ast::{ use sqlparser::ast::{
ArrayElemTypeDef, BinaryOperator as SQLBinaryOperator, DataType as SQLDataType, ArrayElemTypeDef, BinaryOperator as SQLBinaryOperator, DataType as SQLDataType,
DuplicateTreatment, Expr as SqlExpr, Function as SQLFunction, FunctionArguments, Expr as SqlExpr, Function as SQLFunction, Value as SqlValue, WindowType,
Value as SqlValue, WindowType,
}; };
fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> { fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
@@ -34,7 +33,7 @@ fn map_sql_polars_datatype(data_type: &SQLDataType) -> Result<DataType> {
SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds), SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds),
SQLDataType::Array(array_type_def) => match array_type_def { SQLDataType::Array(array_type_def) => match array_type_def {
ArrayElemTypeDef::AngleBracket(inner_type) ArrayElemTypeDef::AngleBracket(inner_type)
| ArrayElemTypeDef::SquareBracket(inner_type, _) => { | ArrayElemTypeDef::SquareBracket(inner_type) => {
DataType::List(Box::new(map_sql_polars_datatype(inner_type)?)) DataType::List(Box::new(map_sql_polars_datatype(inner_type)?))
} }
_ => { _ => {
@@ -121,7 +120,9 @@ pub fn parse_sql_expr(expr: &SqlExpr) -> Result<Expr> {
} }
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, .. expr,
data_type,
format: _,
} => cast_(parse_sql_expr(expr)?, data_type)?, } => 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)?,
@@ -161,17 +162,8 @@ fn parse_sql_function(sql_function: &SQLFunction) -> Result<Expr> {
use sqlparser::ast::{FunctionArg, FunctionArgExpr}; use sqlparser::ast::{FunctionArg, FunctionArgExpr};
// Function name mostly do not have name space, so it mostly take the first args // Function name mostly do not have name space, so it mostly take the first args
let function_name = sql_function.name.0[0].value.to_ascii_lowercase(); let function_name = sql_function.name.0[0].value.to_ascii_lowercase();
let args = sql_function
// One day this should support the additional argument types supported with 0.40 .args
let (args, distinct) = match &sql_function.args {
FunctionArguments::List(list) => (
list.args.clone(),
list.duplicate_treatment == Some(DuplicateTreatment::Distinct),
),
_ => (vec![], false),
};
let args = args
.iter() .iter()
.map(|arg| match arg { .map(|arg| match arg {
FunctionArg::Named { arg, .. } => arg, FunctionArg::Named { arg, .. } => arg,
@@ -182,15 +174,15 @@ fn parse_sql_function(sql_function: &SQLFunction) -> Result<Expr> {
match ( match (
function_name.as_str(), function_name.as_str(),
args.as_slice(), args.as_slice(),
distinct, sql_function.distinct,
) { ) {
("sum", [FunctionArgExpr::Expr(ref expr)], false) => { ("sum", [FunctionArgExpr::Expr(expr)], false) => {
apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.sum() apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.sum()
} }
("count", [FunctionArgExpr::Expr(ref expr)], false) => { ("count", [FunctionArgExpr::Expr(expr)], false) => {
apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.count() apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.count()
} }
("count", [FunctionArgExpr::Expr(ref expr)], true) => { ("count", [FunctionArgExpr::Expr(expr)], true) => {
apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.n_unique() apply_window_spec(parse_sql_expr(expr)?, sql_function.over.as_ref())?.n_unique()
} }
// Special case for wildcard args to count function. // Special case for wildcard args to count function.

View File

@@ -1,12 +1,6 @@
use crate::{values::CustomValueSupport, PolarsPlugin}; use crate::dataframe::values::{Column, NuDataFrame};
use nu_engine::command_prelude::*;
use super::super::values::{Column, NuDataFrame};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use polars::{ use polars::{
chunked_array::ChunkedArray, chunked_array::ChunkedArray,
prelude::{ prelude::{
@@ -18,11 +12,9 @@ use polars::{
#[derive(Clone)] #[derive(Clone)]
pub struct Summary; pub struct Summary;
impl PluginCommand for Summary { impl Command for Summary {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars summary" "dfr summary"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -38,7 +30,7 @@ impl PluginCommand for Summary {
) )
.named( .named(
"quantiles", "quantiles",
SyntaxShape::List(Box::new(SyntaxShape::Float)), SyntaxShape::Table(vec![]),
"provide optional quantiles", "provide optional quantiles",
Some('q'), Some('q'),
) )
@@ -47,7 +39,7 @@ impl PluginCommand for Summary {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "list dataframe descriptives", description: "list dataframe descriptives",
example: "[[a b]; [1 1] [1 1]] | polars into-df | polars summary", example: "[[a b]; [1 1] [1 1]] | dfr into-df | dfr summary",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -107,22 +99,22 @@ impl PluginCommand for Summary {
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 quantiles: Option<Vec<Value>> = call.get_flag("quantiles")?; let quantiles: Option<Vec<Value>> = call.get_flag(engine_state, stack, "quantiles")?;
let quantiles = quantiles.map(|values| { let quantiles = quantiles.map(|values| {
values values
.iter() .iter()
@@ -175,7 +167,7 @@ fn command(
labels.append(&mut quantiles_labels); labels.append(&mut quantiles_labels);
labels.push(Some("max".to_string())); labels.push(Some("max".to_string()));
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; let df = NuDataFrame::try_from_pipeline(input, call.head)?;
let names = ChunkedArray::<StringType>::from_slice_options("descriptor", &labels).into_series(); let names = ChunkedArray::<StringType>::from_slice_options("descriptor", &labels).into_series();
@@ -189,19 +181,53 @@ fn command(
.map(|col| { .map(|col| {
let count = col.len() as f64; let count = col.len() as f64;
let sum = col.sum::<f64>().ok(); let sum = col.sum_as_series().ok().and_then(|series| {
let mean = col.mean(); series
let median = col.median(); .cast(&DataType::Float64)
let std = col.std(0); .ok()
let min = col.min::<f64>().ok().flatten(); .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() {
Ok(v) => match v.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
},
_ => None,
};
let std = match col.std_as_series(0) {
Ok(v) => match v.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
},
_ => None,
};
let min = col.min_as_series().ok().and_then(|series| {
series
.cast(&DataType::Float64)
.ok()
.and_then(|ca| match ca.get(0) {
Ok(AnyValue::Float64(v)) => Some(v),
_ => None,
})
});
let mut quantiles = quantiles let mut quantiles = quantiles
.clone() .clone()
.into_iter() .into_iter()
.map(|q| { .map(|q| {
col.quantile_reduce(q, QuantileInterpolOptions::default()) col.quantile_as_series(q, QuantileInterpolOptions::default())
.ok() .ok()
.map(|s| s.into_series("quantile"))
.and_then(|ca| ca.cast(&DataType::Float64).ok()) .and_then(|ca| ca.cast(&DataType::Float64).ok())
.and_then(|ca| match ca.get(0) { .and_then(|ca| match ca.get(0) {
Ok(AnyValue::Float64(v)) => Some(v), Ok(AnyValue::Float64(v)) => Some(v),
@@ -210,7 +236,15 @@ fn command(
}) })
.collect::<Vec<Option<f64>>>(); .collect::<Vec<Option<f64>>>();
let max = col.max::<f64>().ok().flatten(); let max = col.max_as_series().ok().and_then(|series| {
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]; let mut descriptors = vec![Some(count), sum, mean, median, std, min];
descriptors.append(&mut quantiles); descriptors.append(&mut quantiles);
@@ -222,27 +256,24 @@ fn command(
let res = head.chain(tail).collect::<Vec<Series>>(); let res = head.chain(tail).collect::<Vec<Series>>();
let polars_df = DataFrame::new(res).map_err(|e| ShellError::GenericError { DataFrame::new(res)
error: "Dataframe Error".into(), .map_err(|e| ShellError::GenericError {
msg: e.to_string(), error: "Dataframe Error".into(),
span: Some(call.head), msg: e.to_string(),
help: None, span: Some(call.head),
inner: vec![], help: None,
})?; inner: vec![],
})
let df = NuDataFrame::new(df.from_lazy, polars_df); .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(&Summary) test_dataframe(vec![Box::new(Summary {})])
} }
} }

View File

@@ -1,22 +1,14 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{Column, NuDataFrame};
use nu_protocol::{ use nu_engine::command_prelude::*;
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
use polars::prelude::DataType; use polars::prelude::DataType;
use crate::{dataframe::values::Column, values::CustomValueSupport, PolarsPlugin};
use super::super::values::NuDataFrame;
#[derive(Clone)] #[derive(Clone)]
pub struct TakeDF; pub struct TakeDF;
impl PluginCommand for TakeDF { impl Command for TakeDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars take" "dfr take"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -41,9 +33,9 @@ impl PluginCommand for TakeDF {
vec![ vec![
Example { Example {
description: "Takes selected rows from dataframe", description: "Takes selected rows from dataframe",
example: r#"let df = ([[a b]; [4 1] [5 2] [4 3]] | polars into-df); example: r#"let df = ([[a b]; [4 1] [5 2] [4 3]] | dfr into-df);
let indices = ([0 2] | polars into-df); let indices = ([0 2] | dfr into-df);
$df | polars take $indices"#, $df | dfr take $indices"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -64,9 +56,9 @@ impl PluginCommand for TakeDF {
}, },
Example { Example {
description: "Takes selected rows from series", description: "Takes selected rows from series",
example: r#"let series = ([4 1 5 2 4 3] | polars into-df); example: r#"let series = ([4 1 5 2 4 3] | dfr into-df);
let indices = ([0 2] | polars into-df); let indices = ([0 2] | dfr into-df);
$series | polars take $indices"#, $series | dfr take $indices"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -84,25 +76,24 @@ impl PluginCommand for TakeDF {
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 index_value: Value = call.req(0)?; let index_value: Value = call.req(engine_state, stack, 0)?;
let index_span = index_value.span(); let index_span = index_value.span();
let index = NuDataFrame::try_from_value_coerce(plugin, &index_value, call.head)? let index = NuDataFrame::try_from_value(index_value)?.as_series(index_span)?;
.as_series(index_span)?;
let casted = match index.dtype() { let casted = match index.dtype() {
DataType::UInt32 | DataType::UInt64 | DataType::Int32 | DataType::Int64 => index DataType::UInt32 | DataType::UInt64 | DataType::Int32 | DataType::Int64 => index
@@ -131,29 +122,27 @@ fn command(
inner: vec![], inner: vec![],
})?; })?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?; NuDataFrame::try_from_pipeline(input, call.head).and_then(|df| {
let polars_df = df df.as_ref()
.to_polars() .take(indices)
.take(indices) .map_err(|e| ShellError::GenericError {
.map_err(|e| ShellError::GenericError { error: "Error taking values".into(),
error: "Error taking values".into(), msg: e.to_string(),
msg: e.to_string(), span: Some(call.head),
span: Some(call.head), help: None,
help: None, inner: vec![],
inner: vec![], })
})?; .map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
})
let df = NuDataFrame::new(df.from_lazy, polars_df);
df.to_pipeline_data(plugin, engine, call.head)
} }
#[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(&TakeDF) test_dataframe(vec![Box::new(TakeDF {})])
} }
} }

View File

@@ -0,0 +1,79 @@
use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use polars::prelude::{IpcWriter, SerWriter};
use std::{fs::File, path::PathBuf};
#[derive(Clone)]
pub struct ToArrow;
impl Command for ToArrow {
fn name(&self) -> &str {
"dfr to-arrow"
}
fn usage(&self) -> &str {
"Saves dataframe to arrow file."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Saves dataframe to arrow file",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-arrow test.arrow",
result: None,
}]
}
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 file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
let mut file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
error: "Error with file name".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
IpcWriter::new(&mut file)
.finish(df.as_mut())
.map_err(|e| ShellError::GenericError {
error: "Error saving file".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
Ok(PipelineData::Value(
Value::list(vec![file_value], call.head),
None,
))
}

View File

@@ -0,0 +1,109 @@
use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use polars_io::{
avro::{AvroCompression, AvroWriter},
SerWriter,
};
use std::{fs::File, path::PathBuf};
#[derive(Clone)]
pub struct ToAvro;
impl Command for ToAvro {
fn name(&self) -> &str {
"dfr to-avro"
}
fn usage(&self) -> &str {
"Saves dataframe to avro file."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.named(
"compression",
SyntaxShape::String,
"use compression, supports deflate or snappy",
Some('c'),
)
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Saves dataframe to avro file",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-avro test.avro",
result: None,
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
command(engine_state, stack, call, input)
}
}
fn get_compression(call: &Call) -> Result<Option<AvroCompression>, ShellError> {
if let Some((compression, span)) = call
.get_flag_expr("compression")
.and_then(|e| e.as_string().map(|s| (s, e.span)))
{
match compression.as_ref() {
"snappy" => Ok(Some(AvroCompression::Snappy)),
"deflate" => Ok(Some(AvroCompression::Deflate)),
_ => Err(ShellError::IncorrectValue {
msg: "compression must be one of deflate or snappy".to_string(),
val_span: span,
call_span: span,
}),
}
} else {
Ok(None)
}
}
fn command(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let compression = get_compression(call)?;
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
let file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
error: "Error with file name".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
AvroWriter::new(file)
.with_compression(compression)
.finish(df.as_mut())
.map_err(|e| ShellError::GenericError {
error: "Error saving file".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
Ok(PipelineData::Value(
Value::list(vec![file_value], call.head),
None,
))
}

View File

@@ -0,0 +1,125 @@
use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use polars::prelude::{CsvWriter, SerWriter};
use std::{fs::File, path::PathBuf};
#[derive(Clone)]
pub struct ToCSV;
impl Command for ToCSV {
fn name(&self) -> &str {
"dfr to-csv"
}
fn usage(&self) -> &str {
"Saves dataframe to CSV file."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
.named(
"delimiter",
SyntaxShape::String,
"file delimiter character",
Some('d'),
)
.switch("no-header", "Indicates if file doesn't have header", None)
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Saves dataframe to CSV file",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-csv test.csv",
result: None,
},
Example {
description: "Saves dataframe to CSV file using other delimiter",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-csv test.csv --delimiter '|'",
result: None,
},
]
}
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 file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let delimiter: Option<Spanned<String>> = call.get_flag(engine_state, stack, "delimiter")?;
let no_header: bool = call.has_flag(engine_state, stack, "no-header")?;
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
let mut file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
error: "Error with file name".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let writer = CsvWriter::new(&mut file);
let writer = if no_header {
writer.include_header(false)
} else {
writer.include_header(true)
};
let mut writer = match delimiter {
None => writer,
Some(d) => {
if d.item.len() != 1 {
return Err(ShellError::GenericError {
error: "Incorrect delimiter".into(),
msg: "Delimiter has to be one char".into(),
span: Some(d.span),
help: None,
inner: vec![],
});
} else {
let delimiter = match d.item.chars().next() {
Some(d) => d as u8,
None => unreachable!(),
};
writer.with_separator(delimiter)
}
}
};
writer
.finish(df.as_mut())
.map_err(|e| ShellError::GenericError {
error: "Error writing to file".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
Ok(PipelineData::Value(
Value::list(vec![file_value], call.head),
None,
))
}

View File

@@ -1,28 +1,14 @@
use crate::{ use crate::dataframe::values::{Column, NuDataFrame, NuSchema};
dataframe::values::NuSchema, use nu_engine::command_prelude::*;
values::{Column, CustomValueSupport},
PolarsPlugin,
};
use super::super::values::NuDataFrame; use polars::prelude::*;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value,
};
use polars::{
prelude::{AnyValue, DataType, Field, NamedFrom},
series::Series,
};
#[derive(Clone)] #[derive(Clone)]
pub struct ToDataFrame; pub struct ToDataFrame;
impl PluginCommand for ToDataFrame { impl Command for ToDataFrame {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars into-df" "dfr into-df"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -37,11 +23,6 @@ impl PluginCommand for ToDataFrame {
r#"Polars Schema in format [{name: str}]. CSV, JSON, and JSONL files"#, r#"Polars Schema in format [{name: str}]. CSV, JSON, and JSONL files"#,
Some('s'), Some('s'),
) )
.switch(
"as-columns",
r#"When input shape is record of lists, treat each list as column values."#,
Some('c'),
)
.input_output_type(Type::Any, Type::Custom("dataframe".into())) .input_output_type(Type::Any, Type::Custom("dataframe".into()))
.category(Category::Custom("dataframe".into())) .category(Category::Custom("dataframe".into()))
} }
@@ -50,28 +31,7 @@ impl PluginCommand for ToDataFrame {
vec![ vec![
Example { Example {
description: "Takes a dictionary and creates a dataframe", description: "Takes a dictionary and creates a dataframe",
example: "[[a b];[1 2] [3 4]] | polars into-df", example: "[[a b];[1 2] [3 4]] | dfr into-df",
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)],
),
],
None,
)
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Takes a record of lists and creates a dataframe",
example: "{a: [1 3], b: [2 4]} | polars into-df --as-columns",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -92,7 +52,7 @@ impl PluginCommand for ToDataFrame {
}, },
Example { Example {
description: "Takes a list of tables and creates a dataframe", description: "Takes a list of tables and creates a dataframe",
example: "[[1 2 a] [3 4 b] [5 6 c]] | polars into-df", example: "[[1 2 a] [3 4 b] [5 6 c]] | dfr into-df",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -121,7 +81,7 @@ impl PluginCommand for ToDataFrame {
}, },
Example { Example {
description: "Takes a list and creates a dataframe", description: "Takes a list and creates a dataframe",
example: "[a b c] | polars into-df", example: "[a b c] | dfr into-df",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -140,7 +100,7 @@ impl PluginCommand for ToDataFrame {
}, },
Example { Example {
description: "Takes a list of booleans and creates a dataframe", description: "Takes a list of booleans and creates a dataframe",
example: "[true true false] | polars into-df", example: "[true true false] | dfr into-df",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -159,9 +119,9 @@ impl PluginCommand for ToDataFrame {
}, },
Example { Example {
description: "Convert to a dataframe and provide a schema", description: "Convert to a dataframe and provide a schema",
example: "{a: 1, b: {a: [1 2 3]}, c: [a b c]}| polars into-df -s {a: u8, b: {a: list<u64>}, c: list<str>}", example: "{a: 1, b: {a: [1 2 3]}, c: [a b c]}| dfr into-df -s {a: u8, b: {a: list<u64>}, c: list<str>}",
result: Some( result: Some(
NuDataFrame::try_from_series_vec(vec![ NuDataFrame::try_from_series(vec![
Series::new("a", &[1u8]), Series::new("a", &[1u8]),
{ {
let dtype = DataType::Struct(vec![Field::new("a", DataType::List(Box::new(DataType::UInt64)))]); let dtype = DataType::Struct(vec![Field::new("a", DataType::List(Box::new(DataType::UInt64)))]);
@@ -183,8 +143,8 @@ impl PluginCommand for ToDataFrame {
}, },
Example { Example {
description: "Convert to a dataframe and provide a schema that adds a new column", description: "Convert to a dataframe and provide a schema that adds a new column",
example: r#"[[a b]; [1 "foo"] [2 "bar"]] | polars into-df -s {a: u8, b:str, c:i64} | polars fill-null 3"#, example: r#"[[a b]; [1 "foo"] [2 "bar"]] | dfr into-df -s {a: u8, b:str, c:i64} | dfr fill-null 3"#,
result: Some(NuDataFrame::try_from_series_vec(vec![ result: Some(NuDataFrame::try_from_series(vec![
Series::new("a", [1u8, 2]), Series::new("a", [1u8, 2]),
Series::new("b", ["foo", "bar"]), Series::new("b", ["foo", "bar"]),
Series::new("c", [3i64, 3]), Series::new("c", [3i64, 3]),
@@ -198,63 +158,32 @@ impl PluginCommand for ToDataFrame {
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> {
let maybe_schema = call let maybe_schema = call
.get_flag("schema")? .get_flag(engine_state, stack, "schema")?
.map(|schema| NuSchema::try_from(&schema)) .map(|schema| NuSchema::try_from(&schema))
.transpose()?; .transpose()?;
let maybe_as_columns = call.has_flag("as-columns")?; let df = NuDataFrame::try_from_iter(input.into_iter(), maybe_schema.clone())?;
let df = if !maybe_as_columns { Ok(PipelineData::Value(
NuDataFrame::try_from_iter(plugin, input.into_iter(), maybe_schema.clone())? NuDataFrame::into_value(df, call.head),
} else { None,
match &input { ))
PipelineData::Value(Value::Record { val, .. }, _) => {
let items: Result<Vec<(String, Vec<Value>)>, &str> = val
.iter()
.map(|(k, v)| match v.to_owned().into_list() {
Ok(v) => Ok((k.to_owned(), v)),
_ => Err("error"),
})
.collect();
match items {
Ok(items) => {
let columns = items
.iter()
.map(|(k, v)| Column::new(k.to_owned(), v.to_owned()))
.collect::<Vec<Column>>();
NuDataFrame::try_from_columns(columns, maybe_schema)?
}
Err(_) => NuDataFrame::try_from_iter(
plugin,
input.into_iter(),
maybe_schema.clone(),
)?,
}
}
_ => NuDataFrame::try_from_iter(plugin, input.into_iter(), maybe_schema.clone())?,
}
};
df.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from)
} }
} }
#[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::*;
use nu_protocol::ShellError;
#[test] #[test]
fn test_into_df() -> Result<(), ShellError> { fn test_examples() {
test_polars_plugin_command(&ToDataFrame) test_dataframe(vec![Box::new(ToDataFrame {})])
} }
} }

View File

@@ -0,0 +1,80 @@
use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use polars::prelude::{JsonWriter, SerWriter};
use std::{fs::File, io::BufWriter, path::PathBuf};
#[derive(Clone)]
pub struct ToJsonLines;
impl Command for ToJsonLines {
fn name(&self) -> &str {
"dfr to-jsonl"
}
fn usage(&self) -> &str {
"Saves dataframe to a JSON lines file."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Saves dataframe to JSON lines file",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-jsonl test.jsonl",
result: None,
}]
}
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 file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
let file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
error: "Error with file name".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let buf_writer = BufWriter::new(file);
JsonWriter::new(buf_writer)
.finish(df.as_mut())
.map_err(|e| ShellError::GenericError {
error: "Error saving file".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
Ok(PipelineData::Value(
Value::list(vec![file_value], call.head),
None,
))
}

View File

@@ -1,29 +1,16 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use crate::dataframe::values::{NuDataFrame, NuExpression};
use nu_protocol::{ use nu_engine::command_prelude::*;
record, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use crate::{
dataframe::values::NuExpression,
values::{CustomValueSupport, NuLazyFrame},
PolarsPlugin,
};
use super::super::values::NuDataFrame;
#[derive(Clone)] #[derive(Clone)]
pub struct ToNu; pub struct ToNu;
impl PluginCommand for ToNu { impl Command for ToNu {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars into-nu" "dfr into-nu"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Converts a dataframe or an expression into nushell value for access and exploration." "Converts a dataframe or an expression into into nushell value for access and exploration."
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
@@ -35,11 +22,11 @@ impl PluginCommand for ToNu {
Some('n'), Some('n'),
) )
.switch("tail", "shows tail rows", Some('t')) .switch("tail", "shows tail rows", Some('t'))
.switch("index", "add an index column", Some('i'))
.input_output_types(vec![ .input_output_types(vec![
(Type::Custom("expression".into()), Type::Any), (Type::Custom("expression".into()), Type::Any),
(Type::Custom("dataframe".into()), Type::table()), (Type::Custom("dataframe".into()), Type::Table(vec![])),
]) ])
//.input_output_type(Type::Any, Type::Any)
.category(Category::Custom("dataframe".into())) .category(Category::Custom("dataframe".into()))
} }
@@ -63,18 +50,17 @@ impl PluginCommand for ToNu {
vec![ vec![
Example { Example {
description: "Shows head rows from dataframe", description: "Shows head rows from dataframe",
example: "[[a b]; [1 2] [3 4]] | polars into-df | polars into-nu --index", example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr into-nu",
result: Some(Value::list(vec![rec_1, rec_2], Span::test_data())), result: Some(Value::list(vec![rec_1, rec_2], Span::test_data())),
}, },
Example { Example {
description: "Shows tail rows from dataframe", description: "Shows tail rows from dataframe",
example: example: "[[a b]; [1 2] [5 6] [3 4]] | dfr into-df | dfr into-nu --tail --rows 1",
"[[a b]; [1 2] [5 6] [3 4]] | polars into-df | polars into-nu --tail --rows 1 --index",
result: Some(Value::list(vec![rec_3], Span::test_data())), result: Some(Value::list(vec![rec_3], Span::test_data())),
}, },
Example { Example {
description: "Convert a col expression into a nushell value", description: "Convert a col expression into a nushell value",
example: "polars col a | polars into-nu --index", example: "dfr col a | dfr into-nu",
result: Some(Value::test_record(record! { result: Some(Value::test_record(record! {
"expr" => Value::test_string("column"), "expr" => Value::test_string("column"),
"value" => Value::test_string("a"), "value" => Value::test_string("a"),
@@ -85,40 +71,39 @@ impl PluginCommand for ToNu {
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> {
let value = input.into_value(call.head)?; let value = input.into_value(call.head);
if NuDataFrame::can_downcast(&value) || NuLazyFrame::can_downcast(&value) { if NuDataFrame::can_downcast(&value) {
dataframe_command(plugin, call, value) dataframe_command(engine_state, stack, call, value)
} else { } else {
expression_command(plugin, call, value) expression_command(call, value)
} }
.map_err(|e| e.into())
} }
} }
fn dataframe_command( fn dataframe_command(
plugin: &PolarsPlugin, engine_state: &EngineState,
call: &EvaluatedCall, stack: &mut Stack,
call: &Call,
input: Value, input: Value,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let rows: Option<usize> = call.get_flag("rows")?; let rows: Option<usize> = call.get_flag(engine_state, stack, "rows")?;
let tail: bool = call.has_flag("tail")?; let tail: bool = call.has_flag(engine_state, stack, "tail")?;
let index: bool = call.has_flag("index")?;
let df = NuDataFrame::try_from_value_coerce(plugin, &input, call.head)?; let df = NuDataFrame::try_from_value(input)?;
let values = if tail { let values = if tail {
df.tail(rows, index, call.head)? df.tail(rows, call.head)?
} else { } else {
// if rows is specified, return those rows, otherwise return everything // if rows is specified, return those rows, otherwise return everything
if rows.is_some() { if rows.is_some() {
df.head(rows, index, call.head)? df.head(rows, call.head)?
} else { } else {
df.head(Some(df.height()), index, call.head)? df.head(Some(df.height()), call.head)?
} }
}; };
@@ -126,13 +111,8 @@ fn dataframe_command(
Ok(PipelineData::Value(value, None)) Ok(PipelineData::Value(value, None))
} }
fn expression_command(call: &Call, input: Value) -> Result<PipelineData, ShellError> {
fn expression_command( let expr = NuExpression::try_from_value(input)?;
plugin: &PolarsPlugin,
call: &EvaluatedCall,
input: Value,
) -> Result<PipelineData, ShellError> {
let expr = NuExpression::try_from_value(plugin, &input)?;
let value = expr.to_value(call.head)?; let value = expr.to_value(call.head)?;
Ok(PipelineData::Value(value, None)) Ok(PipelineData::Value(value, None))
@@ -140,11 +120,17 @@ fn expression_command(
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::super::super::expressions::ExprCol;
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_dataframe_input() {
test_polars_plugin_command(&ToNu) test_dataframe(vec![Box::new(ToNu {})])
}
#[test]
fn test_examples_expression_input() {
test_dataframe(vec![Box::new(ToNu {}), Box::new(ExprCol {})])
} }
} }

View File

@@ -0,0 +1,79 @@
use crate::dataframe::values::NuDataFrame;
use nu_engine::command_prelude::*;
use polars::prelude::ParquetWriter;
use std::{fs::File, path::PathBuf};
#[derive(Clone)]
pub struct ToParquet;
impl Command for ToParquet {
fn name(&self) -> &str {
"dfr to-parquet"
}
fn usage(&self) -> &str {
"Saves dataframe to parquet file."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("file", SyntaxShape::Filepath, "file path to save dataframe")
.input_output_type(Type::Custom("dataframe".into()), Type::Any)
.category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Saves dataframe to parquet file",
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr to-parquet test.parquet",
result: None,
}]
}
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 file_name: Spanned<PathBuf> = call.req(engine_state, stack, 0)?;
let mut df = NuDataFrame::try_from_pipeline(input, call.head)?;
let file = File::create(&file_name.item).map_err(|e| ShellError::GenericError {
error: "Error with file name".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
ParquetWriter::new(file)
.finish(df.as_mut())
.map_err(|e| ShellError::GenericError {
error: "Error saving file".into(),
msg: e.to_string(),
span: Some(file_name.span),
help: None,
inner: vec![],
})?;
let file_value = Value::string(format!("saved {:?}", &file_name.item), file_name.span);
Ok(PipelineData::Value(
Value::list(vec![file_value], call.head),
None,
))
}

View File

@@ -1,23 +1,12 @@
use super::super::values::{Column, NuDataFrame}; use crate::dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame};
use crate::{ use nu_engine::command_prelude::*;
dataframe::values::{NuExpression, NuLazyFrame},
values::{CustomValueSupport, PolarsPluginObject},
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct WithColumn; pub struct WithColumn;
impl PluginCommand for WithColumn { impl Command for WithColumn {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars with-column" "dfr with-column"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -44,8 +33,8 @@ impl PluginCommand for WithColumn {
Example { Example {
description: "Adds a series to the dataframe", description: "Adds a series to the dataframe",
example: r#"[[a b]; [1 2] [3 4]] example: r#"[[a b]; [1 2] [3 4]]
| polars into-df | dfr into-df
| polars with-column ([5 6] | polars into-df) --name c"#, | dfr with-column ([5 6] | dfr into-df) --name c"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -71,12 +60,12 @@ impl PluginCommand for WithColumn {
Example { Example {
description: "Adds a series to the dataframe", description: "Adds a series to the dataframe",
example: r#"[[a b]; [1 2] [3 4]] example: r#"[[a b]; [1 2] [3 4]]
| polars into-lazy | dfr into-lazy
| polars with-column [ | dfr with-column [
((polars col a) * 2 | polars as "c") ((dfr col a) * 2 | dfr as "c")
((polars col a) * 3 | polars as "d") ((dfr col a) * 3 | dfr as "d")
] ]
| polars collect"#, | dfr collect"#,
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![ vec![
@@ -108,55 +97,59 @@ impl PluginCommand for WithColumn {
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> {
let value = input.into_value(call.head)?; let value = input.into_value(call.head);
match PolarsPluginObject::try_from_value(plugin, &value)? {
PolarsPluginObject::NuDataFrame(df) => command_eager(plugin, engine, call, df), if NuLazyFrame::can_downcast(&value) {
PolarsPluginObject::NuLazyFrame(lazy) => command_lazy(plugin, engine, call, lazy), let df = NuLazyFrame::try_from_value(value)?;
_ => Err(ShellError::CantConvert { command_lazy(engine_state, stack, call, df)
} else if NuDataFrame::can_downcast(&value) {
let df = NuDataFrame::try_from_value(value)?;
command_eager(engine_state, stack, call, df)
} else {
Err(ShellError::CantConvert {
to_type: "lazy or eager dataframe".into(), to_type: "lazy or eager dataframe".into(),
from_type: value.get_type().to_string(), from_type: value.get_type().to_string(),
span: value.span(), span: value.span(),
help: None, help: None,
}), })
} }
.map_err(LabeledError::from)
} }
} }
fn command_eager( fn command_eager(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
df: NuDataFrame, mut df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let new_column: Value = call.req(0)?; let new_column: Value = call.req(engine_state, stack, 0)?;
let column_span = new_column.span(); let column_span = new_column.span();
if NuExpression::can_downcast(&new_column) { if NuExpression::can_downcast(&new_column) {
let vals: Vec<Value> = call.rest(0)?; let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
let value = Value::list(vals, call.head); let value = Value::list(vals, call.head);
let expressions = NuExpression::extract_exprs(plugin, value)?; let expressions = NuExpression::extract_exprs(value)?;
let lazy = NuLazyFrame::new(true, df.lazy().to_polars().with_columns(&expressions)); let lazy = NuLazyFrame::new(true, df.lazy().with_columns(&expressions));
let df = lazy.collect(call.head)?;
df.to_pipeline_data(plugin, engine, call.head)
} else {
let mut other = NuDataFrame::try_from_value_coerce(plugin, &new_column, call.head)?
.as_series(column_span)?;
let name = match call.get_flag::<String>("name")? { let df = lazy.collect(call.head)?;
Ok(PipelineData::Value(df.into_value(call.head), None))
} else {
let mut other = NuDataFrame::try_from_value(new_column)?.as_series(column_span)?;
let name = match call.get_flag::<String>(engine_state, stack, "name")? {
Some(name) => name, Some(name) => name,
None => other.name().to_string(), None => other.name().to_string(),
}; };
let series = other.rename(&name).clone(); let series = other.rename(&name).clone();
let mut polars_df = df.to_polars(); df.as_mut()
polars_df
.with_column(series) .with_column(series)
.map_err(|e| ShellError::GenericError { .map_err(|e| ShellError::GenericError {
error: "Error adding column to dataframe".into(), error: "Error adding column to dataframe".into(),
@@ -164,33 +157,47 @@ fn command_eager(
span: Some(column_span), span: Some(column_span),
help: None, help: None,
inner: vec![], inner: vec![],
})?; })
.map(|df| {
let df = NuDataFrame::new(df.from_lazy, polars_df); PipelineData::Value(
df.to_pipeline_data(plugin, engine, call.head) NuDataFrame::dataframe_into_value(df.clone(), call.head),
None,
)
})
} }
} }
fn command_lazy( fn command_lazy(
plugin: &PolarsPlugin, engine_state: &EngineState,
engine: &EngineInterface, stack: &mut Stack,
call: &EvaluatedCall, call: &Call,
lazy: NuLazyFrame, lazy: NuLazyFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let vals: Vec<Value> = call.rest(0)?; let vals: Vec<Value> = call.rest(engine_state, stack, 0)?;
let value = Value::list(vals, call.head); let value = Value::list(vals, call.head);
let expressions = NuExpression::extract_exprs(plugin, value)?; let expressions = NuExpression::extract_exprs(value)?;
let lazy: NuLazyFrame = lazy.to_polars().with_columns(&expressions).into();
lazy.to_pipeline_data(plugin, engine, call.head) let lazy: NuLazyFrame = lazy.into_polars().with_columns(&expressions).into();
Ok(PipelineData::Value(
NuLazyFrame::into_value(lazy, call.head)?,
None,
))
} }
#[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; use crate::dataframe::expressions::ExprAlias;
use crate::dataframe::expressions::ExprCol;
#[test] #[test]
fn test_examples() -> Result<(), ShellError> { fn test_examples() {
test_polars_plugin_command(&WithColumn) test_dataframe(vec![
Box::new(WithColumn {}),
Box::new(ExprAlias {}),
Box::new(ExprCol {}),
])
} }
} }

View File

@@ -1,20 +1,12 @@
use crate::{values::CustomValueSupport, PolarsPlugin}; use crate::dataframe::values::NuExpression;
use nu_engine::command_prelude::*;
use super::super::values::NuExpression;
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
record, Category, Example, LabeledError, PipelineData, Signature, SyntaxShape, Type, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct ExprAlias; pub struct ExprAlias;
impl PluginCommand for ExprAlias { impl Command for ExprAlias {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars as" "dfr as"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -38,7 +30,7 @@ impl PluginCommand for ExprAlias {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Creates and alias expression", description: "Creates and alias expression",
example: "polars col a | polars as new_a | polars into-nu", example: "dfr col a | dfr as new_a | dfr into-nu",
result: { result: {
let record = Value::test_record(record! { let record = Value::test_record(record! {
"expr" => Value::test_record(record! { "expr" => Value::test_record(record! {
@@ -59,28 +51,36 @@ impl PluginCommand for ExprAlias {
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> {
let alias: String = call.req(0)?; let alias: String = call.req(engine_state, stack, 0)?;
let expr = NuExpression::try_from_pipeline(plugin, input, call.head)?; let expr = NuExpression::try_from_pipeline(input, call.head)?;
let expr: NuExpression = expr.into_polars().alias(alias.as_str()).into(); let expr: NuExpression = expr.into_polars().alias(alias.as_str()).into();
expr.to_pipeline_data(plugin, engine, call.head) Ok(PipelineData::Value(
.map_err(LabeledError::from) NuExpression::into_value(expr, call.head),
None,
))
} }
} }
#[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; use crate::dataframe::eager::ToNu;
use crate::dataframe::expressions::ExprCol;
#[test] #[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> { fn test_examples() {
test_polars_plugin_command(&ExprAlias) test_dataframe(vec![
Box::new(ExprAlias {}),
Box::new(ExprCol {}),
Box::new(ToNu {}),
])
} }
} }

View File

@@ -1,22 +1,14 @@
use crate::{ use crate::dataframe::values::{Column, NuDataFrame, NuExpression};
dataframe::values::{Column, NuDataFrame, NuExpression}, use nu_engine::command_prelude::*;
values::CustomValueSupport,
PolarsPlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, Signature, Span, SyntaxShape, Type, Value,
};
use polars::prelude::arg_where; use polars::prelude::arg_where;
#[derive(Clone)] #[derive(Clone)]
pub struct ExprArgWhere; pub struct ExprArgWhere;
impl PluginCommand for ExprArgWhere { impl Command for ExprArgWhere {
type Plugin = PolarsPlugin;
fn name(&self) -> &str { fn name(&self) -> &str {
"polars arg-where" "dfr arg-where"
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@@ -33,8 +25,8 @@ impl PluginCommand for ExprArgWhere {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![Example {
description: "Return a dataframe where the value match the expression", description: "Return a dataframe where the value match the expression",
example: "let df = ([[a b]; [one 1] [two 2] [three 3]] | polars into-df); example: "let df = ([[a b]; [one 1] [two 2] [three 3]] | dfr into-df);
$df | polars select (polars arg-where ((polars col b) >= 2) | polars as b_arg)", $df | dfr select (dfr arg-where ((dfr col b) >= 2) | dfr as b_arg)",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@@ -55,26 +47,32 @@ impl PluginCommand for ExprArgWhere {
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> {
let value: Value = call.req(0)?; let value: Value = call.req(engine_state, stack, 0)?;
let expr = NuExpression::try_from_value(plugin, &value)?; let expr = NuExpression::try_from_value(value)?;
let expr: NuExpression = arg_where(expr.into_polars()).into(); let expr: NuExpression = arg_where(expr.into_polars()).into();
expr.to_pipeline_data(plugin, engine, call.head)
.map_err(LabeledError::from) Ok(PipelineData::Value(expr.into_value(call.head), None))
} }
} }
#[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; use crate::dataframe::expressions::ExprAlias;
use crate::dataframe::lazy::LazySelect;
#[test] #[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> { fn test_examples() {
test_polars_plugin_command(&ExprArgWhere) test_dataframe(vec![
Box::new(ExprArgWhere {}),
Box::new(ExprAlias {}),
Box::new(LazySelect {}),
])
} }
} }

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