mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 15:11:52 +02:00
Compare commits
79 Commits
Author | SHA1 | Date | |
---|---|---|---|
791f7dd9c3 | |||
a4c1b092ba | |||
6e71c1008d | |||
906d0b920f | |||
efbf4f48c6 | |||
2ddab3e8ce | |||
35dc7438a5 | |||
2a54ee0c54 | |||
cad2741e9e | |||
ae5f3c8210 | |||
a5e97ca549 | |||
06f87cfbe8 | |||
d4e78c6f47 | |||
3653400ebc | |||
81a48d6d0e | |||
f030ab3f12 | |||
0dc0c6a10a | |||
53c8185af3 | |||
36b5d063c1 | |||
a7ec00a037 | |||
918822ae0d | |||
ab5e24a0e7 | |||
b5ea522f0e | |||
afa963fd50 | |||
1e343ff00c | |||
21a543a901 | |||
390deb4ff7 | |||
1c4cb30d64 | |||
1ec2ec72b5 | |||
0d244a9701 | |||
b36d21e76f | |||
d8c4565413 | |||
22ba4c2a2f | |||
8d19b21b9f | |||
45a3afdc79 | |||
2d078849cb | |||
b6363f3ce1 | |||
5ca9e12b7f | |||
5b0b2f1ddd | |||
3afb53b8ce | |||
b40d16310c | |||
d3718d00db | |||
f716f61fc1 | |||
b2ce669791 | |||
cd155f63e1 | |||
9eaa6877f3 | |||
a6b6afbca9 | |||
62666bebc9 | |||
d1fcce0cd3 | |||
a2443fbe02 | |||
db16b56fe1 | |||
54bf671a50 | |||
755d0e648b | |||
e440d8c939 | |||
01dd358a18 | |||
50fb97f6b7 | |||
ebf139f5e5 | |||
8925ca5da3 | |||
287652573b | |||
db24ad8f36 | |||
f88674f353 | |||
59cb0ba381 | |||
c4cfab5e16 | |||
b2c5af457e | |||
c731a5b628 | |||
f97f9d4af3 | |||
ed7d3fed66 | |||
7304d06c0b | |||
ca615d9389 | |||
6d096206b6 | |||
2a8cb24309 | |||
8d38743e27 | |||
eabfa2de54 | |||
a86a0abb90 | |||
adcda450d5 | |||
147b9d4436 | |||
c43a58d9d6 | |||
e38442782e | |||
b98f893217 |
@ -59,4 +59,3 @@ steps:
|
|||||||
- bash: cargo fmt --all -- --check
|
- bash: cargo fmt --all -- --check
|
||||||
condition: eq(variables['style'], 'fmt')
|
condition: eq(variables['style'], 'fmt')
|
||||||
displayName: Lint
|
displayName: Lint
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
[build]
|
[build]
|
||||||
|
|
||||||
#rustflags = ["--cfg", "coloring_in_tokens"]
|
#rustflags = ["--cfg", "data_processing_primitives"]
|
||||||
|
@ -38,7 +38,7 @@ workflows:
|
|||||||
extra_build_args: --cache-from=quay.io/nushell/nu-base:devel
|
extra_build_args: --cache-from=quay.io/nushell/nu-base:devel
|
||||||
filters:
|
filters:
|
||||||
branches:
|
branches:
|
||||||
ignore:
|
ignore:
|
||||||
- master
|
- master
|
||||||
before_build:
|
before_build:
|
||||||
- pull_cache
|
- pull_cache
|
||||||
|
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@ -0,0 +1 @@
|
|||||||
|
target
|
18
.github/workflows/docker-publish.yml
vendored
18
.github/workflows/docker-publish.yml
vendored
@ -52,15 +52,15 @@ jobs:
|
|||||||
- glibc
|
- glibc
|
||||||
- musl
|
- musl
|
||||||
include:
|
include:
|
||||||
- { tag: alpine, base-image: alpine, arch: x86_64-unknown-linux-musl, plugin: true }
|
- { tag: alpine, base-image: alpine, arch: x86_64-unknown-linux-musl, plugin: true, use-patch: false}
|
||||||
- { tag: slim, base-image: 'debian:stable-slim', arch: x86_64-unknown-linux-gnu, plugin: true }
|
- { tag: slim, base-image: 'debian:stable-slim', arch: x86_64-unknown-linux-gnu, plugin: true, use-patch: false}
|
||||||
- { tag: debian, base-image: debian, arch: x86_64-unknown-linux-gnu, plugin: true }
|
- { tag: debian, base-image: debian, arch: x86_64-unknown-linux-gnu, plugin: true, use-patch: false}
|
||||||
- { tag: glibc-busybox, base-image: 'busybox:glibc', arch: x86_64-unknown-linux-gnu, use-patch: true }
|
- { tag: glibc-busybox, base-image: 'busybox:glibc', arch: x86_64-unknown-linux-gnu, plugin: false, use-patch: true }
|
||||||
- { tag: musl-busybox, base-image: 'busybox:musl', arch: x86_64-unknown-linux-musl, }
|
- { tag: musl-busybox, base-image: 'busybox:musl', arch: x86_64-unknown-linux-musl, plugin: false, use-patch: false}
|
||||||
- { tag: musl-distroless, base-image: 'gcr.io/distroless/static', arch: x86_64-unknown-linux-musl, }
|
- { tag: musl-distroless, base-image: 'gcr.io/distroless/static', arch: x86_64-unknown-linux-musl, plugin: false, use-patch: false}
|
||||||
- { tag: glibc-distroless, base-image: 'gcr.io/distroless/cc', arch: x86_64-unknown-linux-gnu, use-patch: true }
|
- { tag: glibc-distroless, base-image: 'gcr.io/distroless/cc', arch: x86_64-unknown-linux-gnu, plugin: false, use-patch: true }
|
||||||
- { tag: glibc, base-image: scratch, arch: x86_64-unknown-linux-gnu, }
|
- { tag: glibc, base-image: scratch, arch: x86_64-unknown-linux-gnu, plugin: false, use-patch: false}
|
||||||
- { tag: musl, base-image: scratch, arch: x86_64-unknown-linux-musl, }
|
- { tag: musl, base-image: scratch, arch: x86_64-unknown-linux-musl, plugin: false, use-patch: false}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- uses: actions/download-artifact@master
|
- uses: actions/download-artifact@master
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,6 +4,7 @@
|
|||||||
history.txt
|
history.txt
|
||||||
tests/fixtures/nuplayground
|
tests/fixtures/nuplayground
|
||||||
crates/*/target
|
crates/*/target
|
||||||
|
|
||||||
# Debian/Ubuntu
|
# Debian/Ubuntu
|
||||||
debian/.debhelper/
|
debian/.debhelper/
|
||||||
debian/debhelper-build-stamp
|
debian/debhelper-build-stamp
|
||||||
|
@ -26,3 +26,4 @@ vscode:
|
|||||||
- serayuzgur.crates@0.4.7:HMkoguLcXp9M3ud7ac3eIw==
|
- serayuzgur.crates@0.4.7:HMkoguLcXp9M3ud7ac3eIw==
|
||||||
- belfz.search-crates-io@1.2.1:kSLnyrOhXtYPjQpKnMr4eQ==
|
- belfz.search-crates-io@1.2.1:kSLnyrOhXtYPjQpKnMr4eQ==
|
||||||
- vadimcn.vscode-lldb@1.4.5:lwHCNwtm0kmOBXeQUIPGMQ==
|
- vadimcn.vscode-lldb@1.4.5:lwHCNwtm0kmOBXeQUIPGMQ==
|
||||||
|
- bungcip.better-toml@0.3.2:3QfgGxxYtGHfJKQU7H0nEw==
|
||||||
|
1004
Cargo.lock
generated
1004
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
189
Cargo.toml
189
Cargo.toml
@ -1,8 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "nu"
|
name = "nu"
|
||||||
version = "0.10.0"
|
version = "0.12.0"
|
||||||
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
authors = ["The Nu Project Contributors"]
|
||||||
description = "A shell for the GitHub era"
|
description = "A new kind of shell"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -13,137 +13,59 @@ documentation = "https://www.nushell.sh/book/"
|
|||||||
exclude = ["images"]
|
exclude = ["images"]
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
members = ["crates/*/"]
|
||||||
members = [
|
|
||||||
"crates/nu-macros",
|
|
||||||
"crates/nu-errors",
|
|
||||||
"crates/nu-source",
|
|
||||||
"crates/nu_plugin_average",
|
|
||||||
"crates/nu_plugin_binaryview",
|
|
||||||
"crates/nu_plugin_fetch",
|
|
||||||
"crates/nu_plugin_inc",
|
|
||||||
"crates/nu_plugin_match",
|
|
||||||
"crates/nu_plugin_post",
|
|
||||||
"crates/nu_plugin_ps",
|
|
||||||
"crates/nu_plugin_str",
|
|
||||||
"crates/nu_plugin_sum",
|
|
||||||
"crates/nu_plugin_sys",
|
|
||||||
"crates/nu_plugin_textview",
|
|
||||||
"crates/nu_plugin_tree",
|
|
||||||
"crates/nu-protocol",
|
|
||||||
"crates/nu-plugin",
|
|
||||||
"crates/nu-parser",
|
|
||||||
"crates/nu-value-ext",
|
|
||||||
"crates/nu-build"
|
|
||||||
]
|
|
||||||
|
|
||||||
# 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-source = { version = "0.10.0", path = "./crates/nu-source" }
|
nu-cli = { version = "0.12.0", path = "./crates/nu-cli" }
|
||||||
nu-plugin = { version = "0.10.0", path = "./crates/nu-plugin" }
|
nu-source = { version = "0.12.0", path = "./crates/nu-source" }
|
||||||
nu-protocol = { version = "0.10.0", path = "./crates/nu-protocol" }
|
nu-plugin = { version = "0.12.0", path = "./crates/nu-plugin" }
|
||||||
nu-errors = { version = "0.10.0", path = "./crates/nu-errors" }
|
nu-protocol = { version = "0.12.0", path = "./crates/nu-protocol" }
|
||||||
nu-parser = { version = "0.10.0", path = "./crates/nu-parser" }
|
nu-errors = { version = "0.12.0", path = "./crates/nu-errors" }
|
||||||
nu-value-ext = { version = "0.10.0", path = "./crates/nu-value-ext" }
|
nu-parser = { version = "0.12.0", path = "./crates/nu-parser" }
|
||||||
nu_plugin_average = { version = "0.10.0", path = "./crates/nu_plugin_average", optional=true }
|
nu-value-ext = { version = "0.12.0", path = "./crates/nu-value-ext" }
|
||||||
nu_plugin_binaryview = { version = "0.10.0", path = "./crates/nu_plugin_binaryview", optional=true }
|
nu_plugin_average = { version = "0.12.0", path = "./crates/nu_plugin_average", optional=true }
|
||||||
nu_plugin_fetch = { version = "0.10.0", path = "./crates/nu_plugin_fetch", optional=true }
|
nu_plugin_binaryview = { version = "0.12.0", path = "./crates/nu_plugin_binaryview", optional=true }
|
||||||
nu_plugin_inc = { version = "0.10.0", path = "./crates/nu_plugin_inc", optional=true }
|
nu_plugin_fetch = { version = "0.12.0", path = "./crates/nu_plugin_fetch", optional=true }
|
||||||
nu_plugin_match = { version = "0.10.0", path = "./crates/nu_plugin_match", optional=true }
|
nu_plugin_inc = { version = "0.12.0", path = "./crates/nu_plugin_inc", optional=true }
|
||||||
nu_plugin_post = { version = "0.10.0", path = "./crates/nu_plugin_post", optional=true }
|
nu_plugin_match = { version = "0.12.0", path = "./crates/nu_plugin_match", optional=true }
|
||||||
nu_plugin_ps = { version = "0.10.0", path = "./crates/nu_plugin_ps", optional=true }
|
nu_plugin_post = { version = "0.12.0", path = "./crates/nu_plugin_post", optional=true }
|
||||||
nu_plugin_str = { version = "0.10.0", path = "./crates/nu_plugin_str", optional=true }
|
nu_plugin_ps = { version = "0.12.0", path = "./crates/nu_plugin_ps", optional=true }
|
||||||
nu_plugin_sum = { version = "0.10.0", path = "./crates/nu_plugin_sum", optional=true }
|
nu_plugin_str = { version = "0.12.0", path = "./crates/nu_plugin_str", optional=true }
|
||||||
nu_plugin_sys = { version = "0.10.0", path = "./crates/nu_plugin_sys", optional=true }
|
nu_plugin_sys = { version = "0.12.0", path = "./crates/nu_plugin_sys", optional=true }
|
||||||
nu_plugin_textview = { version = "0.10.0", path = "./crates/nu_plugin_textview", optional=true }
|
nu_plugin_textview = { version = "0.12.0", path = "./crates/nu_plugin_textview", optional=true }
|
||||||
nu_plugin_tree = { version = "0.10.0", path = "./crates/nu_plugin_tree", optional=true }
|
nu_plugin_tree = { version = "0.12.0", path = "./crates/nu_plugin_tree", optional=true }
|
||||||
nu-macros = { version = "0.10.0", path = "./crates/nu-macros" }
|
nu-macros = { version = "0.12.0", path = "./crates/nu-macros" }
|
||||||
|
|
||||||
query_interface = "0.3.5"
|
crossterm = { version = "0.16.0", optional = true }
|
||||||
typetag = "0.1.4"
|
onig_sys = { version = "=69.1.0", optional = true }
|
||||||
rustyline = "6.0.0"
|
semver = { version = "0.9.0", optional = true }
|
||||||
chrono = { version = "0.4.10", features = ["serde"] }
|
syntect = { version = "3.2.0", optional = true }
|
||||||
derive-new = "0.5.8"
|
url = { version = "2.1.1", optional = true }
|
||||||
prettytable-rs = "0.8.0"
|
|
||||||
itertools = "0.8.2"
|
clap = "2.33.0"
|
||||||
ansi_term = "0.12.1"
|
ctrlc = "3.1.4"
|
||||||
nom = "5.0.1"
|
|
||||||
dunce = "1.0.0"
|
dunce = "1.0.0"
|
||||||
indexmap = { version = "1.3.2", features = ["serde-1"] }
|
|
||||||
byte-unit = "3.0.3"
|
|
||||||
base64 = "0.11"
|
|
||||||
futures = { version = "0.3", features = ["compat", "io-compat"] }
|
futures = { version = "0.3", features = ["compat", "io-compat"] }
|
||||||
async-stream = "0.2"
|
|
||||||
futures_codec = "0.4"
|
|
||||||
num-traits = "0.2.11"
|
|
||||||
term = "0.5.2"
|
|
||||||
bytes = "0.4.12"
|
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
serde = { version = "1.0.104", features = ["derive"] }
|
|
||||||
bson = { version = "0.14.0", features = ["decimal128"] }
|
[dev-dependencies]
|
||||||
serde_json = "1.0.47"
|
pretty_assertions = "0.6.1"
|
||||||
serde-hjson = "0.9.1"
|
nu-test-support = { version = "0.12.0", path = "./crates/nu-test-support" }
|
||||||
serde_yaml = "0.8"
|
|
||||||
serde_bytes = "0.11.3"
|
[build-dependencies]
|
||||||
getset = "0.0.9"
|
|
||||||
language-reporting = "0.4.0"
|
|
||||||
app_dirs = "1.2.1"
|
|
||||||
csv = "1.1"
|
|
||||||
toml = "0.5.6"
|
toml = "0.5.6"
|
||||||
clap = "2.33.0"
|
serde = { version = "1.0.105", features = ["derive"] }
|
||||||
git2 = { version = "0.11.0", default_features = false }
|
nu-build = { version = "0.12.0", path = "./crates/nu-build" }
|
||||||
dirs = "2.0.2"
|
|
||||||
glob = "0.3.0"
|
|
||||||
ctrlc = "3.1.3"
|
|
||||||
roxmltree = "0.9.1"
|
|
||||||
nom_locate = "1.0.0"
|
|
||||||
nom-tracable = "0.4.1"
|
|
||||||
unicode-xid = "0.2.0"
|
|
||||||
serde_ini = "0.2.0"
|
|
||||||
pretty-hex = "0.1.1"
|
|
||||||
hex = "0.4"
|
|
||||||
tempfile = "3.1.0"
|
|
||||||
which = "3.1.0"
|
|
||||||
ichwh = "0.3"
|
|
||||||
textwrap = {version = "0.11.0", features = ["term_size"]}
|
|
||||||
shellexpand = "1.1.1"
|
|
||||||
pin-utils = "0.1.0-alpha.4"
|
|
||||||
num-bigint = { version = "0.2.6", features = ["serde"] }
|
|
||||||
bigdecimal = { version = "0.1.0", features = ["serde"] }
|
|
||||||
serde_urlencoded = "0.6.1"
|
|
||||||
trash = "1.0.0"
|
|
||||||
regex = "1"
|
|
||||||
cfg-if = "0.1"
|
|
||||||
strip-ansi-escapes = "0.1.0"
|
|
||||||
calamine = "0.16"
|
|
||||||
umask = "0.1"
|
|
||||||
futures-util = "0.3.4"
|
|
||||||
termcolor = "1.1.0"
|
|
||||||
natural = "0.3.0"
|
|
||||||
parking_lot = "0.10.0"
|
|
||||||
meval = "0.2"
|
|
||||||
|
|
||||||
clipboard = {version = "0.5", optional = true }
|
|
||||||
ptree = {version = "0.2" }
|
|
||||||
starship = { version = "0.35.1", optional = true}
|
|
||||||
syntect = {version = "3.2.0", optional = true }
|
|
||||||
onig_sys = {version = "=69.1.0", optional = true }
|
|
||||||
crossterm = {version = "0.16.0", optional = true}
|
|
||||||
url = {version = "2.1.1", optional = true}
|
|
||||||
semver = {version = "0.9.0", optional = true}
|
|
||||||
filesize = "0.1.0"
|
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
|
||||||
users = "0.9"
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
# Test executables
|
# Test executables
|
||||||
test-bins = []
|
test-bins = []
|
||||||
|
|
||||||
default = ["sys", "ps", "textview", "inc", "str"]
|
default = ["sys", "ps", "textview", "inc", "str"]
|
||||||
stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "sum", "post", "fetch", "clipboard"]
|
stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "post", "fetch", "clipboard-cli"]
|
||||||
|
|
||||||
# Default
|
# Default
|
||||||
textview = ["crossterm", "syntect", "onig_sys", "url", "nu_plugin_textview"]
|
textview = ["crossterm", "syntect", "onig_sys", "url", "nu_plugin_textview"]
|
||||||
@ -158,28 +80,11 @@ binaryview = ["nu_plugin_binaryview"]
|
|||||||
fetch = ["nu_plugin_fetch"]
|
fetch = ["nu_plugin_fetch"]
|
||||||
match = ["nu_plugin_match"]
|
match = ["nu_plugin_match"]
|
||||||
post = ["nu_plugin_post"]
|
post = ["nu_plugin_post"]
|
||||||
starship-prompt = ["starship"]
|
|
||||||
sum = ["nu_plugin_sum"]
|
|
||||||
trace = ["nu-parser/trace"]
|
trace = ["nu-parser/trace"]
|
||||||
tree = ["nu_plugin_tree"]
|
tree = ["nu_plugin_tree"]
|
||||||
|
|
||||||
[dependencies.rusqlite]
|
clipboard-cli = ["nu-cli/clipboard-cli"]
|
||||||
version = "0.20.0"
|
starship-prompt = ["nu-cli/starship-prompt"]
|
||||||
features = ["bundled", "blob"]
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
pretty_assertions = "0.6.1"
|
|
||||||
nu-test-support = { version = "0.10.0", path = "./crates/nu-test-support" }
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
toml = "0.5.6"
|
|
||||||
serde = { version = "1.0.104", features = ["derive"] }
|
|
||||||
nu-build = { version = "0.10.0", path = "./crates/nu-build" }
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "nu"
|
|
||||||
doctest = false
|
|
||||||
path = "src/lib.rs"
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "fail"
|
name = "fail"
|
||||||
@ -201,6 +106,11 @@ name = "nonu"
|
|||||||
path = "crates/nu-test-support/src/bins/nonu.rs"
|
path = "crates/nu-test-support/src/bins/nonu.rs"
|
||||||
required-features = ["test-bins"]
|
required-features = ["test-bins"]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "iecho"
|
||||||
|
path = "crates/nu-test-support/src/bins/iecho.rs"
|
||||||
|
required-features = ["test-bins"]
|
||||||
|
|
||||||
# Core plugins that ship with `cargo install nu` by default
|
# Core plugins that ship with `cargo install nu` by default
|
||||||
# Currently, Cargo limits us to installing only one binary
|
# Currently, Cargo limits us to installing only one binary
|
||||||
# unless we use [[bin]], so we use this as a workaround
|
# unless we use [[bin]], so we use this as a workaround
|
||||||
@ -255,11 +165,6 @@ name = "nu_plugin_stable_post"
|
|||||||
path = "src/plugins/nu_plugin_stable_post.rs"
|
path = "src/plugins/nu_plugin_stable_post.rs"
|
||||||
required-features = ["post"]
|
required-features = ["post"]
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "nu_plugin_stable_sum"
|
|
||||||
path = "src/plugins/nu_plugin_stable_sum.rs"
|
|
||||||
required-features = ["sum"]
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "nu_plugin_stable_tree"
|
name = "nu_plugin_stable_tree"
|
||||||
path = "src/plugins/nu_plugin_stable_tree.rs"
|
path = "src/plugins/nu_plugin_stable_tree.rs"
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2019 Yehuda Katz, Jonathan Turner
|
Copyright (c) 2019 - 2020 Yehuda Katz, Jonathan Turner
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
64
README.md
64
README.md
@ -13,15 +13,22 @@ A new type of shell.
|
|||||||
|
|
||||||
# Status
|
# Status
|
||||||
|
|
||||||
This project has reached a minimum-viable product level of quality. While contributors dogfood it as their daily driver, it may be unstable for some commands. Future releases will work to fill out missing features and improve stability. Its design is also subject to change as it matures.
|
This project has reached a minimum-viable product level of quality.
|
||||||
|
While contributors dogfood it as their daily driver, it may be unstable for some commands.
|
||||||
|
Future releases will work to fill out missing features and improve stability.
|
||||||
|
Its design is also subject to change as it matures.
|
||||||
|
|
||||||
Nu comes with a set of built-in commands (listed below). If a command is unknown, the command will shell-out and execute it (using cmd on Windows or bash on Linux and macOS), correctly passing through stdin, stdout, and stderr, so things like your daily git workflows and even `vim` will work just fine.
|
Nu comes with a set of built-in commands (listed below).
|
||||||
|
If a command is unknown, the command will shell-out and execute it (using cmd on Windows or bash on Linux and macOS), correctly passing through stdin, stdout, and stderr, so things like your daily git workflows and even `vim` will work just fine.
|
||||||
|
|
||||||
# Learning more
|
# Learning more
|
||||||
|
|
||||||
There are a few good resources to learn about Nu. There is a [book](https://www.nushell.sh/book/) about Nu that is currently in progress. The book focuses on using Nu and its core concepts.
|
There are a few good resources to learn about Nu.
|
||||||
|
There is a [book](https://www.nushell.sh/book/) about Nu that is currently in progress.
|
||||||
|
The book focuses on using Nu and its core concepts.
|
||||||
|
|
||||||
If you're a developer who would like to contribute to Nu, we're also working on a [book for developers](https://www.nushell.sh/contributor-book/) to help you get started. There are also [good first issues](https://github.com/nushell/nushell/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22) to help you dive in.
|
If you're a developer who would like to contribute to Nu, we're also working on a [book for developers](https://www.nushell.sh/contributor-book/) to help you get started.
|
||||||
|
There are also [good first issues](https://github.com/nushell/nushell/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22) to help you dive in.
|
||||||
|
|
||||||
We also have an active [Discord](https://discord.gg/NtAbbGn) and [Twitter](https://twitter.com/nu_shell) if you'd like to come and chat with us.
|
We also have an active [Discord](https://discord.gg/NtAbbGn) and [Twitter](https://twitter.com/nu_shell) if you'd like to come and chat with us.
|
||||||
|
|
||||||
@ -63,6 +70,16 @@ cargo build --workspace --features=stable
|
|||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
|
### Quickstart
|
||||||
|
|
||||||
|
Want to try Nu right away? Execute the following to get started.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -it quay.io/nushell/nu:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Guide
|
||||||
|
|
||||||
If you want to pull a pre-built container, you can browse tags for the [nushell organization](https://quay.io/organization/nushell)
|
If you want to pull a pre-built container, you can browse tags for the [nushell organization](https://quay.io/organization/nushell)
|
||||||
on Quay.io. Pulling a container would come down to:
|
on Quay.io. Pulling a container would come down to:
|
||||||
|
|
||||||
@ -107,11 +124,18 @@ The second container is a bit smaller if the size is important to you.
|
|||||||
|
|
||||||
# Philosophy
|
# Philosophy
|
||||||
|
|
||||||
Nu draws inspiration from projects like PowerShell, functional programming languages, and modern CLI tools. Rather than thinking of files and services as raw streams of text, Nu looks at each input as something with structure. For example, when you list the contents of a directory, what you get back is a table of rows, where each row represents an item in that directory. These values can be piped through a series of steps, in a series of commands called a 'pipeline'.
|
Nu draws inspiration from projects like PowerShell, functional programming languages, and modern CLI tools.
|
||||||
|
Rather than thinking of files and services as raw streams of text, Nu looks at each input as something with structure.
|
||||||
|
For example, when you list the contents of a directory, what you get back is a table of rows, where each row represents an item in that directory.
|
||||||
|
These values can be piped through a series of steps, in a series of commands called a 'pipeline'.
|
||||||
|
|
||||||
## Pipelines
|
## Pipelines
|
||||||
|
|
||||||
In Unix, it's common to pipe between commands to split up a sophisticated command over multiple steps. Nu takes this a step further and builds heavily on the idea of _pipelines_. Just as the Unix philosophy, Nu allows commands to output from stdout and read from stdin. Additionally, commands can output structured data (you can think of this as a third kind of stream). Commands that work in the pipeline fit into one of three categories:
|
In Unix, it's common to pipe between commands to split up a sophisticated command over multiple steps.
|
||||||
|
Nu takes this a step further and builds heavily on the idea of _pipelines_.
|
||||||
|
Just as the Unix philosophy, Nu allows commands to output from stdout and read from stdin.
|
||||||
|
Additionally, commands can output structured data (you can think of this as a third kind of stream).
|
||||||
|
Commands that work in the pipeline fit into one of three categories:
|
||||||
|
|
||||||
* Commands that produce a stream (eg, `ls`)
|
* Commands that produce a stream (eg, `ls`)
|
||||||
* Commands that filter a stream (eg, `where type == "Directory"`)
|
* Commands that filter a stream (eg, `where type == "Directory"`)
|
||||||
@ -135,13 +159,15 @@ Commands are separated by the pipe symbol (`|`) to denote a pipeline flowing lef
|
|||||||
────┴───────────┴───────────┴──────────┴────────┴──────────────┴────────────────
|
────┴───────────┴───────────┴──────────┴────────┴──────────────┴────────────────
|
||||||
```
|
```
|
||||||
|
|
||||||
Because most of the time you'll want to see the output of a pipeline, `autoview` is assumed. We could have also written the above:
|
Because most of the time you'll want to see the output of a pipeline, `autoview` is assumed.
|
||||||
|
We could have also written the above:
|
||||||
|
|
||||||
```
|
```
|
||||||
/home/jonathan/Source/nushell(master)> ls | where type == Directory
|
/home/jonathan/Source/nushell(master)> ls | where type == Directory
|
||||||
```
|
```
|
||||||
|
|
||||||
Being able to use the same commands and compose them differently is an important philosophy in Nu. For example, we could use the built-in `ps` command as well to get a list of the running processes, using the same `where` as above.
|
Being able to use the same commands and compose them differently is an important philosophy in Nu.
|
||||||
|
For example, we could use the built-in `ps` command as well to get a list of the running processes, using the same `where` as above.
|
||||||
|
|
||||||
```text
|
```text
|
||||||
/home/jonathan/Source/nushell(master)> ps | where cpu > 0
|
/home/jonathan/Source/nushell(master)> ps | where cpu > 0
|
||||||
@ -157,7 +183,8 @@ Being able to use the same commands and compose them differently is an important
|
|||||||
|
|
||||||
## Opening files
|
## Opening files
|
||||||
|
|
||||||
Nu can load file and URL contents as raw text or as structured data (if it recognizes the format). For example, you can load a .toml file as structured data and explore it:
|
Nu can load file and URL contents as raw text or as structured data (if it recognizes the format).
|
||||||
|
For example, you can load a .toml file as structured data and explore it:
|
||||||
|
|
||||||
```
|
```
|
||||||
/home/jonathan/Source/nushell(master)> open Cargo.toml
|
/home/jonathan/Source/nushell(master)> open Cargo.toml
|
||||||
@ -210,19 +237,26 @@ To set one of these variables, you can use `config --set`. For example:
|
|||||||
|
|
||||||
## Shells
|
## Shells
|
||||||
|
|
||||||
Nu will work inside of a single directory and allow you to navigate around your filesystem by default. Nu also offers a way of adding additional working directories that you can jump between, allowing you to work in multiple directories at the same time.
|
Nu will work inside of a single directory and allow you to navigate around your filesystem by default.
|
||||||
|
Nu also offers a way of adding additional working directories that you can jump between, allowing you to work in multiple directories at the same time.
|
||||||
|
|
||||||
To do so, use the `enter` command, which will allow you create a new "shell" and enter it at the specified path. You can toggle between this new shell and the original shell with the `p` (for previous) and `n` (for next), allowing you to navigate around a ring buffer of shells. Once you're done with a shell, you can `exit` it and remove it from the ring buffer.
|
To do so, use the `enter` command, which will allow you create a new "shell" and enter it at the specified path.
|
||||||
|
You can toggle between this new shell and the original shell with the `p` (for previous) and `n` (for next), allowing you to navigate around a ring buffer of shells.
|
||||||
|
Once you're done with a shell, you can `exit` it and remove it from the ring buffer.
|
||||||
|
|
||||||
Finally, to get a list of all the current shells, you can use the `shells` command.
|
Finally, to get a list of all the current shells, you can use the `shells` command.
|
||||||
|
|
||||||
## Plugins
|
## Plugins
|
||||||
|
|
||||||
Nu supports plugins that offer additional functionality to the shell and follow the same structured data model that built-in commands use. This allows you to extend nu for your needs.
|
Nu supports plugins that offer additional functionality to the shell and follow the same structured data model that built-in commands use.
|
||||||
|
This allows you to extend nu for your needs.
|
||||||
|
|
||||||
There are a few examples in the `plugins` directory.
|
There are a few examples in the `plugins` directory.
|
||||||
|
|
||||||
Plugins are binaries that are available in your path and follow a `nu_plugin_*` naming convention. These binaries interact with nu via a simple JSON-RPC protocol where the command identifies itself and passes along its configuration, which then makes it available for use. If the plugin is a filter, data streams to it one element at a time, and it can stream data back in return via stdin/stdout. If the plugin is a sink, it is given the full vector of final data and is given free reign over stdin/stdout to use as it pleases.
|
Plugins are binaries that are available in your path and follow a `nu_plugin_*` naming convention.
|
||||||
|
These binaries interact with nu via a simple JSON-RPC protocol where the command identifies itself and passes along its configuration, which then makes it available for use.
|
||||||
|
If the plugin is a filter, data streams to it one element at a time, and it can stream data back in return via stdin/stdout.
|
||||||
|
If the plugin is a sink, it is given the full vector of final data and is given free reign over stdin/stdout to use as it pleases.
|
||||||
|
|
||||||
# Goals
|
# Goals
|
||||||
|
|
||||||
@ -240,9 +274,9 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
|
|||||||
|
|
||||||
# Commands
|
# Commands
|
||||||
|
|
||||||
You can find a list of Nu commands, complete with documentation, in [quick command references](https://www.nushell.sh/documentation.html#quick-command-references).
|
You can find a list of Nu commands, complete with documentation, in [quick command references](https://www.nushell.sh/documentation.html#quick-command-references).
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
The project is made available under the MIT license. See "LICENSE" for more information.
|
The project is made available under the MIT license. See the `LICENSE` file for more information.
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "nu-build"
|
name = "nu-build"
|
||||||
version = "0.10.0"
|
version = "0.12.0"
|
||||||
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Core build system for nushell"
|
description = "Core build system for nushell"
|
||||||
|
110
crates/nu-cli/Cargo.toml
Normal file
110
crates/nu-cli/Cargo.toml
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
[package]
|
||||||
|
name = "nu-cli"
|
||||||
|
version = "0.12.0"
|
||||||
|
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||||
|
description = "CLI for nushell"
|
||||||
|
edition = "2018"
|
||||||
|
license = "MIT"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
doctest = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nu-source = { version = "0.12.0", path = "../nu-source" }
|
||||||
|
nu-plugin = { version = "0.12.0", path = "../nu-plugin" }
|
||||||
|
nu-protocol = { version = "0.12.0", path = "../nu-protocol" }
|
||||||
|
nu-errors = { version = "0.12.0", path = "../nu-errors" }
|
||||||
|
nu-parser = { version = "0.12.0", path = "../nu-parser" }
|
||||||
|
nu-value-ext = { version = "0.12.0", path = "../nu-value-ext" }
|
||||||
|
nu-macros = { version = "0.12.0", path = "../nu-macros" }
|
||||||
|
nu-test-support = { version = "0.12.0", path = "../nu-test-support" }
|
||||||
|
|
||||||
|
ansi_term = "0.12.1"
|
||||||
|
app_dirs = "1.2.1"
|
||||||
|
async-stream = "0.2"
|
||||||
|
base64 = "0.12.0"
|
||||||
|
bigdecimal = { version = "0.1.0", features = ["serde"] }
|
||||||
|
bson = { version = "0.14.1", features = ["decimal128"] }
|
||||||
|
byte-unit = "3.0.3"
|
||||||
|
bytes = "0.5.4"
|
||||||
|
calamine = "0.16"
|
||||||
|
cfg-if = "0.1"
|
||||||
|
chrono = { version = "0.4.11", features = ["serde"] }
|
||||||
|
clap = "2.33.0"
|
||||||
|
csv = "1.1"
|
||||||
|
ctrlc = "3.1.4"
|
||||||
|
derive-new = "0.5.8"
|
||||||
|
dirs = "2.0.2"
|
||||||
|
dunce = "1.0.0"
|
||||||
|
filesize = "0.1.0"
|
||||||
|
futures = { version = "0.3", features = ["compat", "io-compat"] }
|
||||||
|
futures-util = "0.3.4"
|
||||||
|
futures_codec = "0.4"
|
||||||
|
getset = "0.1.0"
|
||||||
|
git2 = { version = "0.13.0", default_features = false }
|
||||||
|
glob = "0.3.0"
|
||||||
|
hex = "0.4"
|
||||||
|
htmlescape = "0.3.1"
|
||||||
|
ical = "0.6.*"
|
||||||
|
ichwh = "0.3"
|
||||||
|
indexmap = { version = "1.3.2", features = ["serde-1"] }
|
||||||
|
itertools = "0.9.0"
|
||||||
|
language-reporting = "0.4.0"
|
||||||
|
log = "0.4.8"
|
||||||
|
meval = "0.2"
|
||||||
|
natural = "0.5.0"
|
||||||
|
nom = "5.0.1"
|
||||||
|
nom-tracable = "0.4.1"
|
||||||
|
nom_locate = "1.0.0"
|
||||||
|
num-bigint = { version = "0.2.6", features = ["serde"] }
|
||||||
|
num-traits = "0.2.11"
|
||||||
|
parking_lot = "0.10.0"
|
||||||
|
pin-utils = "0.1.0-alpha.4"
|
||||||
|
pretty-hex = "0.1.1"
|
||||||
|
pretty_env_logger = "0.4.0"
|
||||||
|
prettytable-rs = "0.8.0"
|
||||||
|
ptree = {version = "0.2" }
|
||||||
|
query_interface = "0.3.5"
|
||||||
|
rand = "0.7"
|
||||||
|
regex = "1"
|
||||||
|
roxmltree = "0.10.0"
|
||||||
|
rustyline = "6.0.0"
|
||||||
|
serde = { version = "1.0.105", features = ["derive"] }
|
||||||
|
serde-hjson = "0.9.1"
|
||||||
|
serde_bytes = "0.11.3"
|
||||||
|
serde_ini = "0.2.0"
|
||||||
|
serde_json = "1.0.48"
|
||||||
|
serde_urlencoded = "0.6.1"
|
||||||
|
serde_yaml = "0.8"
|
||||||
|
shellexpand = "2.0.0"
|
||||||
|
strip-ansi-escapes = "0.1.0"
|
||||||
|
tempfile = "3.1.0"
|
||||||
|
term = "0.5.2"
|
||||||
|
termcolor = "1.1.0"
|
||||||
|
textwrap = {version = "0.11.0", features = ["term_size"]}
|
||||||
|
toml = "0.5.6"
|
||||||
|
trash = "1.0.0"
|
||||||
|
typetag = "0.1.4"
|
||||||
|
umask = "0.1"
|
||||||
|
unicode-xid = "0.2.0"
|
||||||
|
|
||||||
|
clipboard = { version = "0.5", optional = true }
|
||||||
|
starship = { version = "0.38.0", optional = true }
|
||||||
|
|
||||||
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
users = "0.9"
|
||||||
|
|
||||||
|
[dependencies.rusqlite]
|
||||||
|
version = "0.21.0"
|
||||||
|
features = ["bundled", "blob"]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
pretty_assertions = "0.6.1"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
nu-build = { version = "0.12.0", path = "../nu-build" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
stable = []
|
||||||
|
starship-prompt = ["starship"]
|
||||||
|
clipboard-cli = ["clipboard"]
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::commands::classified::external::{MaybeTextCodec, StringOrBinary};
|
||||||
use crate::commands::classified::pipeline::run_pipeline;
|
use crate::commands::classified::pipeline::run_pipeline;
|
||||||
use crate::commands::plugin::JsonRpc;
|
use crate::commands::plugin::JsonRpc;
|
||||||
use crate::commands::plugin::{PluginCommand, PluginSink};
|
use crate::commands::plugin::{PluginCommand, PluginSink};
|
||||||
@ -6,9 +7,13 @@ use crate::context::Context;
|
|||||||
#[cfg(not(feature = "starship-prompt"))]
|
#[cfg(not(feature = "starship-prompt"))]
|
||||||
use crate::git::current_branch;
|
use crate::git::current_branch;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use futures_codec::{FramedRead, LinesCodec};
|
use futures_codec::FramedRead;
|
||||||
|
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_parser::{ClassifiedPipeline, PipelineShape, SpannedToken, TokensIterator};
|
use nu_parser::{
|
||||||
|
ClassifiedCommand, ClassifiedPipeline, ExternalCommand, PipelineShape, SpannedToken,
|
||||||
|
TokensIterator,
|
||||||
|
};
|
||||||
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
||||||
|
|
||||||
use log::{debug, log_enabled, trace};
|
use log::{debug, log_enabled, trace};
|
||||||
@ -93,44 +98,27 @@ fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), Shel
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_paths() -> Vec<std::path::PathBuf> {
|
fn search_paths() -> Vec<std::path::PathBuf> {
|
||||||
|
use std::env;
|
||||||
|
|
||||||
let mut search_paths = Vec::new();
|
let mut search_paths = Vec::new();
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
// Automatically add path `nu` is in as a search path
|
||||||
{
|
if let Ok(exe_path) = env::current_exe() {
|
||||||
// Use our debug plugins in debug mode
|
if let Some(exe_dir) = exe_path.parent() {
|
||||||
let mut path = std::path::PathBuf::from(".");
|
search_paths.push(exe_dir.to_path_buf());
|
||||||
path.push("target");
|
|
||||||
path.push("debug");
|
|
||||||
|
|
||||||
if path.exists() {
|
|
||||||
search_paths.push(path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
{
|
{
|
||||||
use std::env;
|
|
||||||
|
|
||||||
match env::var_os("PATH") {
|
match env::var_os("PATH") {
|
||||||
Some(paths) => {
|
Some(paths) => {
|
||||||
search_paths = env::split_paths(&paths).collect::<Vec<_>>();
|
search_paths.extend(env::split_paths(&paths).collect::<Vec<_>>());
|
||||||
}
|
}
|
||||||
None => println!("PATH is not defined in the environment."),
|
None => println!("PATH is not defined in the environment."),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use our release plugins in release mode
|
|
||||||
let mut path = std::path::PathBuf::from(".");
|
|
||||||
path.push("target");
|
|
||||||
path.push("release");
|
|
||||||
|
|
||||||
if path.exists() {
|
|
||||||
search_paths.push(path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// permit Nu finding and picking up development plugins
|
|
||||||
// if there are any first.
|
|
||||||
search_paths.reverse();
|
|
||||||
search_paths
|
search_paths
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,13 +243,13 @@ pub fn create_default_context(
|
|||||||
per_item_command(Ls),
|
per_item_command(Ls),
|
||||||
per_item_command(Du),
|
per_item_command(Du),
|
||||||
whole_stream_command(Cd),
|
whole_stream_command(Cd),
|
||||||
whole_stream_command(Env),
|
|
||||||
per_item_command(Remove),
|
per_item_command(Remove),
|
||||||
per_item_command(Open),
|
per_item_command(Open),
|
||||||
whole_stream_command(Config),
|
whole_stream_command(Config),
|
||||||
per_item_command(Help),
|
per_item_command(Help),
|
||||||
per_item_command(History),
|
per_item_command(History),
|
||||||
whole_stream_command(Save),
|
whole_stream_command(Save),
|
||||||
|
per_item_command(Touch),
|
||||||
per_item_command(Cpy),
|
per_item_command(Cpy),
|
||||||
whole_stream_command(Date),
|
whole_stream_command(Date),
|
||||||
per_item_command(Calc),
|
per_item_command(Calc),
|
||||||
@ -317,18 +305,24 @@ pub fn create_default_context(
|
|||||||
whole_stream_command(Default),
|
whole_stream_command(Default),
|
||||||
whole_stream_command(SkipWhile),
|
whole_stream_command(SkipWhile),
|
||||||
whole_stream_command(Range),
|
whole_stream_command(Range),
|
||||||
|
whole_stream_command(Rename),
|
||||||
whole_stream_command(Uniq),
|
whole_stream_command(Uniq),
|
||||||
// Table manipulation
|
// Table manipulation
|
||||||
|
whole_stream_command(Shuffle),
|
||||||
whole_stream_command(Wrap),
|
whole_stream_command(Wrap),
|
||||||
whole_stream_command(Pivot),
|
whole_stream_command(Pivot),
|
||||||
|
whole_stream_command(Headers),
|
||||||
// Data processing
|
// Data processing
|
||||||
whole_stream_command(Histogram),
|
whole_stream_command(Histogram),
|
||||||
|
whole_stream_command(Sum),
|
||||||
// File format output
|
// File format output
|
||||||
whole_stream_command(ToBSON),
|
whole_stream_command(ToBSON),
|
||||||
whole_stream_command(ToCSV),
|
whole_stream_command(ToCSV),
|
||||||
|
whole_stream_command(ToHTML),
|
||||||
whole_stream_command(ToJSON),
|
whole_stream_command(ToJSON),
|
||||||
whole_stream_command(ToSQLite),
|
whole_stream_command(ToSQLite),
|
||||||
whole_stream_command(ToDB),
|
whole_stream_command(ToDB),
|
||||||
|
whole_stream_command(ToMarkdown),
|
||||||
whole_stream_command(ToTOML),
|
whole_stream_command(ToTOML),
|
||||||
whole_stream_command(ToTSV),
|
whole_stream_command(ToTSV),
|
||||||
whole_stream_command(ToURL),
|
whole_stream_command(ToURL),
|
||||||
@ -349,6 +343,8 @@ pub fn create_default_context(
|
|||||||
whole_stream_command(FromXML),
|
whole_stream_command(FromXML),
|
||||||
whole_stream_command(FromYAML),
|
whole_stream_command(FromYAML),
|
||||||
whole_stream_command(FromYML),
|
whole_stream_command(FromYML),
|
||||||
|
whole_stream_command(FromIcs),
|
||||||
|
whole_stream_command(FromVcf),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
@ -378,7 +374,7 @@ pub async fn run_pipeline_standalone(
|
|||||||
redirect_stdin: bool,
|
redirect_stdin: bool,
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
let line = process_line(Ok(pipeline), context, redirect_stdin).await;
|
let line = process_line(Ok(pipeline), context, redirect_stdin, false).await;
|
||||||
|
|
||||||
match line {
|
match line {
|
||||||
LineResult::Success(line) => {
|
LineResult::Success(line) => {
|
||||||
@ -527,7 +523,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
|||||||
initial_command = None;
|
initial_command = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let line = process_line(readline, &mut context, false).await;
|
let line = process_line(readline, &mut context, false, true).await;
|
||||||
|
|
||||||
// Check the config to see if we need to update the path
|
// Check the config to see if we need to update the path
|
||||||
// TODO: make sure config is cached so we don't path this load every call
|
// TODO: make sure config is cached so we don't path this load every call
|
||||||
@ -610,6 +606,7 @@ async fn process_line(
|
|||||||
readline: Result<String, ReadlineError>,
|
readline: Result<String, ReadlineError>,
|
||||||
ctx: &mut Context,
|
ctx: &mut Context,
|
||||||
redirect_stdin: bool,
|
redirect_stdin: bool,
|
||||||
|
cli_mode: bool,
|
||||||
) -> LineResult {
|
) -> LineResult {
|
||||||
match &readline {
|
match &readline {
|
||||||
Ok(line) if line.trim() == "" => LineResult::Success(line.clone()),
|
Ok(line) if line.trim() == "" => LineResult::Success(line.clone()),
|
||||||
@ -628,22 +625,84 @@ async fn process_line(
|
|||||||
debug!("=== Parsed ===");
|
debug!("=== Parsed ===");
|
||||||
debug!("{:#?}", result);
|
debug!("{:#?}", result);
|
||||||
|
|
||||||
let pipeline = classify_pipeline(&result, ctx, &Text::from(line));
|
let pipeline = classify_pipeline(&result, &ctx, &Text::from(line));
|
||||||
|
|
||||||
if let Some(failure) = pipeline.failed {
|
if let Some(failure) = pipeline.failed {
|
||||||
return LineResult::Error(line.to_string(), failure.into());
|
return LineResult::Error(line.to_string(), failure.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// There's a special case to check before we process the pipeline:
|
||||||
|
// If we're giving a path by itself
|
||||||
|
// ...and it's not a command in the path
|
||||||
|
// ...and it doesn't have any arguments
|
||||||
|
// ...and we're in the CLI
|
||||||
|
// ...then change to this directory
|
||||||
|
if cli_mode && pipeline.commands.list.len() == 1 {
|
||||||
|
if let ClassifiedCommand::External(ExternalCommand {
|
||||||
|
ref name, ref args, ..
|
||||||
|
}) = pipeline.commands.list[0]
|
||||||
|
{
|
||||||
|
if dunce::canonicalize(name).is_ok()
|
||||||
|
&& PathBuf::from(name).is_dir()
|
||||||
|
&& ichwh::which(name).await.unwrap_or(None).is_none()
|
||||||
|
&& args.list.is_empty()
|
||||||
|
{
|
||||||
|
// Here we work differently if we're in Windows because of the expected Windows behavior
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
if name.ends_with(':') {
|
||||||
|
// This looks like a drive shortcut. We need to a) switch drives and b) go back to the previous directory we were viewing on that drive
|
||||||
|
// But first, we need to save where we are now
|
||||||
|
let current_path = ctx.shell_manager.path();
|
||||||
|
|
||||||
|
let split_path: Vec<_> = current_path.split(':').collect();
|
||||||
|
if split_path.len() > 1 {
|
||||||
|
ctx.windows_drives_previous_cwd
|
||||||
|
.lock()
|
||||||
|
.insert(split_path[0].to_string(), current_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
let name = name.to_uppercase();
|
||||||
|
let new_drive: Vec<_> = name.split(':').collect();
|
||||||
|
|
||||||
|
if let Some(val) =
|
||||||
|
ctx.windows_drives_previous_cwd.lock().get(new_drive[0])
|
||||||
|
{
|
||||||
|
ctx.shell_manager.set_path(val.to_string());
|
||||||
|
return LineResult::Success(line.to_string());
|
||||||
|
} else {
|
||||||
|
ctx.shell_manager.set_path(name.to_string());
|
||||||
|
return LineResult::Success(line.to_string());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctx.shell_manager.set_path(name.to_string());
|
||||||
|
return LineResult::Success(line.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
{
|
||||||
|
ctx.shell_manager.set_path(name.to_string());
|
||||||
|
return LineResult::Success(line.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let input_stream = if redirect_stdin {
|
let input_stream = if redirect_stdin {
|
||||||
let file = futures::io::AllowStdIo::new(
|
let file = futures::io::AllowStdIo::new(std::io::stdin());
|
||||||
crate::commands::classified::external::StdoutWithNewline::new(std::io::stdin()),
|
let stream = FramedRead::new(file, MaybeTextCodec).map(|line| {
|
||||||
);
|
|
||||||
let stream = FramedRead::new(file, LinesCodec).map(|line| {
|
|
||||||
if let Ok(line) = line {
|
if let Ok(line) = line {
|
||||||
Ok(Value {
|
match line {
|
||||||
value: UntaggedValue::Primitive(Primitive::String(line)),
|
StringOrBinary::String(s) => Ok(Value {
|
||||||
tag: Tag::unknown(),
|
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||||
})
|
tag: Tag::unknown(),
|
||||||
|
}),
|
||||||
|
StringOrBinary::Binary(b) => Ok(Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::Binary(
|
||||||
|
b.into_iter().collect(),
|
||||||
|
)),
|
||||||
|
tag: Tag::unknown(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("Internal error: could not read lines of text from stdin")
|
panic!("Internal error: could not read lines of text from stdin")
|
||||||
}
|
}
|
||||||
@ -682,9 +741,8 @@ async fn process_line(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
Ok(None) => break,
|
||||||
break;
|
Err(e) => return LineResult::Error(line.to_string(), e),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,7 +23,6 @@ pub(crate) mod du;
|
|||||||
pub(crate) mod echo;
|
pub(crate) mod echo;
|
||||||
pub(crate) mod edit;
|
pub(crate) mod edit;
|
||||||
pub(crate) mod enter;
|
pub(crate) mod enter;
|
||||||
pub(crate) mod env;
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub(crate) mod evaluate_by;
|
pub(crate) mod evaluate_by;
|
||||||
pub(crate) mod exit;
|
pub(crate) mod exit;
|
||||||
@ -31,6 +30,7 @@ pub(crate) mod first;
|
|||||||
pub(crate) mod format;
|
pub(crate) mod format;
|
||||||
pub(crate) mod from_bson;
|
pub(crate) mod from_bson;
|
||||||
pub(crate) mod from_csv;
|
pub(crate) mod from_csv;
|
||||||
|
pub(crate) mod from_ics;
|
||||||
pub(crate) mod from_ini;
|
pub(crate) mod from_ini;
|
||||||
pub(crate) mod from_json;
|
pub(crate) mod from_json;
|
||||||
pub(crate) mod from_ods;
|
pub(crate) mod from_ods;
|
||||||
@ -39,11 +39,13 @@ pub(crate) mod from_ssv;
|
|||||||
pub(crate) mod from_toml;
|
pub(crate) mod from_toml;
|
||||||
pub(crate) mod from_tsv;
|
pub(crate) mod from_tsv;
|
||||||
pub(crate) mod from_url;
|
pub(crate) mod from_url;
|
||||||
|
pub(crate) mod from_vcf;
|
||||||
pub(crate) mod from_xlsx;
|
pub(crate) mod from_xlsx;
|
||||||
pub(crate) mod from_xml;
|
pub(crate) mod from_xml;
|
||||||
pub(crate) mod from_yaml;
|
pub(crate) mod from_yaml;
|
||||||
pub(crate) mod get;
|
pub(crate) mod get;
|
||||||
pub(crate) mod group_by;
|
pub(crate) mod group_by;
|
||||||
|
pub(crate) mod headers;
|
||||||
pub(crate) mod help;
|
pub(crate) mod help;
|
||||||
pub(crate) mod histogram;
|
pub(crate) mod histogram;
|
||||||
pub(crate) mod history;
|
pub(crate) mod history;
|
||||||
@ -69,10 +71,12 @@ pub(crate) mod range;
|
|||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub(crate) mod reduce_by;
|
pub(crate) mod reduce_by;
|
||||||
pub(crate) mod reject;
|
pub(crate) mod reject;
|
||||||
|
pub(crate) mod rename;
|
||||||
pub(crate) mod reverse;
|
pub(crate) mod reverse;
|
||||||
pub(crate) mod rm;
|
pub(crate) mod rm;
|
||||||
pub(crate) mod save;
|
pub(crate) mod save;
|
||||||
pub(crate) mod shells;
|
pub(crate) mod shells;
|
||||||
|
pub(crate) mod shuffle;
|
||||||
pub(crate) mod size;
|
pub(crate) mod size;
|
||||||
pub(crate) mod skip;
|
pub(crate) mod skip;
|
||||||
pub(crate) mod skip_while;
|
pub(crate) mod skip_while;
|
||||||
@ -80,13 +84,16 @@ pub(crate) mod sort_by;
|
|||||||
pub(crate) mod split_by;
|
pub(crate) mod split_by;
|
||||||
pub(crate) mod split_column;
|
pub(crate) mod split_column;
|
||||||
pub(crate) mod split_row;
|
pub(crate) mod split_row;
|
||||||
|
pub(crate) mod sum;
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub(crate) mod t_sort_by;
|
pub(crate) mod t_sort_by;
|
||||||
pub(crate) mod table;
|
pub(crate) mod table;
|
||||||
pub(crate) mod tags;
|
pub(crate) mod tags;
|
||||||
pub(crate) mod to_bson;
|
pub(crate) mod to_bson;
|
||||||
pub(crate) mod to_csv;
|
pub(crate) mod to_csv;
|
||||||
|
pub(crate) mod to_html;
|
||||||
pub(crate) mod to_json;
|
pub(crate) mod to_json;
|
||||||
|
pub(crate) mod to_md;
|
||||||
pub(crate) mod to_sqlite;
|
pub(crate) mod to_sqlite;
|
||||||
pub(crate) mod to_toml;
|
pub(crate) mod to_toml;
|
||||||
pub(crate) mod to_tsv;
|
pub(crate) mod to_tsv;
|
||||||
@ -123,8 +130,8 @@ pub(crate) mod kill;
|
|||||||
pub(crate) use kill::Kill;
|
pub(crate) use kill::Kill;
|
||||||
pub(crate) mod clear;
|
pub(crate) mod clear;
|
||||||
pub(crate) use clear::Clear;
|
pub(crate) use clear::Clear;
|
||||||
|
pub(crate) mod touch;
|
||||||
pub(crate) use enter::Enter;
|
pub(crate) use enter::Enter;
|
||||||
pub(crate) use env::Env;
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub(crate) use evaluate_by::EvaluateBy;
|
pub(crate) use evaluate_by::EvaluateBy;
|
||||||
pub(crate) use exit::Exit;
|
pub(crate) use exit::Exit;
|
||||||
@ -132,6 +139,7 @@ pub(crate) use first::First;
|
|||||||
pub(crate) use format::Format;
|
pub(crate) use format::Format;
|
||||||
pub(crate) use from_bson::FromBSON;
|
pub(crate) use from_bson::FromBSON;
|
||||||
pub(crate) use from_csv::FromCSV;
|
pub(crate) use from_csv::FromCSV;
|
||||||
|
pub(crate) use from_ics::FromIcs;
|
||||||
pub(crate) use from_ini::FromINI;
|
pub(crate) use from_ini::FromINI;
|
||||||
pub(crate) use from_json::FromJSON;
|
pub(crate) use from_json::FromJSON;
|
||||||
pub(crate) use from_ods::FromODS;
|
pub(crate) use from_ods::FromODS;
|
||||||
@ -141,12 +149,14 @@ pub(crate) use from_ssv::FromSSV;
|
|||||||
pub(crate) use from_toml::FromTOML;
|
pub(crate) use from_toml::FromTOML;
|
||||||
pub(crate) use from_tsv::FromTSV;
|
pub(crate) use from_tsv::FromTSV;
|
||||||
pub(crate) use from_url::FromURL;
|
pub(crate) use from_url::FromURL;
|
||||||
|
pub(crate) use from_vcf::FromVcf;
|
||||||
pub(crate) use from_xlsx::FromXLSX;
|
pub(crate) use from_xlsx::FromXLSX;
|
||||||
pub(crate) use from_xml::FromXML;
|
pub(crate) use from_xml::FromXML;
|
||||||
pub(crate) use from_yaml::FromYAML;
|
pub(crate) use from_yaml::FromYAML;
|
||||||
pub(crate) use from_yaml::FromYML;
|
pub(crate) use from_yaml::FromYML;
|
||||||
pub(crate) use get::Get;
|
pub(crate) use get::Get;
|
||||||
pub(crate) use group_by::GroupBy;
|
pub(crate) use group_by::GroupBy;
|
||||||
|
pub(crate) use headers::Headers;
|
||||||
pub(crate) use help::Help;
|
pub(crate) use help::Help;
|
||||||
pub(crate) use histogram::Histogram;
|
pub(crate) use histogram::Histogram;
|
||||||
pub(crate) use history::History;
|
pub(crate) use history::History;
|
||||||
@ -171,10 +181,12 @@ pub(crate) use range::Range;
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub(crate) use reduce_by::ReduceBy;
|
pub(crate) use reduce_by::ReduceBy;
|
||||||
pub(crate) use reject::Reject;
|
pub(crate) use reject::Reject;
|
||||||
|
pub(crate) use rename::Rename;
|
||||||
pub(crate) use reverse::Reverse;
|
pub(crate) use reverse::Reverse;
|
||||||
pub(crate) use rm::Remove;
|
pub(crate) use rm::Remove;
|
||||||
pub(crate) use save::Save;
|
pub(crate) use save::Save;
|
||||||
pub(crate) use shells::Shells;
|
pub(crate) use shells::Shells;
|
||||||
|
pub(crate) use shuffle::Shuffle;
|
||||||
pub(crate) use size::Size;
|
pub(crate) use size::Size;
|
||||||
pub(crate) use skip::Skip;
|
pub(crate) use skip::Skip;
|
||||||
pub(crate) use skip_while::SkipWhile;
|
pub(crate) use skip_while::SkipWhile;
|
||||||
@ -182,19 +194,23 @@ pub(crate) use sort_by::SortBy;
|
|||||||
pub(crate) use split_by::SplitBy;
|
pub(crate) use split_by::SplitBy;
|
||||||
pub(crate) use split_column::SplitColumn;
|
pub(crate) use split_column::SplitColumn;
|
||||||
pub(crate) use split_row::SplitRow;
|
pub(crate) use split_row::SplitRow;
|
||||||
|
pub(crate) use sum::Sum;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub(crate) use t_sort_by::TSortBy;
|
pub(crate) use t_sort_by::TSortBy;
|
||||||
pub(crate) use table::Table;
|
pub(crate) use table::Table;
|
||||||
pub(crate) use tags::Tags;
|
pub(crate) use tags::Tags;
|
||||||
pub(crate) use to_bson::ToBSON;
|
pub(crate) use to_bson::ToBSON;
|
||||||
pub(crate) use to_csv::ToCSV;
|
pub(crate) use to_csv::ToCSV;
|
||||||
|
pub(crate) use to_html::ToHTML;
|
||||||
pub(crate) use to_json::ToJSON;
|
pub(crate) use to_json::ToJSON;
|
||||||
|
pub(crate) use to_md::ToMarkdown;
|
||||||
pub(crate) use to_sqlite::ToDB;
|
pub(crate) use to_sqlite::ToDB;
|
||||||
pub(crate) use to_sqlite::ToSQLite;
|
pub(crate) use to_sqlite::ToSQLite;
|
||||||
pub(crate) use to_toml::ToTOML;
|
pub(crate) use to_toml::ToTOML;
|
||||||
pub(crate) use to_tsv::ToTSV;
|
pub(crate) use to_tsv::ToTSV;
|
||||||
pub(crate) use to_url::ToURL;
|
pub(crate) use to_url::ToURL;
|
||||||
pub(crate) use to_yaml::ToYAML;
|
pub(crate) use to_yaml::ToYAML;
|
||||||
|
pub(crate) use touch::Touch;
|
||||||
pub(crate) use trim::Trim;
|
pub(crate) use trim::Trim;
|
||||||
pub(crate) use uniq::Uniq;
|
pub(crate) use uniq::Uniq;
|
||||||
pub(crate) use version::Version;
|
pub(crate) use version::Version;
|
@ -92,6 +92,7 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let stream = stream.to_input_stream();
|
let stream = stream.to_input_stream();
|
||||||
|
|
||||||
if let Some(table) = table {
|
if let Some(table) = table {
|
||||||
let command_args = create_default_command_args(&context).with_input(stream);
|
let command_args = create_default_command_args(&context).with_input(stream);
|
||||||
let result = table.run(command_args, &context.commands);
|
let result = table.run(command_args, &context.commands);
|
||||||
@ -111,14 +112,14 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
|
|||||||
let result = text.run(command_args, &context.commands);
|
let result = text.run(command_args, &context.commands);
|
||||||
result.collect::<Vec<_>>().await;
|
result.collect::<Vec<_>>().await;
|
||||||
} else {
|
} else {
|
||||||
outln!("{}", s);
|
out!("{}", s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
outln!("{}", s);
|
out!("{}", s);
|
||||||
}
|
}
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::Line(ref s)),
|
value: UntaggedValue::Primitive(Primitive::Line(ref s)),
|
||||||
@ -131,32 +132,32 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
|
|||||||
let result = text.run(command_args, &context.commands);
|
let result = text.run(command_args, &context.commands);
|
||||||
result.collect::<Vec<_>>().await;
|
result.collect::<Vec<_>>().await;
|
||||||
} else {
|
} else {
|
||||||
outln!("{}\n", s);
|
out!("{}\n", s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::Line(s)),
|
value: UntaggedValue::Primitive(Primitive::Line(s)),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
outln!("{}\n", s);
|
out!("{}\n", s);
|
||||||
}
|
}
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::Path(s)),
|
value: UntaggedValue::Primitive(Primitive::Path(s)),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
outln!("{}", s.display());
|
out!("{}", s.display());
|
||||||
}
|
}
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::Int(n)),
|
value: UntaggedValue::Primitive(Primitive::Int(n)),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
outln!("{}", n);
|
out!("{}", n);
|
||||||
}
|
}
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Primitive(Primitive::Decimal(n)),
|
value: UntaggedValue::Primitive(Primitive::Decimal(n)),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
outln!("{}", n);
|
out!("{}", n);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value { value: UntaggedValue::Primitive(Primitive::Binary(ref b)), .. } => {
|
Value { value: UntaggedValue::Primitive(Primitive::Binary(ref b)), .. } => {
|
||||||
@ -168,13 +169,83 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
|
|||||||
result.collect::<Vec<_>>().await;
|
result.collect::<Vec<_>>().await;
|
||||||
} else {
|
} else {
|
||||||
use pretty_hex::*;
|
use pretty_hex::*;
|
||||||
outln!("{:?}", b.hex_dump());
|
out!("{:?}", b.hex_dump());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value { value: UntaggedValue::Error(e), .. } => {
|
Value { value: UntaggedValue::Error(e), .. } => {
|
||||||
yield Err(e);
|
yield Err(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value { value: UntaggedValue::Row(row), ..} => {
|
||||||
|
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
|
||||||
|
use prettytable::{color, Attr, Cell, Row, Table};
|
||||||
|
use crate::data::value::{format_leaf, style_leaf};
|
||||||
|
use textwrap::fill;
|
||||||
|
|
||||||
|
let termwidth = std::cmp::max(textwrap::termwidth(), 20);
|
||||||
|
|
||||||
|
enum TableMode {
|
||||||
|
Light,
|
||||||
|
Normal,
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut table = Table::new();
|
||||||
|
let table_mode = crate::data::config::config(Tag::unknown());
|
||||||
|
|
||||||
|
let table_mode = if let Some(s) = table_mode?.get("table_mode") {
|
||||||
|
match s.as_string() {
|
||||||
|
Ok(typ) if typ == "light" => TableMode::Light,
|
||||||
|
_ => TableMode::Normal,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TableMode::Normal
|
||||||
|
};
|
||||||
|
|
||||||
|
match table_mode {
|
||||||
|
TableMode::Light => {
|
||||||
|
table.set_format(
|
||||||
|
FormatBuilder::new()
|
||||||
|
.separator(LinePosition::Title, LineSeparator::new('─', '─', ' ', ' '))
|
||||||
|
.padding(1, 1)
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
table.set_format(
|
||||||
|
FormatBuilder::new()
|
||||||
|
.column_separator('│')
|
||||||
|
.separator(LinePosition::Top, LineSeparator::new('─', '┬', ' ', ' '))
|
||||||
|
.separator(LinePosition::Title, LineSeparator::new('─', '┼', ' ', ' '))
|
||||||
|
.separator(LinePosition::Bottom, LineSeparator::new('─', '┴', ' ', ' '))
|
||||||
|
.padding(1, 1)
|
||||||
|
.build(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut max_key_len = 0;
|
||||||
|
for (key, _) in row.entries.iter() {
|
||||||
|
max_key_len = std::cmp::max(max_key_len, key.chars().count());
|
||||||
|
}
|
||||||
|
|
||||||
|
if max_key_len > (termwidth/2 - 1) {
|
||||||
|
max_key_len = termwidth/2 - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let max_val_len = termwidth - max_key_len - 5;
|
||||||
|
|
||||||
|
for (key, value) in row.entries.iter() {
|
||||||
|
table.add_row(Row::new(vec![Cell::new(&fill(&key, max_key_len)).with_style(Attr::ForegroundColor(color::GREEN)).with_style(Attr::Bold),
|
||||||
|
Cell::new(&fill(&format_leaf(value).plain_string(100_000), max_val_len))]));
|
||||||
|
}
|
||||||
|
|
||||||
|
table.printstd();
|
||||||
|
|
||||||
|
// table.print_term(&mut *context.host.lock().out_terminal().ok_or_else(|| ShellError::untagged_runtime_error("Could not open terminal for output"))?)
|
||||||
|
// .map_err(|_| ShellError::untagged_runtime_error("Internal error: could not print to terminal (for unix systems check to make sure TERM is set)"))?;
|
||||||
|
}
|
||||||
|
|
||||||
Value { value: ref item, .. } => {
|
Value { value: ref item, .. } => {
|
||||||
if let Some(table) = table {
|
if let Some(table) = table {
|
||||||
let mut stream = VecDeque::new();
|
let mut stream = VecDeque::new();
|
||||||
@ -183,7 +254,7 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
|
|||||||
let result = table.run(command_args, &context.commands);
|
let result = table.run(command_args, &context.commands);
|
||||||
result.collect::<Vec<_>>().await;
|
result.collect::<Vec<_>>().await;
|
||||||
} else {
|
} else {
|
||||||
outln!("{:?}", item);
|
out!("{:?}", item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -191,7 +262,7 @@ pub fn autoview(context: RunnableContext) -> Result<OutputStream, ShellError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
//outln!("<no results>");
|
//out!("<no results>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,9 @@
|
|||||||
|
use crate::futures::ThreadedReceiver;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use bytes::{BufMut, Bytes, BytesMut};
|
||||||
|
use futures::executor::block_on_stream;
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
use futures_codec::{FramedRead, LinesCodec};
|
use futures_codec::FramedRead;
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_parser::commands::classified::external::ExternalArg;
|
use nu_parser::commands::classified::external::ExternalArg;
|
||||||
@ -11,6 +14,71 @@ use nu_value_ext::as_column_path;
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
use std::sync::mpsc;
|
||||||
|
|
||||||
|
pub enum StringOrBinary {
|
||||||
|
String(String),
|
||||||
|
Binary(Vec<u8>),
|
||||||
|
}
|
||||||
|
pub struct MaybeTextCodec;
|
||||||
|
|
||||||
|
impl futures_codec::Encoder for MaybeTextCodec {
|
||||||
|
type Item = StringOrBinary;
|
||||||
|
type Error = std::io::Error;
|
||||||
|
|
||||||
|
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
|
||||||
|
match item {
|
||||||
|
StringOrBinary::String(s) => {
|
||||||
|
dst.reserve(s.len());
|
||||||
|
dst.put(s.as_bytes());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
StringOrBinary::Binary(b) => {
|
||||||
|
dst.reserve(b.len());
|
||||||
|
dst.put(Bytes::from(b));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl futures_codec::Decoder for MaybeTextCodec {
|
||||||
|
type Item = StringOrBinary;
|
||||||
|
type Error = std::io::Error;
|
||||||
|
|
||||||
|
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
|
||||||
|
let v: Vec<u8> = src.to_vec();
|
||||||
|
match String::from_utf8(v) {
|
||||||
|
Ok(s) => {
|
||||||
|
src.clear();
|
||||||
|
if s.is_empty() {
|
||||||
|
Ok(None)
|
||||||
|
} else {
|
||||||
|
Ok(Some(StringOrBinary::String(s)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
// Note: the longest UTF-8 character per Unicode spec is currently 6 bytes. If we fail somewhere earlier than the last 6 bytes,
|
||||||
|
// we know that we're failing to understand the string encoding and not just seeing a partial character. When this happens, let's
|
||||||
|
// fall back to assuming it's a binary buffer.
|
||||||
|
if src.is_empty() {
|
||||||
|
Ok(None)
|
||||||
|
} else if src.len() > 6 && (src.len() - err.utf8_error().valid_up_to() > 6) {
|
||||||
|
// Fall back to assuming binary
|
||||||
|
let buf = src.to_vec();
|
||||||
|
src.clear();
|
||||||
|
Ok(Some(StringOrBinary::Binary(buf)))
|
||||||
|
} else {
|
||||||
|
// Looks like a utf-8 string, so let's assume that
|
||||||
|
let buf = src.split_to(err.utf8_error().valid_up_to() + 1);
|
||||||
|
String::from_utf8(buf.to_vec())
|
||||||
|
.map(|x| Some(StringOrBinary::String(x)))
|
||||||
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn nu_value_to_string(command: &ExternalCommand, from: &Value) -> Result<String, ShellError> {
|
pub fn nu_value_to_string(command: &ExternalCommand, from: &Value) -> Result<String, ShellError> {
|
||||||
match &from.value {
|
match &from.value {
|
||||||
@ -26,25 +94,6 @@ pub fn nu_value_to_string(command: &ExternalCommand, from: &Value) -> Result<Str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn nu_value_to_string_for_stdin(
|
|
||||||
command: &ExternalCommand,
|
|
||||||
from: &Value,
|
|
||||||
) -> Result<Option<String>, ShellError> {
|
|
||||||
match &from.value {
|
|
||||||
UntaggedValue::Primitive(Primitive::Nothing) => Ok(None),
|
|
||||||
UntaggedValue::Primitive(Primitive::String(s))
|
|
||||||
| UntaggedValue::Primitive(Primitive::Line(s)) => Ok(Some(s.clone())),
|
|
||||||
unsupported => Err(ShellError::labeled_error(
|
|
||||||
format!(
|
|
||||||
"Received unexpected type from pipeline ({})",
|
|
||||||
unsupported.type_name()
|
|
||||||
),
|
|
||||||
"expected a string",
|
|
||||||
&command.name_tag,
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn run_external_command(
|
pub(crate) async fn run_external_command(
|
||||||
command: ExternalCommand,
|
command: ExternalCommand,
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
@ -53,7 +102,7 @@ pub(crate) async fn run_external_command(
|
|||||||
) -> Result<Option<InputStream>, ShellError> {
|
) -> Result<Option<InputStream>, ShellError> {
|
||||||
trace!(target: "nu::run::external", "-> {}", command.name);
|
trace!(target: "nu::run::external", "-> {}", command.name);
|
||||||
|
|
||||||
if !did_find_command(&command.name) {
|
if !did_find_command(&command.name).await {
|
||||||
return Err(ShellError::labeled_error(
|
return Err(ShellError::labeled_error(
|
||||||
"Command not found",
|
"Command not found",
|
||||||
"command not found",
|
"command not found",
|
||||||
@ -62,9 +111,9 @@ pub(crate) async fn run_external_command(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if command.has_it_argument() || command.has_nu_argument() {
|
if command.has_it_argument() || command.has_nu_argument() {
|
||||||
run_with_iterator_arg(command, context, input, is_last).await
|
run_with_iterator_arg(command, context, input, is_last)
|
||||||
} else {
|
} else {
|
||||||
run_with_stdin(command, context, input, is_last).await
|
run_with_stdin(command, context, input, is_last)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +163,7 @@ fn to_column_path(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_with_iterator_arg(
|
fn run_with_iterator_arg(
|
||||||
command: ExternalCommand,
|
command: ExternalCommand,
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
input: Option<InputStream>,
|
input: Option<InputStream>,
|
||||||
@ -336,7 +385,7 @@ async fn run_with_iterator_arg(
|
|||||||
Ok(Some(stream.to_input_stream()))
|
Ok(Some(stream.to_input_stream()))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_with_stdin(
|
fn run_with_stdin(
|
||||||
command: ExternalCommand,
|
command: ExternalCommand,
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
input: Option<InputStream>,
|
input: Option<InputStream>,
|
||||||
@ -379,45 +428,6 @@ async fn run_with_stdin(
|
|||||||
spawn(&command, &path, &process_args[..], input, is_last)
|
spawn(&command, &path, &process_args[..], input, is_last)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is a wrapper for stdout-like readers that ensure a carriage return ends the stream
|
|
||||||
pub struct StdoutWithNewline<T: std::io::Read> {
|
|
||||||
stdout: T,
|
|
||||||
ended_in_newline: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: std::io::Read> StdoutWithNewline<T> {
|
|
||||||
pub fn new(stdout: T) -> StdoutWithNewline<T> {
|
|
||||||
StdoutWithNewline {
|
|
||||||
stdout,
|
|
||||||
ended_in_newline: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<T: std::io::Read> std::io::Read for StdoutWithNewline<T> {
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
|
||||||
match self.stdout.read(buf) {
|
|
||||||
Err(e) => Err(e),
|
|
||||||
Ok(0) => {
|
|
||||||
if !self.ended_in_newline && !buf.is_empty() {
|
|
||||||
self.ended_in_newline = true;
|
|
||||||
buf[0] = b'\n';
|
|
||||||
Ok(1)
|
|
||||||
} else {
|
|
||||||
Ok(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(len) => {
|
|
||||||
if buf[len - 1] == b'\n' {
|
|
||||||
self.ended_in_newline = true;
|
|
||||||
} else {
|
|
||||||
self.ended_in_newline = false;
|
|
||||||
}
|
|
||||||
Ok(len)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn spawn(
|
fn spawn(
|
||||||
command: &ExternalCommand,
|
command: &ExternalCommand,
|
||||||
path: &str,
|
path: &str,
|
||||||
@ -426,7 +436,6 @@ fn spawn(
|
|||||||
is_last: bool,
|
is_last: bool,
|
||||||
) -> Result<Option<InputStream>, ShellError> {
|
) -> Result<Option<InputStream>, ShellError> {
|
||||||
let command = command.clone();
|
let command = command.clone();
|
||||||
let name_tag = command.name_tag.clone();
|
|
||||||
|
|
||||||
let mut process = {
|
let mut process = {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
@ -467,76 +476,134 @@ fn spawn(
|
|||||||
|
|
||||||
trace!(target: "nu::run::external", "built command {:?}", process);
|
trace!(target: "nu::run::external", "built command {:?}", process);
|
||||||
|
|
||||||
|
// TODO Switch to async_std::process once it's stabilized
|
||||||
if let Ok(mut child) = process.spawn() {
|
if let Ok(mut child) = process.spawn() {
|
||||||
let stream = async_stream! {
|
let (tx, rx) = mpsc::sync_channel(0);
|
||||||
if let Some(mut input) = input {
|
|
||||||
let mut stdin_write = child.stdin
|
let mut stdin = child.stdin.take();
|
||||||
|
|
||||||
|
let stdin_write_tx = tx.clone();
|
||||||
|
let stdout_read_tx = tx;
|
||||||
|
let stdin_name_tag = command.name_tag.clone();
|
||||||
|
let stdout_name_tag = command.name_tag;
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
if let Some(input) = input {
|
||||||
|
let mut stdin_write = stdin
|
||||||
.take()
|
.take()
|
||||||
.expect("Internal error: could not get stdin pipe for external command");
|
.expect("Internal error: could not get stdin pipe for external command");
|
||||||
|
|
||||||
while let Some(value) = input.next().await {
|
for value in block_on_stream(input) {
|
||||||
let input_string = match nu_value_to_string_for_stdin(&command, &value) {
|
match &value.value {
|
||||||
Ok(None) => continue,
|
UntaggedValue::Primitive(Primitive::Nothing) => continue,
|
||||||
Ok(Some(v)) => v,
|
UntaggedValue::Primitive(Primitive::String(s))
|
||||||
Err(e) => {
|
| UntaggedValue::Primitive(Primitive::Line(s)) => {
|
||||||
yield Ok(Value {
|
if let Err(e) = stdin_write.write(s.as_bytes()) {
|
||||||
value: UntaggedValue::Error(e),
|
let message = format!("Unable to write to stdin (error = {})", e);
|
||||||
tag: name_tag
|
|
||||||
});
|
let _ = stdin_write_tx.send(Ok(Value {
|
||||||
return;
|
value: UntaggedValue::Error(ShellError::labeled_error(
|
||||||
|
message,
|
||||||
|
"application may have closed before completing pipeline",
|
||||||
|
&stdin_name_tag,
|
||||||
|
)),
|
||||||
|
tag: stdin_name_tag,
|
||||||
|
}));
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UntaggedValue::Primitive(Primitive::Binary(b)) => {
|
||||||
|
if let Err(e) = stdin_write.write(b) {
|
||||||
|
let message = format!("Unable to write to stdin (error = {})", e);
|
||||||
|
|
||||||
|
let _ = stdin_write_tx.send(Ok(Value {
|
||||||
|
value: UntaggedValue::Error(ShellError::labeled_error(
|
||||||
|
message,
|
||||||
|
"application may have closed before completing pipeline",
|
||||||
|
&stdin_name_tag,
|
||||||
|
)),
|
||||||
|
tag: stdin_name_tag,
|
||||||
|
}));
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsupported => {
|
||||||
|
let _ = stdin_write_tx.send(Ok(Value {
|
||||||
|
value: UntaggedValue::Error(ShellError::labeled_error(
|
||||||
|
format!(
|
||||||
|
"Received unexpected type from pipeline ({})",
|
||||||
|
unsupported.type_name()
|
||||||
|
),
|
||||||
|
"expected a string",
|
||||||
|
stdin_name_tag.clone(),
|
||||||
|
)),
|
||||||
|
tag: stdin_name_tag,
|
||||||
|
}));
|
||||||
|
return Err(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(e) = stdin_write.write(input_string.as_bytes()) {
|
|
||||||
let message = format!("Unable to write to stdin (error = {})", e);
|
|
||||||
|
|
||||||
yield Ok(Value {
|
|
||||||
value: UntaggedValue::Error(ShellError::labeled_error(
|
|
||||||
message,
|
|
||||||
"application may have closed before completing pipeline",
|
|
||||||
&name_tag)),
|
|
||||||
tag: name_tag
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
if !is_last {
|
if !is_last {
|
||||||
let stdout = if let Some(stdout) = child.stdout.take() {
|
let stdout = if let Some(stdout) = child.stdout.take() {
|
||||||
stdout
|
stdout
|
||||||
} else {
|
} else {
|
||||||
yield Ok(Value {
|
let _ = stdout_read_tx.send(Ok(Value {
|
||||||
value: UntaggedValue::Error(ShellError::labeled_error(
|
value: UntaggedValue::Error(ShellError::labeled_error(
|
||||||
"Can't redirect the stdout for external command",
|
"Can't redirect the stdout for external command",
|
||||||
"can't redirect stdout",
|
"can't redirect stdout",
|
||||||
&name_tag)),
|
&stdout_name_tag,
|
||||||
tag: name_tag
|
)),
|
||||||
});
|
tag: stdout_name_tag,
|
||||||
return;
|
}));
|
||||||
|
return Err(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let file = futures::io::AllowStdIo::new(StdoutWithNewline::new(stdout));
|
let file = futures::io::AllowStdIo::new(stdout);
|
||||||
let mut stream = FramedRead::new(file, LinesCodec);
|
let stream = FramedRead::new(file, MaybeTextCodec);
|
||||||
|
|
||||||
while let Some(line) = stream.next().await {
|
for line in block_on_stream(stream) {
|
||||||
if let Ok(line) = line {
|
match line {
|
||||||
yield Ok(Value {
|
Ok(line) => match line {
|
||||||
value: UntaggedValue::Primitive(Primitive::Line(line)),
|
StringOrBinary::String(s) => {
|
||||||
tag: name_tag.clone(),
|
let result = stdout_read_tx.send(Ok(Value {
|
||||||
});
|
value: UntaggedValue::Primitive(Primitive::String(s.clone())),
|
||||||
} else {
|
tag: stdout_name_tag.clone(),
|
||||||
yield Ok(Value {
|
}));
|
||||||
value: UntaggedValue::Error(
|
|
||||||
ShellError::labeled_error(
|
if result.is_err() {
|
||||||
"Unable to read lines from stdout. This usually happens when the output does not end with a newline.",
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StringOrBinary::Binary(b) => {
|
||||||
|
let result = stdout_read_tx.send(Ok(Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::Binary(
|
||||||
|
b.into_iter().collect(),
|
||||||
|
)),
|
||||||
|
tag: stdout_name_tag.clone(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
if result.is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
let _ = stdout_read_tx.send(Ok(Value {
|
||||||
|
value: UntaggedValue::Error(ShellError::labeled_error(
|
||||||
|
"Unable to read from stdout.",
|
||||||
"unable to read from stdout",
|
"unable to read from stdout",
|
||||||
&name_tag,
|
&stdout_name_tag,
|
||||||
)
|
)),
|
||||||
),
|
tag: stdout_name_tag.clone(),
|
||||||
tag: name_tag.clone(),
|
}));
|
||||||
});
|
break;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -547,40 +614,41 @@ fn spawn(
|
|||||||
let cfg = crate::data::config::config(Tag::unknown());
|
let cfg = crate::data::config::config(Tag::unknown());
|
||||||
if let Ok(cfg) = cfg {
|
if let Ok(cfg) = cfg {
|
||||||
if cfg.contains_key("nonzero_exit_errors") {
|
if cfg.contains_key("nonzero_exit_errors") {
|
||||||
yield Ok(Value {
|
let _ = stdout_read_tx.send(Ok(Value {
|
||||||
value: UntaggedValue::Error(
|
value: UntaggedValue::Error(ShellError::labeled_error(
|
||||||
ShellError::labeled_error(
|
"External command failed",
|
||||||
"External command failed",
|
"command failed",
|
||||||
"command failed",
|
&stdout_name_tag,
|
||||||
&name_tag,
|
)),
|
||||||
)
|
tag: stdout_name_tag,
|
||||||
),
|
}));
|
||||||
tag: name_tag,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
let stream = ThreadedReceiver::new(rx);
|
||||||
Ok(Some(stream.to_input_stream()))
|
Ok(Some(stream.to_input_stream()))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::labeled_error(
|
Err(ShellError::labeled_error(
|
||||||
"Command not found",
|
"Failed to spawn process",
|
||||||
"command not found",
|
"failed to spawn",
|
||||||
&command.name_tag,
|
&command.name_tag,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn did_find_command(name: &str) -> bool {
|
async fn did_find_command(name: &str) -> bool {
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
{
|
{
|
||||||
which::which(name).is_ok()
|
ichwh::which(name).await.unwrap_or(None).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
{
|
{
|
||||||
if which::which(name).is_ok() {
|
if ichwh::which(name).await.unwrap_or(None).is_some() {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
let cmd_builtins = [
|
let cmd_builtins = [
|
||||||
@ -612,8 +680,8 @@ fn argument_is_quoted(argument: &str) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
((argument.starts_with('"') && argument.ends_with('"'))
|
(argument.starts_with('"') && argument.ends_with('"'))
|
||||||
|| (argument.starts_with('\'') && argument.ends_with('\'')))
|
|| (argument.starts_with('\'') && argument.ends_with('\''))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
@ -5,7 +5,7 @@ use nu_errors::ShellError;
|
|||||||
use nu_parser::InternalCommand;
|
use nu_parser::InternalCommand;
|
||||||
use nu_protocol::{CommandAction, Primitive, ReturnSuccess, UntaggedValue, Value};
|
use nu_protocol::{CommandAction, Primitive, ReturnSuccess, UntaggedValue, Value};
|
||||||
|
|
||||||
pub(crate) async fn run_internal_command(
|
pub(crate) fn run_internal_command(
|
||||||
command: InternalCommand,
|
command: InternalCommand,
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
input: Option<InputStream>,
|
input: Option<InputStream>,
|
@ -31,7 +31,7 @@ pub(crate) async fn run_pipeline(
|
|||||||
(_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()),
|
(_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()),
|
||||||
|
|
||||||
(Some(ClassifiedCommand::Internal(left)), _) => {
|
(Some(ClassifiedCommand::Internal(left)), _) => {
|
||||||
run_internal_command(left, ctx, input, Text::from(line)).await?
|
run_internal_command(left, ctx, input, Text::from(line))?
|
||||||
}
|
}
|
||||||
|
|
||||||
(Some(ClassifiedCommand::External(left)), None) => {
|
(Some(ClassifiedCommand::External(left)), None) => {
|
@ -9,7 +9,6 @@ use nu_errors::ShellError;
|
|||||||
use nu_protocol::{CallInfo, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
use nu_protocol::{CallInfo, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
|
|
||||||
const NAME: &str = "du";
|
const NAME: &str = "du";
|
||||||
const GLOB_PARAMS: MatchOptions = MatchOptions {
|
const GLOB_PARAMS: MatchOptions = MatchOptions {
|
||||||
@ -42,7 +41,7 @@ impl PerItemCommand for Du {
|
|||||||
.optional("path", SyntaxShape::Pattern, "starting directory")
|
.optional("path", SyntaxShape::Pattern, "starting directory")
|
||||||
.switch(
|
.switch(
|
||||||
"all",
|
"all",
|
||||||
"Output File sizes as well as directory sizes",
|
"Output file sizes as well as directory sizes",
|
||||||
Some('a'),
|
Some('a'),
|
||||||
)
|
)
|
||||||
.switch(
|
.switch(
|
||||||
@ -90,51 +89,33 @@ impl PerItemCommand for Du {
|
|||||||
fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result<OutputStream, ShellError> {
|
fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result<OutputStream, ShellError> {
|
||||||
let tag = ctx.name.clone();
|
let tag = ctx.name.clone();
|
||||||
|
|
||||||
let exclude = args
|
let exclude = args.exclude.map_or(Ok(None), move |x| {
|
||||||
.exclude
|
Pattern::new(&x.item)
|
||||||
.clone()
|
.map(Option::Some)
|
||||||
.map_or(Ok(None), move |x| match Pattern::new(&x.item) {
|
.map_err(|e| ShellError::labeled_error(e.msg, "glob error", x.tag.clone()))
|
||||||
Ok(p) => Ok(Some(p)),
|
})?;
|
||||||
Err(e) => Err(ShellError::labeled_error(
|
|
||||||
e.msg,
|
let include_files = args.all;
|
||||||
"Glob error",
|
let paths = match args.path {
|
||||||
x.tag.clone(),
|
Some(p) => {
|
||||||
)),
|
let p = p.item.to_str().expect("Why isn't this encoded properly?");
|
||||||
})?;
|
glob::glob_with(p, GLOB_PARAMS)
|
||||||
let path = args.path.clone();
|
}
|
||||||
let filter_files = path.is_none();
|
None => glob::glob_with("*", GLOB_PARAMS),
|
||||||
let paths = match path {
|
}
|
||||||
Some(p) => match glob::glob_with(
|
.map_err(|e| ShellError::labeled_error(e.msg, "glob error", tag.clone()))?
|
||||||
p.item.to_str().expect("Why isn't this encoded properly?"),
|
|
||||||
GLOB_PARAMS,
|
|
||||||
) {
|
|
||||||
Ok(g) => Ok(g),
|
|
||||||
Err(e) => Err(ShellError::labeled_error(
|
|
||||||
e.msg,
|
|
||||||
"Glob error",
|
|
||||||
p.tag.clone(),
|
|
||||||
)),
|
|
||||||
},
|
|
||||||
None => match glob::glob_with("*", GLOB_PARAMS) {
|
|
||||||
Ok(g) => Ok(g),
|
|
||||||
Err(e) => Err(ShellError::labeled_error(e.msg, "Glob error", tag.clone())),
|
|
||||||
},
|
|
||||||
}?
|
|
||||||
.filter(move |p| {
|
.filter(move |p| {
|
||||||
if filter_files {
|
if include_files {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
match p {
|
match p {
|
||||||
Ok(f) if f.is_dir() => true,
|
Ok(f) if f.is_dir() => true,
|
||||||
Err(e) if e.path().is_dir() => true,
|
Err(e) if e.path().is_dir() => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(move |p| match p {
|
.map(|v| v.map_err(glob_err_into));
|
||||||
Err(e) => Err(glob_err_into(e)),
|
|
||||||
Ok(s) => Ok(s),
|
|
||||||
});
|
|
||||||
|
|
||||||
let ctrl_c = ctx.ctrl_c.clone();
|
let ctrl_c = ctx.ctrl_c.clone();
|
||||||
let all = args.all;
|
let all = args.all;
|
||||||
@ -142,35 +123,29 @@ fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result<OutputStream, ShellE
|
|||||||
let max_depth = args.max_depth.map(|f| f.item);
|
let max_depth = args.max_depth.map(|f| f.item);
|
||||||
let min_size = args.min_size.map(|f| f.item);
|
let min_size = args.min_size.map(|f| f.item);
|
||||||
|
|
||||||
let stream = async_stream! {
|
let params = DirBuilder {
|
||||||
let params = DirBuilder {
|
tag: tag.clone(),
|
||||||
tag: tag.clone(),
|
min: min_size,
|
||||||
min: min_size,
|
deref,
|
||||||
deref,
|
exclude,
|
||||||
ex: exclude,
|
all,
|
||||||
all,
|
|
||||||
};
|
|
||||||
for path in paths {
|
|
||||||
if ctrl_c.load(Ordering::SeqCst) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
match path {
|
|
||||||
Ok(p) => {
|
|
||||||
if p.is_dir() {
|
|
||||||
yield Ok(ReturnSuccess::Value(
|
|
||||||
DirInfo::new(p, ¶ms, max_depth).into(),
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
match FileInfo::new(p, deref, tag.clone()) {
|
|
||||||
Ok(f) => yield Ok(ReturnSuccess::Value(f.into())),
|
|
||||||
Err(e) => yield Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => yield Err(e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let stream = futures::stream::iter(paths)
|
||||||
|
.interruptible(ctrl_c)
|
||||||
|
.map(move |path| match path {
|
||||||
|
Ok(p) => {
|
||||||
|
if p.is_dir() {
|
||||||
|
Ok(ReturnSuccess::Value(
|
||||||
|
DirInfo::new(p, ¶ms, max_depth).into(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
FileInfo::new(p, deref, tag.clone()).map(|v| ReturnSuccess::Value(v.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => Err(e),
|
||||||
|
});
|
||||||
|
|
||||||
Ok(stream.to_output_stream())
|
Ok(stream.to_output_stream())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +153,7 @@ struct DirBuilder {
|
|||||||
tag: Tag,
|
tag: Tag,
|
||||||
min: Option<u64>,
|
min: Option<u64>,
|
||||||
deref: bool,
|
deref: bool,
|
||||||
ex: Option<Pattern>,
|
exclude: Option<Pattern>,
|
||||||
all: bool,
|
all: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,15 +218,11 @@ impl DirInfo {
|
|||||||
for f in d {
|
for f in d {
|
||||||
match f {
|
match f {
|
||||||
Ok(i) => match i.file_type() {
|
Ok(i) => match i.file_type() {
|
||||||
Ok(t) if t.is_dir() => {
|
Ok(t) if t.is_dir() => s = s.add_dir(i.path(), depth, ¶ms),
|
||||||
s = s.add_dir(i.path(), depth, ¶ms);
|
Ok(_t) => s = s.add_file(i.path(), ¶ms),
|
||||||
}
|
Err(e) => s = s.add_error(e.into()),
|
||||||
Ok(_t) => {
|
|
||||||
s = s.add_file(i.path(), ¶ms);
|
|
||||||
}
|
|
||||||
Err(e) => s = s.add_error(ShellError::from(e)),
|
|
||||||
},
|
},
|
||||||
Err(e) => s = s.add_error(ShellError::from(e)),
|
Err(e) => s = s.add_error(e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -283,8 +254,11 @@ impl DirInfo {
|
|||||||
|
|
||||||
fn add_file(mut self, f: impl Into<PathBuf>, params: &DirBuilder) -> Self {
|
fn add_file(mut self, f: impl Into<PathBuf>, params: &DirBuilder) -> Self {
|
||||||
let f = f.into();
|
let f = f.into();
|
||||||
let ex = params.ex.as_ref().map_or(false, |x| x.matches_path(&f));
|
let include = params
|
||||||
if !ex {
|
.exclude
|
||||||
|
.as_ref()
|
||||||
|
.map_or(true, |x| !x.matches_path(&f));
|
||||||
|
if include {
|
||||||
match FileInfo::new(f, params.deref, self.tag.clone()) {
|
match FileInfo::new(f, params.deref, self.tag.clone()) {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
let inc = params.min.map_or(true, |s| file.size >= s);
|
let inc = params.min.map_or(true, |s| file.size >= s);
|
||||||
@ -313,55 +287,51 @@ fn glob_err_into(e: GlobError) -> ShellError {
|
|||||||
ShellError::from(e)
|
ShellError::from(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn value_from_vec<V>(vec: Vec<V>, tag: &Tag) -> Value
|
||||||
|
where
|
||||||
|
V: Into<Value>,
|
||||||
|
{
|
||||||
|
if vec.is_empty() {
|
||||||
|
UntaggedValue::nothing()
|
||||||
|
} else {
|
||||||
|
let values = vec.into_iter().map(Into::into).collect::<Vec<Value>>();
|
||||||
|
UntaggedValue::Table(values)
|
||||||
|
}
|
||||||
|
.retag(tag)
|
||||||
|
}
|
||||||
|
|
||||||
impl From<DirInfo> for Value {
|
impl From<DirInfo> for Value {
|
||||||
fn from(d: DirInfo) -> Self {
|
fn from(d: DirInfo) -> Self {
|
||||||
let mut r: IndexMap<String, Value> = IndexMap::new();
|
let mut r: IndexMap<String, Value> = IndexMap::new();
|
||||||
|
|
||||||
r.insert(
|
r.insert(
|
||||||
"path".to_string(),
|
"path".to_string(),
|
||||||
UntaggedValue::path(d.path).retag(d.tag.clone()),
|
UntaggedValue::path(d.path).retag(&d.tag),
|
||||||
);
|
);
|
||||||
|
|
||||||
r.insert(
|
r.insert(
|
||||||
"apparent".to_string(),
|
"apparent".to_string(),
|
||||||
UntaggedValue::bytes(d.size).retag(d.tag.clone()),
|
UntaggedValue::bytes(d.size).retag(&d.tag),
|
||||||
);
|
);
|
||||||
|
|
||||||
r.insert(
|
r.insert(
|
||||||
"physical".to_string(),
|
"physical".to_string(),
|
||||||
UntaggedValue::bytes(d.blocks).retag(d.tag.clone()),
|
UntaggedValue::bytes(d.blocks).retag(&d.tag),
|
||||||
);
|
);
|
||||||
if !d.files.is_empty() {
|
|
||||||
let v = Value {
|
r.insert("directories".to_string(), value_from_vec(d.dirs, &d.tag));
|
||||||
value: UntaggedValue::Table(
|
|
||||||
d.files
|
r.insert("files".to_string(), value_from_vec(d.files, &d.tag));
|
||||||
.into_iter()
|
|
||||||
.map(move |f| f.into())
|
|
||||||
.collect::<Vec<Value>>(),
|
|
||||||
),
|
|
||||||
tag: d.tag.clone(),
|
|
||||||
};
|
|
||||||
r.insert("files".to_string(), v);
|
|
||||||
}
|
|
||||||
if !d.dirs.is_empty() {
|
|
||||||
let v = Value {
|
|
||||||
value: UntaggedValue::Table(
|
|
||||||
d.dirs
|
|
||||||
.into_iter()
|
|
||||||
.map(move |d| d.into())
|
|
||||||
.collect::<Vec<Value>>(),
|
|
||||||
),
|
|
||||||
tag: d.tag.clone(),
|
|
||||||
};
|
|
||||||
r.insert("directories".to_string(), v);
|
|
||||||
}
|
|
||||||
if !d.errors.is_empty() {
|
if !d.errors.is_empty() {
|
||||||
let v = Value {
|
let v = UntaggedValue::Table(
|
||||||
value: UntaggedValue::Table(
|
d.errors
|
||||||
d.errors
|
.into_iter()
|
||||||
.into_iter()
|
.map(move |e| UntaggedValue::Error(e).into_untagged_value())
|
||||||
.map(move |e| UntaggedValue::Error(e).into_untagged_value())
|
.collect::<Vec<Value>>(),
|
||||||
.collect::<Vec<Value>>(),
|
)
|
||||||
),
|
.retag(&d.tag);
|
||||||
tag: d.tag.clone(),
|
|
||||||
};
|
|
||||||
r.insert("errors".to_string(), v);
|
r.insert("errors".to_string(), v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -375,22 +345,32 @@ impl From<DirInfo> for Value {
|
|||||||
impl From<FileInfo> for Value {
|
impl From<FileInfo> for Value {
|
||||||
fn from(f: FileInfo) -> Self {
|
fn from(f: FileInfo) -> Self {
|
||||||
let mut r: IndexMap<String, Value> = IndexMap::new();
|
let mut r: IndexMap<String, Value> = IndexMap::new();
|
||||||
|
|
||||||
r.insert(
|
r.insert(
|
||||||
"path".to_string(),
|
"path".to_string(),
|
||||||
UntaggedValue::path(f.path).retag(f.tag.clone()),
|
UntaggedValue::path(f.path).retag(&f.tag),
|
||||||
);
|
);
|
||||||
|
|
||||||
r.insert(
|
r.insert(
|
||||||
"apparent".to_string(),
|
"apparent".to_string(),
|
||||||
UntaggedValue::bytes(f.size).retag(f.tag.clone()),
|
UntaggedValue::bytes(f.size).retag(&f.tag),
|
||||||
);
|
);
|
||||||
let b = match f.blocks {
|
|
||||||
Some(k) => UntaggedValue::bytes(k).retag(f.tag.clone()),
|
let b = f
|
||||||
None => UntaggedValue::nothing().retag(f.tag.clone()),
|
.blocks
|
||||||
};
|
.map(UntaggedValue::bytes)
|
||||||
|
.unwrap_or_else(UntaggedValue::nothing)
|
||||||
|
.retag(&f.tag);
|
||||||
|
|
||||||
r.insert("physical".to_string(), b);
|
r.insert("physical".to_string(), b);
|
||||||
Value {
|
|
||||||
value: UntaggedValue::row(r),
|
r.insert(
|
||||||
tag: f.tag,
|
"directories".to_string(),
|
||||||
}
|
UntaggedValue::nothing().retag(&f.tag),
|
||||||
|
);
|
||||||
|
|
||||||
|
r.insert("files".to_string(), UntaggedValue::nothing().retag(&f.tag));
|
||||||
|
|
||||||
|
UntaggedValue::row(r).retag(&f.tag)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,11 @@ use crate::commands::PerItemCommand;
|
|||||||
use crate::context::CommandRegistry;
|
use crate::context::CommandRegistry;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{CallInfo, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
use nu_protocol::{
|
||||||
|
CallInfo, ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
||||||
|
};
|
||||||
|
use nu_source::Tagged;
|
||||||
|
use nu_value_ext::{as_column_path, get_data_by_column_path};
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
|
|
||||||
use nom::{
|
use nom::{
|
||||||
@ -45,35 +49,46 @@ impl PerItemCommand for Format {
|
|||||||
ShellError::labeled_error(
|
ShellError::labeled_error(
|
||||||
"Could not create format pattern",
|
"Could not create format pattern",
|
||||||
"could not create format pattern",
|
"could not create format pattern",
|
||||||
pattern_tag,
|
&pattern_tag,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
let commands = format_pattern.1;
|
let commands = format_pattern.1;
|
||||||
|
|
||||||
let output = if let Value {
|
let output = match value {
|
||||||
value: UntaggedValue::Row(dict),
|
value
|
||||||
..
|
@
|
||||||
} = value
|
Value {
|
||||||
{
|
value: UntaggedValue::Row(_),
|
||||||
let mut output = String::new();
|
..
|
||||||
|
} => {
|
||||||
|
let mut output = String::new();
|
||||||
|
|
||||||
for command in &commands {
|
for command in &commands {
|
||||||
match command {
|
match command {
|
||||||
FormatCommand::Text(s) => {
|
FormatCommand::Text(s) => {
|
||||||
output.push_str(s);
|
output.push_str(s);
|
||||||
}
|
}
|
||||||
FormatCommand::Column(c) => {
|
FormatCommand::Column(c) => {
|
||||||
if let Some(c) = dict.entries.get(c) {
|
let key = to_column_path(&c, &pattern_tag)?;
|
||||||
output.push_str(&value::format_leaf(c.borrow()).plain_string(100_000))
|
|
||||||
|
let fetcher = get_data_by_column_path(
|
||||||
|
&value,
|
||||||
|
&key,
|
||||||
|
Box::new(move |(_, _, error)| error),
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Ok(c) = fetcher {
|
||||||
|
output
|
||||||
|
.push_str(&value::format_leaf(c.borrow()).plain_string(100_000))
|
||||||
|
}
|
||||||
|
// That column doesn't match, so don't emit anything
|
||||||
}
|
}
|
||||||
// That column doesn't match, so don't emit anything
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
output
|
output
|
||||||
} else {
|
}
|
||||||
String::new()
|
_ => String::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(futures::stream::iter(vec![ReturnSuccess::value(
|
Ok(futures::stream::iter(vec![ReturnSuccess::value(
|
||||||
@ -116,3 +131,27 @@ fn format(input: &str) -> IResult<&str, Vec<FormatCommand>> {
|
|||||||
|
|
||||||
Ok((loop_input, output))
|
Ok((loop_input, output))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_column_path(
|
||||||
|
path_members: &str,
|
||||||
|
tag: impl Into<Tag>,
|
||||||
|
) -> Result<Tagged<ColumnPath>, ShellError> {
|
||||||
|
let tag = tag.into();
|
||||||
|
|
||||||
|
as_column_path(
|
||||||
|
&UntaggedValue::Table(
|
||||||
|
path_members
|
||||||
|
.split('.')
|
||||||
|
.map(|x| {
|
||||||
|
let member = match x.parse::<u64>() {
|
||||||
|
Ok(v) => UntaggedValue::int(v),
|
||||||
|
Err(_) => UntaggedValue::string(x),
|
||||||
|
};
|
||||||
|
|
||||||
|
member.into_value(&tag)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
.into_value(&tag),
|
||||||
|
)
|
||||||
|
}
|
@ -205,32 +205,18 @@ fn from_bson(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
|
|||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let bytes = input.collect_binary(tag.clone()).await?;
|
||||||
|
|
||||||
for value in values {
|
match from_bson_bytes_to_value(bytes.item, tag.clone()) {
|
||||||
let value_tag = &value.tag;
|
Ok(x) => yield ReturnSuccess::value(x),
|
||||||
match value.value {
|
Err(_) => {
|
||||||
UntaggedValue::Primitive(Primitive::Binary(vb)) =>
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
match from_bson_bytes_to_value(vb, tag.clone()) {
|
"Could not parse as BSON",
|
||||||
Ok(x) => yield ReturnSuccess::value(x),
|
"input cannot be parsed as BSON",
|
||||||
Err(_) => {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Could not parse as BSON",
|
|
||||||
"input cannot be parsed as BSON",
|
|
||||||
tag.clone(),
|
|
||||||
"value originates from here",
|
|
||||||
value_tag,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
tag.clone(),
|
tag.clone(),
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
value_tag,
|
bytes.tag,
|
||||||
)),
|
))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
169
crates/nu-cli/src/commands/from_delimited_data.rs
Normal file
169
crates/nu-cli/src/commands/from_delimited_data.rs
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_parser::hir::syntax_shape::{ExpandContext, SignatureRegistry};
|
||||||
|
use nu_parser::utils::{parse_line_with_separator as parse, LineSeparatedShape};
|
||||||
|
use nu_parser::TokensIterator;
|
||||||
|
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
||||||
|
use nu_source::nom_input;
|
||||||
|
|
||||||
|
use derive_new::new;
|
||||||
|
|
||||||
|
pub fn from_delimited_data(
|
||||||
|
headerless: bool,
|
||||||
|
sep: char,
|
||||||
|
format_name: &'static str,
|
||||||
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let name_tag = name;
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
|
let concat_string = input.collect_string(name_tag.clone()).await?;
|
||||||
|
|
||||||
|
match from_delimited_string_to_value(concat_string.item, headerless, sep, name_tag.clone()) {
|
||||||
|
Ok(rows) => {
|
||||||
|
for row in rows {
|
||||||
|
match row {
|
||||||
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
|
for l in list {
|
||||||
|
yield ReturnSuccess::value(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x => yield ReturnSuccess::value(x),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
let line_one = format!("Could not parse as {}", format_name);
|
||||||
|
let line_two = format!("input cannot be parsed as {}", format_name);
|
||||||
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
|
line_one,
|
||||||
|
line_two,
|
||||||
|
name_tag.clone(),
|
||||||
|
"value originates from here",
|
||||||
|
concat_string.tag,
|
||||||
|
))
|
||||||
|
} ,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, new)]
|
||||||
|
pub struct EmptyRegistry {
|
||||||
|
#[new(default)]
|
||||||
|
signatures: indexmap::IndexMap<String, Signature>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EmptyRegistry {}
|
||||||
|
|
||||||
|
impl SignatureRegistry for EmptyRegistry {
|
||||||
|
fn has(&self, _name: &str) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
fn get(&self, _name: &str) -> Option<Signature> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
fn clone_box(&self) -> Box<dyn SignatureRegistry> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_delimited_string_to_value(
|
||||||
|
s: String,
|
||||||
|
headerless: bool,
|
||||||
|
sep: char,
|
||||||
|
tag: impl Into<Tag>,
|
||||||
|
) -> Result<Vec<Value>, ShellError> {
|
||||||
|
let tag = tag.into();
|
||||||
|
|
||||||
|
let mut entries = s.lines();
|
||||||
|
|
||||||
|
let mut fields = vec![];
|
||||||
|
let mut out = vec![];
|
||||||
|
|
||||||
|
if let Some(first_entry) = entries.next() {
|
||||||
|
let tokens = match parse(&sep.to_string(), nom_input(first_entry)) {
|
||||||
|
Ok((_, tokens)) => tokens,
|
||||||
|
Err(err) => return Err(ShellError::parse_error(err)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let tokens_span = tokens.span;
|
||||||
|
let source: nu_source::Text = tokens_span.slice(&first_entry).into();
|
||||||
|
|
||||||
|
if !headerless {
|
||||||
|
fields = tokens
|
||||||
|
.item
|
||||||
|
.iter()
|
||||||
|
.filter(|token| !token.is_separator())
|
||||||
|
.map(|field| field.source(&source).to_string())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
let registry = Box::new(EmptyRegistry::new());
|
||||||
|
let ctx = ExpandContext::new(registry, &source, None);
|
||||||
|
|
||||||
|
let mut iterator = TokensIterator::new(&tokens.item, ctx, tokens_span);
|
||||||
|
let (results, tokens_identified) = iterator.expand(LineSeparatedShape);
|
||||||
|
let results = results?;
|
||||||
|
|
||||||
|
let mut row = TaggedDictBuilder::new(&tag);
|
||||||
|
|
||||||
|
if headerless {
|
||||||
|
let fallback_columns = (1..=tokens_identified)
|
||||||
|
.map(|i| format!("Column{}", i))
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
for (idx, field) in results.into_iter().enumerate() {
|
||||||
|
let key = if headerless {
|
||||||
|
&fallback_columns[idx]
|
||||||
|
} else {
|
||||||
|
&fields[idx]
|
||||||
|
};
|
||||||
|
|
||||||
|
row.insert_value(key, field.into_value(&tag));
|
||||||
|
}
|
||||||
|
|
||||||
|
out.push(row.into_value())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for entry in entries {
|
||||||
|
let tokens = match parse(&sep.to_string(), nom_input(entry)) {
|
||||||
|
Ok((_, tokens)) => tokens,
|
||||||
|
Err(err) => return Err(ShellError::parse_error(err)),
|
||||||
|
};
|
||||||
|
let tokens_span = tokens.span;
|
||||||
|
|
||||||
|
let source: nu_source::Text = tokens_span.slice(&entry).into();
|
||||||
|
let registry = Box::new(EmptyRegistry::new());
|
||||||
|
let ctx = ExpandContext::new(registry, &source, None);
|
||||||
|
|
||||||
|
let mut iterator = TokensIterator::new(&tokens.item, ctx, tokens_span);
|
||||||
|
let (results, tokens_identified) = iterator.expand(LineSeparatedShape);
|
||||||
|
let results = results?;
|
||||||
|
|
||||||
|
let mut row = TaggedDictBuilder::new(&tag);
|
||||||
|
|
||||||
|
let fallback_columns = (1..=tokens_identified)
|
||||||
|
.map(|i| format!("Column{}", i))
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
for (idx, field) in results.into_iter().enumerate() {
|
||||||
|
let key = if headerless {
|
||||||
|
&fallback_columns[idx]
|
||||||
|
} else {
|
||||||
|
match fields.get(idx) {
|
||||||
|
Some(key) => key,
|
||||||
|
None => &fallback_columns[idx],
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
row.insert_value(key, field.into_value(&tag));
|
||||||
|
}
|
||||||
|
|
||||||
|
out.push(row.into_value())
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(out)
|
||||||
|
}
|
240
crates/nu-cli/src/commands/from_ics.rs
Normal file
240
crates/nu-cli/src/commands/from_ics.rs
Normal file
@ -0,0 +1,240 @@
|
|||||||
|
extern crate ical;
|
||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use ical::parser::ical::component::*;
|
||||||
|
use ical::property::Property;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
||||||
|
use std::io::BufReader;
|
||||||
|
|
||||||
|
pub struct FromIcs;
|
||||||
|
|
||||||
|
impl WholeStreamCommand for FromIcs {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"from-ics"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("from-ics")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Parse text as .ics and create table."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
from_ics(args, registry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ics(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
|
let args = args.evaluate_once(registry)?;
|
||||||
|
let tag = args.name_tag();
|
||||||
|
let input = args.input;
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
|
let input_string = input.collect_string(tag.clone()).await?.item;
|
||||||
|
let input_bytes = input_string.as_bytes();
|
||||||
|
let buf_reader = BufReader::new(input_bytes);
|
||||||
|
let parser = ical::IcalParser::new(buf_reader);
|
||||||
|
|
||||||
|
for calendar in parser {
|
||||||
|
match calendar {
|
||||||
|
Ok(c) => yield ReturnSuccess::value(calendar_to_value(c, tag.clone())),
|
||||||
|
Err(_) => yield Err(ShellError::labeled_error(
|
||||||
|
"Could not parse as .ics",
|
||||||
|
"input cannot be parsed as .ics",
|
||||||
|
tag.clone()
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calendar_to_value(calendar: IcalCalendar, tag: Tag) -> Value {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(calendar.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.insert_untagged("events", events_to_value(calendar.events, tag.clone()));
|
||||||
|
row.insert_untagged("alarms", alarms_to_value(calendar.alarms, tag.clone()));
|
||||||
|
row.insert_untagged("to-Dos", todos_to_value(calendar.todos, tag.clone()));
|
||||||
|
row.insert_untagged(
|
||||||
|
"journals",
|
||||||
|
journals_to_value(calendar.journals, tag.clone()),
|
||||||
|
);
|
||||||
|
row.insert_untagged(
|
||||||
|
"free-busys",
|
||||||
|
free_busys_to_value(calendar.free_busys, tag.clone()),
|
||||||
|
);
|
||||||
|
row.insert_untagged("timezones", timezones_to_value(calendar.timezones, tag));
|
||||||
|
|
||||||
|
row.into_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn events_to_value(events: Vec<IcalEvent>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&events
|
||||||
|
.into_iter()
|
||||||
|
.map(|event| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(event.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.insert_untagged("alarms", alarms_to_value(event.alarms, tag.clone()));
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn alarms_to_value(alarms: Vec<IcalAlarm>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&alarms
|
||||||
|
.into_iter()
|
||||||
|
.map(|alarm| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(alarm.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn todos_to_value(todos: Vec<IcalTodo>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&todos
|
||||||
|
.into_iter()
|
||||||
|
.map(|todo| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(todo.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.insert_untagged("alarms", alarms_to_value(todo.alarms, tag.clone()));
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn journals_to_value(journals: Vec<IcalJournal>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&journals
|
||||||
|
.into_iter()
|
||||||
|
.map(|journal| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(journal.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn free_busys_to_value(free_busys: Vec<IcalFreeBusy>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&free_busys
|
||||||
|
.into_iter()
|
||||||
|
.map(|free_busy| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(free_busy.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timezones_to_value(timezones: Vec<IcalTimeZone>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&timezones
|
||||||
|
.into_iter()
|
||||||
|
.map(|timezone| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(timezone.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.insert_untagged(
|
||||||
|
"transitions",
|
||||||
|
timezone_transitions_to_value(timezone.transitions, tag.clone()),
|
||||||
|
);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timezone_transitions_to_value(
|
||||||
|
transitions: Vec<IcalTimeZoneTransition>,
|
||||||
|
tag: Tag,
|
||||||
|
) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&transitions
|
||||||
|
.into_iter()
|
||||||
|
.map(|transition| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged(
|
||||||
|
"properties",
|
||||||
|
properties_to_value(transition.properties, tag.clone()),
|
||||||
|
);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn properties_to_value(properties: Vec<Property>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&properties
|
||||||
|
.into_iter()
|
||||||
|
.map(|prop| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
|
||||||
|
let name = UntaggedValue::string(prop.name);
|
||||||
|
let value = match prop.value {
|
||||||
|
Some(val) => UntaggedValue::string(val),
|
||||||
|
None => UntaggedValue::Primitive(Primitive::Nothing),
|
||||||
|
};
|
||||||
|
let params = match prop.params {
|
||||||
|
Some(param_list) => params_to_value(param_list, tag.clone()).into(),
|
||||||
|
None => UntaggedValue::Primitive(Primitive::Nothing),
|
||||||
|
};
|
||||||
|
|
||||||
|
row.insert_untagged("name", name);
|
||||||
|
row.insert_untagged("value", value);
|
||||||
|
row.insert_untagged("params", params);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn params_to_value(params: Vec<(String, Vec<String>)>, tag: Tag) -> Value {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag);
|
||||||
|
|
||||||
|
for (param_name, param_values) in params {
|
||||||
|
let values: Vec<Value> = param_values.into_iter().map(|val| val.into()).collect();
|
||||||
|
let values = UntaggedValue::table(&values);
|
||||||
|
row.insert_untagged(param_name, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
row.into_value()
|
||||||
|
}
|
@ -66,32 +66,12 @@ pub fn from_ini_string_to_value(
|
|||||||
fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
let args = args.evaluate_once(registry)?;
|
let args = args.evaluate_once(registry)?;
|
||||||
let tag = args.name_tag();
|
let tag = args.name_tag();
|
||||||
let span = tag.span;
|
|
||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(tag.clone()).await?;
|
||||||
|
|
||||||
let mut concat_string = String::new();
|
match from_ini_string_to_value(concat_string.item, tag.clone()) {
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
|
|
||||||
for value in values {
|
|
||||||
latest_tag = Some(value.tag.clone());
|
|
||||||
let value_span = value.tag.span;
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
} else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
span,
|
|
||||||
"value originates from here",
|
|
||||||
value_span,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match from_ini_string_to_value(concat_string, tag.clone()) {
|
|
||||||
Ok(x) => match x {
|
Ok(x) => match x {
|
||||||
Value { value: UntaggedValue::Table(list), .. } => {
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
for l in list {
|
for l in list {
|
||||||
@ -100,15 +80,15 @@ fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
|
|||||||
}
|
}
|
||||||
x => yield ReturnSuccess::value(x),
|
x => yield ReturnSuccess::value(x),
|
||||||
},
|
},
|
||||||
Err(_) => if let Some(last_tag) = latest_tag {
|
Err(_) => {
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Could not parse as INI",
|
"Could not parse as INI",
|
||||||
"input cannot be parsed as INI",
|
"input cannot be parsed as INI",
|
||||||
&tag,
|
&tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
last_tag,
|
concat_string.tag,
|
||||||
))
|
))
|
||||||
} ,
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -74,35 +74,13 @@ fn from_json(
|
|||||||
FromJSONArgs { objects }: FromJSONArgs,
|
FromJSONArgs { objects }: FromJSONArgs,
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let name_span = name.span;
|
|
||||||
let name_tag = name;
|
let name_tag = name;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(name_tag.clone()).await?;
|
||||||
|
|
||||||
let mut concat_string = String::new();
|
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
|
|
||||||
for value in values {
|
|
||||||
latest_tag = Some(value.tag.clone());
|
|
||||||
let value_span = value.tag.span;
|
|
||||||
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
} else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
name_span,
|
|
||||||
"value originates from here",
|
|
||||||
value_span,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if objects {
|
if objects {
|
||||||
for json_str in concat_string.lines() {
|
for json_str in concat_string.item.lines() {
|
||||||
if json_str.is_empty() {
|
if json_str.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -111,23 +89,21 @@ fn from_json(
|
|||||||
Ok(x) =>
|
Ok(x) =>
|
||||||
yield ReturnSuccess::value(x),
|
yield ReturnSuccess::value(x),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let Some(ref last_tag) = latest_tag {
|
let mut message = "Could not parse as JSON (".to_string();
|
||||||
let mut message = "Could not parse as JSON (".to_string();
|
message.push_str(&e.to_string());
|
||||||
message.push_str(&e.to_string());
|
message.push_str(")");
|
||||||
message.push_str(")");
|
|
||||||
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
message,
|
message,
|
||||||
"input cannot be parsed as JSON",
|
"input cannot be parsed as JSON",
|
||||||
&name_tag,
|
&name_tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
last_tag))
|
concat_string.tag.clone()))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match from_json_string_to_value(concat_string, name_tag.clone()) {
|
match from_json_string_to_value(concat_string.item, name_tag.clone()) {
|
||||||
Ok(x) =>
|
Ok(x) =>
|
||||||
match x {
|
match x {
|
||||||
Value { value: UntaggedValue::Table(list), .. } => {
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
@ -138,18 +114,16 @@ fn from_json(
|
|||||||
x => yield ReturnSuccess::value(x),
|
x => yield ReturnSuccess::value(x),
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let Some(last_tag) = latest_tag {
|
let mut message = "Could not parse as JSON (".to_string();
|
||||||
let mut message = "Could not parse as JSON (".to_string();
|
message.push_str(&e.to_string());
|
||||||
message.push_str(&e.to_string());
|
message.push_str(")");
|
||||||
message.push_str(")");
|
|
||||||
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
message,
|
message,
|
||||||
"input cannot be parsed as JSON",
|
"input cannot be parsed as JSON",
|
||||||
name_tag,
|
name_tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
last_tag))
|
concat_string.tag))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
98
crates/nu-cli/src/commands/from_ods.rs
Normal file
98
crates/nu-cli/src/commands/from_ods.rs
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use crate::TaggedListBuilder;
|
||||||
|
use calamine::*;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue};
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
pub struct FromODS;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct FromODSArgs {
|
||||||
|
headerless: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WholeStreamCommand for FromODS {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"from-ods"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("from-ods").switch(
|
||||||
|
"headerless",
|
||||||
|
"don't treat the first row as column names",
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Parse OpenDocument Spreadsheet(.ods) data and create table."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
args.process(registry, from_ods)?.run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ods(
|
||||||
|
FromODSArgs {
|
||||||
|
headerless: _headerless,
|
||||||
|
}: FromODSArgs,
|
||||||
|
runnable_context: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let input = runnable_context.input;
|
||||||
|
let tag = runnable_context.name;
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
|
let bytes = input.collect_binary(tag.clone()).await?;
|
||||||
|
let mut buf: Cursor<Vec<u8>> = Cursor::new(bytes.item);
|
||||||
|
let mut ods = Ods::<_>::new(buf).map_err(|_| ShellError::labeled_error(
|
||||||
|
"Could not load ods file",
|
||||||
|
"could not load ods file",
|
||||||
|
&tag))?;
|
||||||
|
|
||||||
|
let mut dict = TaggedDictBuilder::new(&tag);
|
||||||
|
|
||||||
|
let sheet_names = ods.sheet_names().to_owned();
|
||||||
|
|
||||||
|
for sheet_name in &sheet_names {
|
||||||
|
let mut sheet_output = TaggedListBuilder::new(&tag);
|
||||||
|
|
||||||
|
if let Some(Ok(current_sheet)) = ods.worksheet_range(sheet_name) {
|
||||||
|
for row in current_sheet.rows() {
|
||||||
|
let mut row_output = TaggedDictBuilder::new(&tag);
|
||||||
|
for (i, cell) in row.iter().enumerate() {
|
||||||
|
let value = match cell {
|
||||||
|
DataType::Empty => UntaggedValue::nothing(),
|
||||||
|
DataType::String(s) => UntaggedValue::string(s),
|
||||||
|
DataType::Float(f) => UntaggedValue::decimal(*f),
|
||||||
|
DataType::Int(i) => UntaggedValue::int(*i),
|
||||||
|
DataType::Bool(b) => UntaggedValue::boolean(*b),
|
||||||
|
_ => UntaggedValue::nothing(),
|
||||||
|
};
|
||||||
|
|
||||||
|
row_output.insert_untagged(&format!("Column{}", i), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
sheet_output.push_untagged(row_output.into_untagged_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
dict.insert_untagged(sheet_name, sheet_output.into_untagged_value());
|
||||||
|
} else {
|
||||||
|
yield Err(ShellError::labeled_error(
|
||||||
|
"Could not load sheet",
|
||||||
|
"could not load sheet",
|
||||||
|
&tag));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(dict.into_value());
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
@ -138,40 +138,25 @@ fn from_sqlite(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
|
|||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let bytes = input.collect_binary(tag.clone()).await?;
|
||||||
|
match from_sqlite_bytes_to_value(bytes.item, tag.clone()) {
|
||||||
for value in values {
|
Ok(x) => match x {
|
||||||
let value_tag = &value.tag;
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
match value.value {
|
for l in list {
|
||||||
UntaggedValue::Primitive(Primitive::Binary(vb)) =>
|
yield ReturnSuccess::value(l);
|
||||||
match from_sqlite_bytes_to_value(vb, tag.clone()) {
|
|
||||||
Ok(x) => match x {
|
|
||||||
Value { value: UntaggedValue::Table(list), .. } => {
|
|
||||||
for l in list {
|
|
||||||
yield ReturnSuccess::value(l);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => yield ReturnSuccess::value(x),
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
println!("{:?}", err);
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Could not parse as SQLite",
|
|
||||||
"input cannot be parsed as SQLite",
|
|
||||||
&tag,
|
|
||||||
"value originates from here",
|
|
||||||
value_tag,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => yield Err(ShellError::labeled_error_with_secondary(
|
}
|
||||||
"Expected binary data from pipeline",
|
_ => yield ReturnSuccess::value(x),
|
||||||
"requires binary data input",
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("{:?}", err);
|
||||||
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
|
"Could not parse as SQLite",
|
||||||
|
"input cannot be parsed as SQLite",
|
||||||
&tag,
|
&tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
value_tag,
|
bytes.tag,
|
||||||
)),
|
))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -259,45 +259,26 @@ fn from_ssv(
|
|||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(name.clone()).await?;
|
||||||
let mut concat_string = String::new();
|
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
let split_at = match minimum_spaces {
|
let split_at = match minimum_spaces {
|
||||||
Some(number) => number.item,
|
Some(number) => number.item,
|
||||||
None => DEFAULT_MINIMUM_SPACES
|
None => DEFAULT_MINIMUM_SPACES
|
||||||
};
|
};
|
||||||
|
|
||||||
for value in values {
|
match from_ssv_string_to_value(&concat_string.item, headerless, aligned_columns, split_at, name.clone()) {
|
||||||
let value_tag = value.tag.clone();
|
|
||||||
latest_tag = Some(value_tag.clone());
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary (
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
&name,
|
|
||||||
"value originates from here",
|
|
||||||
&value_tag
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match from_ssv_string_to_value(&concat_string, headerless, aligned_columns, split_at, name.clone()) {
|
|
||||||
Some(x) => match x {
|
Some(x) => match x {
|
||||||
Value { value: UntaggedValue::Table(list), ..} => {
|
Value { value: UntaggedValue::Table(list), ..} => {
|
||||||
for l in list { yield ReturnSuccess::value(l) }
|
for l in list { yield ReturnSuccess::value(l) }
|
||||||
}
|
}
|
||||||
x => yield ReturnSuccess::value(x)
|
x => yield ReturnSuccess::value(x)
|
||||||
},
|
},
|
||||||
None => if let Some(tag) = latest_tag {
|
None => {
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Could not parse as SSV",
|
"Could not parse as SSV",
|
||||||
"input cannot be parsed ssv",
|
"input cannot be parsed ssv",
|
||||||
&name,
|
&name,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
&tag,
|
&concat_string.tag,
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
}
|
}
|
@ -69,33 +69,11 @@ pub fn from_toml(
|
|||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let args = args.evaluate_once(registry)?;
|
let args = args.evaluate_once(registry)?;
|
||||||
let tag = args.name_tag();
|
let tag = args.name_tag();
|
||||||
let name_span = tag.span;
|
|
||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(tag.clone()).await?;
|
||||||
|
match from_toml_string_to_value(concat_string.item, tag.clone()) {
|
||||||
let mut concat_string = String::new();
|
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
|
|
||||||
for value in values {
|
|
||||||
latest_tag = Some(value.tag.clone());
|
|
||||||
let value_span = value.tag.span;
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
name_span,
|
|
||||||
"value originates from here",
|
|
||||||
value_span,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match from_toml_string_to_value(concat_string, tag.clone()) {
|
|
||||||
Ok(x) => match x {
|
Ok(x) => match x {
|
||||||
Value { value: UntaggedValue::Table(list), .. } => {
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
for l in list {
|
for l in list {
|
||||||
@ -104,15 +82,15 @@ pub fn from_toml(
|
|||||||
}
|
}
|
||||||
x => yield ReturnSuccess::value(x),
|
x => yield ReturnSuccess::value(x),
|
||||||
},
|
},
|
||||||
Err(_) => if let Some(last_tag) = latest_tag {
|
Err(_) => {
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Could not parse as TOML",
|
"Could not parse as TOML",
|
||||||
"input cannot be parsed as TOML",
|
"input cannot be parsed as TOML",
|
||||||
&tag,
|
&tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
last_tag,
|
concat_string.tag,
|
||||||
))
|
))
|
||||||
} ,
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue};
|
||||||
|
|
||||||
pub struct FromURL;
|
pub struct FromURL;
|
||||||
|
|
||||||
@ -30,32 +30,12 @@ impl WholeStreamCommand for FromURL {
|
|||||||
fn from_url(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
fn from_url(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
let args = args.evaluate_once(registry)?;
|
let args = args.evaluate_once(registry)?;
|
||||||
let tag = args.name_tag();
|
let tag = args.name_tag();
|
||||||
let name_span = tag.span;
|
|
||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(tag.clone()).await?;
|
||||||
|
|
||||||
let mut concat_string = String::new();
|
let result = serde_urlencoded::from_str::<Vec<(String, String)>>(&concat_string.item);
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
|
|
||||||
for value in values {
|
|
||||||
latest_tag = Some(value.tag.clone());
|
|
||||||
let value_span = value.tag.span;
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
} else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
name_span,
|
|
||||||
"value originates from here",
|
|
||||||
value_span,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = serde_urlencoded::from_str::<Vec<(String, String)>>(&concat_string);
|
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
@ -68,15 +48,13 @@ fn from_url(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
|
|||||||
yield ReturnSuccess::value(row.into_value());
|
yield ReturnSuccess::value(row.into_value());
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(last_tag) = latest_tag {
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
"String not compatible with url-encoding",
|
||||||
"String not compatible with url-encoding",
|
"input not url-encoded",
|
||||||
"input not url-encoded",
|
tag,
|
||||||
tag,
|
"value originates from here",
|
||||||
"value originates from here",
|
concat_string.tag,
|
||||||
last_tag,
|
));
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
102
crates/nu-cli/src/commands/from_vcf.rs
Normal file
102
crates/nu-cli/src/commands/from_vcf.rs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
extern crate ical;
|
||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use ical::parser::vcard::component::*;
|
||||||
|
use ical::property::Property;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
||||||
|
use std::io::BufReader;
|
||||||
|
|
||||||
|
pub struct FromVcf;
|
||||||
|
|
||||||
|
impl WholeStreamCommand for FromVcf {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"from-vcf"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("from-vcf")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Parse text as .vcf and create table."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
from_vcf(args, registry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_vcf(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
|
let args = args.evaluate_once(registry)?;
|
||||||
|
let tag = args.name_tag();
|
||||||
|
let input = args.input;
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
|
let input_string = input.collect_string(tag.clone()).await?.item;
|
||||||
|
let input_bytes = input_string.as_bytes();
|
||||||
|
let buf_reader = BufReader::new(input_bytes);
|
||||||
|
let parser = ical::VcardParser::new(buf_reader);
|
||||||
|
|
||||||
|
for contact in parser {
|
||||||
|
match contact {
|
||||||
|
Ok(c) => yield ReturnSuccess::value(contact_to_value(c, tag.clone())),
|
||||||
|
Err(_) => yield Err(ShellError::labeled_error(
|
||||||
|
"Could not parse as .vcf",
|
||||||
|
"input cannot be parsed as .vcf",
|
||||||
|
tag.clone()
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn contact_to_value(contact: VcardContact, tag: Tag) -> Value {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
row.insert_untagged("properties", properties_to_value(contact.properties, tag));
|
||||||
|
row.into_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn properties_to_value(properties: Vec<Property>, tag: Tag) -> UntaggedValue {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&properties
|
||||||
|
.into_iter()
|
||||||
|
.map(|prop| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
||||||
|
|
||||||
|
let name = UntaggedValue::string(prop.name);
|
||||||
|
let value = match prop.value {
|
||||||
|
Some(val) => UntaggedValue::string(val),
|
||||||
|
None => UntaggedValue::Primitive(Primitive::Nothing),
|
||||||
|
};
|
||||||
|
let params = match prop.params {
|
||||||
|
Some(param_list) => params_to_value(param_list, tag.clone()).into(),
|
||||||
|
None => UntaggedValue::Primitive(Primitive::Nothing),
|
||||||
|
};
|
||||||
|
|
||||||
|
row.insert_untagged("name", name);
|
||||||
|
row.insert_untagged("value", value);
|
||||||
|
row.insert_untagged("params", params);
|
||||||
|
row.into_value()
|
||||||
|
})
|
||||||
|
.collect::<Vec<Value>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn params_to_value(params: Vec<(String, Vec<String>)>, tag: Tag) -> Value {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag);
|
||||||
|
|
||||||
|
for (param_name, param_values) in params {
|
||||||
|
let values: Vec<Value> = param_values.into_iter().map(|val| val.into()).collect();
|
||||||
|
let values = UntaggedValue::table(&values);
|
||||||
|
row.insert_untagged(param_name, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
row.into_value()
|
||||||
|
}
|
99
crates/nu-cli/src/commands/from_xlsx.rs
Normal file
99
crates/nu-cli/src/commands/from_xlsx.rs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use crate::TaggedListBuilder;
|
||||||
|
use calamine::*;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue};
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
pub struct FromXLSX;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct FromXLSXArgs {
|
||||||
|
headerless: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WholeStreamCommand for FromXLSX {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"from-xlsx"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("from-xlsx").switch(
|
||||||
|
"headerless",
|
||||||
|
"don't treat the first row as column names",
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Parse binary Excel(.xlsx) data and create table."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
args.process(registry, from_xlsx)?.run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_xlsx(
|
||||||
|
FromXLSXArgs {
|
||||||
|
headerless: _headerless,
|
||||||
|
}: FromXLSXArgs,
|
||||||
|
runnable_context: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let input = runnable_context.input;
|
||||||
|
let tag = runnable_context.name;
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
|
let value = input.collect_binary(tag.clone()).await?;
|
||||||
|
|
||||||
|
let mut buf: Cursor<Vec<u8>> = Cursor::new(value.item);
|
||||||
|
let mut xls = Xlsx::<_>::new(buf).map_err(|_| {
|
||||||
|
ShellError::labeled_error("Could not load xlsx file", "could not load xlsx file", &tag)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let mut dict = TaggedDictBuilder::new(&tag);
|
||||||
|
|
||||||
|
let sheet_names = xls.sheet_names().to_owned();
|
||||||
|
|
||||||
|
for sheet_name in &sheet_names {
|
||||||
|
let mut sheet_output = TaggedListBuilder::new(&tag);
|
||||||
|
|
||||||
|
if let Some(Ok(current_sheet)) = xls.worksheet_range(sheet_name) {
|
||||||
|
for row in current_sheet.rows() {
|
||||||
|
let mut row_output = TaggedDictBuilder::new(&tag);
|
||||||
|
for (i, cell) in row.iter().enumerate() {
|
||||||
|
let value = match cell {
|
||||||
|
DataType::Empty => UntaggedValue::nothing(),
|
||||||
|
DataType::String(s) => UntaggedValue::string(s),
|
||||||
|
DataType::Float(f) => UntaggedValue::decimal(*f),
|
||||||
|
DataType::Int(i) => UntaggedValue::int(*i),
|
||||||
|
DataType::Bool(b) => UntaggedValue::boolean(*b),
|
||||||
|
_ => UntaggedValue::nothing(),
|
||||||
|
};
|
||||||
|
|
||||||
|
row_output.insert_untagged(&format!("Column{}", i), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
sheet_output.push_untagged(row_output.into_untagged_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
dict.insert_untagged(sheet_name, sheet_output.into_untagged_value());
|
||||||
|
} else {
|
||||||
|
yield Err(ShellError::labeled_error(
|
||||||
|
"Could not load sheet",
|
||||||
|
"could not load sheet",
|
||||||
|
&tag,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield ReturnSuccess::value(dict.into_value());
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
@ -101,34 +101,12 @@ pub fn from_xml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value,
|
|||||||
fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
let args = args.evaluate_once(registry)?;
|
let args = args.evaluate_once(registry)?;
|
||||||
let tag = args.name_tag();
|
let tag = args.name_tag();
|
||||||
let name_span = tag.span;
|
|
||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(tag.clone()).await?;
|
||||||
|
|
||||||
let mut concat_string = String::new();
|
match from_xml_string_to_value(concat_string.item, tag.clone()) {
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
|
|
||||||
for value in values {
|
|
||||||
latest_tag = Some(value.tag.clone());
|
|
||||||
let value_span = value.tag.span;
|
|
||||||
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
name_span,
|
|
||||||
"value originates from here",
|
|
||||||
value_span,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match from_xml_string_to_value(concat_string, tag.clone()) {
|
|
||||||
Ok(x) => match x {
|
Ok(x) => match x {
|
||||||
Value { value: UntaggedValue::Table(list), .. } => {
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
for l in list {
|
for l in list {
|
||||||
@ -137,13 +115,13 @@ fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
|
|||||||
}
|
}
|
||||||
x => yield ReturnSuccess::value(x),
|
x => yield ReturnSuccess::value(x),
|
||||||
},
|
},
|
||||||
Err(_) => if let Some(last_tag) = latest_tag {
|
Err(_) => {
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Could not parse as XML",
|
"Could not parse as XML",
|
||||||
"input cannot be parsed as XML",
|
"input cannot be parsed as XML",
|
||||||
&tag,
|
&tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
&last_tag,
|
&concat_string.tag,
|
||||||
))
|
))
|
||||||
} ,
|
} ,
|
||||||
}
|
}
|
@ -121,34 +121,12 @@ pub fn from_yaml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value
|
|||||||
fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
let args = args.evaluate_once(registry)?;
|
let args = args.evaluate_once(registry)?;
|
||||||
let tag = args.name_tag();
|
let tag = args.name_tag();
|
||||||
let name_span = tag.span;
|
|
||||||
let input = args.input;
|
let input = args.input;
|
||||||
|
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values: Vec<Value> = input.values.collect().await;
|
let concat_string = input.collect_string(tag.clone()).await?;
|
||||||
|
|
||||||
let mut concat_string = String::new();
|
match from_yaml_string_to_value(concat_string.item, tag.clone()) {
|
||||||
let mut latest_tag: Option<Tag> = None;
|
|
||||||
|
|
||||||
for value in values {
|
|
||||||
latest_tag = Some(value.tag.clone());
|
|
||||||
let value_span = value.tag.span;
|
|
||||||
|
|
||||||
if let Ok(s) = value.as_string() {
|
|
||||||
concat_string.push_str(&s);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
|
||||||
"Expected a string from pipeline",
|
|
||||||
"requires string input",
|
|
||||||
name_span,
|
|
||||||
"value originates from here",
|
|
||||||
value_span,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match from_yaml_string_to_value(concat_string, tag.clone()) {
|
|
||||||
Ok(x) => match x {
|
Ok(x) => match x {
|
||||||
Value { value: UntaggedValue::Table(list), .. } => {
|
Value { value: UntaggedValue::Table(list), .. } => {
|
||||||
for l in list {
|
for l in list {
|
||||||
@ -157,15 +135,15 @@ fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
|
|||||||
}
|
}
|
||||||
x => yield ReturnSuccess::value(x),
|
x => yield ReturnSuccess::value(x),
|
||||||
},
|
},
|
||||||
Err(_) => if let Some(last_tag) = latest_tag {
|
Err(_) => {
|
||||||
yield Err(ShellError::labeled_error_with_secondary(
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
"Could not parse as YAML",
|
"Could not parse as YAML",
|
||||||
"input cannot be parsed as YAML",
|
"input cannot be parsed as YAML",
|
||||||
&tag,
|
&tag,
|
||||||
"value originates from here",
|
"value originates from here",
|
||||||
&last_tag,
|
&concat_string.tag,
|
||||||
))
|
))
|
||||||
} ,
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -1,13 +1,11 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::data::base::shape::Shapes;
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use futures_util::pin_mut;
|
|
||||||
use indexmap::set::IndexSet;
|
use indexmap::set::IndexSet;
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
did_you_mean, ColumnPath, PathMember, ReturnSuccess, ReturnValue, Signature, SyntaxShape,
|
did_you_mean, ColumnPath, PathMember, Primitive, ReturnSuccess, ReturnValue, Signature,
|
||||||
UnspannedPathMember, UntaggedValue, Value,
|
SyntaxShape, UnspannedPathMember, UntaggedValue, Value,
|
||||||
};
|
};
|
||||||
use nu_source::span_for_spanned_list;
|
use nu_source::span_for_spanned_list;
|
||||||
use nu_value_ext::get_data_by_column_path;
|
use nu_value_ext::get_data_by_column_path;
|
||||||
@ -180,23 +178,15 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
|
|||||||
|
|
||||||
pub fn get(
|
pub fn get(
|
||||||
GetArgs { rest: mut fields }: GetArgs,
|
GetArgs { rest: mut fields }: GetArgs,
|
||||||
RunnableContext { input, .. }: RunnableContext,
|
RunnableContext { mut input, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
if fields.is_empty() {
|
if fields.is_empty() {
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let values = input.values;
|
let mut vec = input.drain_vec().await;
|
||||||
pin_mut!(values);
|
|
||||||
|
|
||||||
let mut shapes = Shapes::new();
|
let descs = nu_protocol::merge_descriptors(&vec);
|
||||||
let mut index = 0;
|
for desc in descs {
|
||||||
|
yield ReturnSuccess::value(desc);
|
||||||
while let Some(row) = values.next().await {
|
|
||||||
shapes.add(&row, index);
|
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for row in shapes.to_values() {
|
|
||||||
yield ReturnSuccess::value(row);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -231,6 +221,10 @@ pub fn get(
|
|||||||
result.push_back(ReturnSuccess::value(item.clone()));
|
result.push_back(ReturnSuccess::value(item.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::Nothing),
|
||||||
|
..
|
||||||
|
} => {}
|
||||||
other => result.push_back(ReturnSuccess::value(other.clone())),
|
other => result.push_back(ReturnSuccess::value(other.clone())),
|
||||||
},
|
},
|
||||||
Err(reason) => result.push_back(ReturnSuccess::value(
|
Err(reason) => result.push_back(ReturnSuccess::value(
|
80
crates/nu-cli/src/commands/headers.rs
Normal file
80
crates/nu-cli/src/commands/headers.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::context::CommandRegistry;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use futures::stream::StreamExt;
|
||||||
|
use indexmap::IndexMap;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::Dictionary;
|
||||||
|
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value};
|
||||||
|
|
||||||
|
pub struct Headers;
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct HeadersArgs {}
|
||||||
|
|
||||||
|
impl WholeStreamCommand for Headers {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"headers"
|
||||||
|
}
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("headers")
|
||||||
|
}
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Use the first row of the table as column names"
|
||||||
|
}
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
args.process(registry, headers)?.run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn headers(
|
||||||
|
HeadersArgs {}: HeadersArgs,
|
||||||
|
RunnableContext { input, .. }: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let stream = async_stream! {
|
||||||
|
let rows: Vec<Value> = input.values.collect().await;
|
||||||
|
|
||||||
|
if rows.len() < 1 {
|
||||||
|
yield Err(ShellError::untagged_runtime_error("Couldn't find headers, was the input a properly formatted, non-empty table?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//the headers are the first row in the table
|
||||||
|
let headers: Vec<String> = match &rows[0].value {
|
||||||
|
UntaggedValue::Row(d) => {
|
||||||
|
Ok(d.entries.iter().map(|(k, v)| {
|
||||||
|
match v.as_string() {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(_) => { //If a cell that should contain a header name is empty, we name the column Column[index]
|
||||||
|
match d.entries.get_full(k) {
|
||||||
|
Some((index, _, _)) => format!("Column{}", index),
|
||||||
|
None => "unknownColumn".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).collect())
|
||||||
|
}
|
||||||
|
_ => Err(ShellError::unexpected_eof("Could not get headers, is the table empty?", rows[0].tag.span))
|
||||||
|
}?;
|
||||||
|
|
||||||
|
//Each row is a dictionary with the headers as keys
|
||||||
|
for r in rows.iter().skip(1) {
|
||||||
|
match &r.value {
|
||||||
|
UntaggedValue::Row(d) => {
|
||||||
|
let mut i = 0;
|
||||||
|
let mut entries = IndexMap::new();
|
||||||
|
for (_, v) in d.entries.iter() {
|
||||||
|
entries.insert(headers[i].clone(), v.clone());
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
yield Ok(ReturnSuccess::Value(UntaggedValue::Row(Dictionary{entries}).into_value(r.tag.clone())))
|
||||||
|
}
|
||||||
|
_ => yield Err(ShellError::unexpected_eof("Couldn't iterate through rows, was the input a properly formatted table?", r.tag.span))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
@ -87,8 +87,8 @@ Here are some tips to help you get started.
|
|||||||
* help commands - list all available commands
|
* help commands - list all available commands
|
||||||
* help <command name> - display help about a particular command
|
* help <command name> - display help about a particular command
|
||||||
|
|
||||||
Nushell works on the idea of a "pipeline". Pipelines are commands connected with the '|' character. Each stage
|
Nushell works on the idea of a "pipeline". Pipelines are commands connected with the '|' character.
|
||||||
in the pipeline works together to load, parse, and display information to you.
|
Each stage in the pipeline works together to load, parse, and display information to you.
|
||||||
|
|
||||||
[Examples]
|
[Examples]
|
||||||
|
|
116
crates/nu-cli/src/commands/lines.rs
Normal file
116
crates/nu-cli/src/commands/lines.rs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
||||||
|
|
||||||
|
pub struct Lines;
|
||||||
|
|
||||||
|
impl WholeStreamCommand for Lines {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"lines"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("lines")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Split single string into rows, one per line."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
lines(args, registry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ends_with_line_ending(st: &str) -> bool {
|
||||||
|
let mut temp = st.to_string();
|
||||||
|
let last = temp.pop();
|
||||||
|
if let Some(c) = last {
|
||||||
|
c == '\n'
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
|
let args = args.evaluate_once(registry)?;
|
||||||
|
let tag = args.name_tag();
|
||||||
|
let name_span = tag.span;
|
||||||
|
let mut input = args.input;
|
||||||
|
|
||||||
|
let mut leftover = vec![];
|
||||||
|
let mut leftover_string = String::new();
|
||||||
|
let stream = async_stream! {
|
||||||
|
loop {
|
||||||
|
match input.values.next().await {
|
||||||
|
Some(Value { value: UntaggedValue::Primitive(Primitive::String(st)), ..}) => {
|
||||||
|
let mut st = leftover_string.clone() + &st;
|
||||||
|
leftover.clear();
|
||||||
|
|
||||||
|
let mut lines: Vec<String> = st.lines().map(|x| x.to_string()).collect();
|
||||||
|
|
||||||
|
if !ends_with_line_ending(&st) {
|
||||||
|
if let Some(last) = lines.pop() {
|
||||||
|
leftover_string = last;
|
||||||
|
} else {
|
||||||
|
leftover_string.clear();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
leftover_string.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
let success_lines: Vec<_> = lines.iter().map(|x| ReturnSuccess::value(UntaggedValue::line(x).into_untagged_value())).collect();
|
||||||
|
yield futures::stream::iter(success_lines)
|
||||||
|
}
|
||||||
|
Some(Value { value: UntaggedValue::Primitive(Primitive::Line(st)), ..}) => {
|
||||||
|
let mut st = leftover_string.clone() + &st;
|
||||||
|
leftover.clear();
|
||||||
|
|
||||||
|
let mut lines: Vec<String> = st.lines().map(|x| x.to_string()).collect();
|
||||||
|
if !ends_with_line_ending(&st) {
|
||||||
|
if let Some(last) = lines.pop() {
|
||||||
|
leftover_string = last;
|
||||||
|
} else {
|
||||||
|
leftover_string.clear();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
leftover_string.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
let success_lines: Vec<_> = lines.iter().map(|x| ReturnSuccess::value(UntaggedValue::line(x).into_untagged_value())).collect();
|
||||||
|
yield futures::stream::iter(success_lines)
|
||||||
|
}
|
||||||
|
Some( Value { tag: value_span, ..}) => {
|
||||||
|
yield futures::stream::iter(vec![Err(ShellError::labeled_error_with_secondary(
|
||||||
|
"Expected a string from pipeline",
|
||||||
|
"requires string input",
|
||||||
|
name_span,
|
||||||
|
"value originates from here",
|
||||||
|
value_span,
|
||||||
|
))]);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
if !leftover.is_empty() {
|
||||||
|
let mut st = leftover_string.clone();
|
||||||
|
if let Ok(extra) = String::from_utf8(leftover) {
|
||||||
|
st.push_str(&extra);
|
||||||
|
}
|
||||||
|
yield futures::stream::iter(vec![ReturnSuccess::value(UntaggedValue::string(st).into_untagged_value())])
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !leftover_string.is_empty() {
|
||||||
|
yield futures::stream::iter(vec![ReturnSuccess::value(UntaggedValue::string(leftover_string).into_untagged_value())]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
@ -10,6 +10,7 @@ pub struct Ls;
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct LsArgs {
|
pub struct LsArgs {
|
||||||
pub path: Option<Tagged<PathBuf>>,
|
pub path: Option<Tagged<PathBuf>>,
|
||||||
|
pub all: bool,
|
||||||
pub full: bool,
|
pub full: bool,
|
||||||
#[serde(rename = "short-names")]
|
#[serde(rename = "short-names")]
|
||||||
pub short_names: bool,
|
pub short_names: bool,
|
||||||
@ -29,6 +30,7 @@ impl PerItemCommand for Ls {
|
|||||||
SyntaxShape::Pattern,
|
SyntaxShape::Pattern,
|
||||||
"a path to get the directory contents from",
|
"a path to get the directory contents from",
|
||||||
)
|
)
|
||||||
|
.switch("all", "also show hidden files", Some('a'))
|
||||||
.switch(
|
.switch(
|
||||||
"full",
|
"full",
|
||||||
"list all available columns for each entry",
|
"list all available columns for each entry",
|
@ -80,7 +80,7 @@ fn pick(
|
|||||||
"No data to fetch.",
|
"No data to fetch.",
|
||||||
format!("Couldn't pick column \"{}\"", column),
|
format!("Couldn't pick column \"{}\"", column),
|
||||||
path_member_tried.span,
|
path_member_tried.span,
|
||||||
format!("How about exploring it with \"get\"? Check the input is appropiate originating from here"),
|
format!("How about exploring it with \"get\"? Check the input is appropriate originating from here"),
|
||||||
obj_source.tag.span)
|
obj_source.tag.span)
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,9 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value};
|
use nu_protocol::{
|
||||||
|
merge_descriptors, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue,
|
||||||
|
};
|
||||||
use nu_source::{SpannedItem, Tagged};
|
use nu_source::{SpannedItem, Tagged};
|
||||||
use nu_value_ext::get_data_by_key;
|
use nu_value_ext::get_data_by_key;
|
||||||
|
|
||||||
@ -52,18 +54,6 @@ impl WholeStreamCommand for Pivot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge_descriptors(values: &[Value]) -> Vec<String> {
|
|
||||||
let mut ret = vec![];
|
|
||||||
for value in values {
|
|
||||||
for desc in value.data_descriptors() {
|
|
||||||
if !ret.contains(&desc) {
|
|
||||||
ret.push(desc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pivot(args: PivotArgs, context: RunnableContext) -> Result<OutputStream, ShellError> {
|
pub fn pivot(args: PivotArgs, context: RunnableContext) -> Result<OutputStream, ShellError> {
|
||||||
let stream = async_stream! {
|
let stream = async_stream! {
|
||||||
let input = context.input.into_vec().await;
|
let input = context.input.into_vec().await;
|
97
crates/nu-cli/src/commands/rename.rs
Normal file
97
crates/nu-cli/src/commands/rename.rs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use indexmap::IndexMap;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
|
use nu_source::Tagged;
|
||||||
|
|
||||||
|
pub struct Rename;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Arguments {
|
||||||
|
column_name: Tagged<String>,
|
||||||
|
rest: Vec<Tagged<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WholeStreamCommand for Rename {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"rename"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("rename")
|
||||||
|
.required(
|
||||||
|
"column_name",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the name of the column to rename for",
|
||||||
|
)
|
||||||
|
.rest(
|
||||||
|
SyntaxShape::Member,
|
||||||
|
"Additional column name(s) to rename for",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Creates a new table with columns renamed."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
args.process(registry, rename)?.run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rename(
|
||||||
|
Arguments { column_name, rest }: Arguments,
|
||||||
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let mut new_column_names = vec![vec![column_name]];
|
||||||
|
new_column_names.push(rest);
|
||||||
|
|
||||||
|
let new_column_names = new_column_names.into_iter().flatten().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let stream = input
|
||||||
|
.values
|
||||||
|
.map(move |item| {
|
||||||
|
let mut result = VecDeque::new();
|
||||||
|
|
||||||
|
if let Value {
|
||||||
|
value: UntaggedValue::Row(row),
|
||||||
|
tag,
|
||||||
|
} = item
|
||||||
|
{
|
||||||
|
let mut renamed_row = IndexMap::new();
|
||||||
|
|
||||||
|
for (idx, (key, value)) in row.entries.iter().enumerate() {
|
||||||
|
let key = if idx < new_column_names.len() {
|
||||||
|
&new_column_names[idx].item
|
||||||
|
} else {
|
||||||
|
key
|
||||||
|
};
|
||||||
|
|
||||||
|
renamed_row.insert(key.clone(), value.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let out = UntaggedValue::Row(renamed_row.into()).into_value(tag);
|
||||||
|
|
||||||
|
result.push_back(ReturnSuccess::value(out));
|
||||||
|
} else {
|
||||||
|
result.push_back(ReturnSuccess::value(
|
||||||
|
UntaggedValue::Error(ShellError::labeled_error(
|
||||||
|
"no column names available",
|
||||||
|
"can't rename",
|
||||||
|
&name,
|
||||||
|
))
|
||||||
|
.into_untagged_value(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
futures::stream::iter(result)
|
||||||
|
})
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
@ -10,7 +10,7 @@ pub struct Remove;
|
|||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct RemoveArgs {
|
pub struct RemoveArgs {
|
||||||
pub target: Tagged<PathBuf>,
|
pub rest: Vec<Tagged<PathBuf>>,
|
||||||
pub recursive: Tagged<bool>,
|
pub recursive: Tagged<bool>,
|
||||||
pub trash: Tagged<bool>,
|
pub trash: Tagged<bool>,
|
||||||
}
|
}
|
||||||
@ -22,17 +22,17 @@ impl PerItemCommand for Remove {
|
|||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("rm")
|
Signature::build("rm")
|
||||||
.required("path", SyntaxShape::Pattern, "the file path to remove")
|
|
||||||
.switch(
|
.switch(
|
||||||
"trash",
|
"trash",
|
||||||
"use the platform's recycle bin instead of permanently deleting",
|
"use the platform's recycle bin instead of permanently deleting",
|
||||||
Some('t'),
|
Some('t'),
|
||||||
)
|
)
|
||||||
.switch("recursive", "delete subdirectories recursively", Some('r'))
|
.switch("recursive", "delete subdirectories recursively", Some('r'))
|
||||||
|
.rest(SyntaxShape::Pattern, "the file path(s) to remove")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Remove a file"
|
"Remove file(s)"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
@ -7,6 +7,22 @@ use std::path::{Path, PathBuf};
|
|||||||
|
|
||||||
pub struct Save;
|
pub struct Save;
|
||||||
|
|
||||||
|
macro_rules! process_unknown {
|
||||||
|
($scope:tt, $input:ident, $name_tag:ident) => {{
|
||||||
|
if $input.len() > 0 {
|
||||||
|
match $input[0] {
|
||||||
|
Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::Binary(_)),
|
||||||
|
..
|
||||||
|
} => process_binary!($scope, $input, $name_tag),
|
||||||
|
_ => process_string!($scope, $input, $name_tag),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
process_string!($scope, $input, $name_tag)
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! process_string {
|
macro_rules! process_string {
|
||||||
($scope:tt, $input:ident, $name_tag:ident) => {{
|
($scope:tt, $input:ident, $name_tag:ident) => {{
|
||||||
let mut result_string = String::new();
|
let mut result_string = String::new();
|
||||||
@ -31,6 +47,32 @@ macro_rules! process_string {
|
|||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! process_binary {
|
||||||
|
($scope:tt, $input:ident, $name_tag:ident) => {{
|
||||||
|
let mut result_binary: Vec<u8> = Vec::new();
|
||||||
|
for res in $input {
|
||||||
|
match res {
|
||||||
|
Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::Binary(b)),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
for u in b.into_iter() {
|
||||||
|
result_binary.push(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
break $scope Err(ShellError::labeled_error(
|
||||||
|
"Save could not successfully save",
|
||||||
|
"unexpected data during binary save",
|
||||||
|
$name_tag,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(result_binary)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! process_string_return_success {
|
macro_rules! process_string_return_success {
|
||||||
($scope:tt, $result_vec:ident, $name_tag:ident) => {{
|
($scope:tt, $result_vec:ident, $name_tag:ident) => {{
|
||||||
let mut result_string = String::new();
|
let mut result_string = String::new();
|
||||||
@ -204,10 +246,10 @@ fn save(
|
|||||||
process_string_return_success!('scope, result_vec, name_tag)
|
process_string_return_success!('scope, result_vec, name_tag)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
process_string!('scope, input, name_tag)
|
process_unknown!('scope, input, name_tag)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
process_string!('scope, input, name_tag)
|
process_unknown!('scope, input, name_tag)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(string_from(&input).into_bytes())
|
Ok(string_from(&input).into_bytes())
|
69
crates/nu-cli/src/commands/shuffle.rs
Normal file
69
crates/nu-cli/src/commands/shuffle.rs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::context::CommandRegistry;
|
||||||
|
use crate::prelude::*;
|
||||||
|
use nu_errors::ShellError;
|
||||||
|
use nu_protocol::{ReturnSuccess, ReturnValue, Signature, SyntaxShape, Value};
|
||||||
|
use nu_source::Tagged;
|
||||||
|
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use rand::thread_rng;
|
||||||
|
|
||||||
|
pub struct Shuffle;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Arguments {
|
||||||
|
#[serde(rename = "num")]
|
||||||
|
limit: Option<Tagged<u64>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WholeStreamCommand for Shuffle {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"shuffle"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("shuffle").named(
|
||||||
|
"num",
|
||||||
|
SyntaxShape::Int,
|
||||||
|
"Limit `num` number of rows",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Shuffle rows randomly."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
args.process(registry, shuffle)?.run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shuffle(
|
||||||
|
Arguments { limit }: Arguments,
|
||||||
|
RunnableContext { input, .. }: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let stream = async_stream! {
|
||||||
|
let mut values: Vec<Value> = input.values.collect().await;
|
||||||
|
|
||||||
|
let out = if let Some(n) = limit {
|
||||||
|
let (shuffled, _) = values.partial_shuffle(&mut thread_rng(), *n as usize);
|
||||||
|
shuffled.to_vec()
|
||||||
|
} else {
|
||||||
|
values.shuffle(&mut thread_rng());
|
||||||
|
values.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
for val in out.into_iter() {
|
||||||
|
yield ReturnSuccess::value(val);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let stream: BoxStream<'static, ReturnValue> = stream.boxed();
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user