emdb/cmd/terminal-client/tui/tui.go

217 lines
5.3 KiB
Go
Raw Normal View History

2023-12-22 15:50:42 +01:00
package tui
import (
2023-12-23 12:08:58 +01:00
"fmt"
"strings"
"ewintr.nl/emdb/client"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/viewport"
2023-12-22 15:50:42 +01:00
tea "github.com/charmbracelet/bubbletea"
2023-12-23 12:08:58 +01:00
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
var (
docStyle = lipgloss.NewStyle().Padding(1)
colorNormalForeground = lipgloss.ANSIColor(termenv.ANSIWhite)
colorHighLightForeGround = lipgloss.ANSIColor(termenv.ANSIBrightWhite)
windowStyle = lipgloss.NewStyle().
BorderForeground(colorHighLightForeGround).
Foreground(colorNormalForeground).
Padding(0, 1).
Border(lipgloss.NormalBorder(), true)
2023-12-23 12:44:58 +01:00
logLineCount = 5
2023-12-22 15:50:42 +01:00
)
2023-12-23 12:08:58 +01:00
func New(conf Config) (*tea.Program, error) {
tabs := []string{"Erik's movie database", "The movie database"}
tabContent := []string{"Emdb", "TMDB"}
tmdb, err := client.NewTMDB(conf.TMDBAPIKey)
if err != nil {
return nil, err
}
m := baseModel{
config: conf,
emdb: client.NewEMDB(conf.EMDBBaseURL, conf.EMDBAPIKey),
tmdb: tmdb,
Tabs: tabs,
TabContent: tabContent,
}
return tea.NewProgram(m, tea.WithAltScreen()), nil
}
type baseModel struct {
config Config
emdb *client.EMDB
tmdb *client.TMDB
Tabs []string
TabContent []string
activeTab int
//focused string
//searchInput textinput.Model
//searchResults list.Model
movieList list.Model
logContent string
ready bool
logViewport viewport.Model
windowSize tea.WindowSizeMsg
2023-12-23 12:44:58 +01:00
contentSize tea.WindowSizeMsg
2023-12-23 12:08:58 +01:00
tabSize tea.WindowSizeMsg
}
func (m baseModel) Init() tea.Cmd {
return nil
}
func (m baseModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "right", "tab":
m.activeTab = min(m.activeTab+1, len(m.Tabs)-1)
return m, nil
case "left", "shift+tab":
m.activeTab = max(m.activeTab-1, 0)
return m, nil
}
case tea.WindowSizeMsg:
2023-12-23 12:44:58 +01:00
m.windowSize = msg
2023-12-23 12:08:58 +01:00
if !m.ready {
2023-12-23 12:44:58 +01:00
m.initialModel()
2023-12-23 12:08:58 +01:00
}
2023-12-23 12:44:58 +01:00
//m.Log(fmt.Sprintf("new window size: %dx%d", msg.Width, msg.Height))
m.setSizes()
2023-12-23 12:08:58 +01:00
}
//switch m.focused {
//case "search":
// m.searchInput, cmd = m.searchInput.Update(msg)
//case "result":
// m.searchResults, cmd = m.searchResults.Update(msg)
//}
m.logViewport, cmd = m.logViewport.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m *baseModel) Log(msg string) {
m.logContent = fmt.Sprintf("%s\n%s", m.logContent, msg)
m.logViewport.SetContent(m.logContent)
m.logViewport.GotoBottom()
}
//func (m *model) Search() {
// m.Log("start search")
// movies, err := m.tmdb.Search(m.searchInput.Value())
// if err != nil {
// m.Log(fmt.Sprintf("error: %v", err))
// return
// }
//
// m.Log(fmt.Sprintf("found %d results", len(movies)))
// items := []list.Item{}
// for _, res := range movies {
// items = append(items, Movie{m: res})
// }
//
// m.searchResults.SetItems(items)
// m.focused = "result"
//}
func (m baseModel) View() string {
if !m.ready {
return "\n Initializing..."
2023-12-22 15:50:42 +01:00
}
2023-12-23 12:08:58 +01:00
doc := strings.Builder{}
2023-12-23 12:44:58 +01:00
doc.WriteString(m.renderMenu())
2023-12-23 12:08:58 +01:00
doc.WriteString("\n")
2023-12-23 12:44:58 +01:00
doc.WriteString(m.renderTabContent())
2023-12-23 12:08:58 +01:00
doc.WriteString("\n")
2023-12-23 12:44:58 +01:00
doc.WriteString(m.renderLog())
2023-12-23 12:08:58 +01:00
return docStyle.Render(doc.String())
}
2023-12-23 12:44:58 +01:00
func (m *baseModel) renderMenu() string {
2023-12-23 12:08:58 +01:00
var items []string
for i, t := range m.Tabs {
if i == m.activeTab {
items = append(items, lipgloss.NewStyle().
Foreground(colorHighLightForeGround).
Render(fmt.Sprintf(" * %s ", t)))
continue
}
items = append(items, lipgloss.NewStyle().
Foreground(colorNormalForeground).
Render(fmt.Sprintf(" %s ", t)))
}
2023-12-23 12:44:58 +01:00
return lipgloss.PlaceHorizontal(m.contentSize.Width, lipgloss.Left, lipgloss.JoinHorizontal(lipgloss.Top, items...))
2023-12-23 12:08:58 +01:00
}
2023-12-23 12:44:58 +01:00
func (m *baseModel) renderTabContent() string {
2023-12-23 12:08:58 +01:00
content := m.TabContent[m.activeTab]
2023-12-23 12:44:58 +01:00
switch m.activeTab {
case 0:
content = m.movieList.View()
case 1:
content = "tmdb"
}
2023-12-23 12:08:58 +01:00
2023-12-23 12:44:58 +01:00
return windowStyle.Width(m.contentSize.Width).Height(m.contentSize.Height).Render(content)
2023-12-23 12:08:58 +01:00
}
2023-12-23 12:44:58 +01:00
func (m *baseModel) renderLog() string {
return windowStyle.Width(m.contentSize.Width).Height(logLineCount).Render(m.logViewport.View())
}
func (m *baseModel) initialModel() {
m.movieList = list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
m.movieList.Title = "Movies"
m.movieList.SetShowHelp(false)
m.logViewport = viewport.New(0, 0)
m.logViewport.KeyMap = viewport.KeyMap{}
m.setSizes()
m.refreshMovieList()
m.ready = true
2023-12-23 12:08:58 +01:00
}
2023-12-23 12:44:58 +01:00
func (m *baseModel) setSizes() {
logHeight := logLineCount + docStyle.GetVerticalFrameSize()
menuHeight := 1
m.contentSize.Width = m.windowSize.Width - windowStyle.GetHorizontalFrameSize() - docStyle.GetHorizontalFrameSize()
m.contentSize.Height = m.windowSize.Height - windowStyle.GetVerticalFrameSize() - docStyle.GetVerticalFrameSize() - logHeight - menuHeight
2023-12-23 12:08:58 +01:00
2023-12-23 12:44:58 +01:00
m.movieList.SetSize(m.contentSize.Width, m.contentSize.Height)
m.logViewport.Width = m.contentSize.Width
m.logViewport.Height = logLineCount
}
2023-12-23 12:08:58 +01:00
2023-12-23 12:44:58 +01:00
func (m *baseModel) refreshMovieList() {
m.Log("fetch emdb movies...")
2023-12-23 12:08:58 +01:00
ems, err := m.emdb.GetMovies()
if err != nil {
m.Log(err.Error())
}
items := make([]list.Item, len(ems))
for i, em := range ems {
items[i] = list.Item(Movie{m: em})
}
2023-12-23 12:48:03 +01:00
m.movieList.SetItems(items)
2023-12-23 12:08:58 +01:00
m.Log(fmt.Sprintf("found %d movies in in emdb", len(items)))
2023-12-22 15:50:42 +01:00
}