1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-06-16 06:25:23 +00:00

Refactored UI head, navigation -> header

This commit is contained in:
マリウス 2022-12-31 19:03:53 -05:00
parent 2ab0c4be67
commit 54acfe873c
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
3 changed files with 87 additions and 183 deletions

68
ui/header/header.go Normal file
View File

@ -0,0 +1,68 @@
package header
import (
"github.com/mrusme/gobbs/ui/ctx"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
highlight = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}
)
type Model struct {
ctx *ctx.Ctx
spinner spinner.Model
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
ctx: c,
}
m.spinner = spinner.New()
m.spinner.Spinner = spinner.Dot
m.spinner.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return m
}
func (m Model) Init() tea.Cmd {
return m.spinner.Tick
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmds []tea.Cmd
if m.ctx.Loading == true {
cmds = append(cmds, m.spinner.Tick)
}
switch msg := msg.(type) {
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
var items []string
row := lipgloss.JoinHorizontal(
lipgloss.Top,
items...,
)
if m.ctx.Loading == false {
row = lipgloss.JoinHorizontal(lipgloss.Bottom, row, " THING HERE ")
} else {
row = lipgloss.JoinHorizontal(lipgloss.Bottom, row, " THING HERE ", " ", m.spinner.View())
}
return lipgloss.JoinHorizontal(lipgloss.Top, row, "\n\n")
}

View File

@ -1,151 +0,0 @@
package navigation
import (
"strings"
"github.com/mrusme/gobbs/ui/ctx"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
highlight = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}
activeTabBorder = lipgloss.Border{
Top: "─",
Bottom: " ",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "┘",
BottomRight: "└",
}
tabBorder = lipgloss.Border{
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "┴",
BottomRight: "┴",
}
tab = lipgloss.NewStyle().
Border(tabBorder, true).
BorderForeground(highlight).
Padding(0, 1)
activeTab = tab.Copy().Border(activeTabBorder, true)
tabGap = tab.Copy().
BorderTop(false).
BorderLeft(false).
BorderRight(false)
)
var Navigation = []string{}
type Model struct {
CurrentId int
ctx *ctx.Ctx
spinner spinner.Model
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
CurrentId: 0,
ctx: c,
}
m.spinner = spinner.New()
m.spinner.Spinner = spinner.Dot
m.spinner.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return m
}
func (m Model) Init() tea.Cmd {
return m.spinner.Tick
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmds []tea.Cmd
if m.ctx.Loading == true {
cmds = append(cmds, m.spinner.Tick)
}
switch msg := msg.(type) {
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
var items []string
for i, nav := range Navigation {
if m.CurrentId == i {
items = append(items, activeTab.Render(nav))
} else {
items = append(items, tab.Render(nav))
}
}
row := lipgloss.JoinHorizontal(
lipgloss.Top,
items...,
)
if m.ctx.Loading == false {
gap := tabGap.Render(strings.Repeat(" ", max(0, m.ctx.Screen[0]-lipgloss.Width(row)-2)))
row = lipgloss.JoinHorizontal(lipgloss.Bottom, row, gap)
} else {
gap := tabGap.Render(strings.Repeat(" ", max(0, m.ctx.Screen[0]-lipgloss.Width(row)-4)))
row = lipgloss.JoinHorizontal(lipgloss.Bottom, row, gap, " ", m.spinner.View())
}
return lipgloss.JoinHorizontal(lipgloss.Top, row, "\n\n")
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func (m *Model) NthTab(nth int) {
if nth > len(Navigation) {
nth = len(Navigation)
} else if nth < 1 {
nth = 1
}
m.CurrentId = nth - 1
}
func (m *Model) PrevTab() {
m.CurrentId--
if m.CurrentId < 0 {
m.CurrentId = len(Navigation) - 1
}
}
func (m *Model) NextTab() {
m.CurrentId++
if m.CurrentId >= len(Navigation) {
m.CurrentId = 0
}
}

View File

@ -6,7 +6,7 @@ import (
"strings"
"github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/navigation"
"github.com/mrusme/gobbs/ui/header"
"github.com/mrusme/gobbs/ui/views/posts"
"github.com/mrusme/gobbs/ui/views"
@ -16,24 +16,9 @@ import (
)
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
Up key.Binding
Down key.Binding
Quit key.Binding
}
var DefaultKeyMap = KeyMap{
@ -52,19 +37,21 @@ var DefaultKeyMap = KeyMap{
}
type Model struct {
keymap KeyMap
nav navigation.Model
views []views.View
ctx *ctx.Ctx
keymap KeyMap
header header.Model
views []views.View
currentView int
ctx *ctx.Ctx
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
keymap: DefaultKeyMap,
ctx: c,
keymap: DefaultKeyMap,
currentView: 0,
ctx: c,
}
m.nav = navigation.NewModel(m.ctx)
m.header = header.NewModel(m.ctx)
for _, capability := range (*m.ctx.Systems[0]).GetCapabilities() { // TODO
switch capability.ID {
case "posts":
@ -102,12 +89,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
v, cmd := m.views[m.nav.CurrentId].Update(msg)
m.views[m.nav.CurrentId] = v
v, cmd := m.views[m.currentView].Update(msg)
m.views[m.currentView] = v
cmds = append(cmds, cmd)
nav, cmd := m.nav.Update(msg)
m.nav = nav
header, cmd := m.header.Update(msg)
m.header = header
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
@ -115,8 +102,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m Model) View() string {
s := strings.Builder{}
s.WriteString(m.nav.View() + "\n\n")
s.WriteString(m.views[m.nav.CurrentId].View())
s.WriteString(m.header.View() + "\n\n")
s.WriteString(m.views[m.currentView].View())
return s.String()
}