Skip to content

Commit

Permalink
Added finder into home screen
Browse files Browse the repository at this point in the history
  • Loading branch information
grrlopes committed Jul 31, 2023
1 parent 624f71c commit fc3e837
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 37 deletions.
6 changes: 6 additions & 0 deletions ui/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/grrlopes/storydb/entity"
)

func finderCmd(filter string, limit int, offset int ) ([]entity.SqliteCommand, int) {
data, total, _ := usecaseFinder.Execute(filter, limit, offset)
return data, total
}

func finderUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
var cmd tea.Cmd

Expand Down
88 changes: 51 additions & 37 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func NewHome(m *entity.Command) *ModelHome {
pro := progress.New(progress.WithDefaultGradient())
txt := textinput.New()
txt.Placeholder = "type"
txt.Focus()
txt.CharLimit = 156
txt.Width = 20
txt.Prompt = "Finder: "
Expand Down Expand Up @@ -84,43 +83,52 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
return synced, cmd
}

if m.home.ActiveFinderScreen {
m.home.Ready = true
finder, cmd := finderUpdate(msg, m)
return finder, cmd
}
// if m.home.ActiveFinderScreen {
// m.home.Ready = true
// finder, cmd := finderUpdate(msg, m)
// return finder, cmd
// }

var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return &m, tea.Quit
case "up", "k":
if m.home.Cursor > 0 {
m.home.Content = "arrow"
m.home.Cursor--
if m.home.Finder.Focused() {
if msg.String() == "ctrl+c" {
m.home.Finder.Blur()
}
case "down", "j":
if m.home.Cursor < m.home.PageTotal-1 {
m.home.Content = "arrow"
m.home.Cursor++
m.home.Store, *m.home.Count = finderCmd(m.home.Finder.Value(), 18, m.home.Start)
start, end := m.updatepagination()
m.home.Start = start
m.home.End = end
m.home.Finder, cmd = m.home.Finder.Update(msg)
} else {
switch msg.String() {
case "ctrl+c", "q":
return &m, tea.Quit
case "up", "k":
if m.home.Cursor > 0 {
m.home.Content = "arrow"
m.home.Cursor--
}
case "down", "j":
if m.home.Cursor < m.home.PageTotal-1 {
m.home.Content = "arrow"
m.home.Cursor++
}
case "ctrl+g":
m.home.Cursor = m.home.PageTotal - 1
case "s":
m.home.StatusSyncScreen = false
m.home.ActiveSyncScreen = true
m.home.Viewport.SetContent(syncView(&m))
return &m, cmd
case "ctrl+u":
m.home.Cursor = 0
case "enter":
return &m, tea.Quit
case "/":
m.home.Finder.Focus()
}
case "ctrl+g":
m.home.Cursor = m.home.PageTotal - 1
case "s":
m.home.StatusSyncScreen = false
m.home.ActiveSyncScreen = true
m.home.Viewport.SetContent(syncView(&m))
return &m, cmd
case "ctrl+u":
m.home.Cursor = 0
case "enter":
return &m, tea.Quit
case "/":
m.home.ActiveFinderScreen = true
m.home.Viewport.SetContent(finderView(&m))
return &m, cmd
}
case tea.WindowSizeMsg:
m.home.Content = "window"
Expand All @@ -129,10 +137,13 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.Viewport.SetContent(m.GetDataView())
m.home.Ready = true
}

*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
start, end := m.updatepagination()
m.home.Start = start
m.home.End = end
if !m.home.Finder.Focused() {
start, end := m.updatepagination()
m.home.Start = start
m.home.End = end
}

m.home.Viewport.SetContent(m.GetDataView())
return &m, cmd
Expand All @@ -145,7 +156,7 @@ func (m ModelHome) View() string {
return "\n Loading..."
}

return view.Render(m.HeaderView()) + "\n" +
return view.Render(m.HeaderView()) + "\n" + m.home.Finder.View() +
content.Render(m.home.Viewport.View()) + "\n" +
m.FooterView() + "\n" +
m.paginationView()
Expand All @@ -166,8 +177,11 @@ func (m *ModelHome) GetDataView() string {
selecty = m.home.Content
)

m.home.Store, _ = usecasePager.Execute(18, m.home.Start)
if !m.home.Finder.Focused() {
m.home.Store, _ = usecasePager.Execute(18, m.home.Start)
}
m.home.PageTotal = len(m.home.Store)
m.home.Pagination.SetTotalPages(*m.home.Count)
var (
result []string
maxLen = m.home.Viewport.Width
Expand Down

0 comments on commit fc3e837

Please sign in to comment.