From 8c1dcfa396ebf9fb5f65ba7dfd6932cd8bd23471 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sat, 6 Jan 2024 13:00:29 +0100 Subject: [PATCH] slightly less ugly tui --- cmd/terminal-client/tui/tabemdb.go | 158 +++++++++++++++++------------ cmd/terminal-client/tui/tabtmdb.go | 8 +- 2 files changed, 99 insertions(+), 67 deletions(-) diff --git a/cmd/terminal-client/tui/tabemdb.go b/cmd/terminal-client/tui/tabemdb.go index 8d961f9..32b8571 100644 --- a/cmd/terminal-client/tui/tabemdb.go +++ b/cmd/terminal-client/tui/tabemdb.go @@ -7,6 +7,7 @@ import ( "ewintr.nl/emdb/client" "github.com/charmbracelet/bubbles/list" + "github.com/charmbracelet/bubbles/textarea" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -22,16 +23,19 @@ type UpdateForm tea.Msg type StoredMovie struct{} type tabEMDB struct { - initialized bool - emdb *client.EMDB - mode string - focused string - colWidth int - list list.Model - formLabels []string - formInputs []textinput.Model - formFocus int - logger *Logger + initialized bool + emdb *client.EMDB + mode string + focused string + colWidth int + colHeight int + list list.Model + formLabels []string + inputWatchedOn textinput.Model + inputRating textinput.Model + inputComment textarea.Model + formFocus int + logger *Logger } func NewTabEMDB(emdb *client.EMDB, logger *Logger) (tea.Model, tea.Cmd) { @@ -45,22 +49,32 @@ func NewTabEMDB(emdb *client.EMDB, logger *Logger) (tea.Model, tea.Cmd) { "Rating", "Comment", } - formInputs := make([]textinput.Model, len(formLabels)) - for i := range formLabels { - formInputs[i] = textinput.New() - formInputs[i].Prompt = "" - formInputs[i].Width = 50 - formInputs[i].CharLimit = 500 - } + //if l == "Comment" { + // formInputs[i] = textarea.New() + //} + inputWatchedOn := textinput.New() + inputWatchedOn.Prompt = "" + inputWatchedOn.Width = 50 + inputWatchedOn.CharLimit = 500 + inputRating := textinput.New() + inputRating.Prompt = "" + inputRating.Width = 50 + inputRating.CharLimit = 500 + inputComment := textarea.New() + inputComment.SetWidth(50) + inputComment.SetHeight(3) + inputComment.CharLimit = 500 m := tabEMDB{ - focused: "form", - emdb: emdb, - logger: logger, - mode: "view", - list: list, - formLabels: formLabels, - formInputs: formInputs, + focused: "form", + emdb: emdb, + logger: logger, + mode: "view", + list: list, + formLabels: formLabels, + inputWatchedOn: inputWatchedOn, + inputRating: inputRating, + inputComment: inputComment, } logger.Log("search emdb...") @@ -80,7 +94,8 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.initialized = true } m.colWidth = msg.Width / 2 - m.list.SetSize(m.colWidth, msg.Height) + m.colHeight = msg.Height + m.list.SetSize(m.colWidth-4, msg.Height-4) m.list, cmd = m.list.Update(msg) cmds = append(cmds, cmd) case Movies: @@ -124,7 +139,9 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "e": m.mode = "edit" m.formFocus = 0 - m.formInputs[0].Focus() + m.inputWatchedOn.PromptStyle = focusedStyle + m.inputWatchedOn.TextStyle = focusedStyle + cmds = append(cmds, m.inputWatchedOn.Focus()) } } } @@ -133,17 +150,25 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m *tabEMDB) updateFormInputs(msg tea.Msg) tea.Cmd { - cmds := make([]tea.Cmd, len(m.formInputs)) - for i := range m.formInputs { - m.formInputs[i], cmds[i] = m.formInputs[i].Update(msg) - } + cmds := make([]tea.Cmd, 3) + m.inputWatchedOn, cmds[0] = m.inputWatchedOn.Update(msg) + m.inputRating, cmds[1] = m.inputRating.Update(msg) + m.inputComment, cmds[2] = m.inputComment.Update(msg) return tea.Batch(cmds...) } func (m tabEMDB) View() string { - colLeft := lipgloss.NewStyle().Width(m.colWidth).Render(m.list.View()) - colRight := lipgloss.NewStyle().Width(m.colWidth).Render(m.ViewForm()) + colLeft := lipgloss.NewStyle(). + Width(m.colWidth - 2). + Height(m.colHeight - 2). + Padding(1). + Render(m.list.View()) + colRight := lipgloss.NewStyle(). + Width(m.colWidth - 2). + Height(m.colHeight - 2). + Padding(1). + Render(m.ViewForm()) return lipgloss.JoinHorizontal(lipgloss.Top, colLeft, colRight) } @@ -153,9 +178,10 @@ func (m *tabEMDB) UpdateForm() { if !ok { return } - m.formInputs[0].SetValue(movie.m.WatchedOn) - m.formInputs[1].SetValue(fmt.Sprintf("%d", movie.m.Rating)) - m.formInputs[2].SetValue(movie.m.Comment) + m.inputWatchedOn.SetValue(movie.m.WatchedOn) + m.inputRating.SetValue(fmt.Sprintf("%d", movie.m.Rating)) + m.inputComment.SetValue(movie.m.Comment) + m.Log(fmt.Sprintf("showing movie %s", movie.m.ID)) } func (m *tabEMDB) ViewForm() string { @@ -165,32 +191,26 @@ func (m *tabEMDB) ViewForm() string { } labels := []string{ - "ID: ", - "TMDBID: ", - "IMDBID: ", - "Title", - "English title", - "Year", - "Directors", - "Summary", + "Title: ", + "English title: ", + "Year: ", + "Directors: ", + "Summary: ", } for _, l := range m.formLabels { labels = append(labels, fmt.Sprintf("%s: ", l)) } fields := []string{ - movie.m.ID, - fmt.Sprintf("%d", movie.m.TMDBID), - movie.m.IMDBID, movie.m.Title, movie.m.EnglishTitle, fmt.Sprintf("%d", movie.m.Year), strings.Join(movie.m.Directors, ","), movie.m.Summary, } - for _, f := range m.formInputs { - fields = append(fields, f.View()) - } + + fields = append(fields, m.inputWatchedOn.View(), m.inputRating.View(), m.inputComment.View()) + labelView := strings.Join(labels, "\n") fieldsView := strings.Join(fields, "\n") @@ -198,28 +218,38 @@ func (m *tabEMDB) ViewForm() string { } func (m *tabEMDB) NavigateForm(key string) []tea.Cmd { + order := []string{"Watched on", "Rating", "Comment"} + var cmds []tea.Cmd if key == "up" || key == "shift+tab" { m.formFocus-- } else { m.formFocus++ } - if m.formFocus > len(m.formInputs) { + if m.formFocus > len(order) { m.formFocus = 0 } if m.formFocus < 0 { - m.formFocus = len(m.formInputs) + m.formFocus = len(order) } - for i := 0; i <= len(m.formInputs)-1; i++ { - if i == m.formFocus { - m.formInputs[i].PromptStyle = focusedStyle - m.formInputs[i].TextStyle = focusedStyle - cmds = append(cmds, m.formInputs[i].Focus()) - continue - } - m.formInputs[i].Blur() - m.formInputs[i].PromptStyle = noStyle - m.formInputs[i].TextStyle = noStyle + + switch order[m.formFocus] { + case "Watched on": + m.inputWatchedOn.PromptStyle = focusedStyle + m.inputWatchedOn.TextStyle = focusedStyle + cmds = append(cmds, m.inputWatchedOn.Focus()) + m.inputRating.Blur() + m.inputComment.Blur() + case "Rating": + m.inputRating.PromptStyle = focusedStyle + m.inputRating.TextStyle = focusedStyle + cmds = append(cmds, m.inputRating.Focus()) + m.inputWatchedOn.Blur() + m.inputComment.Blur() + case "Comment": + cmds = append(cmds, m.inputComment.Focus()) + m.inputWatchedOn.Blur() + m.inputRating.Blur() } return cmds @@ -228,12 +258,12 @@ func (m *tabEMDB) NavigateForm(key string) []tea.Cmd { func (m *tabEMDB) StoreMovie() tea.Cmd { return func() tea.Msg { updatedMovie := m.list.SelectedItem().(Movie) - updatedMovie.m.WatchedOn = m.formInputs[0].Value() + updatedMovie.m.WatchedOn = m.inputWatchedOn.Value() var err error - if updatedMovie.m.Rating, err = strconv.Atoi(m.formInputs[1].Value()); err != nil { + if updatedMovie.m.Rating, err = strconv.Atoi(m.inputRating.Value()); err != nil { return fmt.Errorf("rating cannot be converted to an int: %w", err) } - updatedMovie.m.Comment = m.formInputs[2].Value() + updatedMovie.m.Comment = m.inputComment.Value() if _, err := m.emdb.CreateMovie(updatedMovie.m); err != nil { return err } diff --git a/cmd/terminal-client/tui/tabtmdb.go b/cmd/terminal-client/tui/tabtmdb.go index e9c6b8f..1a61f91 100644 --- a/cmd/terminal-client/tui/tabtmdb.go +++ b/cmd/terminal-client/tui/tabtmdb.go @@ -62,14 +62,16 @@ func (m tabTMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "left", "shift+tab": cmds = append(cmds, SelectPrevTab(), m.ResetCmd()) case "enter": - switch m.focused { - case "search": + if m.focused == "search" { cmds = append(cmds, m.SearchTMDBCmd(m.searchInput.Value())) m.searchInput.Blur() m.Log("search tmdb...") - case "result": + } + case "i": + if m.focused == "result" { movie := m.searchResults.SelectedItem().(Movie) cmds = append(cmds, m.ImportMovieCmd(movie), m.ResetCmd()) + m.Log(fmt.Sprintf("imported movie %s", movie.Title())) } } case Movies: