/
main.go
186 lines (165 loc) · 3.8 KB
/
main.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"golang.org/x/term"
)
// Color defaults
const (
KeyColor = "#A0D8EE"
SepColor = "#888888"
DescColor = "#A0D8EE"
HeaderColor = "#A0D8EE"
)
// Number of columns
// TODO: Make this configurable
const ncols = 3
type command struct {
Desc string
Key string
Command string
}
type menu struct {
Desc string
Key string
Action []command
Menu []menu
}
type config struct {
KeyColor string
HeaderColor string
SepColor string
DescColor string
Menu []menu
Action []command
}
type model struct {
Config config
CurrentMenu *menu
err error
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
key := msg.String()
if key == "ctrl+c" {
return m, tea.Quit
}
cMenu := *m.CurrentMenu
for _, c := range cMenu.Menu {
if c.Key == key {
m.CurrentMenu = &c
return m, nil
}
}
for _, c := range cMenu.Action {
if c.Key == key {
fmt.Fprintln(os.Stdout, c.Command)
return m, tea.Quit
}
}
}
return m, nil
}
// Rendering helper
func GetKeyString(key, desc string, keyStyle, desStyle, parStyle lipgloss.Style) string {
return strings.Join([]string{
parStyle.Render(" ["),
keyStyle.Render(key),
parStyle.Render("]"),
desStyle.Render(desc),
}, "")
}
func (m model) View() string {
if m.err != nil {
return "Error: " + m.err.Error() + "\n"
}
// Style definitions
width, _, _ := term.GetSize(int(os.Stderr.Fd()))
columnWidth := (width / 3) - 3
headerStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color(m.Config.HeaderColor))
keyStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color(m.Config.KeyColor))
desStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(m.Config.DescColor)).
PaddingLeft(2)
parStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(m.Config.SepColor))
padStyle := lipgloss.NewStyle().
Width(columnWidth).
Align(lipgloss.Left)
c := *m.CurrentMenu
i := 0
var modeData = make([]string, ncols)
var row string
for _, k := range c.Menu {
row = GetKeyString(k.Key, k.Desc, keyStyle, desStyle, parStyle)
modeData[i%ncols] += padStyle.Render(row) + "\n"
i++
}
for _, k := range c.Action {
row = GetKeyString(k.Key, k.Desc, keyStyle, desStyle, parStyle)
modeData[i%ncols] += padStyle.Render(row) + "\n"
i++
}
return headerStyle.Render(c.Desc) + "\n" + lipgloss.JoinHorizontal(
lipgloss.Top,
modeData...,
)
}
func main() {
configDir, err := os.UserConfigDir()
if err != nil {
os.Exit(1)
}
// TODO: Define a proper fallback mechanism
locations := []string{
os.ExpandEnv("$HOME/ghydra/config.toml"),
configDir + "/ghydra/config.toml",
}
var configLocation string
for _, configLocation = range locations {
if _, err := os.Stat(configLocation); err == nil {
break
}
}
if configLocation == "" {
panic("Error: no configuration file in $XDG_CONFIG_HOME/ghydra/config.toml or $HOME/ghydra/config.toml")
}
// Set default colors
var cfg config
cfg.KeyColor = KeyColor
cfg.HeaderColor = HeaderColor
cfg.SepColor = SepColor
cfg.DescColor = DescColor
if _, err := toml.DecodeFile(configLocation, &cfg); err != nil {
os.Exit(1)
}
Menu := menu{
Desc: "Main:",
Key: "",
Action: cfg.Action,
Menu: cfg.Menu,
}
// Model initialization --> config and current menu setup
m := model{}
m.Config = cfg
m.CurrentMenu = &Menu
lipgloss.SetColorProfile(termenv.TrueColor)
program := tea.NewProgram(m, tea.WithOutput(os.Stderr), tea.WithAltScreen())
if _, err := program.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}