2023-12-24 13:33:15 +01:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TabSet struct {
|
|
|
|
active int
|
|
|
|
order []string
|
|
|
|
title map[string]string
|
|
|
|
tabs map[string]tea.Model
|
2023-12-24 17:11:29 +01:00
|
|
|
size TabSizeMsgType
|
2023-12-24 13:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTabSet() *TabSet {
|
|
|
|
return &TabSet{
|
|
|
|
order: make([]string, 0),
|
|
|
|
title: make(map[string]string),
|
|
|
|
tabs: make(map[string]tea.Model),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TabSet) AddTab(name, title string, model tea.Model) {
|
|
|
|
t.order = append(t.order, name)
|
|
|
|
t.title[name] = title
|
|
|
|
t.tabs[name] = model
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TabSet) Next() {
|
|
|
|
t.active++
|
|
|
|
if t.active > len(t.order)-1 {
|
|
|
|
t.active = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TabSet) Previous() {
|
|
|
|
t.active--
|
|
|
|
if t.active < 0 {
|
|
|
|
t.active = len(t.order) - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 09:21:00 +01:00
|
|
|
func (t *TabSet) Select(name string) {
|
|
|
|
for i, n := range t.order {
|
|
|
|
if n == name {
|
|
|
|
t.active = i
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-24 13:33:15 +01:00
|
|
|
func (t *TabSet) Update(msg tea.Msg) tea.Cmd {
|
|
|
|
var cmd tea.Cmd
|
2023-12-24 13:54:02 +01:00
|
|
|
var cmds []tea.Cmd
|
2023-12-24 13:33:15 +01:00
|
|
|
|
2023-12-24 13:54:02 +01:00
|
|
|
switch msg.(type) {
|
|
|
|
case TabSizeMsgType:
|
|
|
|
for _, name := range t.order {
|
|
|
|
t.tabs[name], cmd = t.tabs[name].Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
2023-12-24 17:11:29 +01:00
|
|
|
t.size = msg.(TabSizeMsgType)
|
2023-12-25 09:21:00 +01:00
|
|
|
//case ImportMovieMsg:
|
|
|
|
// t.Select("emdb")
|
|
|
|
// t.tabs["emdb"], cmd = t.tabs["emdb"].Update(msg)
|
2023-12-24 13:54:02 +01:00
|
|
|
default:
|
|
|
|
name := t.order[t.active]
|
|
|
|
t.tabs[name], cmd = t.tabs[name].Update(msg)
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tea.Batch(cmds...)
|
2023-12-24 13:33:15 +01:00
|
|
|
}
|
|
|
|
|
2023-12-24 17:11:29 +01:00
|
|
|
func (t *TabSet) View() string {
|
2023-12-24 13:33:15 +01:00
|
|
|
var items []string
|
|
|
|
for i, name := range t.order {
|
|
|
|
if i == t.active {
|
|
|
|
items = append(items, lipgloss.NewStyle().
|
|
|
|
Foreground(colorHighLightForeGround).
|
|
|
|
Render(fmt.Sprintf(" * %s ", t.title[name])))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, lipgloss.NewStyle().
|
|
|
|
Foreground(colorNormalForeground).
|
|
|
|
Render(fmt.Sprintf(" %s ", t.title[name])))
|
|
|
|
}
|
2023-12-24 17:11:29 +01:00
|
|
|
menu := lipgloss.JoinHorizontal(lipgloss.Top, items...)
|
|
|
|
pane := t.tabs[t.order[t.active]].View()
|
|
|
|
lipgloss.PlaceHorizontal(t.size.Width, lipgloss.Left, menu)
|
2023-12-24 13:33:15 +01:00
|
|
|
|
2023-12-24 17:11:29 +01:00
|
|
|
return fmt.Sprintf("%s\n%s", menu, pane)
|
2023-12-24 13:33:15 +01:00
|
|
|
}
|