1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-11-03 04:27:16 -05:00
neonmodem/ui/ui.go

129 lines
2.6 KiB
Go
Raw Normal View History

2022-12-28 22:22:36 -05:00
package ui
import (
// "fmt"
"strings"
"github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/navigation"
2022-12-28 22:27:46 -05:00
"github.com/mrusme/gobbs/ui/views/posts"
2022-12-28 22:22:36 -05:00
"github.com/mrusme/gobbs/ui/views"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
type KeyMap struct {
FirstTab key.Binding
SecondTab key.Binding
ThirdTab key.Binding
FourthTab key.Binding
FifthTab key.Binding
SixthTab key.Binding
SeventhTab key.Binding
EightTab key.Binding
NinthTab key.Binding
TenthTab key.Binding
EleventhTab key.Binding
TwelfthTab key.Binding
ThirteenthTab key.Binding
PrevTab key.Binding
NextTab key.Binding
Up key.Binding
Down key.Binding
Quit key.Binding
}
var DefaultKeyMap = KeyMap{
Up: key.NewBinding(
key.WithKeys("k", "up"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("j", "down"),
key.WithHelp("↓/j", "move down"),
),
Quit: key.NewBinding(
key.WithKeys("q", "ctrl+q"),
key.WithHelp("q/Q", "quit"),
),
}
type Model struct {
keymap KeyMap
nav navigation.Model
views []views.View
ctx *ctx.Ctx
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
keymap: DefaultKeyMap,
ctx: c,
}
m.nav = navigation.NewModel(m.ctx)
2022-12-28 22:27:46 -05:00
for _, capability := range (*m.ctx.Systems[0]).GetCapabilities() { // TODO
switch capability.ID {
case "posts":
m.views = append(m.views, posts.NewModel(m.ctx))
// case "groups":
// m.views = append(m.views, groups.NewModel(m.ctx))
// case "search":
// m.views = append(m.views, search.NewModel(m.ctx))
}
}
2022-12-28 22:22:36 -05:00
return m
}
func (m Model) Init() tea.Cmd {
return tea.Batch(tea.EnterAltScreen)
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := make([]tea.Cmd, 0)
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.Quit):
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.setSizes(msg.Width, msg.Height)
for i := range m.views {
v, cmd := m.views[i].Update(msg)
m.views[i] = v
cmds = append(cmds, cmd)
}
}
v, cmd := m.views[m.nav.CurrentId].Update(msg)
m.views[m.nav.CurrentId] = v
cmds = append(cmds, cmd)
nav, cmd := m.nav.Update(msg)
m.nav = nav
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
s := strings.Builder{}
s.WriteString(m.nav.View() + "\n\n")
s.WriteString(m.views[m.nav.CurrentId].View())
return s.String()
}
func (m Model) setSizes(winWidth int, winHeight int) {
(*m.ctx).Screen[0] = winWidth
(*m.ctx).Screen[1] = winHeight
m.ctx.Content[0] = m.ctx.Screen[0]
m.ctx.Content[1] = m.ctx.Screen[1] - 5
}