Skip to main content

ruvix_shell/commands/
info.rs

1//! Kernel information command implementation.
2
3use crate::ShellBackend;
4use alloc::format;
5use alloc::string::String;
6
7/// Format uptime in a human-readable way.
8fn format_uptime(nanos: u64) -> String {
9    let total_secs = nanos / 1_000_000_000;
10    let days = total_secs / 86400;
11    let hours = (total_secs % 86400) / 3600;
12    let minutes = (total_secs % 3600) / 60;
13    let secs = total_secs % 60;
14
15    if days > 0 {
16        format!("{}d {}h {}m {}s", days, hours, minutes, secs)
17    } else if hours > 0 {
18        format!("{}h {}m {}s", hours, minutes, secs)
19    } else if minutes > 0 {
20        format!("{}m {}s", minutes, secs)
21    } else {
22        format!("{}s", secs)
23    }
24}
25
26/// Execute the info command.
27#[must_use]
28pub fn execute<B: ShellBackend>(backend: &B) -> String {
29    let info = backend.kernel_info();
30    let uptime_ns = info.current_time_ns.saturating_sub(info.boot_time_ns);
31
32    format!(
33        r"RuVix Cognition Kernel
34======================
35Version:    {}
36Build:      {}
37CPUs:       {} online
38Uptime:     {}",
39        info.version,
40        info.build_time,
41        info.cpu_count,
42        format_uptime(uptime_ns)
43    )
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_format_uptime_seconds() {
52        assert_eq!(format_uptime(5_000_000_000), "5s");
53        assert_eq!(format_uptime(59_000_000_000), "59s");
54    }
55
56    #[test]
57    fn test_format_uptime_minutes() {
58        assert_eq!(format_uptime(60_000_000_000), "1m 0s");
59        assert_eq!(format_uptime(125_000_000_000), "2m 5s");
60    }
61
62    #[test]
63    fn test_format_uptime_hours() {
64        assert_eq!(format_uptime(3600_000_000_000), "1h 0m 0s");
65        assert_eq!(format_uptime(3725_000_000_000), "1h 2m 5s");
66    }
67
68    #[test]
69    fn test_format_uptime_days() {
70        assert_eq!(format_uptime(86400_000_000_000), "1d 0h 0m 0s");
71        assert_eq!(format_uptime(90125_000_000_000), "1d 1h 2m 5s");
72    }
73}