set resize message and init to all tabs

This commit is contained in:
Erik Winter 2023-12-24 13:54:02 +01:00
parent 3fe1ca0474
commit 33ff36052a
6 changed files with 61 additions and 32 deletions

View File

@ -14,13 +14,11 @@ type baseModel struct {
emdb *client.EMDB
tmdb *client.TMDB
tabs *TabSet
activeTab int
initialized bool
logger *Logger
logViewport viewport.Model
windowSize tea.WindowSizeMsg
contentSize tea.WindowSizeMsg
tabSize tea.WindowSizeMsg
}
func NewBaseModel(emdb *client.EMDB, tmdb *client.TMDB, logger *Logger) (tea.Model, tea.Cmd) {
@ -108,17 +106,6 @@ func (m baseModel) View() string {
return docStyle.Render(doc.String())
}
func (m *baseModel) newTab() (tea.Model, tea.Cmd) {
switch m.activeTab {
case 0:
return NewTabEMDB(m.emdb, m.logger)
case 1:
return NewTabTMDB(m.tmdb, m.logger)
default:
return nil, nil
}
}
func (m *baseModel) renderLog() string {
return windowStyle.Width(m.contentSize.Width).Height(logLineCount).Render(m.logViewport.View())
}

View File

@ -1 +0,0 @@
package tui

View File

@ -40,7 +40,6 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case TabSizeMsgType:
if !m.initialized {
//cmds = append(cmds, FetchMovieList(m.emdb, m.logger))
m.initialized = true
}
m.list.SetSize(msg.Width, msg.Height)

View File

@ -44,10 +44,21 @@ func (t *TabSet) Previous() {
func (t *TabSet) Update(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd
name := t.order[t.active]
t.tabs[name], cmd = t.tabs[name].Update(msg)
var cmds []tea.Cmd
return cmd
switch msg.(type) {
case TabSizeMsgType:
for _, name := range t.order {
t.tabs[name], cmd = t.tabs[name].Update(msg)
cmds = append(cmds, cmd)
}
default:
name := t.order[t.active]
t.tabs[name], cmd = t.tabs[name].Update(msg)
cmds = append(cmds, cmd)
}
return tea.Batch(cmds...)
}
func (t *TabSet) ViewMenu() string {

View File

@ -1,21 +1,21 @@
package tui
import (
"fmt"
"ewintr.nl/emdb/client"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
//focused string
//searchInput textinput.Model
//searchResults list.Model
//movieLis
type tabTMDB struct {
initialized bool
results list.Model
tmdb *client.TMDB
logger *Logger
initialized bool
focused string
searchInput textinput.Model
searchResults list.Model
tmdb *client.TMDB
logger *Logger
}
func NewTabTMDB(tmdb *client.TMDB, logger *Logger) (tea.Model, tea.Cmd) {
@ -34,23 +34,56 @@ func (m tabTMDB) Init() tea.Cmd {
func (m tabTMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
m.Log(fmt.Sprintf("%v", msg))
switch msg := msg.(type) {
case TabSizeMsgType:
if !m.initialized {
m.Log(fmt.Sprintf("tmdb initialized. focused: %s", m.focused))
m.initialModel(msg.Width, msg.Height)
}
m.initialized = true
m.searchResults.SetSize(msg.Width, msg.Height-10)
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
}
}
m.results, cmd = m.results.Update(msg)
m.Log(fmt.Sprintf("focused: %s", m.focused))
switch m.focused {
case "search":
m.Log("search")
m.searchInput, cmd = m.searchInput.Update(msg)
case "result":
m.Log("result")
m.searchResults, cmd = m.searchResults.Update(msg)
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m tabTMDB) View() string {
return "tmdb"
return fmt.Sprintf("%s\n%s\n", m.searchInput.View(), m.searchResults.View())
}
func (m *tabTMDB) Log(s string) {
m.logger.Log(s)
}
func (m *tabTMDB) initialModel(width, height int) {
si := textinput.New()
si.Placeholder = "title"
si.CharLimit = 156
si.Width = 20
m.searchInput = si
m.searchInput.Focus()
m.searchResults = list.New([]list.Item{}, list.NewDefaultDelegate(), width, height-50)
m.searchResults.Title = "Search results"
m.searchResults.SetShowHelp(false)
m.focused = "search"
}
//func (m *model) Search() {

View File

@ -17,7 +17,7 @@ var (
Foreground(colorNormalForeground).
Padding(0, 1).
Border(lipgloss.NormalBorder(), true)
logLineCount = 5
logLineCount = 10
)
type Logger struct {