-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Broderick-Westrope/fix/get-search-working
fix: searching list
- Loading branch information
Showing
6 changed files
with
202 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug TUI interactively", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}", | ||
"console": "integratedTerminal" | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package views | ||
|
||
import tea "github.com/charmbracelet/bubbletea" | ||
|
||
var _ tea.Model = SearchModel{} | ||
|
||
type DownloadModel struct { | ||
} | ||
|
||
func (m DownloadModel) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m DownloadModel) Update(tea.Msg) (tea.Model, tea.Cmd) { | ||
return m, nil | ||
} | ||
|
||
// View implements tea.Model. | ||
func (m DownloadModel) View() string { | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package views | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"tui/test/models" | ||
|
||
"github.com/charmbracelet/bubbles/list" | ||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
var _ tea.Model = SearchModel{} | ||
|
||
type searchResultMsg []list.Item | ||
|
||
type apiErrorMsg error | ||
|
||
type SearchModel struct { | ||
apiState models.ApiState | ||
searchInput textinput.Model | ||
resultsList list.Model | ||
} | ||
|
||
var appStyle = lipgloss.NewStyle().Padding(1, 2) | ||
|
||
func NewSearchModel() SearchModel { | ||
searchInput := textinput.New() | ||
searchInput.Prompt = "Search: " | ||
searchInput.Placeholder = "Type to search..." | ||
searchInput.Focus() | ||
|
||
resultsList := list.New(make([]list.Item, 0), list.NewDefaultDelegate(), 0, 0) | ||
resultsList.Title = "Results" | ||
resultsList.SetShowPagination(false) | ||
|
||
return SearchModel{ | ||
searchInput: searchInput, | ||
resultsList: resultsList, | ||
} | ||
} | ||
|
||
func (m SearchModel) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m SearchModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmds []tea.Cmd | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
if msg.Type == tea.KeyRunes || msg.Type == tea.KeyBackspace || msg.Type == tea.KeySpace { | ||
m.searchInput, cmd = m.searchInput.Update(msg) | ||
cmds = append(cmds, cmd) | ||
|
||
cmd = m.handleSearch() | ||
cmds = append(cmds, cmd) | ||
|
||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
case tea.WindowSizeMsg: | ||
h, v := appStyle.GetFrameSize() | ||
m.resultsList.SetSize(msg.Width-h, msg.Height-v) | ||
return m, nil | ||
|
||
case searchResultMsg: | ||
cmd := m.resultsList.SetItems(msg) | ||
return m, cmd | ||
|
||
case apiErrorMsg: | ||
m.apiState.Status = models.Error | ||
m.apiState.Error = msg | ||
return m, nil | ||
} | ||
|
||
// Update the components | ||
m.resultsList, cmd = m.resultsList.Update(msg) | ||
cmds = append(cmds, cmd) | ||
|
||
return m, tea.Batch(cmds...) | ||
|
||
} | ||
|
||
func (m SearchModel) handleSearch() tea.Cmd { | ||
m.apiState.Status = models.Loading | ||
return tea.Tick(time.Millisecond*300, func(t time.Time) tea.Msg { | ||
characters, err := models.FetchCharacters(m.searchInput.Value()) | ||
if err != nil { | ||
return apiErrorMsg(err) | ||
} | ||
|
||
return searchResultMsg(characters) | ||
}) | ||
} | ||
|
||
// View implements tea.Model. | ||
func (m SearchModel) View() string { | ||
return fmt.Sprintf( | ||
"%s\n%s", | ||
m.searchInput.View(), | ||
m.resultsList.View(), | ||
) | ||
} |