Skip to content

Commit

Permalink
support multiple resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
xbt573 committed Dec 7, 2022
1 parent b582ec3 commit cd9b065
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
24 changes: 17 additions & 7 deletions info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,24 @@ func GetInfoString(options map[string]string) string {
lines++

case "resolution":
resolution := getRawScreenResolution()
resolutions := getRawScreenResolutions()

output += formatAndColor(
"\x1b[%vG${caccent}Resolution${creset}: %v\n",
offset,
resolution,
)
lines++
for _, res := range resolutions {
output += formatAndColor(
"\x1b[%vG${caccent}Resolution${creset}: %v\n",
offset,
res,
)
lines++
}

if len(resolutions) == 0 {
output += formatAndColor(
"\x1b[%vG${caccent}Resolution${creset}: n/a\n",
offset,
)
lines++
}

case "cpu":
cpu := getRawCpu()
Expand Down
8 changes: 4 additions & 4 deletions info/raw_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ func getRawGpus() []string {
}

// Returns main screen resolution
func getRawScreenResolution() string {
func getRawScreenResolutions() []string {
out, err := exec.Command("system_profiler", "SPDisplaysDataType").Output()
if err != nil {
return "n/a"
return []string{}
}

contents := string(out)
match := extractResolutionRegex.FindStringSubmatch(contents)
if len(match) == 0 {
return "n/a"
return []string{}
}

return match[1]
return []string{match[1]}
}

// Returns OS pretty name
Expand Down
23 changes: 19 additions & 4 deletions info/raw_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var (
getGpuModelRegex = regexp.MustCompile(`.*\[(.*)\]`)
)

// Regex used to extract resolutions from "xrandr --nograb --current"
var getResolutionRegex = regexp.MustCompile(`connected(?: primary)? (\d+)x(\d+)`)

// Regex used to remove too much spaces between words
var removeExtraSpacesRegex = regexp.MustCompile(`\s+`)

Expand Down Expand Up @@ -216,13 +219,25 @@ func getRawGpus() []string {
}

// Returns main screen resolution
func getRawScreenResolution() string {
raw, err := os.ReadFile("/sys/class/graphics/fb0/virtual_size")
func getRawScreenResolutions() []string {
out, err := exec.Command("xrandr", "--nograb", "--current").Output()
if err != nil {
return "n/a"
return []string{}
}

contents := string(out)
match := getResolutionRegex.FindAllStringSubmatch(contents, -1)
if len(match) == 0 {
return []string{}
}

resolutions := []string{}

for _, resMatch := range match {
resolutions = append(resolutions, fmt.Sprintf("%vx%v", resMatch[1], resMatch[2]))
}

return strings.Replace(string(raw), ",", "x", -1)
return resolutions
}

// Returns OS pretty name
Expand Down

0 comments on commit cd9b065

Please sign in to comment.