///| extern "C" fn moonfetch_uname_info_ffi() -> Bytes = "moonfetch_uname_info" ///| extern "C" fn moonfetch_get_username_ffi() -> Bytes = "moonfetch_get_username" ///| #borrow(path) extern "C" fn moonfetch_get_disk_usage_ffi(path : Bytes) -> Bytes = "moonfetch_get_disk_usage" ///| extern "C" fn moonfetch_get_term_size_ffi() -> Bytes = "moonfetch_get_term_size" ///| extern "C" fn moonfetch_get_local_ip_ffi() -> Bytes = "moonfetch_get_local_ip" ///| #borrow(cmd) extern "C" fn moonfetch_popen_first_line_ffi(cmd : Bytes) -> Bytes = "moonfetch_popen_first_line" ///| #borrow(path) extern "C" fn moonfetch_read_file_stream_ffi(path : Bytes) -> Bytes = "moonfetch_read_file_stream" ///| fn to_c_bytes(s : String) -> Bytes { @utf8.encode(s) + Bytes::make(1, (0).to_byte()) } ///| fn from_c_bytes(b : Bytes) -> String { @utf8.decode_lossy(b) } ///| fn try_read_file(path : String) -> String? { if !@fs.path_exists(path) { return None } let bytes = moonfetch_read_file_stream_ffi(to_c_bytes(path)) if bytes.length() == 0 { None } else { Some(@utf8.decode_lossy(bytes)) } } ///| fn read_trimmed_file(path : String) -> String { match try_read_file(path) { Some(s) => s.trim().to_owned() None => "" } } ///| pub(all) struct SystemInfo { username : String hostname : String os_id : String os_name : String host_model : String kernel : String arch : String uptime : String uptime_secs : Int32 packages : String shell : String terminal : String de_wm : String cpu : String memory : String mem_used_kib : UInt64 mem_total_kib : UInt64 disk : String disk_used_bytes : UInt64 disk_total_bytes : UInt64 local_ip : String locale : String } ///| fn basename(path : String) -> String { let parts : Array[String] = [] for p in path.split("/") { let s = p.to_owned() if !s.is_empty() { parts.push(s) } } if parts.is_empty() { path } else { parts[parts.length() - 1] } } ///| fn detect_packages() -> String { let counts : Array[String] = [] match try_read_file("/var/lib/dpkg/status") { Some(content) => { let n = count_dpkg_installed(content) if n > 0 { counts.push("\{n} (dpkg)") } } None => () } match try_read_file("/lib/apk/db/installed") { Some(content) => { let n = count_apk_installed(content) if n > 0 { counts.push("\{n} (apk)") } } None => () } if counts.is_empty() && @fs.path_exists("/var/lib/pacman/local") { try { let entries = @fs.read_dir("/var/lib/pacman/local") let mut n = 0 for e in entries { if e != "ALPM_DB_VERSION" { n += 1 } } if n > 0 { counts.push("\{n} (pacman)") } } catch { _ => () } } if counts.is_empty() { "Unknown" } else { counts.join(", ") } } ///| fn detect_host_model() -> String { let prod = read_trimmed_file("/sys/devices/virtual/dmi/id/product_name") let ver = read_trimmed_file("/sys/devices/virtual/dmi/id/product_version") let vendor = read_trimmed_file("/sys/devices/virtual/dmi/id/sys_vendor") if !prod.is_empty() && prod != "None" { if !ver.is_empty() && ver != "None" && ver != prod { return "\{prod} (\{ver})" } return prod } if !vendor.is_empty() && vendor != "None" { return vendor } let dt_model = read_trimmed_file("/proc/device-tree/model") if !dt_model.is_empty() { return dt_model } if @fs.path_exists("/.dockerenv") { return "Docker Container" } "Linux VM / Host" } ///| fn detect_shell() -> String { match @env.get_env_var("SHELL") { Some(sh) => { let name = basename(sh) if name == "bash" { let ver = from_c_bytes( moonfetch_popen_first_line_ffi( to_c_bytes("bash --version 2>/dev/null | head -n1"), ), ) match ver.find("version ") { Some(idx) => { let rest = ver.unsafe_substring(start=idx + 8, end=ver.length()) match rest.find("(") { Some(p) => "bash " + rest.unsafe_substring(start=0, end=p).trim().to_owned() None => "bash" } } None => "bash" } } else { name } } None => "Unknown" } } ///| fn detect_terminal() -> String { let term_prog = match @env.get_env_var("TERM_PROGRAM") { Some(v) => v None => match @env.get_env_var("TERM") { Some(v) => v None => "Unknown" } } let size_raw = from_c_bytes(moonfetch_get_term_size_ffi()) if size_raw.is_empty() { term_prog } else { match size_raw.find(",") { Some(idx) => { let cols = size_raw.unsafe_substring(start=0, end=idx) let rows = size_raw.unsafe_substring( start=idx + 1, end=size_raw.length(), ) "\{term_prog} (\{cols}x\{rows})" } None => term_prog } } } ///| fn detect_de_wm() -> String { match @env.get_env_var("XDG_CURRENT_DESKTOP") { Some(v) => if !v.is_empty() { return v } None => () } match @env.get_env_var("DESKTOP_SESSION") { Some(v) => if !v.is_empty() { return v } None => () } match @env.get_env_var("WAYLAND_DISPLAY") { Some(v) => if !v.is_empty() { return "Wayland (\{v})" } None => () } match @env.get_env_var("DISPLAY") { Some(v) => if !v.is_empty() { return "X11 (\{v})" } None => () } "Headless / TTY" } ///| fn detect_locale() -> String { match @env.get_env_var("LC_ALL") { Some(v) => if !v.is_empty() { return v } None => () } match @env.get_env_var("LANG") { Some(v) => if !v.is_empty() { return v } None => () } "C" } ///| pub fn collect_system_info() -> SystemInfo { let uname_raw = from_c_bytes(moonfetch_uname_info_ffi()) let uname_parts : Array[String] = [] for part in uname_raw.split("\n") { uname_parts.push(part.to_owned()) } let sysname = if uname_parts.length() > 0 { uname_parts[0] } else { "Linux" } let nodename = if uname_parts.length() > 1 { uname_parts[1] } else { "localhost" } let release = if uname_parts.length() > 2 { uname_parts[2] } else { "" } let arch = if uname_parts.length() > 3 { uname_parts[3] } else { "" } let username = from_c_bytes(moonfetch_get_username_ffi()) let os_rel = match try_read_file("/etc/os-release") { Some(c) => parse_os_release(c) None => { pretty_name: sysname, id: "linux", name: sysname, version_id: "", } } let os_name = if os_rel.pretty_name.is_empty() { "\{sysname} \{arch}" } else if arch.is_empty() { os_rel.pretty_name } else { "\{os_rel.pretty_name} \{arch}" } let kernel = if release.is_empty() { sysname } else { "\{sysname} \{release}" } let uptime_secs = match try_read_file("/proc/uptime") { Some(c) => parse_uptime_seconds(c) None => 0 } let uptime = format_uptime(uptime_secs) let cpu_parsed = match try_read_file("/proc/cpuinfo") { Some(c) => parse_cpuinfo(c) None => { model: "Unknown CPU", cores: 1, mhz: "", } } let cpu = if cpu_parsed.model.is_empty() { "Unknown (\{cpu_parsed.cores} cores)" } else { "\{cpu_parsed.model} (\{cpu_parsed.cores})" } let mem_parsed = match try_read_file("/proc/meminfo") { Some(c) => parse_meminfo(c) None => { total_kib: 0UL, available_kib: 0UL, used_kib: 0UL, } } let memory = format_usage( mem_parsed.used_kib * 1024UL, mem_parsed.total_kib * 1024UL, ) let disk_raw = from_c_bytes(moonfetch_get_disk_usage_ffi(to_c_bytes("/"))) let mut disk_used_bytes : UInt64 = 0UL let mut disk_total_bytes : UInt64 = 0UL match disk_raw.find(",") { Some(idx) => { disk_used_bytes = parse_uint64_str( disk_raw.unsafe_substring(start=0, end=idx), ) disk_total_bytes = parse_uint64_str( disk_raw.unsafe_substring(start=idx + 1, end=disk_raw.length()), ) } None => () } let disk = format_usage(disk_used_bytes, disk_total_bytes) let local_ip_raw = from_c_bytes(moonfetch_get_local_ip_ffi()) let local_ip = if local_ip_raw.is_empty() { "None" } else { local_ip_raw } { username, hostname: nodename, os_id: os_rel.id, os_name, host_model: detect_host_model(), kernel, arch, uptime, uptime_secs, packages: detect_packages(), shell: detect_shell(), terminal: detect_terminal(), de_wm: detect_de_wm(), cpu, memory, mem_used_kib: mem_parsed.used_kib, mem_total_kib: mem_parsed.total_kib, disk, disk_used_bytes, disk_total_bytes, local_ip, locale: detect_locale(), } } ///| pub fn escape_json_string(s : String) -> String { let buf = StringBuilder() for ch in s { match ch { '"' => buf.write_string("\\\"") '\\' => buf.write_string("\\\\") '\n' => buf.write_string("\\n") '\r' => buf.write_string("\\r") '\t' => buf.write_string("\\t") _ => buf.write_char(ch) } } buf.to_string() } ///| pub fn SystemInfo::to_json(self : SystemInfo) -> String { let e = escape_json_string let lines = [ "{", " \"username\": \"\{e(self.username)}\",", " \"hostname\": \"\{e(self.hostname)}\",", " \"os_id\": \"\{e(self.os_id)}\",", " \"os\": \"\{e(self.os_name)}\",", " \"host\": \"\{e(self.host_model)}\",", " \"kernel\": \"\{e(self.kernel)}\",", " \"arch\": \"\{e(self.arch)}\",", " \"uptime\": \"\{e(self.uptime)}\",", " \"uptime_secs\": \{self.uptime_secs},", " \"packages\": \"\{e(self.packages)}\",", " \"shell\": \"\{e(self.shell)}\",", " \"terminal\": \"\{e(self.terminal)}\",", " \"de_wm\": \"\{e(self.de_wm)}\",", " \"cpu\": \"\{e(self.cpu)}\",", " \"memory\": \"\{e(self.memory)}\",", " \"mem_used_kib\": \{self.mem_used_kib},", " \"mem_total_kib\": \{self.mem_total_kib},", " \"disk\": \"\{e(self.disk)}\",", " \"disk_used_bytes\": \{self.disk_used_bytes},", " \"disk_total_bytes\": \{self.disk_total_bytes},", " \"local_ip\": \"\{e(self.local_ip)}\",", " \"locale\": \"\{e(self.locale)}\"", "}", ] lines.join("\n") } ///| pub fn resolve_options(args : Array[String]) -> CliOptions { let cli_opts = parse_args(args) match cli_opts.error { Some(_) => return cli_opts None => () } match cli_opts.config_path { Some(path) => match try_read_file(path) { Some(content) => { let from_cfg = parse_config_content(content, CliOptions::default()) let mut merged = from_cfg let cli_only = parse_args(args) if cli_only.show_help { merged = { ..merged, show_help: true, } } if cli_only.show_version { merged = { ..merged, show_version: true, } } if cli_only.no_logo { merged = { ..merged, no_logo: true, } } if cli_only.json { merged = { ..merged, json: true, } } if cli_only.no_color { merged = { ..merged, no_color: true, } } match cli_only.logo { Some(l) => merged = { ..merged, logo: Some(l), } None => () } if cli_only.structure != default_structure() { merged = { ..merged, structure: cli_only.structure, } } { ..merged, config_path: Some(path), } } None => { ..cli_opts, error: Some("Config file not found: \{path}"), } } None => cli_opts } }