Files
2026-11-19 10:03:28 +00:00

222 lines
5.7 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
}
///|
fn render_logo_line(line : String, logo : Logo, no_color : Bool) -> String {
if line.is_empty() {
return ""
}
let buf = StringBuilder()
let mut has_color = false
if !no_color &&
!logo.primary_color.is_empty() &&
!line.has_prefix("$1") &&
!line.has_prefix("$2") {
buf.write_string(logo.primary_color)
has_color = true
}
let mut i = 0
let len = line.length()
while i < len {
if i + 1 < len && line[i] == '$' {
let next = line[i + 1]
if next == '1' {
if !no_color && !logo.primary_color.is_empty() {
buf.write_string(logo.primary_color)
has_color = true
}
i += 2
continue
} else if next == '2' {
if !no_color && !logo.secondary_color.is_empty() {
buf.write_string(logo.secondary_color)
has_color = true
}
i += 2
continue
}
}
buf.write_char(Int32::unsafe_to_char(line[i].to_int()))
i += 1
}
if has_color {
buf.write_string("\u001b[0m")
}
buf.to_string()
}
///|
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 rendered_logo : Array[String] = []
let mut logo_width = 0
for l in logo.lines {
let rl = render_logo_line(l, logo, opts.no_color)
rendered_logo.push(rl)
let w = visible_len(rl)
if w > logo_width {
logo_width = w
}
}
let gap = 3
let total_rows = if rendered_logo.length() > info_lines.length() {
rendered_logo.length()
} else {
info_lines.length()
}
let out : Array[String] = []
for i in 0..<total_rows {
let left_raw = if i < rendered_logo.length() {
rendered_logo[i]
} else {
""
}
let left_padded = pad_right_visible(left_raw, logo_width + gap)
let right = if i < info_lines.length() { info_lines[i] } else { "" }
out.push("\{left_padded}\{right}")
}
out.join("\n")
}