Files
nandi ca918a4a34
Copilot Setup Steps / copilot-setup-steps (push) Successful in 21s
feat: implement moonfetch (fastfetch clone in MoonBit native)
2026-11-17 22:19:58 +00:00

189 lines
5.5 KiB
MoonBit

///|
#derive(Eq, Debug)
pub struct CliOptions {
show_help : Bool
show_version : Bool
logo : String?
no_logo : Bool
json : Bool
no_color : Bool
structure : Array[String]
config_path : String?
error : String?
}
///|
pub fn default_structure() -> Array[String] {
[
"title", "separator", "os", "host", "kernel", "uptime", "packages", "shell",
"terminal", "de", "cpu", "memory", "disk", "localip", "locale", "break", "colors",
]
}
///|
pub fn CliOptions::default() -> CliOptions {
{
show_help: false,
show_version: false,
logo: None,
no_logo: false,
json: false,
no_color: false,
structure: default_structure(),
config_path: None,
error: None,
}
}
///|
pub fn parse_structure(s : String) -> Array[String] {
let normalized = s.replace_all(old=",", new=":")
let out : Array[String] = []
for part in normalized.split(":") {
let item = part.trim().to_owned().to_lower()
if !item.is_empty() {
out.push(item)
}
}
if out.is_empty() {
default_structure()
} else {
out
}
}
///|
pub fn parse_config_content(content : String, base : CliOptions) -> CliOptions {
let mut logo = base.logo
let mut no_logo = base.no_logo
let mut json = base.json
let mut no_color = base.no_color
let mut structure = base.structure
for line_view in content.split("\n") {
let line = line_view.trim().to_owned()
if line.is_empty() || line.has_prefix("#") || line == "{" || line == "}" {
continue
}
let clean = if line.has_suffix(",") {
line.unsafe_substring(start=0, end=line.length() - 1)
} else {
line
}
let sep_idx = match clean.find("=") {
Some(i) => Some(i)
None => clean.find(":")
}
match sep_idx {
Some(idx) => {
let key = trim_quotes(clean.unsafe_substring(start=0, end=idx)).to_lower()
let val = trim_quotes(
clean.unsafe_substring(start=idx + 1, end=clean.length()),
)
match key {
"logo" =>
if val.to_lower() == "none" {
no_logo = true
} else if !val.is_empty() {
logo = Some(val)
}
"no_logo" | "no-logo" => no_logo = val.to_lower() == "true"
"json" => json = val.to_lower() == "true"
"no_color" | "no-color" => no_color = val.to_lower() == "true"
"structure" => structure = parse_structure(val)
_ => ()
}
}
None => ()
}
}
{ ..base, logo, no_logo, json, no_color, structure, }
}
///|
pub fn parse_args(args : Array[String]) -> CliOptions {
let mut opts = CliOptions::default()
let mut i = 0
while i < args.length() {
let arg = args[i]
if arg == "-h" || arg == "--help" {
opts = { ..opts, show_help: true, }
} else if arg == "-v" || arg == "--version" {
opts = { ..opts, show_version: true, }
} else if arg == "--no-logo" {
opts = { ..opts, no_logo: true, }
} else if arg == "--json" {
opts = { ..opts, json: true, }
} else if arg == "--no-color" {
opts = { ..opts, no_color: true, }
} else if arg == "-l" || arg == "--logo" {
if i + 1 >= args.length() {
return { ..opts, error: Some("Missing value for \{arg}"), }
}
i += 1
let val = args[i]
if val.to_lower() == "none" {
opts = { ..opts, no_logo: true, }
} else {
opts = { ..opts, logo: Some(val), }
}
} else if arg.has_prefix("--logo=") {
let val = arg.unsafe_substring(start=7, end=arg.length())
if val.to_lower() == "none" {
opts = { ..opts, no_logo: true, }
} else {
opts = { ..opts, logo: Some(val), }
}
} else if arg == "-s" || arg == "--structure" {
if i + 1 >= args.length() {
return { ..opts, error: Some("Missing value for \{arg}"), }
}
i += 1
opts = { ..opts, structure: parse_structure(args[i]), }
} else if arg.has_prefix("--structure=") {
let val = arg.unsafe_substring(start=12, end=arg.length())
opts = { ..opts, structure: parse_structure(val), }
} else if arg == "-c" || arg == "--config" {
if i + 1 >= args.length() {
return { ..opts, error: Some("Missing value for \{arg}"), }
}
i += 1
opts = { ..opts, config_path: Some(args[i]), }
} else if arg.has_prefix("--config=") {
let val = arg.unsafe_substring(start=9, end=arg.length())
opts = { ..opts, config_path: Some(val), }
} else {
return { ..opts, error: Some("Unknown argument: \{arg}"), }
}
i += 1
}
opts
}
///|
pub fn version_text() -> String {
"moonfetch 0.1.0 (MoonBit native)"
}
///|
pub fn help_text() -> String {
let logos = available_logos().join(", ")
[
"moonfetch - A fastfetch clone written in MoonBit",
"",
"Usage: moonfetch [OPTIONS]",
"",
"Options:",
" -h, --help Show this help message and exit",
" -v, --version Show version information and exit",
" -l, --logo <name> Select ASCII logo (\{logos})",
" --no-logo Disable the ASCII logo",
" --no-color Disable ANSI color output",
" --json Output system information as JSON",
" -s, --structure <modules> Colon-separated modules to display",
" (title, separator, os, host, kernel, uptime,",
" packages, shell, terminal, de, cpu, memory,",
" disk, localip, locale, break, colors)",
" -c, --config <path> Load options from a config file",
].join("\n")
}