Skip to content

Commit

Permalink
feat: more info at smashfetch
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Sep 29, 2024
1 parent 7dfb874 commit 1b5a6aa
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 52 deletions.
17 changes: 16 additions & 1 deletion internal/system/system.go
Original file line number Diff line number Diff line change
@@ -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, ""))
}
Expand Down
65 changes: 48 additions & 17 deletions internal/system/system_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package system

import (
"os"
"os/user"
"os/exec"
"path/filepath"
"runtime/debug"
"smash/internal/color"
Expand All @@ -30,7 +30,7 @@ const (
color.FgBlue + ` kMMMMMMMMMMMMMMMMMMMMMMd` + "\n" +
color.FgBlue + ` ;KMMMMMMMWXXWMMMMMMMk.` + "\n" +
color.FgBlue + ` "cooc*" "*coo'"` + "\n"
fetchInfoCount = 7
fetchInfoCount = 9
)

var (
Expand All @@ -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:
Expand All @@ -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
}
34 changes: 19 additions & 15 deletions internal/system/system_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package system
import (
"bufio"
"os"
"os/user"
"path/filepath"
"runtime/debug"
"smash/internal/color"
Expand All @@ -21,7 +20,7 @@ var (
)

const (
fetchInfoCount = 5
fetchInfoCount = 6
)

const (
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
Expand Down
44 changes: 25 additions & 19 deletions internal/system/system_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,59 @@ 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" +
"\n" +
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
} else {
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
}
Expand Down

0 comments on commit 1b5a6aa

Please sign in to comment.