From e400e338a038403dae1b6d05761819f2a75ecd91 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 21 Dec 2025 17:16:05 +0200 Subject: [PATCH] Rename and improve cpus module --- config/config.yaml | 2 +- src/hardware.go | 40 +++++++++++++++++++++++++-------------- src/modules.go | 47 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 62 insertions(+), 27 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index fbfe842..cc950b5 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -7,7 +7,7 @@ modules: - name: shell - name: init - name: motherboard - - name: cpu + - name: cpus - name: gpus - name: memory - name: partitions diff --git a/src/hardware.go b/src/hardware.go index bd88ffe..d18da66 100644 --- a/src/hardware.go +++ b/src/hardware.go @@ -10,29 +10,41 @@ import ( "github.com/jackmordaunt/ghw" ) +type CPU struct { + Vendor string + Model string + Cores int + Threads int +} + type Monitor struct { Width int Height int RefreshRate int } -func GetCPUModel() string { - cpu, err := ghw.CPU() - if err != nil { - return "" - } - if len(cpu.Processors) == 0 { - return "" - } - return cpu.Processors[0].Model -} +func GetCPUs(hiddenCPUs []int) []CPU { + ret := make([]CPU, 0) -func GetCPUThreads() int { - cpu, err := ghw.CPU() + cpus, err := ghw.CPU() if err != nil { - return 0 + return ret } - return int(cpu.TotalThreads) + + for i, cpu := range cpus.Processors { + if slices.Contains(hiddenCPUs, i+1) { + continue + } + + ret = append(ret, CPU{ + Vendor: cpu.Vendor, + Model: cpu.Model, + Cores: int(cpu.NumCores), + Threads: int(cpu.NumThreads), + }) + } + + return ret } func GetGPUModels(hiddenGPUS []int) (ret []string) { diff --git a/src/modules.go b/src/modules.go index 792f6f9..5883481 100644 --- a/src/modules.go +++ b/src/modules.go @@ -171,19 +171,42 @@ func initializeModuleMap() { RegisterModule(MotherboardModule) // Motherboard module - cpuModule := StormfetchModule{stormfetchModuleConfig: stormfetchModuleConfig{Name: "cpu", Format: "%3CPU: %4$CPU_MODEL ($CPU_THREADS threads)"}, Execute: func(sm StormfetchModule) string { - return os.Expand(sm.Format, func(s string) string { - switch s { - case "CPU_MODEL": - return GetCPUModel() - case "CPU_THREADS": - return strconv.Itoa(GetCPUThreads()) - default: - return "" - } - }) + cpusModule := StormfetchModule{stormfetchModuleConfig: stormfetchModuleConfig{Name: "cpus", Format: "%3CPU: %4$CPU_MODEL ($CPU_THREADS threads)"}, Execute: func(sm StormfetchModule) string { + hiddenCPUsInterface, _ := sm.GetData("hidden_cpus", make([]any, 0)) + + // Convert interface slices to string slices + hiddenCPUs := make([]int, 0) + for _, value := range hiddenCPUsInterface.([]any) { + hiddenCPUs = append(hiddenCPUs, value.(int)) + } + + builder := strings.Builder{} + cpus := GetCPUs(hiddenCPUs) + + for i, cpu := range cpus { + expanded := os.Expand(sm.Format, func(s string) string { + switch s { + case "CPU_NUM": + return strconv.Itoa(i + 1) + case "CPU_VENDOR": + return cpu.Vendor + case "CPU_MODEL": + return cpu.Model + case "CPU_CORES": + return strconv.Itoa(cpu.Cores) + case "CPU_THREADS": + return strconv.Itoa(cpu.Threads) + default: + return "" + } + }) + + builder.WriteString(expanded + "\n") + } + + return builder.String() }} - RegisterModule(cpuModule) + RegisterModule(cpusModule) // GPUs module gpusModule := StormfetchModule{stormfetchModuleConfig: stormfetchModuleConfig{Name: "gpus", Format: "%3GPU: %4$GPU_MODEL"}, Execute: func(sm StormfetchModule) string {