263 lines
6.1 KiB
MoonBit
263 lines
6.1 KiB
MoonBit
///|
|
|
#derive(Eq, Debug)
|
|
pub struct OsRelease {
|
|
pretty_name : String
|
|
id : String
|
|
name : String
|
|
version_id : String
|
|
}
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub struct CpuInfo {
|
|
model : String
|
|
cores : Int32
|
|
mhz : String
|
|
}
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub struct MemInfo {
|
|
total_kib : UInt64
|
|
available_kib : UInt64
|
|
used_kib : UInt64
|
|
}
|
|
|
|
///|
|
|
pub fn trim_quotes(s : String) -> String {
|
|
let trimmed = s.trim().to_owned()
|
|
if trimmed.length() >= 2 {
|
|
if (trimmed.has_prefix("\"") && trimmed.has_suffix("\"")) ||
|
|
(trimmed.has_prefix("'") && trimmed.has_suffix("'")) {
|
|
return trimmed.unsafe_substring(start=1, end=trimmed.length() - 1)
|
|
}
|
|
}
|
|
trimmed
|
|
}
|
|
|
|
///|
|
|
pub fn parse_os_release(content : String) -> OsRelease {
|
|
let mut pretty_name = ""
|
|
let mut id = ""
|
|
let mut name = ""
|
|
let mut version_id = ""
|
|
for line_view in content.split("\n") {
|
|
let line = line_view.trim().to_owned()
|
|
if line.is_empty() || line.has_prefix("#") {
|
|
continue
|
|
}
|
|
match line.find("=") {
|
|
Some(idx) => {
|
|
let key = line.unsafe_substring(start=0, end=idx).trim().to_owned()
|
|
let val = trim_quotes(
|
|
line.unsafe_substring(start=idx + 1, end=line.length()),
|
|
)
|
|
match key {
|
|
"PRETTY_NAME" => pretty_name = val
|
|
"ID" => id = val.to_lower()
|
|
"NAME" => name = val
|
|
"VERSION_ID" => version_id = val
|
|
_ => ()
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
if pretty_name.is_empty() && !name.is_empty() {
|
|
pretty_name = if version_id.is_empty() {
|
|
name
|
|
} else {
|
|
"\{name} \{version_id}"
|
|
}
|
|
}
|
|
{ pretty_name, id, name, version_id, }
|
|
}
|
|
|
|
///|
|
|
pub fn parse_uint64_str(s : String) -> UInt64 {
|
|
let mut acc : UInt64 = 0UL
|
|
let mut started = false
|
|
for ch in s {
|
|
if ch >= '0' && ch <= '9' {
|
|
started = true
|
|
acc = acc * 10UL + (ch.to_int() - '0'.to_int()).to_uint64()
|
|
} else if started {
|
|
break
|
|
}
|
|
}
|
|
acc
|
|
}
|
|
|
|
///|
|
|
pub fn parse_int_str(s : String) -> Int32 {
|
|
parse_uint64_str(s).to_int()
|
|
}
|
|
|
|
///|
|
|
pub fn parse_cpuinfo(content : String) -> CpuInfo {
|
|
let mut model = ""
|
|
let mut cores = 0
|
|
let mut mhz = ""
|
|
for line_view in content.split("\n") {
|
|
let line = line_view.trim().to_owned()
|
|
if line.is_empty() {
|
|
continue
|
|
}
|
|
match line.find(":") {
|
|
Some(idx) => {
|
|
let key = line.unsafe_substring(start=0, end=idx).trim().to_owned()
|
|
let val = line
|
|
.unsafe_substring(start=idx + 1, end=line.length())
|
|
.trim()
|
|
.to_owned()
|
|
match key {
|
|
"processor" => cores += 1
|
|
"model name" | "Hardware" | "Processor" =>
|
|
if model.is_empty() {
|
|
model = val
|
|
}
|
|
"cpu MHz" => if mhz.is_empty() { mhz = val }
|
|
_ => ()
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
if cores == 0 && !model.is_empty() {
|
|
cores = 1
|
|
}
|
|
{ model, cores, mhz, }
|
|
}
|
|
|
|
///|
|
|
pub fn parse_meminfo(content : String) -> MemInfo {
|
|
let mut total_kib : UInt64 = 0UL
|
|
let mut free_kib : UInt64 = 0UL
|
|
let mut available_kib : UInt64 = 0UL
|
|
let mut buffers_kib : UInt64 = 0UL
|
|
let mut cached_kib : UInt64 = 0UL
|
|
let mut has_available = false
|
|
for line_view in content.split("\n") {
|
|
let line = line_view.trim().to_owned()
|
|
match line.find(":") {
|
|
Some(idx) => {
|
|
let key = line.unsafe_substring(start=0, end=idx).trim().to_owned()
|
|
let val_str = line
|
|
.unsafe_substring(start=idx + 1, end=line.length())
|
|
.trim()
|
|
.to_owned()
|
|
let val = parse_uint64_str(val_str)
|
|
match key {
|
|
"MemTotal" => total_kib = val
|
|
"MemFree" => free_kib = val
|
|
"MemAvailable" => {
|
|
available_kib = val
|
|
has_available = true
|
|
}
|
|
"Buffers" => buffers_kib = val
|
|
"Cached" => cached_kib = val
|
|
_ => ()
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
}
|
|
if !has_available {
|
|
available_kib = free_kib + buffers_kib + cached_kib
|
|
}
|
|
let used_kib = if total_kib >= available_kib {
|
|
total_kib - available_kib
|
|
} else {
|
|
0UL
|
|
}
|
|
{ total_kib, available_kib, used_kib, }
|
|
}
|
|
|
|
///|
|
|
pub fn parse_uptime_seconds(content : String) -> Int32 {
|
|
let trimmed = content.trim().to_owned()
|
|
let mut secs = 0
|
|
for ch in trimmed {
|
|
if ch >= '0' && ch <= '9' {
|
|
secs = secs * 10 + (ch.to_int() - '0'.to_int())
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
secs
|
|
}
|
|
|
|
///|
|
|
pub fn format_uptime(total_secs : Int32) -> String {
|
|
if total_secs < 60 {
|
|
return "\{total_secs} secs"
|
|
}
|
|
let days = total_secs / 86400
|
|
let hours = total_secs % 86400 / 3600
|
|
let mins = total_secs % 3600 / 60
|
|
let parts : Array[String] = []
|
|
if days > 0 {
|
|
parts.push(if days == 1 { "1 day" } else { "\{days} days" })
|
|
}
|
|
if hours > 0 {
|
|
parts.push(if hours == 1 { "1 hour" } else { "\{hours} hours" })
|
|
}
|
|
if mins > 0 || parts.is_empty() {
|
|
parts.push(if mins == 1 { "1 min" } else { "\{mins} mins" })
|
|
}
|
|
parts.join(", ")
|
|
}
|
|
|
|
///|
|
|
fn format_two_decimals(hundredths : UInt64) -> String {
|
|
let whole = hundredths / 100UL
|
|
let frac = hundredths % 100UL
|
|
let frac_str = if frac < 10UL { "0\{frac}" } else { "\{frac}" }
|
|
"\{whole}.\{frac_str}"
|
|
}
|
|
|
|
///|
|
|
pub fn format_bytes_human(bytes : UInt64) -> String {
|
|
let gib : UInt64 = 1073741824UL
|
|
let mib : UInt64 = 1048576UL
|
|
if bytes >= gib {
|
|
let hundredths = bytes * 100UL / gib
|
|
"\{format_two_decimals(hundredths)} GiB"
|
|
} else {
|
|
let hundredths = bytes * 100UL / mib
|
|
"\{format_two_decimals(hundredths)} MiB"
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn format_usage(used_bytes : UInt64, total_bytes : UInt64) -> String {
|
|
if total_bytes == 0UL {
|
|
return "Unknown"
|
|
}
|
|
let pct = used_bytes * 100UL / total_bytes
|
|
"\{format_bytes_human(used_bytes)} / \{format_bytes_human(total_bytes)} (\{pct}%)"
|
|
}
|
|
|
|
///|
|
|
pub fn count_dpkg_installed(content : String) -> Int32 {
|
|
let mut count = 0
|
|
for line_view in content.split("\n") {
|
|
let line = line_view.trim().to_owned()
|
|
if line == "Status: install ok installed" {
|
|
count += 1
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
///|
|
|
pub fn count_apk_installed(content : String) -> Int32 {
|
|
let mut count = 0
|
|
for line_view in content.split("\n") {
|
|
if line_view.has_prefix("P:") {
|
|
count += 1
|
|
}
|
|
}
|
|
count
|
|
}
|