Created
October 8, 2025 12:53
-
-
Save xyproto/c44017e27f4a0b7908f69ee1c5fd41d4 to your computer and use it in GitHub Desktop.
Show uptime + selected uname info in a nice and brief way, with colors, for Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "flag" | |
| "fmt" | |
| "io" | |
| "os" | |
| "strconv" | |
| "strings" | |
| "syscall" | |
| ) | |
| // ANSI color codes for terminal output | |
| const ( | |
| ColorReset = "\x1b[0m" | |
| ColorBold = "\x1b[1m" | |
| ColorErrorRed = "\x1b[91m" | |
| ColorWhite = "\x1b[97m" | |
| ColorRed = "\x1b[31m" | |
| ColorOrange = "\x1b[33m" | |
| ColorYellow = "\x1b[93m" | |
| ) | |
| // trimNullBytes converts a null-terminated []int8 slice to a Go string. | |
| // It explicitly copies the int8 values to a byte slice. | |
| func trimNullBytes(s []int8) string { | |
| b := make([]byte, len(s)) | |
| for i, v := range s { | |
| b[i] = byte(v) | |
| } | |
| if i := bytes.IndexByte(b, 0); i != -1 { | |
| b = b[:i] | |
| } | |
| return string(b) | |
| } | |
| func runUpsie() error { | |
| fullKernelVersion := flag.Bool("l", false, "Display full kernel version") | |
| flag.Parse() | |
| // Retrieve system information using uname() | |
| var unameData syscall.Utsname | |
| if err := syscall.Uname(&unameData); err != nil { | |
| return fmt.Errorf("failed to get system information (uname): %w", err) | |
| } | |
| hostname := trimNullBytes(unameData.Nodename[:]) | |
| kernelRelease := trimNullBytes(unameData.Release[:]) | |
| machineArch := trimNullBytes(unameData.Machine[:]) | |
| // Retrieve uptime information from /proc/uptime | |
| uptimeBytes, err := os.ReadFile("/proc/uptime") | |
| if err != nil { | |
| return fmt.Errorf("failed to open /proc/uptime: %w", err) | |
| } | |
| uptimeStr := strings.Fields(string(uptimeBytes))[0] | |
| uptimeSeconds, err := strconv.ParseFloat(uptimeStr, 64) | |
| if err != nil { | |
| return fmt.Errorf("failed to parse uptime from /proc/uptime: %w", err) | |
| } | |
| // Prepare kernel version string based on 'fullKernelVersion' flag | |
| kernelVersionDisplay := kernelRelease | |
| if !*fullKernelVersion { | |
| parts := strings.Split(kernelRelease, ".") | |
| if len(parts) >= 2 { | |
| kernelVersionDisplay = parts[0] + "." + parts[1] | |
| } | |
| } | |
| // Output the info | |
| fmt.Printf( | |
| "%s%s%s @ %s%s%s (%s%s%s) - %s%s%s ", | |
| ColorBold+ColorWhite, hostname, ColorReset, | |
| ColorBold+ColorRed, kernelVersionDisplay, ColorReset, | |
| ColorBold+ColorOrange, machineArch, ColorReset, | |
| ColorBold+ColorYellow, "Up:", ColorReset, | |
| ) | |
| fmt.Print(ColorYellow) | |
| printUptime(os.Stdout, int64(uptimeSeconds)) | |
| fmt.Printf("%s\n", ColorReset) | |
| return nil | |
| } | |
| // printUptime formats and prints the uptime duration to the given writer. | |
| func printUptime(w io.Writer, totalSeconds int64) { | |
| if totalSeconds == 0 { | |
| fmt.Fprint(w, "just started") | |
| return | |
| } | |
| if totalSeconds < 60 { | |
| fmt.Fprint(w, "less than 1m") | |
| return | |
| } | |
| var b strings.Builder | |
| totalMinutes := totalSeconds / 60 | |
| minutes := totalMinutes % 60 | |
| totalHours := totalMinutes / 60 | |
| hours := totalHours % 24 | |
| totalDays := totalHours / 24 | |
| days := totalDays % 7 | |
| weeks := totalDays / 7 | |
| if weeks > 0 { | |
| fmt.Fprintf(&b, "%dw", weeks) | |
| } | |
| if days > 0 { | |
| if b.Len() > 0 { | |
| b.WriteString(", ") | |
| } | |
| fmt.Fprintf(&b, "%dd", days) | |
| } | |
| if hours > 0 { | |
| if b.Len() > 0 { | |
| b.WriteString(", ") | |
| } | |
| fmt.Fprintf(&b, "%dh", hours) | |
| } | |
| if minutes > 0 { | |
| if b.Len() > 0 { | |
| b.WriteString(", ") | |
| } | |
| fmt.Fprintf(&b, "%dm", minutes) | |
| } | |
| fmt.Fprint(w, b.String()) | |
| } | |
| func main() { | |
| if err := runUpsie(); err != nil { | |
| fmt.Fprintf(os.Stderr, "%sError: %v%s\n", ColorErrorRed, err, ColorReset) | |
| os.Exit(1) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment