mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
lib: collapse print_system_info into single write! call; custom logo support
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I589075f0e4b1629713c872397c602ac66a6a6964
This commit is contained in:
parent
14fc728181
commit
8324b35661
1 changed files with 155 additions and 152 deletions
|
|
@ -289,6 +289,49 @@ struct Fields {
|
||||||
colors: String,
|
colors: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Minimal, stack-allocated writer implementing `core::fmt::Write`. Avoids heap
|
||||||
|
/// allocation for the output buffer.
|
||||||
|
struct StackWriter<'a> {
|
||||||
|
buf: &'a mut [u8],
|
||||||
|
pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> StackWriter<'a> {
|
||||||
|
#[inline]
|
||||||
|
const fn new(buf: &'a mut [u8]) -> Self {
|
||||||
|
Self { buf, pos: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn written(&self) -> &[u8] {
|
||||||
|
&self.buf[..self.pos]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Write for StackWriter<'_> {
|
||||||
|
#[inline]
|
||||||
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
|
let bytes = s.as_bytes();
|
||||||
|
let to_write = bytes.len().min(self.buf.len() - self.pos);
|
||||||
|
self.buf[self.pos..self.pos + to_write].copy_from_slice(&bytes[..to_write]);
|
||||||
|
self.pos += to_write;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Custom logo art embedded at compile time via the `MICROFETCH_LOGO`
|
||||||
|
/// environment variable. Set it to 9 newline-separated lines of ASCII/Unicode
|
||||||
|
/// art when building to replace the default NixOS logo:
|
||||||
|
///
|
||||||
|
/// `MICROFETCH_LOGO="$(cat my_logo.txt)"` cargo build --release
|
||||||
|
///
|
||||||
|
/// Each line maps to one info row. When unset, the built-in two-tone NixOS
|
||||||
|
/// logo is used.
|
||||||
|
const CUSTOM_LOGO: &str = match option_env!("MICROFETCH_LOGO") {
|
||||||
|
Some(s) => s,
|
||||||
|
None => "",
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
||||||
fn print_system_info(fields: &Fields) -> Result<(), Error> {
|
fn print_system_info(fields: &Fields) -> Result<(), Error> {
|
||||||
let Fields {
|
let Fields {
|
||||||
|
|
@ -304,168 +347,128 @@ fn print_system_info(fields: &Fields) -> Result<(), Error> {
|
||||||
} = fields;
|
} = fields;
|
||||||
|
|
||||||
let no_color = colors::is_no_color();
|
let no_color = colors::is_no_color();
|
||||||
let colors_obj = colors::Colors::new(no_color);
|
let c = colors::Colors::new(no_color);
|
||||||
let cyan = colors_obj.cyan;
|
|
||||||
let blue = colors_obj.blue;
|
|
||||||
let reset = colors_obj.reset;
|
|
||||||
|
|
||||||
// Build output string
|
|
||||||
let mut buf = [0u8; 2048];
|
let mut buf = [0u8; 2048];
|
||||||
let mut pos = 0usize;
|
let mut w = StackWriter::new(&mut buf);
|
||||||
|
|
||||||
// Helper to write to buffer
|
if CUSTOM_LOGO.is_empty() {
|
||||||
let mut write_str = |s: &str| {
|
// Default two-tone NixOS logo rendered as a single write! pass.
|
||||||
let bytes = s.as_bytes();
|
core::fmt::write(
|
||||||
let remaining = buf.len() - pos;
|
&mut w,
|
||||||
let to_write = bytes.len().min(remaining);
|
format_args!(
|
||||||
buf[pos..pos + to_write].copy_from_slice(&bytes[..to_write]);
|
"\n {b} ▟█▖ {cy}▝█▙ ▗█▛ {user_info} ~{rs}\n {b} \
|
||||||
pos += to_write;
|
▗▄▄▟██▄▄▄▄▄{cy}▝█▙█▛ {b}▖ {cy}\u{F313} {b}System{rs} \
|
||||||
};
|
{os_name}\n {b} ▀▀▀▀▀▀▀▀▀▀▀▘{cy}▝██ {b}▟█▖ {cy}\u{E712} \
|
||||||
|
{b}Kernel{rs} {kernel_version}\n {cy} ▟█▛ \
|
||||||
|
{cy}▝█▘{b}▟█▛ {cy}\u{E795} {b}Shell{rs} {shell}\n \
|
||||||
|
{cy}▟█████▛ {b}▟█████▛ {cy}\u{F017} {b}Uptime{rs} \
|
||||||
|
{uptime}\n {cy} ▟█▛{b}▗█▖ {b}▟█▛ {cy}\u{F2D2} \
|
||||||
|
{b}Desktop{rs} {desktop}\n {cy} ▝█▛ \
|
||||||
|
{b}██▖{cy}▗▄▄▄▄▄▄▄▄▄▄▄ {cy}\u{F035B} {b}Memory{rs} \
|
||||||
|
{memory_usage}\n {cy} ▝ {b}▟█▜█▖{cy}▀▀▀▀▀██▛▀▀▘ \
|
||||||
|
{cy}\u{F194E} {b}Storage (/){rs} {storage}\n {b} ▟█▘ ▜█▖ \
|
||||||
|
{cy}▝█▛ {cy}\u{E22B} {b}Colors{rs} {colors}\n\n",
|
||||||
|
b = c.blue,
|
||||||
|
cy = c.cyan,
|
||||||
|
rs = c.reset,
|
||||||
|
user_info = user_info,
|
||||||
|
os_name = os_name,
|
||||||
|
kernel_version = kernel_version,
|
||||||
|
shell = shell,
|
||||||
|
uptime = uptime,
|
||||||
|
desktop = desktop,
|
||||||
|
memory_usage = memory_usage,
|
||||||
|
storage = storage,
|
||||||
|
colors = colors,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
|
} else {
|
||||||
|
// Custom logo is 9 lines from MICROFETCH_LOGO env var, one per info row.
|
||||||
|
// Lines beyond 9 are ignored; missing lines render as empty.
|
||||||
|
let mut lines = CUSTOM_LOGO.split('\n');
|
||||||
|
let logo_rows: [&str; 9] =
|
||||||
|
core::array::from_fn(|_| lines.next().unwrap_or(""));
|
||||||
|
|
||||||
write_str("\n ");
|
// Row format mirrors the default logo path exactly.
|
||||||
write_str(blue);
|
let rows: [(&str, &str, &str, &str, &str); 9] = [
|
||||||
write_str(" ▟█▖ ");
|
("", "", user_info.as_str(), " ", " ~"),
|
||||||
write_str(cyan);
|
("\u{F313} ", "System", os_name.as_str(), " ", ""),
|
||||||
write_str("▝█▙ ▗█▛ ");
|
(
|
||||||
write_str(user_info);
|
"\u{E712} ",
|
||||||
write_str(" ~");
|
"Kernel",
|
||||||
write_str(reset);
|
kernel_version.as_str(),
|
||||||
write_str("\n");
|
" ",
|
||||||
|
"",
|
||||||
|
),
|
||||||
|
("\u{E795} ", "Shell", shell.as_str(), " ", ""),
|
||||||
|
("\u{F017} ", "Uptime", uptime.as_str(), " ", ""),
|
||||||
|
("\u{F2D2} ", "Desktop", desktop.as_str(), " ", ""),
|
||||||
|
(
|
||||||
|
"\u{F035B} ",
|
||||||
|
"Memory",
|
||||||
|
memory_usage.as_str(),
|
||||||
|
" ",
|
||||||
|
"",
|
||||||
|
),
|
||||||
|
("\u{F194E} ", "Storage (/)", storage.as_str(), " ", ""),
|
||||||
|
("\u{E22B} ", "Colors", colors.as_str(), " ", ""),
|
||||||
|
];
|
||||||
|
|
||||||
write_str(" ");
|
core::fmt::write(&mut w, format_args!("\n")).ok();
|
||||||
write_str(blue);
|
for i in 0..9 {
|
||||||
write_str(" ▗▄▄▟██▄▄▄▄▄");
|
let (icon, key, value, spacing, suffix) = rows[i];
|
||||||
write_str(cyan);
|
if key.is_empty() {
|
||||||
write_str("▝█▙█▛ ");
|
// Row 1 has no icon/key, just logo + user_info
|
||||||
write_str(blue);
|
core::fmt::write(
|
||||||
write_str("▖ ");
|
&mut w,
|
||||||
write_str(cyan);
|
format_args!(
|
||||||
write_str(" ");
|
" {cy}{logo}{rs} {value}{suffix}\n",
|
||||||
write_str(blue);
|
cy = c.cyan,
|
||||||
write_str("System");
|
rs = c.reset,
|
||||||
write_str(reset);
|
logo = logo_rows[i],
|
||||||
write_str(" ");
|
value = value,
|
||||||
write_str(os_name);
|
suffix = suffix,
|
||||||
write_str("\n");
|
),
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
|
} else {
|
||||||
|
core::fmt::write(
|
||||||
|
&mut w,
|
||||||
|
format_args!(
|
||||||
|
" {cy}{logo}{rs} \
|
||||||
|
{cy}{icon}{b}{key}{rs}{spacing}{value}{suffix}\n",
|
||||||
|
cy = c.cyan,
|
||||||
|
b = c.blue,
|
||||||
|
rs = c.reset,
|
||||||
|
logo = logo_rows[i],
|
||||||
|
icon = icon,
|
||||||
|
key = key,
|
||||||
|
spacing = spacing,
|
||||||
|
value = value,
|
||||||
|
suffix = suffix,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
core::fmt::write(&mut w, format_args!("\n")).ok();
|
||||||
|
}
|
||||||
|
|
||||||
write_str(" ");
|
// Single syscall for the entire output.
|
||||||
write_str(blue);
|
let out = w.written();
|
||||||
write_str(" ▀▀▀▀▀▀▀▀▀▀▀▘");
|
let written = unsafe { sys_write(1, out.as_ptr(), out.len()) };
|
||||||
write_str(cyan);
|
|
||||||
write_str("▝██ ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("▟█▖ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Kernel");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(kernel_version);
|
|
||||||
write_str("\n");
|
|
||||||
|
|
||||||
write_str(" ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ▟█▛ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str("▝█▘");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("▟█▛ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Shell");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(shell);
|
|
||||||
write_str("\n");
|
|
||||||
|
|
||||||
write_str(" ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str("▟█████▛ ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("▟█████▛ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Uptime");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(uptime);
|
|
||||||
write_str("\n");
|
|
||||||
|
|
||||||
write_str(" ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ▟█▛");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("▗█▖ ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("▟█▛ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Desktop");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(desktop);
|
|
||||||
write_str("\n");
|
|
||||||
|
|
||||||
write_str(" ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ▝█▛ ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("██▖");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str("▗▄▄▄▄▄▄▄▄▄▄▄ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Memory");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(memory_usage);
|
|
||||||
write_str("\n");
|
|
||||||
|
|
||||||
write_str(" ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ▝ ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("▟█▜█▖");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str("▀▀▀▀▀██▛▀▀▘ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Storage (/)");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(storage);
|
|
||||||
write_str("\n");
|
|
||||||
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str(" ▟█▘ ▜█▖ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str("▝█▛ ");
|
|
||||||
write_str(cyan);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(blue);
|
|
||||||
write_str("Colors");
|
|
||||||
write_str(reset);
|
|
||||||
write_str(" ");
|
|
||||||
write_str(colors);
|
|
||||||
write_str("\n\n");
|
|
||||||
|
|
||||||
// Direct syscall to avoid stdout buffering allocation
|
|
||||||
let written = unsafe { sys_write(1, buf.as_ptr(), pos) };
|
|
||||||
if written < 0 {
|
if written < 0 {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
return Err(Error::OsError(written as i32));
|
return Err(Error::OsError(written as i32));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cast_sign_loss)]
|
#[allow(clippy::cast_sign_loss)]
|
||||||
if written as usize != pos {
|
if written as usize != out.len() {
|
||||||
return Err(Error::WriteError);
|
return Err(Error::WriteError);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue