slightly less ugly tui
This commit is contained in:
parent
ea03191348
commit
8c1dcfa396
|
@ -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"
|
||||
|
@ -27,9 +28,12 @@ type tabEMDB struct {
|
|||
mode string
|
||||
focused string
|
||||
colWidth int
|
||||
colHeight int
|
||||
list list.Model
|
||||
formLabels []string
|
||||
formInputs []textinput.Model
|
||||
inputWatchedOn textinput.Model
|
||||
inputRating textinput.Model
|
||||
inputComment textarea.Model
|
||||
formFocus int
|
||||
logger *Logger
|
||||
}
|
||||
|
@ -45,13 +49,21 @@ 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",
|
||||
|
@ -60,7 +72,9 @@ func NewTabEMDB(emdb *client.EMDB, logger *Logger) (tea.Model, tea.Cmd) {
|
|||
mode: "view",
|
||||
list: list,
|
||||
formLabels: formLabels,
|
||||
formInputs: formInputs,
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue