rwho: format output
This commit is contained in:
+51
-4
@@ -1,5 +1,6 @@
|
||||
use anyhow::Context;
|
||||
use clap::Parser;
|
||||
use roowho2_lib::server::rwhod::RwhodClientProxy;
|
||||
use roowho2_lib::{proto::WhodUserEntry, server::rwhod::RwhodClientProxy};
|
||||
|
||||
/// Check who is logged in on local machines.
|
||||
///
|
||||
@@ -16,13 +17,17 @@ pub struct Args {
|
||||
#[arg(long, short)]
|
||||
all: bool,
|
||||
|
||||
/// Print the output with the old formatting
|
||||
#[arg(long, short)]
|
||||
old: bool,
|
||||
|
||||
/// Output in JSON format
|
||||
#[arg(long, short)]
|
||||
json: bool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let mut conn = zlink::unix::connect("/run/roowho2/roowho2.varlink")
|
||||
@@ -32,7 +37,49 @@ async fn main() {
|
||||
let reply = conn
|
||||
.rwho(args.all)
|
||||
.await
|
||||
.expect("Failed to send rwho request");
|
||||
.context("Failed to send rwho request")?
|
||||
.map_err(|e| anyhow::anyhow!("Server returned an error for rwho request: {:?}", e))?;
|
||||
|
||||
println!("{:?}", reply);
|
||||
if args.json {
|
||||
println!("{}", serde_json::to_string_pretty(&reply).unwrap());
|
||||
} else if args.old {
|
||||
for (hostname, user) in &reply {
|
||||
let entry_str = old_format_user_entry(hostname, user);
|
||||
println!("{}", entry_str);
|
||||
}
|
||||
} else {
|
||||
for (hostname, user) in &reply {
|
||||
let entry_str = old_format_user_entry(hostname, user);
|
||||
println!("{}", entry_str);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn old_format_user_entry(hostname: &str, user: &WhodUserEntry) -> String {
|
||||
let idle_str = {
|
||||
let hours = user.idle_time.num_hours().min(99);
|
||||
let minutes = user.idle_time.num_minutes() % 60;
|
||||
format!(
|
||||
"{:>2}:{:02}",
|
||||
if hours < 0 {
|
||||
"".to_string()
|
||||
} else {
|
||||
hours.to_string()
|
||||
},
|
||||
minutes
|
||||
)
|
||||
};
|
||||
|
||||
format!(
|
||||
"{:<8} {:<8}:{:<8} {} {}",
|
||||
user.user_id,
|
||||
hostname,
|
||||
user.tty,
|
||||
user.login_time.format("%b %d %H:%M"),
|
||||
idle_str,
|
||||
)
|
||||
}
|
||||
|
||||
// larseie hildring:pts/10 Jan 5 14:53 :01
|
||||
|
||||
Reference in New Issue
Block a user