-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhelp.go
More file actions
136 lines (112 loc) · 3.9 KB
/
help.go
File metadata and controls
136 lines (112 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package components
import (
"bbrew/internal/ui/theme"
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// HelpScreen displays a modal overlay with all keyboard shortcuts
type HelpScreen struct {
pages *tview.Pages
theme *theme.Theme
isBrewfile bool
}
// NewHelpScreen creates a new help screen component
func NewHelpScreen(theme *theme.Theme) *HelpScreen {
return &HelpScreen{
theme: theme,
}
}
// View returns the help screen pages (for overlay functionality)
func (h *HelpScreen) View() *tview.Pages {
return h.pages
}
// SetBrewfileMode sets whether Brewfile-specific commands should be shown
func (h *HelpScreen) SetBrewfileMode(enabled bool) {
h.isBrewfile = enabled
}
// Build creates the help screen as an overlay on top of the main content
func (h *HelpScreen) Build(mainContent tview.Primitive) *tview.Pages {
content := h.buildHelpContent()
textView := tview.NewTextView().
SetDynamicColors(true).
SetText(content).
SetTextAlign(tview.AlignLeft)
textView.SetBackgroundColor(h.theme.ModalBgColor)
textView.SetTextColor(h.theme.DefaultTextColor)
// Create a frame around the text
frame := tview.NewFrame(textView).
SetBorders(1, 1, 1, 1, 2, 2)
frame.SetBackgroundColor(h.theme.ModalBgColor)
frame.SetBorderColor(h.theme.BorderColor)
frame.SetBorder(true).
SetTitle(" Help ").
SetTitleAlign(tview.AlignCenter)
// Calculate box dimensions
boxHeight := 22
boxWidth := 55
if h.isBrewfile {
boxHeight = 26 // Extra space for Brewfile section
}
// Center the frame in a flex layout
centered := tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(frame, boxHeight, 0, true).
AddItem(nil, 0, 1, false),
boxWidth, 0, true).
AddItem(nil, 0, 1, false)
// Create pages with main content as background and help as overlay
h.pages = tview.NewPages().
AddPage("main", mainContent, true, true).
AddPage("help", centered, true, true)
return h.pages
}
// buildHelpContent generates the formatted help text
func (h *HelpScreen) buildHelpContent() string {
var sb strings.Builder
// Navigation section
sb.WriteString(h.formatSection("NAVIGATION"))
sb.WriteString(h.formatKey("↑/↓, j/k", "Navigate list"))
sb.WriteString(h.formatKey("/", "Focus search"))
sb.WriteString(h.formatKey("Esc", "Back to table"))
sb.WriteString(h.formatKey("q", "Quit"))
sb.WriteString("\n")
// Filters section
sb.WriteString(h.formatSection("FILTERS"))
sb.WriteString(h.formatKey("f", "Toggle installed"))
sb.WriteString(h.formatKey("o", "Toggle outdated"))
sb.WriteString(h.formatKey("l", "Toggle leaves"))
sb.WriteString(h.formatKey("c", "Toggle casks"))
sb.WriteString("\n")
// Actions section
sb.WriteString(h.formatSection("ACTIONS"))
sb.WriteString(h.formatKey("i", "Install selected"))
sb.WriteString(h.formatKey("u", "Update selected"))
sb.WriteString(h.formatKey("r", "Remove selected"))
sb.WriteString(h.formatKey("Ctrl+U", "Update all"))
// Brewfile section (only if in Brewfile mode)
if h.isBrewfile {
sb.WriteString("\n")
sb.WriteString(h.formatSection("BREWFILE"))
sb.WriteString(h.formatKey("Ctrl+A", "Install all"))
sb.WriteString(h.formatKey("Ctrl+R", "Remove all"))
}
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("[%s]Press any key to close[-]", h.getColorTag(h.theme.LegendColor)))
return sb.String()
}
// formatSection formats a section header
func (h *HelpScreen) formatSection(title string) string {
return fmt.Sprintf("[%s::b]%s[-:-:-]\n", h.getColorTag(h.theme.SuccessColor), title)
}
// formatKey formats a key-description pair
func (h *HelpScreen) formatKey(key, description string) string {
return fmt.Sprintf(" [%s]%-12s[-] %s\n", h.getColorTag(h.theme.WarningColor), key, description)
}
// getColorTag converts a tcell.Color to a tview color tag
func (h *HelpScreen) getColorTag(color tcell.Color) string {
return fmt.Sprintf("#%06x", color.Hex())
}