From ab4ef706577c5405ca94a68fb1ab66e82da92b31 Mon Sep 17 00:00:00 2001 From: nicknase27 Date: Wed, 7 Oct 2026 18:34:31 +0200 Subject: [PATCH] init --- .gitignore | 1 + Cargo.lock | 7 +++ Cargo.toml | 7 +++ LICENSE | 21 +++++++++ src/main.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..4b32df2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rfetch" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..046d7b0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "rfetch" +version = "0.1.0" +edition = "2024" +license = "MIT" + +[dependencies] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e5ae24e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 nicknase27.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5f03458 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,121 @@ +use std::env; +use std::fs::read_to_string; + +fn print_logo() { + print!( + r" + --- + (.ยท | + (<> | + / __ \ + ( / \\ /| +_/\ __)/_) +\/-____\/ +" + ); +} + +#[cfg(target_os = "linux")] +fn get_os() -> String { + let contents: Vec = read_to_string("/etc/os-release") + .unwrap() + .lines() + .map(String::from) + .collect(); + + let os = contents[0] + .trim_start_matches("NAME=") + .trim_matches('"') + .to_string(); + return os; +} + +#[cfg(unix)] +fn get_shell() -> String { + match env::var("SHELL") { + Ok(shell) => shell.split('/').last().unwrap_or(&shell).to_string(), + Err(e) => e.to_string(), + } +} + +#[cfg(unix)] +fn get_host() -> String { + read_to_string("/etc/hostname").unwrap().trim().to_string() +} + +#[cfg(target_os = "linux")] +fn get_kernel() -> String { + let kernel = read_to_string("/proc/sys/kernel/osrelease") + .unwrap() + .trim() + .to_string(); + format!("Linux {}", kernel) +} + +#[cfg(not(target_os = "linux"))] +fn get_kernel() -> String { + String::new() +} + +fn get_terminal() -> String { + match env::var("TERM") { + Ok(term) => term, + Err(e) => e.to_string(), + } +} + +#[cfg(target_os = "linux")] +fn get_memory() -> String { + let content = read_to_string("/proc/meminfo").unwrap_or_default(); + let mut mem_total: u64 = 0; + let mut mem_avail: u64 = 0; + + for line in content.lines() { + if line.starts_with("MemTotal:") { + mem_total = line + .split_whitespace() + .nth(1) + .unwrap_or("0") + .parse() + .unwrap_or(0); + } else if line.starts_with("MemAvailable:") { + mem_avail = line + .split_whitespace() + .nth(1) + .unwrap_or("0") + .parse() + .unwrap_or(0); + } + } + + let mem_used = mem_total.saturating_sub(mem_avail); + let used_gib = mem_used as f64 / 1048576.0; + let total_gib = mem_total as f64 / 1048576.0; + + if used_gib > 1.0 { + format!("{:.2} GiB / {:.2} GiB", used_gib, total_gib) + } else { + let used_mib = mem_used as f64 / 1024.0; + format!("{:.1} MiB / {:.2} GiB", used_mib, total_gib) + } +} + +#[cfg(not(target_os = "linux"))] +fn get_memory() -> String { + String::new() +} + +fn main() { + let os = get_os(); + let shell = get_shell(); + let kernel = get_kernel(); + let host = get_host(); + let term = get_terminal(); + let memory = get_memory(); + + print_logo(); + println!( + "\nOS: {}\nShell: {}\nKernel: {}\nHost: {}\nTerm: {}\nMemory: {}", + os, shell, kernel, host, term, memory + ); +}