diff --git a/internal/system/system.go b/internal/system/system.go index 09be948..db8a306 100644 --- a/internal/system/system.go +++ b/internal/system/system.go @@ -1,14 +1,29 @@ package system import ( + "os" + "os/user" "regexp" "strings" ) var ( - ansiRe = regexp.MustCompile(`\x1b\[[0-9]*m`) + ansiRe = regexp.MustCompile(`\x1b\[[0-9]*m`) + username string + hostname string + hr string ) +func init() { + if u, err := user.Current(); err == nil { + username = u.Username + } + if h, err := os.Hostname(); err == nil { + hostname = h + } + hr = strings.Repeat("-", len(username)+len(hostname)+1) +} + func visLen(s string) int { return len(ansiRe.ReplaceAllString(s, "")) } diff --git a/internal/system/system_darwin.go b/internal/system/system_darwin.go index a546cce..5ddf551 100644 --- a/internal/system/system_darwin.go +++ b/internal/system/system_darwin.go @@ -4,7 +4,7 @@ package system import ( "os" - "os/user" + "os/exec" "path/filepath" "runtime/debug" "smash/internal/color" @@ -30,7 +30,7 @@ const ( color.FgBlue + ` kMMMMMMMMMMMMMMMMMMMMMMd` + "\n" + color.FgBlue + ` ;KMMMMMMMWXXWMMMMMMMk.` + "\n" + color.FgBlue + ` "cooc*" "*coo'"` + "\n" - fetchInfoCount = 7 + fetchInfoCount = 9 ) var ( @@ -43,22 +43,10 @@ func init() { } } -var hr string - func fetchInfo(i int) (string, bool) { switch i { case 0: - username := "unknown" - hostname := "unknown" - if u, err := user.Current(); err == nil { - username = u.Username - } - if h, err := os.Hostname(); err == nil { - hostname = h - } - x := color.FgMagenta + username + color.Reset + "@" + color.FgMagenta + hostname + color.Reset - hr = strings.Repeat("-", len(username)+len(hostname)+1) - return x, true + return color.FgGreen + username + color.Reset + "@" + color.FgGreen + hostname + color.Reset, true case 1: return color.Reset + hr, true case 2: @@ -72,10 +60,53 @@ func fetchInfo(i int) (string, bool) { case 4: return color.FgYellow + "Default Shell" + color.Reset + ": " + DefaultShell, true case 5: - return color.FgYellow + "DE" + color.Reset + ": Aqua" + DefaultShell, true + return color.FgYellow + "Desktop Environment" + color.Reset + ": " + "Aqua", true case 6: - return color.FgYellow + "WM" + color.Reset + ": Quartz Compositor", true + return color.FgYellow + "Window Manager" + color.Reset + ": " + "Quartz Compositor", true + case 7: + sb := strings.Builder{} + sb.WriteString(color.FgYellow) + sb.WriteString("Terminal") + sb.WriteString(color.Reset) + sb.WriteString(": ") + tOk := false + if termProgram, ok := os.LookupEnv("TERM_PROGRAM"); ok { + sb.WriteString(termProgram) + sb.WriteString(" ") + tOk = true + } + if term, ok := os.LookupEnv("TERM"); ok { + sb.WriteString(term) + tOk = true + } + return sb.String(), tOk + case 8: + sb := strings.Builder{} + sb.WriteString(color.FgYellow) + sb.WriteString("CPU") + sb.WriteString(color.Reset) + sb.WriteString(": ") + cOk := false + if cpu, err := sysctl("machdep.cpu.brand_string"); err == nil { + sb.WriteString(cpu) + sb.WriteString(" ") + cOk = true + } + if cores, err := sysctl("hw.logicalcpu_max"); err == nil { + sb.WriteString(cores) + sb.WriteString("-Core") + cOk = true + } + return sb.String(), cOk default: return "", false } } + +func sysctl(name string) (string, error) { + out, err := exec.Command("sysctl", "-n", name).Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(out)), nil +} diff --git a/internal/system/system_linux.go b/internal/system/system_linux.go index e92550d..7764436 100644 --- a/internal/system/system_linux.go +++ b/internal/system/system_linux.go @@ -5,7 +5,6 @@ package system import ( "bufio" "os" - "os/user" "path/filepath" "runtime/debug" "smash/internal/color" @@ -21,7 +20,7 @@ var ( ) const ( - fetchInfoCount = 5 + fetchInfoCount = 6 ) const ( @@ -285,22 +284,10 @@ func setAsciiArt(id string) { } } -var hr string - func fetchInfo(i int) (string, bool) { switch i { case 0: - username := "unknown" - hostname := "unknown" - if u, err := user.Current(); err == nil { - username = u.Username - } - if h, err := os.Hostname(); err == nil { - hostname = h - } - x := color.FgMagenta + username + color.Reset + "@" + color.FgMagenta + hostname + color.Reset - hr = strings.Repeat("-", len(username)+len(hostname)+1) - return x, true + return color.FgYellow + username + color.Reset + "@" + color.FgYellow + hostname + color.Reset, true case 1: return color.Reset + hr, true case 2: @@ -313,6 +300,23 @@ func fetchInfo(i int) (string, bool) { } case 4: return color.FgYellow + "Default Shell" + color.Reset + ": " + DefaultShell, true + case 5: + sb := strings.Builder{} + sb.WriteString(color.FgYellow) + sb.WriteString("Terminal") + sb.WriteString(color.Reset) + sb.WriteString(": ") + tOk := false + if termProgram, ok := os.LookupEnv("TERM_PROGRAM"); ok { + sb.WriteString(termProgram) + sb.WriteString(" ") + tOk = true + } + if term, ok := os.LookupEnv("TERM"); ok { + sb.WriteString(term) + tOk = true + } + return sb.String(), tOk default: return "", false } diff --git a/internal/system/system_windows.go b/internal/system/system_windows.go index 0d95181..def5c75 100644 --- a/internal/system/system_windows.go +++ b/internal/system/system_windows.go @@ -4,14 +4,12 @@ package system import ( "os" - "os/user" "runtime/debug" "smash/internal/color" "strings" ) const ( - Name = "Windows" Ascii = color.FgRed + "lllllll " + color.FgGreen + "lllllll\n" + color.FgRed + "lllllll " + color.FgGreen + "lllllll\n" + color.FgRed + "lllllll " + color.FgGreen + "lllllll\n" + @@ -19,30 +17,17 @@ const ( color.FgBlue + "lllllll " + color.FgYellow + "lllllll\n" + color.FgBlue + "lllllll " + color.FgYellow + "lllllll\n" + color.FgBlue + "lllllll " + color.FgYellow + "lllllll\n" - DefaultShell = "cmd.exe" - fetchInfoCount = 5 + fetchInfoCount = 8 ) -var hr string - func fetchInfo(i int) (string, bool) { switch i { case 0: - username := "unknown" - hostname := "unknown" - if u, err := user.Current(); err == nil { - username = u.Username - } - if h, err := os.Hostname(); err == nil { - hostname = h - } - x := color.FgMagenta + username + color.Reset + "@" + color.FgMagenta + hostname + color.Reset - hr = strings.Repeat("-", len(username)+len(hostname)+1) - return x, true + return color.FgBlue + username + color.Reset + "@" + color.FgBlue + hostname + color.Reset, true case 1: return color.Reset + hr, true case 2: - return color.FgYellow + "OS" + color.Reset + ": " + Name, true + return color.FgYellow + "OS" + color.Reset + ": Windows", true case 3: if info, ok := debug.ReadBuildInfo(); ok { return color.FgYellow + "Interactive Shell" + color.Reset + ": smash " + info.Main.Version, true @@ -50,7 +35,28 @@ func fetchInfo(i int) (string, bool) { return color.FgYellow + "Interactive Shell" + color.Reset + ": smash", true } case 4: - return color.FgYellow + "Default Shell" + color.Reset + ": " + DefaultShell, true + return color.FgYellow + "Default Shell" + color.Reset + ": cmd.exe", true + case 5: + return color.FgYellow + "Desktop Environment" + color.Reset + ": " + "Windows Desktop Environment", true + case 6: + return color.FgYellow + "Window Manager" + color.Reset + ": " + "Desktop Window Manager", true + case 7: + sb := strings.Builder{} + sb.WriteString(color.FgYellow) + sb.WriteString("Terminal") + sb.WriteString(color.Reset) + sb.WriteString(": ") + tOk := false + if termProgram, ok := os.LookupEnv("TERM_PROGRAM"); ok { + sb.WriteString(termProgram) + sb.WriteString(" ") + tOk = true + } + if term, ok := os.LookupEnv("TERM"); ok { + sb.WriteString(term) + tOk = true + } + return sb.String(), tOk default: return "", false }