amd_pstate.go RawHistoryFolder
1package models
2
3import legacysystem "github.com/bnema/archup/internal/system"
4
5// AMDPStateOption represents a selectable AMD P-State mode.
6type AMDPStateOption struct {
7 Mode legacysystem.AMDPStateMode
8 Label string
9 Recommended bool
10}
11
12// AMDPStateModelImpl holds AMD P-State selection state.
13type AMDPStateModelImpl struct {
14 cpuInfo *legacysystem.CPUInfo
15 options []AMDPStateOption
16 selected int
17}
18
19// NewAMDPStateModel creates a new AMD P-State selection model.
20func NewAMDPStateModel() *AMDPStateModelImpl {
21 return &AMDPStateModelImpl{
22 options: []AMDPStateOption{},
23 selected: 0,
24 }
25}
26
27// SetCPUInfo sets CPU info and rebuilds options.
28func (am *AMDPStateModelImpl) SetCPUInfo(info *legacysystem.CPUInfo) {
29 am.cpuInfo = info
30 am.options = []AMDPStateOption{}
31 am.selected = 0
32
33 if info == nil {
34 return
35 }
36
37 for _, mode := range info.AMDPStateModes {
38 desc := legacysystem.GetPStateModeDescription(mode)
39 label := string(mode)
40 if desc != "" {
41 label = label + " - " + desc
42 }
43 option := AMDPStateOption{
44 Mode: mode,
45 Label: label,
46 Recommended: mode == info.RecommendedPStateMode,
47 }
48 am.options = append(am.options, option)
49 }
50
51 for i, option := range am.options {
52 if option.Recommended {
53 am.selected = i
54 break
55 }
56 }
57}
58
59// CPUInfo returns detected CPU info.
60func (am *AMDPStateModelImpl) CPUInfo() *legacysystem.CPUInfo {
61 return am.cpuInfo
62}
63
64// Options returns the selectable options.
65func (am *AMDPStateModelImpl) Options() []AMDPStateOption {
66 return am.options
67}
68
69// SelectedIndex returns the current selection index.
70func (am *AMDPStateModelImpl) SelectedIndex() int {
71 return am.selected
72}
73
74// SelectedOption returns the currently selected option.
75func (am *AMDPStateModelImpl) SelectedOption() AMDPStateOption {
76 if len(am.options) == 0 {
77 return AMDPStateOption{}
78 }
79 if am.selected < 0 || am.selected >= len(am.options) {
80 return am.options[0]
81 }
82 return am.options[am.selected]
83}
84
85// MoveUp moves selection up.
86func (am *AMDPStateModelImpl) MoveUp() {
87 if len(am.options) == 0 {
88 return
89 }
90 if am.selected == 0 {
91 am.selected = len(am.options) - 1
92 return
93 }
94 am.selected--
95}
96
97// MoveDown moves selection down.
98func (am *AMDPStateModelImpl) MoveDown() {
99 if len(am.options) == 0 {
100 return
101 }
102 am.selected = (am.selected + 1) % len(am.options)
103}
104
105// ShouldPrompt returns true if AMD P-State selection should be shown.
106func (am *AMDPStateModelImpl) ShouldPrompt() bool {
107 if am.cpuInfo == nil {
108 return false
109 }
110 if am.cpuInfo.Vendor != legacysystem.CPUVendorAMD {
111 return false
112 }
113 return len(am.options) > 0
114}