This commit is contained in:
abs3nt 2023-01-13 16:35:42 -08:00
parent 323dd26c83
commit c52bbd3d48

View File

@ -79,8 +79,31 @@ func (m mainModel) View() string {
return DocStyle.Render(m.list.View() + "\n") return DocStyle.Render(m.list.View() + "\n")
} }
func (m *mainModel) Typing(msg tea.KeyMsg) (bool, tea.Cmd) {
if msg.String() == "enter" {
m.list.NewStatusMessage("Setting view to search for " + m.input.Value())
items, err := SearchView(m.ctx, m.client, m.input.Value())
if err != nil {
fmt.Println(err.Error())
return false, tea.Quit
}
m.search = m.input.Value()
m.list.SetItems(items)
m.list.ResetSelected()
m.input.SetValue("")
m.input.Blur()
return true, nil
}
if msg.String() == "esc" {
m.input.SetValue("")
m.input.Blur()
return false, nil
}
m.input, _ = m.input.Update(msg)
return false, nil
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
search := false
m.list.NewStatusMessage(currentlyPlaying) m.list.NewStatusMessage(currentlyPlaying)
select { select {
case update := <-main_updates: case update := <-main_updates:
@ -95,22 +118,15 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if m.input.Focused() { if m.input.Focused() {
if msg.String() == "enter" { search, cmd := m.Typing(msg)
m.list.NewStatusMessage("Setting view to search for " + m.input.Value()) if search {
items, err := SearchView(m.ctx, m.client, m.input.Value()) m.mode = "search"
if err != nil {
fmt.Println(err.Error())
return m, tea.Quit
}
m.search = m.input.Value()
m.list.SetItems(items)
m.list.ResetSelected()
m.input.SetValue("")
m.input.Blur()
search = true
} }
m.input, _ = m.input.Update(msg) return m, cmd
} }
if msg.String() == "s" { if msg.String() == "s" {
m.input.Focus() m.input.Focus()
@ -129,20 +145,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
} }
if msg.String() == "backspace" || msg.String() == "esc" || msg.String() == "q" { if msg.String() == "backspace" || msg.String() == "esc" || msg.String() == "q" {
if m.input.Focused() { if m.mode == "album" {
if msg.String() == "esc" {
m.input.SetValue("")
m.input.Blur()
m.list.SetShowPagination(true)
m.mode = "main"
m.list.NewStatusMessage("Setting view to main")
new_items, err := MainView(m.ctx, m.client)
if err != nil {
fmt.Println(err.Error())
}
m.list.SetItems(new_items)
}
} else if m.mode == "album" {
if m.fromArtist { if m.fromArtist {
m.mode = "albums" m.mode = "albums"
m.fromArtist = true m.fromArtist = true
@ -200,9 +203,6 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.list.ResetSelected() m.list.ResetSelected()
page = 0 page = 0
} }
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
if msg.String() == "enter" || msg.String() == "spacebar" { if msg.String() == "enter" || msg.String() == "spacebar" {
switch m.mode { switch m.mode {
case "searchartists": case "searchartists":
@ -424,9 +424,6 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
h, v := DocStyle.GetFrameSize() h, v := DocStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v-1) m.list.SetSize(msg.Width-h, msg.Height-v-1)
} }
if search {
m.mode = "search"
}
var cmd tea.Cmd var cmd tea.Cmd
m.list, cmd = m.list.Update(msg) m.list, cmd = m.list.Update(msg)
return m, cmd return m, cmd