Skip to content

Commit

Permalink
fix: smashfetch info alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Sep 29, 2024
1 parent 37e6c49 commit 2e49e95
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions internal/system/system.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package system

import (
"os"
"os/user"
"regexp"
"runtime/debug"
"smash/internal/color"
"strings"
)

const (
fetchInfoCount = 3
fetchInfoCount = 5
)

var (
ansiRe = regexp.MustCompile(`\x1b\[[0-9]*m`)
)

func visLen(s string) int {
return len(ansiRe.ReplaceAllString(s, ""))
}

func SmashFetch() string {
lines := strings.Split(Ascii, "\n")
longest := 0
for _, line := range lines {
if len(line) > longest {
longest = len(line)
l := visLen(line)
if l > longest {
longest = l
}
}
var sb strings.Builder
Expand All @@ -25,21 +37,18 @@ func SmashFetch() string {
sb.WriteString("\n")
}
sb.WriteString(line)
for j := len(line); j < longest; j++ {
sb.WriteString(" ")
}
r := longest - visLen(line) + 1
sb.WriteString(strings.Repeat(" ", r))
if info, ok := fetchInfo(i); ok {
sb.WriteString(info)
i++
}
}
for i < fetchInfoCount {
if info, ok := fetchInfo(i); ok {
for j := 0; j <= longest; j++ {
sb.WriteString(" ")
}
sb.WriteString(info)
sb.WriteString("\n")
sb.WriteString(strings.Repeat(" ", len(hr)))
sb.WriteString(info)
} else {
break
}
Expand All @@ -48,17 +57,33 @@ func SmashFetch() string {
return sb.String()
}

var hr string

func fetchInfo(i int) (string, bool) {
switch i {
case 0:
return color.FgYellow + "OS" + color.Reset + ": " + Name, true
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
case 1:
return color.Reset + hr, true
case 2:
return color.FgYellow + "OS" + color.Reset + ": " + Name, 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 2:
case 4:
return color.FgYellow + "Default Shell" + color.Reset + ": " + DefaultShell, true
default:
return "", false
Expand Down

0 comments on commit 2e49e95

Please sign in to comment.