slightly less ugly tui
This commit is contained in:
parent
ea03191348
commit
8c1dcfa396
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"ewintr.nl/emdb/client"
|
"ewintr.nl/emdb/client"
|
||||||
"github.com/charmbracelet/bubbles/list"
|
"github.com/charmbracelet/bubbles/list"
|
||||||
|
"github.com/charmbracelet/bubbles/textarea"
|
||||||
"github.com/charmbracelet/bubbles/textinput"
|
"github.com/charmbracelet/bubbles/textinput"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
@ -22,16 +23,19 @@ type UpdateForm tea.Msg
|
||||||
type StoredMovie struct{}
|
type StoredMovie struct{}
|
||||||
|
|
||||||
type tabEMDB struct {
|
type tabEMDB struct {
|
||||||
initialized bool
|
initialized bool
|
||||||
emdb *client.EMDB
|
emdb *client.EMDB
|
||||||
mode string
|
mode string
|
||||||
focused string
|
focused string
|
||||||
colWidth int
|
colWidth int
|
||||||
list list.Model
|
colHeight int
|
||||||
formLabels []string
|
list list.Model
|
||||||
formInputs []textinput.Model
|
formLabels []string
|
||||||
formFocus int
|
inputWatchedOn textinput.Model
|
||||||
logger *Logger
|
inputRating textinput.Model
|
||||||
|
inputComment textarea.Model
|
||||||
|
formFocus int
|
||||||
|
logger *Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTabEMDB(emdb *client.EMDB, logger *Logger) (tea.Model, tea.Cmd) {
|
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",
|
"Rating",
|
||||||
"Comment",
|
"Comment",
|
||||||
}
|
}
|
||||||
formInputs := make([]textinput.Model, len(formLabels))
|
//if l == "Comment" {
|
||||||
for i := range formLabels {
|
// formInputs[i] = textarea.New()
|
||||||
formInputs[i] = textinput.New()
|
//}
|
||||||
formInputs[i].Prompt = ""
|
inputWatchedOn := textinput.New()
|
||||||
formInputs[i].Width = 50
|
inputWatchedOn.Prompt = ""
|
||||||
formInputs[i].CharLimit = 500
|
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{
|
m := tabEMDB{
|
||||||
focused: "form",
|
focused: "form",
|
||||||
emdb: emdb,
|
emdb: emdb,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
mode: "view",
|
mode: "view",
|
||||||
list: list,
|
list: list,
|
||||||
formLabels: formLabels,
|
formLabels: formLabels,
|
||||||
formInputs: formInputs,
|
inputWatchedOn: inputWatchedOn,
|
||||||
|
inputRating: inputRating,
|
||||||
|
inputComment: inputComment,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log("search emdb...")
|
logger.Log("search emdb...")
|
||||||
|
@ -80,7 +94,8 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
m.initialized = true
|
m.initialized = true
|
||||||
}
|
}
|
||||||
m.colWidth = msg.Width / 2
|
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)
|
m.list, cmd = m.list.Update(msg)
|
||||||
cmds = append(cmds, cmd)
|
cmds = append(cmds, cmd)
|
||||||
case Movies:
|
case Movies:
|
||||||
|
@ -124,7 +139,9 @@ func (m tabEMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case "e":
|
case "e":
|
||||||
m.mode = "edit"
|
m.mode = "edit"
|
||||||
m.formFocus = 0
|
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 {
|
func (m *tabEMDB) updateFormInputs(msg tea.Msg) tea.Cmd {
|
||||||
cmds := make([]tea.Cmd, len(m.formInputs))
|
cmds := make([]tea.Cmd, 3)
|
||||||
for i := range m.formInputs {
|
m.inputWatchedOn, cmds[0] = m.inputWatchedOn.Update(msg)
|
||||||
m.formInputs[i], cmds[i] = m.formInputs[i].Update(msg)
|
m.inputRating, cmds[1] = m.inputRating.Update(msg)
|
||||||
}
|
m.inputComment, cmds[2] = m.inputComment.Update(msg)
|
||||||
|
|
||||||
return tea.Batch(cmds...)
|
return tea.Batch(cmds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m tabEMDB) View() string {
|
func (m tabEMDB) View() string {
|
||||||
colLeft := lipgloss.NewStyle().Width(m.colWidth).Render(m.list.View())
|
colLeft := lipgloss.NewStyle().
|
||||||
colRight := lipgloss.NewStyle().Width(m.colWidth).Render(m.ViewForm())
|
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)
|
return lipgloss.JoinHorizontal(lipgloss.Top, colLeft, colRight)
|
||||||
}
|
}
|
||||||
|
@ -153,9 +178,10 @@ func (m *tabEMDB) UpdateForm() {
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.formInputs[0].SetValue(movie.m.WatchedOn)
|
m.inputWatchedOn.SetValue(movie.m.WatchedOn)
|
||||||
m.formInputs[1].SetValue(fmt.Sprintf("%d", movie.m.Rating))
|
m.inputRating.SetValue(fmt.Sprintf("%d", movie.m.Rating))
|
||||||
m.formInputs[2].SetValue(movie.m.Comment)
|
m.inputComment.SetValue(movie.m.Comment)
|
||||||
|
m.Log(fmt.Sprintf("showing movie %s", movie.m.ID))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *tabEMDB) ViewForm() string {
|
func (m *tabEMDB) ViewForm() string {
|
||||||
|
@ -165,32 +191,26 @@ func (m *tabEMDB) ViewForm() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
labels := []string{
|
labels := []string{
|
||||||
"ID: ",
|
"Title: ",
|
||||||
"TMDBID: ",
|
"English title: ",
|
||||||
"IMDBID: ",
|
"Year: ",
|
||||||
"Title",
|
"Directors: ",
|
||||||
"English title",
|
"Summary: ",
|
||||||
"Year",
|
|
||||||
"Directors",
|
|
||||||
"Summary",
|
|
||||||
}
|
}
|
||||||
for _, l := range m.formLabels {
|
for _, l := range m.formLabels {
|
||||||
labels = append(labels, fmt.Sprintf("%s: ", l))
|
labels = append(labels, fmt.Sprintf("%s: ", l))
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := []string{
|
fields := []string{
|
||||||
movie.m.ID,
|
|
||||||
fmt.Sprintf("%d", movie.m.TMDBID),
|
|
||||||
movie.m.IMDBID,
|
|
||||||
movie.m.Title,
|
movie.m.Title,
|
||||||
movie.m.EnglishTitle,
|
movie.m.EnglishTitle,
|
||||||
fmt.Sprintf("%d", movie.m.Year),
|
fmt.Sprintf("%d", movie.m.Year),
|
||||||
strings.Join(movie.m.Directors, ","),
|
strings.Join(movie.m.Directors, ","),
|
||||||
movie.m.Summary,
|
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")
|
labelView := strings.Join(labels, "\n")
|
||||||
fieldsView := strings.Join(fields, "\n")
|
fieldsView := strings.Join(fields, "\n")
|
||||||
|
|
||||||
|
@ -198,28 +218,38 @@ func (m *tabEMDB) ViewForm() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *tabEMDB) NavigateForm(key string) []tea.Cmd {
|
func (m *tabEMDB) NavigateForm(key string) []tea.Cmd {
|
||||||
|
order := []string{"Watched on", "Rating", "Comment"}
|
||||||
|
|
||||||
var cmds []tea.Cmd
|
var cmds []tea.Cmd
|
||||||
if key == "up" || key == "shift+tab" {
|
if key == "up" || key == "shift+tab" {
|
||||||
m.formFocus--
|
m.formFocus--
|
||||||
} else {
|
} else {
|
||||||
m.formFocus++
|
m.formFocus++
|
||||||
}
|
}
|
||||||
if m.formFocus > len(m.formInputs) {
|
if m.formFocus > len(order) {
|
||||||
m.formFocus = 0
|
m.formFocus = 0
|
||||||
}
|
}
|
||||||
if 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 {
|
switch order[m.formFocus] {
|
||||||
m.formInputs[i].PromptStyle = focusedStyle
|
case "Watched on":
|
||||||
m.formInputs[i].TextStyle = focusedStyle
|
m.inputWatchedOn.PromptStyle = focusedStyle
|
||||||
cmds = append(cmds, m.formInputs[i].Focus())
|
m.inputWatchedOn.TextStyle = focusedStyle
|
||||||
continue
|
cmds = append(cmds, m.inputWatchedOn.Focus())
|
||||||
}
|
m.inputRating.Blur()
|
||||||
m.formInputs[i].Blur()
|
m.inputComment.Blur()
|
||||||
m.formInputs[i].PromptStyle = noStyle
|
case "Rating":
|
||||||
m.formInputs[i].TextStyle = noStyle
|
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
|
return cmds
|
||||||
|
@ -228,12 +258,12 @@ func (m *tabEMDB) NavigateForm(key string) []tea.Cmd {
|
||||||
func (m *tabEMDB) StoreMovie() tea.Cmd {
|
func (m *tabEMDB) StoreMovie() tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
updatedMovie := m.list.SelectedItem().(Movie)
|
updatedMovie := m.list.SelectedItem().(Movie)
|
||||||
updatedMovie.m.WatchedOn = m.formInputs[0].Value()
|
updatedMovie.m.WatchedOn = m.inputWatchedOn.Value()
|
||||||
var err error
|
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)
|
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 {
|
if _, err := m.emdb.CreateMovie(updatedMovie.m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,14 +62,16 @@ func (m tabTMDB) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
case "left", "shift+tab":
|
case "left", "shift+tab":
|
||||||
cmds = append(cmds, SelectPrevTab(), m.ResetCmd())
|
cmds = append(cmds, SelectPrevTab(), m.ResetCmd())
|
||||||
case "enter":
|
case "enter":
|
||||||
switch m.focused {
|
if m.focused == "search" {
|
||||||
case "search":
|
|
||||||
cmds = append(cmds, m.SearchTMDBCmd(m.searchInput.Value()))
|
cmds = append(cmds, m.SearchTMDBCmd(m.searchInput.Value()))
|
||||||
m.searchInput.Blur()
|
m.searchInput.Blur()
|
||||||
m.Log("search tmdb...")
|
m.Log("search tmdb...")
|
||||||
case "result":
|
}
|
||||||
|
case "i":
|
||||||
|
if m.focused == "result" {
|
||||||
movie := m.searchResults.SelectedItem().(Movie)
|
movie := m.searchResults.SelectedItem().(Movie)
|
||||||
cmds = append(cmds, m.ImportMovieCmd(movie), m.ResetCmd())
|
cmds = append(cmds, m.ImportMovieCmd(movie), m.ResetCmd())
|
||||||
|
m.Log(fmt.Sprintf("imported movie %s", movie.Title()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case Movies:
|
case Movies:
|
||||||
|
|
Loading…
Reference in New Issue