172 lines
4.6 KiB
MoonBit
172 lines
4.6 KiB
MoonBit
///|
|
|
pub fn visible_len(s : String) -> Int32 {
|
|
let mut count = 0
|
|
let mut in_esc = false
|
|
for ch in s {
|
|
if in_esc {
|
|
if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') {
|
|
in_esc = false
|
|
}
|
|
} else if ch == '\u001b' {
|
|
in_esc = true
|
|
} else {
|
|
count += 1
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
///|
|
|
pub fn pad_right_visible(s : String, target_width : Int32) -> String {
|
|
let vlen = visible_len(s)
|
|
if vlen >= target_width {
|
|
return s
|
|
}
|
|
let buf = StringBuilder()
|
|
buf.write_string(s)
|
|
for _ in 0..<(target_width - vlen) {
|
|
buf.write_char(' ')
|
|
}
|
|
buf.to_string()
|
|
}
|
|
|
|
///|
|
|
fn colorize(text : String, ansi : String, no_color : Bool) -> String {
|
|
if no_color || ansi.is_empty() {
|
|
text
|
|
} else {
|
|
"\{ansi}\{text}\u001b[0m"
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn render_colors_bar(no_color : Bool) -> Array[String] {
|
|
if no_color {
|
|
return ["[0][1][2][3][4][5][6][7]", "[8][9][A][B][C][D][E][F]"]
|
|
}
|
|
let row1 = StringBuilder()
|
|
let row2 = StringBuilder()
|
|
for i in 0..<8 {
|
|
let c1 = 30 + i
|
|
let c2 = 90 + i
|
|
row1.write_string("\u001b[\{c1}m███\u001b[0m")
|
|
row2.write_string("\u001b[\{c2}m███\u001b[0m")
|
|
}
|
|
[row1.to_string(), row2.to_string()]
|
|
}
|
|
|
|
///|
|
|
fn format_kv(
|
|
label : String,
|
|
value : String,
|
|
accent : String,
|
|
no_color : Bool,
|
|
) -> String {
|
|
let key = colorize("\{label}:", accent, no_color)
|
|
"\{key} \{value}"
|
|
}
|
|
|
|
///|
|
|
pub fn render_info_lines(
|
|
info : SystemInfo,
|
|
structure : Array[String],
|
|
accent : String,
|
|
no_color : Bool,
|
|
) -> Array[String] {
|
|
let lines : Array[String] = []
|
|
let bold_accent = if accent.is_empty() { "\u001b[1;36m" } else { accent }
|
|
for mod_name in structure {
|
|
match mod_name {
|
|
"title" => {
|
|
let user = colorize(info.username, bold_accent, no_color)
|
|
let host = colorize(info.hostname, bold_accent, no_color)
|
|
lines.push("\{user}@\{host}")
|
|
}
|
|
"separator" => {
|
|
let total = info.username.length() + 1 + info.hostname.length()
|
|
let buf = StringBuilder()
|
|
for _ in 0..<total {
|
|
buf.write_char('-')
|
|
}
|
|
lines.push(buf.to_string())
|
|
}
|
|
"os" => lines.push(format_kv("OS", info.os_name, bold_accent, no_color))
|
|
"host" =>
|
|
lines.push(format_kv("Host", info.host_model, bold_accent, no_color))
|
|
"kernel" =>
|
|
lines.push(format_kv("Kernel", info.kernel, bold_accent, no_color))
|
|
"uptime" =>
|
|
lines.push(format_kv("Uptime", info.uptime, bold_accent, no_color))
|
|
"packages" =>
|
|
lines.push(format_kv("Packages", info.packages, bold_accent, no_color))
|
|
"shell" =>
|
|
lines.push(format_kv("Shell", info.shell, bold_accent, no_color))
|
|
"terminal" =>
|
|
lines.push(format_kv("Terminal", info.terminal, bold_accent, no_color))
|
|
"de" | "wm" | "de/wm" =>
|
|
lines.push(format_kv("DE/WM", info.de_wm, bold_accent, no_color))
|
|
"cpu" => lines.push(format_kv("CPU", info.cpu, bold_accent, no_color))
|
|
"memory" | "mem" =>
|
|
lines.push(format_kv("Memory", info.memory, bold_accent, no_color))
|
|
"disk" =>
|
|
lines.push(format_kv("Disk (/)", info.disk, bold_accent, no_color))
|
|
"localip" | "ip" =>
|
|
lines.push(format_kv("Local IP", info.local_ip, bold_accent, no_color))
|
|
"locale" =>
|
|
lines.push(format_kv("Locale", info.locale, bold_accent, no_color))
|
|
"break" => lines.push("")
|
|
"colors" => {
|
|
let bars = render_colors_bar(no_color)
|
|
for b in bars {
|
|
lines.push(b)
|
|
}
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
lines
|
|
}
|
|
|
|
///|
|
|
pub fn render_fetch(info : SystemInfo, opts : CliOptions) -> String {
|
|
if opts.json {
|
|
return info.to_json()
|
|
}
|
|
let logo_name = match opts.logo {
|
|
Some(name) => name
|
|
None => detect_logo_name(info.os_id)
|
|
}
|
|
let logo = get_logo(logo_name)
|
|
let info_lines = render_info_lines(
|
|
info,
|
|
opts.structure,
|
|
logo.primary_color,
|
|
opts.no_color,
|
|
)
|
|
if opts.no_logo {
|
|
return info_lines.join("\n")
|
|
}
|
|
let mut logo_width = 0
|
|
for l in logo.lines {
|
|
let w = visible_len(l)
|
|
if w > logo_width {
|
|
logo_width = w
|
|
}
|
|
}
|
|
let gap = 3
|
|
let total_rows = if logo.lines.length() > info_lines.length() {
|
|
logo.lines.length()
|
|
} else {
|
|
info_lines.length()
|
|
}
|
|
let out : Array[String] = []
|
|
for i in 0..<total_rows {
|
|
let left_raw = if i < logo.lines.length() { logo.lines[i] } else { "" }
|
|
let left_padded = pad_right_visible(left_raw, logo_width + gap)
|
|
let left_colored = colorize(left_padded, logo.primary_color, opts.no_color)
|
|
let right = if i < info_lines.length() { info_lines[i] } else { "" }
|
|
out.push("\{left_colored}\{right}")
|
|
}
|
|
out.join("\n")
|
|
}
|