-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrust.rs
66 lines (61 loc) · 1.53 KB
/
rust.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::env::var;
use std::fs::{read_dir, read_to_string};
fn mem() -> (i32, i32) {
let mem = read_to_string("/proc/meminfo").unwrap();
let get = |i: usize| -> i32 {
mem.lines()
.nth(i)
.unwrap()
.split_ascii_whitespace()
.nth(1)
.unwrap()
.parse::<i32>()
.unwrap()
/ 1000
};
(get(0), get(2))
}
fn uptime() -> (i32, i32, i32) {
let uptime = read_to_string("/proc/uptime")
.unwrap()
.split(' ')
.next()
.unwrap()
.parse::<f64>()
.unwrap() as i32;
let d = uptime / 60 / 60 / 24;
let h = uptime / 60 / 60 % 24;
let m = uptime / 60 % 60;
(d, h, m)
}
fn main() {
let user = var("USER").unwrap();
let host = read_to_string("/etc/hostname").unwrap().trim().to_string();
let kernel = read_to_string("/proc/version")
.unwrap()
.split(' ')
.nth(2)
.unwrap()
.to_string();
let term = var("TERM").unwrap();
let shell = var("SHELL").unwrap();
let tasks = read_dir("/proc")
.unwrap()
.filter(|entry| {
let name = entry.as_ref().unwrap().file_name().into_string().unwrap();
name.chars().all(|c| c.is_digit(10))
})
.count();
let (total, avail) = mem();
let (d, h, m) = uptime();
println!(
"{}@{}
kernel\t{}
term\t{}
shell\t{}
tasks\t{}
mem\t{}m / {}m
uptime\t{}d {}h {}m",
user, host, kernel, term, shell, tasks, avail, total, d, h, m
);
}