disk.go RawHistoryFolder
1package models
2
3import legacysystem "github.com/bnema/archup/internal/system"
4
5// DiskOption represents a selectable disk option.
6type DiskOption struct {
7 Label string
8 Path string
9 Size string
10 Model string
11}
12
13// DiskModelImpl holds disk selection state.
14type DiskModelImpl struct {
15 disks []legacysystem.Disk
16 options []DiskOption
17 selected int
18 err error
19}
20
21// NewDiskModel creates a new disk selection model.
22func NewDiskModel() *DiskModelImpl {
23 return &DiskModelImpl{
24 options: []DiskOption{},
25 selected: 0,
26 }
27}
28
29// SetDisks populates the disk options from detected disks.
30func (dm *DiskModelImpl) SetDisks(disks []legacysystem.Disk) {
31 dm.disks = disks
32 dm.options = make([]DiskOption, 0, len(disks))
33
34 for _, disk := range disks {
35 label := disk.Path + " (" + disk.Size + ")"
36 if disk.Model != "" {
37 label += " " + disk.Model
38 }
39 if disk.Serial != "" {
40 label += " [" + disk.Serial + "]"
41 }
42 if disk.Vendor != "" && disk.Model == "" {
43 label += " " + disk.Vendor
44 }
45
46 dm.options = append(dm.options, DiskOption{
47 Label: label,
48 Path: disk.Path,
49 Size: disk.Size,
50 Model: disk.Model,
51 })
52 }
53}
54
55// SetError sets an error that occurred during disk detection.
56func (dm *DiskModelImpl) SetError(err error) {
57 dm.err = err
58}
59
60// Error returns any error from disk detection.
61func (dm *DiskModelImpl) Error() error {
62 return dm.err
63}
64
65// Options returns the selectable disk options.
66func (dm *DiskModelImpl) Options() []DiskOption {
67 return dm.options
68}
69
70// SelectedIndex returns the current selection index.
71func (dm *DiskModelImpl) SelectedIndex() int {
72 return dm.selected
73}
74
75// SelectedOption returns the currently selected disk option.
76func (dm *DiskModelImpl) SelectedOption() DiskOption {
77 if len(dm.options) == 0 {
78 return DiskOption{Label: "No disks found", Path: ""}
79 }
80 if dm.selected < 0 || dm.selected >= len(dm.options) {
81 return dm.options[0]
82 }
83 return dm.options[dm.selected]
84}
85
86// MoveUp moves selection up.
87func (dm *DiskModelImpl) MoveUp() {
88 if len(dm.options) == 0 {
89 return
90 }
91 if dm.selected == 0 {
92 dm.selected = len(dm.options) - 1
93 return
94 }
95 dm.selected--
96}
97
98// MoveDown moves selection down.
99func (dm *DiskModelImpl) MoveDown() {
100 if len(dm.options) == 0 {
101 return
102 }
103 dm.selected = (dm.selected + 1) % len(dm.options)
104}
105
106// HasDisks returns true if any disks were detected.
107func (dm *DiskModelImpl) HasDisks() bool {
108 return len(dm.options) > 0
109}