settings_dialog.go RawHistoryFolder
1package modes
2
3import (
4 "github.com/patriceckhart/zot/internal/tui"
5)
6
7type settingsDialog struct {
8 active bool
9 items []settingsItem
10 cursor int
11}
12
13type settingsItem struct {
14 key string
15 label string
16 desc string
17 value bool
18 disabled bool
19 hint string
20}
21
22type settingsAction struct {
23 Toggle bool
24 Key string
25 Value bool
26 Close bool
27}
28
29func newSettingsDialog() *settingsDialog { return &settingsDialog{} }
30
31func (d *settingsDialog) Open(items []settingsItem) bool {
32 if len(items) == 0 {
33 return false
34 }
35 d.items = items
36 d.cursor = 0
37 d.active = true
38 return true
39}
40
41func (d *settingsDialog) Close() { d.active = false }
42func (d *settingsDialog) Active() bool { return d != nil && d.active }
43
44func (d *settingsDialog) HandleKey(k tui.Key) settingsAction {
45 switch k.Kind {
46 case tui.KeyUp:
47 if d.cursor > 0 {
48 d.cursor--
49 }
50 case tui.KeyDown:
51 if d.cursor < len(d.items)-1 {
52 d.cursor++
53 }
54 case tui.KeyEsc:
55 d.Close()
56 return settingsAction{Close: true}
57 case tui.KeyEnter:
58 return d.toggleCurrent()
59 case tui.KeyRune:
60 if k.Rune == ' ' {
61 return d.toggleCurrent()
62 }
63 }
64 return settingsAction{}
65}
66
67func (d *settingsDialog) toggleCurrent() settingsAction {
68 if len(d.items) == 0 {
69 d.Close()
70 return settingsAction{Close: true}
71 }
72 it := d.items[d.cursor]
73 if it.disabled {
74 return settingsAction{}
75 }
76 it.value = !it.value
77 d.items[d.cursor] = it
78 return settingsAction{Toggle: true, Key: it.key, Value: it.value}
79}
80
81func (d *settingsDialog) Render(th tui.Theme, width int) []string {
82 if !d.Active() {
83 return nil
84 }
85 var lines []string
86 lines = append(lines, frameHeader(th, "settings", width))
87 lines = append(lines, th.FG256(th.Muted, "toggle with enter/space, esc to close:"))
88 for i, it := range d.items {
89 box := "[ ]"
90 if it.value {
91 box = "[x]"
92 }
93 plain := " " + box + " " + it.label
94 if it.hint != "" {
95 plain += " " + th.FG256(th.Muted, "("+it.hint+")")
96 }
97 if it.disabled {
98 lines = append(lines, th.FG256(th.Muted, plain))
99 } else if i == d.cursor {
100 lines = append(lines, th.PadHighlight(plain, width))
101 } else {
102 lines = append(lines, plain)
103 }
104 if it.desc != "" {
105 lines = append(lines, th.FG256(th.Muted, " "+it.desc))
106 }
107 }
108 lines = append(lines, frameRule(th, width))
109 return lines
110}