fix for gpu parsing
This commit is contained in:
parent
083db53aa4
commit
5add247d4a
49
main.go
49
main.go
@ -169,39 +169,60 @@ func getCpu() string {
|
||||
}
|
||||
|
||||
func getGpu() string {
|
||||
out, err := exec.Command("sh", "-c", "lspci | grep -E 'VGA|3D'").Output()
|
||||
out, err := exec.Command("sh", "-c", "lspci -mm | grep -E 'VGA|3D'").Output()
|
||||
if err != nil || len(out) == 0 {
|
||||
return "Unknown or no GPU"
|
||||
return "Unknown GPU"
|
||||
}
|
||||
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
var gpus []string
|
||||
|
||||
for _, line := range lines {
|
||||
parts := strings.Split(line, ": ")
|
||||
if len(parts) > 1 {
|
||||
cleaned := cleanGpuString(parts[len(parts)-1])
|
||||
if cleaned != "" {
|
||||
re := regexp.MustCompile(`"([^"]+)"`)
|
||||
matches := re.FindAllStringSubmatch(line, -1)
|
||||
|
||||
if len(matches) >= 3 {
|
||||
vendor := matches[1][1]
|
||||
device := matches[2][1]
|
||||
|
||||
cleaned := cleanGpuString(vendor + " " + device)
|
||||
gpus = append(gpus, cleaned)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(gpus) > 0 {
|
||||
return strings.Join(gpus, " / ")
|
||||
}
|
||||
|
||||
return "GPU not found"
|
||||
}
|
||||
|
||||
func cleanGpuString(raw string) string {
|
||||
re := regexp.MustCompile(`\[.*?]|\(.*?\)`)
|
||||
res := re.ReplaceAllString(raw, "")
|
||||
prettyNameRe := regexp.MustCompile(`\[(.*?)]`)
|
||||
match := prettyNameRe.FindStringSubmatch(raw)
|
||||
|
||||
res = strings.ReplaceAll(res, "Corporation", "")
|
||||
res = strings.ReplaceAll(res, "Controller", "")
|
||||
res = strings.ReplaceAll(res, "VGA compatible", "")
|
||||
res = strings.ReplaceAll(res, "3D", "")
|
||||
res := raw
|
||||
if len(match) > 1 {
|
||||
vendor := ""
|
||||
if strings.Contains(strings.ToLower(raw), "nvidia") {
|
||||
vendor = "NVIDIA "
|
||||
}
|
||||
if strings.Contains(strings.ToLower(raw), "intel") {
|
||||
vendor = "Intel "
|
||||
}
|
||||
if strings.Contains(strings.ToLower(raw), "amd") {
|
||||
vendor = "AMD "
|
||||
}
|
||||
|
||||
res = vendor + match[1]
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`\(.*?\)|\[.*?]`)
|
||||
res = re.ReplaceAllString(res, "")
|
||||
|
||||
fluff := []string{"Corporation", "Controller", "VGA compatible", "3D", "Graphics", "Device"}
|
||||
for _, word := range fluff {
|
||||
res = strings.ReplaceAll(res, word, "")
|
||||
}
|
||||
|
||||
return strings.Join(strings.Fields(res), " ")
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user