From a09361698ee7bc5a26e0300cdeb5e2e8096a6e26 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 27 Jul 2019 19:45:00 +1200 Subject: [PATCH 1/3] Update plugin protocol for begin, and create new sys plugin --- Cargo.lock | 200 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 + src/commands/command.rs | 2 +- src/commands/plugin.rs | 94 +++++++++++-------- src/commands/to_json.rs | 1 + src/commands/to_toml.rs | 3 + src/commands/to_yaml.rs | 1 + src/lib.rs | 2 + src/object/base.rs | 5 + src/parser/registry.rs | 2 +- src/plugin.rs | 24 ++--- src/plugins/add.rs | 4 +- src/plugins/edit.rs | 4 +- src/plugins/inc.rs | 4 +- src/plugins/skip.rs | 4 +- src/plugins/sum.rs | 4 +- src/plugins/sys.rs | 111 ++++++++++++++++++++++ src/plugins/textview.rs | 6 -- src/plugins/tree.rs | 2 - 19 files changed, 402 insertions(+), 76 deletions(-) create mode 100644 src/plugins/sys.rs diff --git a/Cargo.lock b/Cargo.lock index 16dc65e16f..9f66f70445 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1087,6 +1087,11 @@ dependencies = [ "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "h2" version = "0.1.24" @@ -1104,6 +1109,150 @@ dependencies = [ "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "heim" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-cpu 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-disk 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-host 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-memory 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-net 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-process 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-virt 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-common" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-preview 0.3.0-alpha.17 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-cpu" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-derive" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-disk" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-host" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "platforms 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-memory" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-net" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "macaddr 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-process" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heim-virt" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "http" version = "0.1.17" @@ -1450,6 +1599,19 @@ name = "lzw" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "macaddr" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "mach" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "malloc_buf" version = "0.0.6" @@ -1665,6 +1827,7 @@ dependencies = [ "futures_codec 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "getset 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "heim 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.21.2 (registry+https://github.com/rust-lang/crates.io-index)", "indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2006,6 +2169,11 @@ name = "pkg-config" version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "platforms" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "plist" version = "0.4.2" @@ -2277,6 +2445,16 @@ dependencies = [ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "raw-cpuid" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rawkey" version = "0.1.1" @@ -3308,6 +3486,11 @@ name = "wasm-bindgen-shared" version = "0.2.47" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "widestring" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.8" @@ -3536,7 +3719,19 @@ dependencies = [ "checksum getset 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "19fbde0fad0c1c1f9474694b1f5c9ba22b09f2f74f74e6d2bd19c43f6656e2cb" "checksum gif 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "86c2f2b597d6e05c86ee5947b2223bda468fe8dad3e88e2a6520869322aaf568" "checksum git2 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "924b2e7d2986e625dcad89e8a429a7b3adee3c3d71e585f4a66c4f7e78715e31" +"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" "checksum h2 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "69b2a5a3092cbebbc951fe55408402e696ee2ed09019137d1800fc2c411265d2" +"checksum heim 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "82b496d71b38a4b0f12bfaad79171efd3be97aea00a1e017234a670820ee2fa6" +"checksum heim-common 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "699d2246e609d4de22b927bde93bbc7e512b70b1ed97754ac9682d80dc242edc" +"checksum heim-cpu 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bf2a2a36278b48ac5720710fd325b0651dd00e37d4e8d9e9f9dfc002a62beff4" +"checksum heim-derive 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "cdbb08c94f6e0d59664522d24b16940cf9ae3f2a5f850856cf3f589e112576ad" +"checksum heim-disk 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "26198ff923c9a6ba4534e1253964f5d85160359a55c51f9f9fa954f23ca114aa" +"checksum heim-host 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b149c1ab5826dc9077f22f80f483bcc4deda1b73811d633ac0c883d5a8c9918e" +"checksum heim-memory 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "37c50585f3f7ef1bb27462b05ed62210455f80d977ad154f4713cc090e1450c7" +"checksum heim-net 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "efae819801bb807da8008e7b0fc3344595369b5eb1105afefb90671c5655fba3" +"checksum heim-process 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "942d304a4fba16eef1c753c354bc433e8b3734165a2cd60780aacdb88f461600" +"checksum heim-virt 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "de3cee31294ee1a4be69af34df42070273e85406d1562e978fcef647eedd2940" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "eed324f0f0daf6ec10c474f150505af2c143f251722bf9dbd1261bd1f2ee2c1a" "checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83" @@ -3574,6 +3769,8 @@ dependencies = [ "checksum logos 0.10.0-rc2 (registry+https://github.com/rust-lang/crates.io-index)" = "e136962e0902a48fd1d8da8706fac078fdba547bf82f9d9d728cf551d367b41e" "checksum logos-derive 0.10.0-rc2 (registry+https://github.com/rust-lang/crates.io-index)" = "5f03ecd1d993aacc6c4f3a9540e60a4f3811ddac2276dbb66dad4d42671bd5bf" "checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" +"checksum macaddr 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ff4752cb15cffb3e68f7dcb22e0818ac871f8c98fb07a634a81f41fb202a09f" +"checksum mach 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" "checksum malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" @@ -3627,6 +3824,7 @@ dependencies = [ "checksum phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" +"checksum platforms 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6cfec0daac55b13af394ceaaad095d17c790f77bdc9329264f06e49d6cd3206c" "checksum plist 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a9f075f6394100e7c105ed1af73fb1859d6fd14e49d4290d578120beb167f" "checksum png 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)" = "63daf481fdd0defa2d1d2be15c674fbfa1b0fd71882c303a91f9a79b3252c359" "checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b" @@ -3654,6 +3852,7 @@ dependencies = [ "checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" "checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +"checksum raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "30a9d219c32c9132f7be513c18be77c9881c7107d2ab5569d205a6a0f0e6dc7d" "checksum rawkey 0.1.1 (git+https://github.com/jonathandturner/rawkey)" = "" "checksum rayon 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a4b0186e22767d5b9738a05eab7c6ac90b15db17e5b5f9bd87976dd7d89a10a4" "checksum rayon-core 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbe0df8435ac0c397d467b6cad6d25543d06e8a019ef3f6af3c384597515bd2" @@ -3772,6 +3971,7 @@ dependencies = [ "checksum wasm-bindgen-macro 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "15c29f04eb117312931e7b02878453ee63d67a6f291797651890128bf5ee71db" "checksum wasm-bindgen-macro-support 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "92b1356b623816248dfe0e2c4b7e113618d647808907ff6a3d9838ebee8e82ee" "checksum wasm-bindgen-shared 0.2.47 (registry+https://github.com/rust-lang/crates.io-index)" = "15de16ddb30cfd424a87598b30021491bae1607d32e52056979865c98b7913b4" +"checksum widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index 7222fa8a2e..84bc8ff719 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,6 +79,7 @@ semver = "0.9.0" uuid = {version = "0.7.4", features = [ "v4", "serde" ]} syntect = "3.2.0" strip-ansi-escapes = "0.1.0" +heim = "0.0.5" [dev-dependencies] pretty_assertions = "0.6.1" @@ -107,6 +108,10 @@ path = "src/plugins/edit.rs" name = "nu_plugin_skip" path = "src/plugins/skip.rs" +[[bin]] +name = "nu_plugin_sys" +path = "src/plugins/sys.rs" + [[bin]] name = "nu_plugin_tree" path = "src/plugins/tree.rs" diff --git a/src/commands/command.rs b/src/commands/command.rs index e5057c6cd1..825afa5925 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; use uuid::Uuid; -#[derive(Deserialize, Serialize, Debug)] +#[derive(Deserialize, Serialize, Debug, Clone)] pub struct CallInfo { pub args: Args, pub source_map: SourceMap, diff --git a/src/commands/plugin.rs b/src/commands/plugin.rs index 310281ddde..2394a097f4 100644 --- a/src/commands/plugin.rs +++ b/src/commands/plugin.rs @@ -78,46 +78,64 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { - let response = serde_json::from_str::(&input); - match response { - Ok(NuResult::response { params }) => match params { - Ok(_) => {} - Err(e) => { - return Err(e); - } - }, - Err(e) => { - return Err(ShellError::string(format!( - "Error while processing input: {:?} {}", - e, input - ))); - } - } - } - _ => {} - } - } + let mut bos: VecDeque> = VecDeque::new(); + bos.push_back(Value::Primitive(Primitive::BeginningOfStream).spanned_unknown()); let mut eos: VecDeque> = VecDeque::new(); eos.push_back(Value::Primitive(Primitive::EndOfStream).spanned_unknown()); - let stream = args - .input - .values + let call_info = args.call_info; + + let stream = bos + .chain(args.input.values) .chain(eos) .map(move |v| match v { + Spanned { + item: Value::Primitive(Primitive::BeginningOfStream), + .. + } => { + let stdin = child.stdin.as_mut().expect("Failed to open stdin"); + let stdout = child.stdout.as_mut().expect("Failed to open stdout"); + + let mut reader = BufReader::new(stdout); + + let request = JsonRpc::new("begin_filter", call_info.clone()); + let request_raw = serde_json::to_string(&request).unwrap(); + let _ = stdin.write(format!("{}\n", request_raw).as_bytes()); // TODO: Handle error + + let mut input = String::new(); + match reader.read_line(&mut input) { + Ok(_) => { + let response = serde_json::from_str::(&input); + match response { + Ok(NuResult::response { params }) => match params { + Ok(params) => params, + Err(e) => { + let mut result = VecDeque::new(); + result.push_back(ReturnValue::Err(e)); + result + } + }, + Err(e) => { + let mut result = VecDeque::new(); + result.push_back(Err(ShellError::string(format!( + "Error while processing begin_filter response: {:?} {}", + e, input + )))); + result + } + } + } + Err(e) => { + let mut result = VecDeque::new(); + result.push_back(Err(ShellError::string(format!( + "Error while reading begin_filter response: {:?}", + e + )))); + result + } + } + } Spanned { item: Value::Primitive(Primitive::EndOfStream), .. @@ -154,7 +172,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { let mut result = VecDeque::new(); result.push_back(Err(ShellError::string(format!( - "Error while processing input: {:?} {}", + "Error while processing end_filter response: {:?} {}", e, input )))); result @@ -164,7 +182,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { let mut result = VecDeque::new(); result.push_back(Err(ShellError::string(format!( - "Error while processing input: {:?}", + "Error while reading end_filter: {:?}", e )))); result @@ -197,7 +215,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { let mut result = VecDeque::new(); result.push_back(Err(ShellError::string(format!( - "Error while processing input: {:?} {}", + "Error while processing filter response: {:?} {}", e, input )))); result @@ -207,7 +225,7 @@ pub fn filter_plugin(path: String, args: CommandArgs) -> Result { let mut result = VecDeque::new(); result.push_back(Err(ShellError::string(format!( - "Error while processing input: {:?}", + "Error while reading filter response: {:?}", e )))); result diff --git a/src/commands/to_json.rs b/src/commands/to_json.rs index 5cd4c913ce..8c7d25339a 100644 --- a/src/commands/to_json.rs +++ b/src/commands/to_json.rs @@ -9,6 +9,7 @@ pub fn value_to_json_value(v: &Value) -> serde_json::Value { } Value::Primitive(Primitive::Date(d)) => serde_json::Value::String(d.to_string()), Value::Primitive(Primitive::EndOfStream) => serde_json::Value::Null, + Value::Primitive(Primitive::BeginningOfStream) => serde_json::Value::Null, Value::Primitive(Primitive::Float(f)) => { serde_json::Value::Number(serde_json::Number::from_f64(f.into_inner()).unwrap()) } diff --git a/src/commands/to_toml.rs b/src/commands/to_toml.rs index 1551ba78d5..e641410ec2 100644 --- a/src/commands/to_toml.rs +++ b/src/commands/to_toml.rs @@ -9,6 +9,9 @@ pub fn value_to_toml_value(v: &Value) -> toml::Value { Value::Primitive(Primitive::EndOfStream) => { toml::Value::String("".to_string()) } + Value::Primitive(Primitive::BeginningOfStream) => { + toml::Value::String("".to_string()) + } Value::Primitive(Primitive::Float(f)) => toml::Value::Float(f.into_inner()), Value::Primitive(Primitive::Int(i)) => toml::Value::Integer(*i), Value::Primitive(Primitive::Nothing) => toml::Value::String("".to_string()), diff --git a/src/commands/to_yaml.rs b/src/commands/to_yaml.rs index 66db5afa40..db529c0351 100644 --- a/src/commands/to_yaml.rs +++ b/src/commands/to_yaml.rs @@ -9,6 +9,7 @@ pub fn value_to_yaml_value(v: &Value) -> serde_yaml::Value { } Value::Primitive(Primitive::Date(d)) => serde_yaml::Value::String(d.to_string()), Value::Primitive(Primitive::EndOfStream) => serde_yaml::Value::Null, + Value::Primitive(Primitive::BeginningOfStream) => serde_yaml::Value::Null, Value::Primitive(Primitive::Float(f)) => { serde_yaml::Value::Number(serde_yaml::Number::from(f.into_inner())) } diff --git a/src/lib.rs b/src/lib.rs index 9fef9eab1d..9411668b08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,11 +27,13 @@ mod stream; pub use crate::commands::command::{CallInfo, ReturnSuccess, ReturnValue}; pub use crate::context::{SourceMap, SpanSource}; pub use crate::env::host::BasicHost; +pub use crate::parser::parse::span::Span; pub use crate::parser::parse::span::SpannedItem; pub use crate::parser::Spanned; pub use crate::plugin::{serve_plugin, Plugin}; pub use cli::cli; pub use errors::ShellError; pub use object::base::{Primitive, Value}; +pub use object::dict::{Dictionary, SpannedDictBuilder}; pub use parser::parse::text::Text; pub use parser::registry::{Args, CommandConfig, NamedType, PositionalType}; diff --git a/src/object/base.rs b/src/object/base.rs index 168bbdd8ea..3d3aadc6b2 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -43,6 +43,8 @@ pub enum Primitive { Date(DateTime), Path(PathBuf), + // Stream markers (used as bookend markers rather than actual values) + BeginningOfStream, EndOfStream, } @@ -52,6 +54,7 @@ impl Primitive { match self { Nothing => "nothing", + BeginningOfStream => "beginning-of-stream", EndOfStream => "end-of-stream", Path(_) => "path", Int(_) => "int", @@ -69,6 +72,7 @@ impl Primitive { match self { Nothing => write!(f, "Nothing"), + BeginningOfStream => write!(f, "BeginningOfStream"), EndOfStream => write!(f, "EndOfStream"), Int(int) => write!(f, "{}", int), Path(path) => write!(f, "{}", path.display()), @@ -83,6 +87,7 @@ impl Primitive { pub fn format(&self, field_name: Option<&String>) -> String { match self { Primitive::Nothing => format!("{}", Color::Black.bold().paint("-")), + Primitive::BeginningOfStream => format!("{}", Color::Black.bold().paint("-")), Primitive::EndOfStream => format!("{}", Color::Black.bold().paint("-")), Primitive::Path(p) => format!("{}", p.display()), Primitive::Bytes(b) => { diff --git a/src/parser/registry.rs b/src/parser/registry.rs index 804dfe48cd..e734c6725e 100644 --- a/src/parser/registry.rs +++ b/src/parser/registry.rs @@ -79,7 +79,7 @@ pub struct CommandConfig { pub is_sink: bool, } -#[derive(Debug, Default, new, Serialize, Deserialize)] +#[derive(Debug, Default, new, Serialize, Deserialize, Clone)] pub struct Args { pub positional: Option>>, pub named: Option>>, diff --git a/src/plugin.rs b/src/plugin.rs index d368205856..51366b3237 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -5,14 +5,12 @@ use std::io; pub trait Plugin { fn config(&mut self) -> Result; #[allow(unused)] - fn begin_filter(&mut self, call_info: CallInfo) -> Result<(), ShellError> { - Err(ShellError::string( - "`begin_filter` not implemented in plugin", - )) + fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { + Ok(vec![]) } #[allow(unused)] fn filter(&mut self, input: Spanned) -> Result, ShellError> { - Err(ShellError::string("`filter` not implemented in plugin")) + Ok(vec![]) } #[allow(unused)] fn end_filter(&mut self) -> Result, ShellError> { @@ -21,9 +19,7 @@ pub trait Plugin { #[allow(unused)] fn sink(&mut self, call_info: CallInfo, input: Vec>) {} - fn quit(&mut self) { - return; - } + fn quit(&mut self) {} } pub fn serve_plugin(plugin: &mut dyn Plugin) { @@ -37,11 +33,7 @@ pub fn serve_plugin(plugin: &mut dyn Plugin) { send_response(plugin.config()); } Ok(NuCommand::begin_filter { params }) => { - send_response( - plugin - .begin_filter(params) - .map(|_| Vec::::new()), - ); + send_response(plugin.begin_filter(params)); } Ok(NuCommand::filter { params }) => { send_response(plugin.filter(params)); @@ -78,11 +70,7 @@ pub fn serve_plugin(plugin: &mut dyn Plugin) { send_response(plugin.config()); } Ok(NuCommand::begin_filter { params }) => { - send_response( - plugin - .begin_filter(params) - .map(|_| Vec::::new()), - ); + send_response(plugin.begin_filter(params)); } Ok(NuCommand::filter { params }) => { send_response(plugin.filter(params)); diff --git a/src/plugins/add.rs b/src/plugins/add.rs index 7a80a37b54..848bc32d00 100644 --- a/src/plugins/add.rs +++ b/src/plugins/add.rs @@ -53,7 +53,7 @@ impl Plugin for Add { rest_positional: true, }) } - fn begin_filter(&mut self, call_info: CallInfo) -> Result<(), ShellError> { + fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { match &args[0] { Spanned { @@ -76,7 +76,7 @@ impl Plugin for Add { } } - Ok(()) + Ok(vec![]) } fn filter(&mut self, input: Spanned) -> Result, ShellError> { diff --git a/src/plugins/edit.rs b/src/plugins/edit.rs index c37fd2ba66..01e7344a7f 100644 --- a/src/plugins/edit.rs +++ b/src/plugins/edit.rs @@ -53,7 +53,7 @@ impl Plugin for Edit { rest_positional: true, }) } - fn begin_filter(&mut self, call_info: CallInfo) -> Result<(), ShellError> { + fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { match &args[0] { Spanned { @@ -76,7 +76,7 @@ impl Plugin for Edit { } } - Ok(()) + Ok(vec![]) } fn filter(&mut self, input: Spanned) -> Result, ShellError> { diff --git a/src/plugins/inc.rs b/src/plugins/inc.rs index f03edc985f..ee5b03a1f8 100644 --- a/src/plugins/inc.rs +++ b/src/plugins/inc.rs @@ -99,7 +99,7 @@ impl Plugin for Inc { rest_positional: true, }) } - fn begin_filter(&mut self, call_info: CallInfo) -> Result<(), ShellError> { + fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if call_info.args.has("major") { self.major = true; } @@ -129,7 +129,7 @@ impl Plugin for Inc { } } - Ok(()) + Ok(vec![]) } fn filter(&mut self, input: Spanned) -> Result, ShellError> { diff --git a/src/plugins/skip.rs b/src/plugins/skip.rs index a9ce8c630b..321f731a35 100644 --- a/src/plugins/skip.rs +++ b/src/plugins/skip.rs @@ -24,7 +24,7 @@ impl Plugin for Skip { rest_positional: true, }) } - fn begin_filter(&mut self, call_info: CallInfo) -> Result<(), ShellError> { + fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { if let Some(args) = call_info.args.positional { for arg in args { match arg { @@ -45,7 +45,7 @@ impl Plugin for Skip { } } - Ok(()) + Ok(vec![]) } fn filter(&mut self, input: Spanned) -> Result, ShellError> { diff --git a/src/plugins/sum.rs b/src/plugins/sum.rs index b2d7cbfb34..83b5e63266 100644 --- a/src/plugins/sum.rs +++ b/src/plugins/sum.rs @@ -77,8 +77,8 @@ impl Plugin for Sum { rest_positional: true, }) } - fn begin_filter(&mut self, _: CallInfo) -> Result<(), ShellError> { - Ok(()) + fn begin_filter(&mut self, _: CallInfo) -> Result, ShellError> { + Ok(vec![]) } fn filter(&mut self, input: Spanned) -> Result, ShellError> { diff --git a/src/plugins/sys.rs b/src/plugins/sys.rs new file mode 100644 index 0000000000..9ce668f61b --- /dev/null +++ b/src/plugins/sys.rs @@ -0,0 +1,111 @@ +#![feature(async_await)] + +use futures::executor::block_on; +use futures::stream::StreamExt; +use heim::{disk, memory}; +use indexmap::IndexMap; +use nu::{ + serve_plugin, CallInfo, CommandConfig, Plugin, ReturnSuccess, ReturnValue, ShellError, Span, + Spanned, SpannedDictBuilder, Value, +}; +use std::ffi::OsStr; + +struct Sys; +impl Sys { + fn new() -> Sys { + Sys + } +} + +//TODO: add more error checking + +async fn mem(span: Span) -> Spanned { + let memory = memory::memory().await.unwrap(); + //let swap = memory::swap().await.unwrap(); + + let mut dict = SpannedDictBuilder::new(span); + + dict.insert("total", Value::bytes(memory.total().get())); + dict.insert("free", Value::bytes(memory.free().get())); + + dict.into_spanned_value() +} + +async fn swap(span: Span) -> Spanned { + let swap = memory::swap().await.unwrap(); + + let mut dict = SpannedDictBuilder::new(span); + + dict.insert("total", Value::bytes(swap.total().get())); + dict.insert("free", Value::bytes(swap.free().get())); + + dict.into_spanned_value() +} + +async fn disks(span: Span) -> Value { + let mut output = vec![]; + let mut partitions = disk::partitions_physical(); + while let Some(part) = partitions.next().await { + let part = part.unwrap(); + let usage = disk::usage(part.mount_point().to_path_buf()).await.unwrap(); + + let mut dict = SpannedDictBuilder::new(span); + + dict.insert( + "device", + Value::string( + part.device() + .unwrap_or_else(|| OsStr::new("N/A")) + .to_string_lossy(), + ), + ); + + dict.insert("type", Value::string(part.file_system().as_str())); + dict.insert("mount", Value::string(part.mount_point().to_string_lossy())); + dict.insert("total", Value::bytes(usage.total().get())); + dict.insert("used", Value::bytes(usage.used().get())); + dict.insert("free", Value::bytes(usage.free().get())); + + output.push(dict.into_spanned_value()); + } + + Value::List(output) +} + +async fn sysinfo(span: Span) -> Vec> { + let mut sysinfo = SpannedDictBuilder::new(span); + + // Disks + sysinfo.insert("disks", disks(span).await); + sysinfo.insert_spanned("mem", mem(span).await); + sysinfo.insert_spanned("swap", swap(span).await); + + vec![sysinfo.into_spanned_value()] +} + +impl Plugin for Sys { + fn config(&mut self) -> Result { + Ok(CommandConfig { + name: "sys".to_string(), + positional: vec![], + is_filter: true, + is_sink: false, + named: IndexMap::new(), + rest_positional: true, + }) + } + fn begin_filter(&mut self, callinfo: CallInfo) -> Result, ShellError> { + Ok(block_on(sysinfo(callinfo.name_span.unwrap())) + .into_iter() + .map(|x| ReturnSuccess::value(x)) + .collect()) + } + + fn filter(&mut self, _: Spanned) -> Result, ShellError> { + Ok(vec![]) + } +} + +fn main() { + serve_plugin(&mut Sys::new()); +} diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 89b43dbdff..e0f8f63fd1 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -83,12 +83,6 @@ fn paint_textview( } } - // if it's a short buffer, be sure to fill it out - // while pos < (width * height) { - // frame_buffer.push((' ', 0, 0, 0)); - // pos += 1; - // } - let num_frame_buffer_rows = frame_buffer.len() / width; let buffer_needs_scrolling = num_frame_buffer_rows > height; diff --git a/src/plugins/tree.rs b/src/plugins/tree.rs index b2bc9e1ca5..ddcb535d60 100644 --- a/src/plugins/tree.rs +++ b/src/plugins/tree.rs @@ -98,8 +98,6 @@ impl Plugin for TreeViewer { let _ = view.render_view(); } } - - //Ok(()) } } From 2464a33660014505e5c3e845cca82846203b4c17 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 28 Jul 2019 08:09:25 +1200 Subject: [PATCH 2/3] Progress the sys plugin a bit further --- src/object/process.rs | 11 ++++-- src/plugins/binaryview.rs | 7 ++-- src/plugins/sys.rs | 83 ++++++++++++++++++++++++++++++++++----- 3 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/object/process.rs b/src/object/process.rs index ba716c5156..9999724d41 100644 --- a/src/object/process.rs +++ b/src/object/process.rs @@ -5,7 +5,6 @@ use sysinfo::ProcessExt; crate fn process_dict(proc: &sysinfo::Process, span: impl Into) -> Spanned { let mut dict = SpannedDictBuilder::new(span); - dict.insert("name", Value::string(proc.name())); let cmd = proc.cmd(); @@ -15,10 +14,16 @@ crate fn process_dict(proc: &sysinfo::Process, span: impl Into) -> Spanned Value::string(join(cmd, "")) }; - dict.insert("cmd", cmd_value); - dict.insert("cpu", Value::float(proc.cpu_usage() as f64)); dict.insert("pid", Value::int(proc.pid() as i64)); dict.insert("status", Value::string(proc.status().to_string())); + dict.insert("cpu", Value::float(proc.cpu_usage() as f64)); + //dict.insert("name", Value::string(proc.name())); + match cmd_value { + Value::Primitive(Primitive::Nothing) => { + dict.insert("name", Value::string(proc.name())); + } + _ => dict.insert("name", cmd_value), + } dict.into_spanned_value() } diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index b4a2f1218d..14f771757d 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -359,7 +359,7 @@ pub fn view_contents_interactive( None }; - let mut nes = neso::Nes::new(48000.0); + let mut nes = neso::Nes::new(0.0); let rawkey = RawKey::new(); nes.load_rom(&buffer); @@ -445,8 +445,9 @@ pub fn view_contents_interactive( #[allow(unused)] let screen = RawScreen::disable_raw_mode(); - println!(""); - thread::sleep(Duration::from_millis(50)); + println!("Hit enter to return to terminal"); + let mut buf = String::new(); + let _ = std::io::stdin().read_line(&mut buf); Ok(()) } diff --git a/src/plugins/sys.rs b/src/plugins/sys.rs index 9ce668f61b..0a30460dac 100644 --- a/src/plugins/sys.rs +++ b/src/plugins/sys.rs @@ -5,8 +5,8 @@ use futures::stream::StreamExt; use heim::{disk, memory}; use indexmap::IndexMap; use nu::{ - serve_plugin, CallInfo, CommandConfig, Plugin, ReturnSuccess, ReturnValue, ShellError, Span, - Spanned, SpannedDictBuilder, Value, + serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ReturnSuccess, ReturnValue, + ShellError, Span, Spanned, SpannedDictBuilder, Value, }; use std::ffi::OsStr; @@ -19,25 +19,86 @@ impl Sys { //TODO: add more error checking +async fn os(span: Span) -> Option> { + if let (Ok(name), Ok(version)) = (sys_info::os_type(), sys_info::os_release()) { + let mut os_idx = SpannedDictBuilder::new(span); + os_idx.insert("name", Primitive::String(name)); + os_idx.insert("version", Primitive::String(version)); + + Some(os_idx.into_spanned_value()) + } else { + None + } +} + +async fn cpu(span: Span) -> Option> { + if let (Ok(num_cpu), Ok(cpu_speed)) = (sys_info::cpu_num(), sys_info::cpu_speed()) { + let mut cpu_idx = SpannedDictBuilder::new(span); + cpu_idx.insert("cores", Primitive::Int(num_cpu as i64)); + cpu_idx.insert("speed", Primitive::Int(cpu_speed as i64)); + Some(cpu_idx.into_spanned_value()) + } else { + None + } +} + async fn mem(span: Span) -> Spanned { let memory = memory::memory().await.unwrap(); - //let swap = memory::swap().await.unwrap(); + let swap = memory::swap().await.unwrap(); let mut dict = SpannedDictBuilder::new(span); dict.insert("total", Value::bytes(memory.total().get())); dict.insert("free", Value::bytes(memory.free().get())); + dict.insert("swap total", Value::bytes(swap.total().get())); + dict.insert("swap free", Value::bytes(swap.free().get())); + dict.into_spanned_value() } -async fn swap(span: Span) -> Spanned { - let swap = memory::swap().await.unwrap(); - +async fn host(span: Span) -> Spanned { let mut dict = SpannedDictBuilder::new(span); - dict.insert("total", Value::bytes(swap.total().get())); - dict.insert("free", Value::bytes(swap.free().get())); + // OS + if let Ok(platform) = heim::host::platform().await { + dict.insert("name", Value::string(platform.system())); + dict.insert("release", Value::string(platform.release())); + dict.insert("hostname", Value::string(platform.hostname())); + dict.insert("arch", Value::string(platform.architecture().as_str())); + } + + // Uptime + if let Ok(uptime) = heim::host::uptime().await { + let mut uptime_dict = SpannedDictBuilder::new(span); + + let uptime = uptime.get().round() as i64; + let days = uptime / (60 * 60 * 24); + let hours = (uptime - days * 60 * 60 * 24) / (60 * 60); + let minutes = (uptime - days * 60 * 60 * 24 - hours * 60 * 60) / 60; + let seconds = uptime % 60; + + uptime_dict.insert("days", Value::int(days)); + uptime_dict.insert("hours", Value::int(hours)); + uptime_dict.insert("mins", Value::int(minutes)); + uptime_dict.insert("secs", Value::int(seconds)); + + dict.insert_spanned("uptime", uptime_dict.into_spanned_value()); + } + + // Users + let mut users = heim::host::users(); + let mut user_vec = vec![]; + while let Some(user) = users.next().await { + let user = user.unwrap(); + + user_vec.push(Spanned { + item: Value::string(user.username()), + span, + }); + } + let user_list = Value::List(user_vec); + dict.insert("users", user_list); dict.into_spanned_value() } @@ -75,10 +136,12 @@ async fn disks(span: Span) -> Value { async fn sysinfo(span: Span) -> Vec> { let mut sysinfo = SpannedDictBuilder::new(span); - // Disks + sysinfo.insert_spanned("host", host(span).await); + if let Some(cpu) = cpu(span).await { + sysinfo.insert_spanned("cpu", cpu); + } sysinfo.insert("disks", disks(span).await); sysinfo.insert_spanned("mem", mem(span).await); - sysinfo.insert_spanned("swap", swap(span).await); vec![sysinfo.into_spanned_value()] } From c85b7728bcda861696285485297e96ea6d1bf360 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 28 Jul 2019 14:02:42 +1200 Subject: [PATCH 3/3] Remove old sysinfo and finish sys --- README.md | 2 +- src/cli.rs | 1 - src/commands.rs | 1 - src/commands/sysinfo.rs | 133 -------------------------------------- src/lib.rs | 1 + src/plugins/binaryview.rs | 1 - src/plugins/sys.rs | 58 +++++++++++++---- 7 files changed, 47 insertions(+), 150 deletions(-) delete mode 100644 src/commands/sysinfo.rs diff --git a/README.md b/README.md index d792d438f4..4a9fa401a1 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat | ls (path) | View the contents of the current or given path | | date (--utc) | Get the current datetime | | ps | View current processes | -| sysinfo | View information about the current system | +| sys | View information about the current system | | open {filename or url} | Load a file into a cell, convert to table if possible (avoid by appending '--raw') | | rm {file or directory} | Remove a file, (for removing directory append '--recursive') | | exit | Exit the shell | diff --git a/src/cli.rs b/src/cli.rs index a31b6d1a36..d6b1602ba7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -152,7 +152,6 @@ pub async fn cli() -> Result<(), Box> { context.add_commands(vec![ command("ps", Box::new(ps::ps)), command("ls", Box::new(ls::ls)), - command("sysinfo", Box::new(sysinfo::sysinfo)), command("cd", Box::new(cd::cd)), command("first", Box::new(first::first)), command("size", Box::new(size::size)), diff --git a/src/commands.rs b/src/commands.rs index c958ee041a..6e2d348c30 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -33,7 +33,6 @@ crate mod skip_while; crate mod sort_by; crate mod split_column; crate mod split_row; -crate mod sysinfo; crate mod table; crate mod to_array; crate mod to_csv; diff --git a/src/commands/sysinfo.rs b/src/commands/sysinfo.rs deleted file mode 100644 index 425505936f..0000000000 --- a/src/commands/sysinfo.rs +++ /dev/null @@ -1,133 +0,0 @@ -use crate::errors::ShellError; -use crate::object::base::OF64; -use crate::object::SpannedDictBuilder; -use crate::object::{Primitive, Value}; -use crate::prelude::*; -use sys_info::*; -use sysinfo::{ComponentExt, DiskExt, NetworkExt, RefreshKind, SystemExt}; - -pub fn sysinfo(args: CommandArgs) -> Result { - let name_span = args.call_info.name_span; - let mut idx = SpannedDictBuilder::new(name_span); - - if let (Ok(name), Ok(version)) = (os_type(), os_release()) { - let mut os_idx = SpannedDictBuilder::new(name_span); - os_idx.insert("name", Primitive::String(name)); - os_idx.insert("version", Primitive::String(version)); - - idx.insert_spanned("os", os_idx.into_spanned_value()); - } - - if let (Ok(num_cpu), Ok(cpu_speed)) = (cpu_num(), cpu_speed()) { - let mut cpu_idx = SpannedDictBuilder::new(name_span); - cpu_idx.insert("num", Primitive::Int(num_cpu as i64)); - cpu_idx.insert("speed", Primitive::Int(cpu_speed as i64)); - - idx.insert_spanned("cpu", cpu_idx); - } - - if let Ok(x) = loadavg() { - let mut load_idx = SpannedDictBuilder::new(name_span); - - load_idx.insert("1min", Primitive::Float(OF64::from(x.one))); - load_idx.insert("5min", Primitive::Float(OF64::from(x.five))); - load_idx.insert("15min", Primitive::Float(OF64::from(x.fifteen))); - - idx.insert_spanned("load avg", load_idx); - } - - if let Ok(x) = mem_info() { - let mut mem_idx = SpannedDictBuilder::new(name_span); - - mem_idx.insert("total", Primitive::Bytes(x.total as u64 * 1024)); - mem_idx.insert("free", Primitive::Bytes(x.free as u64 * 1024)); - mem_idx.insert("avail", Primitive::Bytes(x.avail as u64 * 1024)); - mem_idx.insert("buffers", Primitive::Bytes(x.buffers as u64 * 1024)); - mem_idx.insert("cached", Primitive::Bytes(x.cached as u64 * 1024)); - mem_idx.insert("swap total", Primitive::Bytes(x.swap_total as u64 * 1024)); - mem_idx.insert("swap free", Primitive::Bytes(x.swap_free as u64 * 1024)); - - idx.insert_spanned("mem", mem_idx); - } - - /* - if let Ok(x) = disk_info() { - let mut disk_idx = indexmap::IndexMap::new(); - disk_idx.insert( - "total".to_string(), - Value::Primitive(Primitive::Bytes(x.total as u128 * 1024)), - ); - disk_idx.insert( - "free".to_string(), - Value::Primitive(Primitive::Bytes(x.free as u128 * 1024)), - ); - } - */ - - if let Ok(x) = hostname() { - idx.insert("hostname", Primitive::String(x)); - } - - #[cfg(not(windows))] - { - if let Ok(x) = boottime() { - let mut boottime_idx = SpannedDictBuilder::new(name_span); - boottime_idx.insert("days", Primitive::Int(x.tv_sec / (24 * 3600))); - boottime_idx.insert("hours", Primitive::Int((x.tv_sec / 3600) % 24)); - boottime_idx.insert("mins", Primitive::Int((x.tv_sec / 60) % 60)); - - idx.insert_spanned("uptime", boottime_idx); - } - } - - let system = sysinfo::System::new_with_specifics(RefreshKind::everything().without_processes()); - let components_list = system.get_components_list(); - if components_list.len() > 0 { - let mut v: Vec> = vec![]; - for component in components_list { - let mut component_idx = SpannedDictBuilder::new(name_span); - component_idx.insert("name", Primitive::String(component.get_label().to_string())); - component_idx.insert( - "temp", - Primitive::Float(OF64::from(component.get_temperature() as f64)), - ); - component_idx.insert( - "max", - Primitive::Float(OF64::from(component.get_max() as f64)), - ); - if let Some(critical) = component.get_critical() { - component_idx.insert("critical", Primitive::Float(OF64::from(critical as f64))); - } - v.push(component_idx.into()); - } - idx.insert("temps", Value::List(v)); - } - - let disks = system.get_disks(); - if disks.len() > 0 { - let mut v = vec![]; - - for disk in disks { - let mut disk_idx = SpannedDictBuilder::new(name_span); - disk_idx.insert("name", Value::string(disk.get_name().to_string_lossy())); - disk_idx.insert("available", Value::bytes(disk.get_available_space())); - disk_idx.insert("total", Value::bytes(disk.get_total_space())); - v.push(disk_idx.into()); - } - - idx.insert("disks", Value::List(v)); - } - - let network = system.get_network(); - let incoming = network.get_income(); - let outgoing = network.get_outcome(); - - let mut network_idx = SpannedDictBuilder::new(name_span); - network_idx.insert("incoming", Value::bytes(incoming)); - network_idx.insert("outgoing", Value::bytes(outgoing)); - idx.insert_spanned("network", network_idx); - - let stream = stream![idx.into_spanned_value()]; - - Ok(stream.from_input_stream()) -} diff --git a/src/lib.rs b/src/lib.rs index 9411668b08..e20046165c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,7 @@ mod stream; pub use crate::commands::command::{CallInfo, ReturnSuccess, ReturnValue}; pub use crate::context::{SourceMap, SpanSource}; pub use crate::env::host::BasicHost; +pub use crate::object::base::OF64; pub use crate::parser::parse::span::Span; pub use crate::parser::parse::span::SpannedItem; pub use crate::parser::Spanned; diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index 14f771757d..0db5673254 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -6,7 +6,6 @@ use nu::{ Value, }; use pretty_hex::*; -use std::{thread, time::Duration}; struct BinaryView; diff --git a/src/plugins/sys.rs b/src/plugins/sys.rs index 0a30460dac..bb95252098 100644 --- a/src/plugins/sys.rs +++ b/src/plugins/sys.rs @@ -6,7 +6,7 @@ use heim::{disk, memory}; use indexmap::IndexMap; use nu::{ serve_plugin, CallInfo, CommandConfig, Plugin, Primitive, ReturnSuccess, ReturnValue, - ShellError, Span, Spanned, SpannedDictBuilder, Value, + ShellError, Span, Spanned, SpannedDictBuilder, Value, OF64, }; use std::ffi::OsStr; @@ -19,18 +19,6 @@ impl Sys { //TODO: add more error checking -async fn os(span: Span) -> Option> { - if let (Ok(name), Ok(version)) = (sys_info::os_type(), sys_info::os_release()) { - let mut os_idx = SpannedDictBuilder::new(span); - os_idx.insert("name", Primitive::String(name)); - os_idx.insert("version", Primitive::String(version)); - - Some(os_idx.into_spanned_value()) - } else { - None - } -} - async fn cpu(span: Span) -> Option> { if let (Ok(num_cpu), Ok(cpu_speed)) = (sys_info::cpu_num(), sys_info::cpu_speed()) { let mut cpu_idx = SpannedDictBuilder::new(span); @@ -133,6 +121,48 @@ async fn disks(span: Span) -> Value { Value::List(output) } +async fn temp(span: Span) -> Value { + use sysinfo::{ComponentExt, RefreshKind, SystemExt}; + let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system()); + let components_list = system.get_components_list(); + if components_list.len() > 0 { + let mut v: Vec> = vec![]; + for component in components_list { + let mut component_idx = SpannedDictBuilder::new(span); + component_idx.insert("name", Primitive::String(component.get_label().to_string())); + component_idx.insert( + "temp", + Primitive::Float(OF64::from(component.get_temperature() as f64)), + ); + component_idx.insert( + "max", + Primitive::Float(OF64::from(component.get_max() as f64)), + ); + if let Some(critical) = component.get_critical() { + component_idx.insert("critical", Primitive::Float(OF64::from(critical as f64))); + } + v.push(component_idx.into()); + } + Value::List(v) + } else { + Value::List(vec![]) + } +} + +async fn net(span: Span) -> Spanned { + use sysinfo::{NetworkExt, RefreshKind, SystemExt}; + let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_network()); + + let network = system.get_network(); + let incoming = network.get_income(); + let outgoing = network.get_outcome(); + + let mut network_idx = SpannedDictBuilder::new(span); + network_idx.insert("incoming", Value::bytes(incoming)); + network_idx.insert("outgoing", Value::bytes(outgoing)); + network_idx.into_spanned_value() +} + async fn sysinfo(span: Span) -> Vec> { let mut sysinfo = SpannedDictBuilder::new(span); @@ -142,6 +172,8 @@ async fn sysinfo(span: Span) -> Vec> { } sysinfo.insert("disks", disks(span).await); sysinfo.insert_spanned("mem", mem(span).await); + sysinfo.insert("temp", temp(span).await); + sysinfo.insert_spanned("net", net(span).await); vec![sysinfo.into_spanned_value()] }