Added basic TUI
Signed-off-by: Louis Hollingworth <louis@hollingworth.ch>
This commit is contained in:
parent
89094f2318
commit
ee4d7b9db5
3 changed files with 155 additions and 0 deletions
85
rt.go
Normal file
85
rt.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type model struct {
|
||||
choices []string
|
||||
cursor int
|
||||
selected map[int]struct{}
|
||||
}
|
||||
|
||||
func initialModel() model {
|
||||
return model{
|
||||
choices: []string{"Init Git", "Add License", "Add .gitignore", "Add Code of Conduct", "Add Contributing doc", "Create ReadMe", "Commit & Sign", "Add Origin & Push"},
|
||||
selected: make(map[int]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
switch msg.String() {
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
case "up", "k":
|
||||
if m.cursor > 0 {
|
||||
m.cursor--
|
||||
}
|
||||
case "down", "j":
|
||||
if m.cursor < len(m.choices)-1 {
|
||||
m.cursor++
|
||||
}
|
||||
case "enter", " ":
|
||||
_, ok := m.selected[m.cursor]
|
||||
if ok {
|
||||
delete(m.selected, m.cursor)
|
||||
} else {
|
||||
m.selected[m.cursor] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
s := "What functions do you want to complete?\n\n"
|
||||
for i, choice := range m.choices {
|
||||
|
||||
// Is the cursor pointing at this choice?
|
||||
cursor := " " // no cursor
|
||||
if m.cursor == i {
|
||||
cursor = ">" // cursor!
|
||||
}
|
||||
|
||||
// Is this choice selected?
|
||||
checked := " " // not selected
|
||||
if _, ok := m.selected[i]; ok {
|
||||
checked = "x" // selected!
|
||||
}
|
||||
|
||||
// Render the row
|
||||
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
|
||||
}
|
||||
|
||||
s += "\nPress q to quit.\n"
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(initialModel())
|
||||
|
||||
if _, err := p.Run(); err != nil {
|
||||
log.Errorf("There has been an error", "error", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue