mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 06:51:28 +02:00
Update plugin protocol for begin, and create new sys plugin
This commit is contained in:
@@ -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<Vec<ReturnValue>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
|
@@ -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<Vec<ReturnValue>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
|
@@ -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<Vec<ReturnValue>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
|
@@ -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<Vec<ReturnValue>, 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<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
|
@@ -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<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
|
111
src/plugins/sys.rs
Normal file
111
src/plugins/sys.rs
Normal file
@@ -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<Value> {
|
||||
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<Value> {
|
||||
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<Spanned<Value>> {
|
||||
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<CommandConfig, ShellError> {
|
||||
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<Vec<ReturnValue>, ShellError> {
|
||||
Ok(block_on(sysinfo(callinfo.name_span.unwrap()))
|
||||
.into_iter()
|
||||
.map(|x| ReturnSuccess::value(x))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn filter(&mut self, _: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Sys::new());
|
||||
}
|
@@ -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;
|
||||
|
||||
|
@@ -98,8 +98,6 @@ impl Plugin for TreeViewer {
|
||||
let _ = view.render_view();
|
||||
}
|
||||
}
|
||||
|
||||
//Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user