From 2464a33660014505e5c3e845cca82846203b4c17 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 28 Jul 2019 08:09:25 +1200 Subject: [PATCH] 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()] }