Skip to content

Commit

Permalink
Added search repository
Browse files Browse the repository at this point in the history
and textinput finder construtor
  • Loading branch information
grrlopes committed Jul 20, 2023
1 parent 5e137d0 commit 6399eed
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
35 changes: 20 additions & 15 deletions entity/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ package entity
import (
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
)

type Command struct {
Content string
Cursor int
Ready bool
Selected string
Viewport viewport.Model
Start int
End int
PageTotal int
Pagination *paginator.Model
Count int
Fcount int
Ftotal int
ActiveSyncScreen bool
StatusSyncScreen bool
ProgressSync progress.Model
Content string
Cursor int
Ready bool
Selected string
Viewport viewport.Model
Start int
End int
PageTotal int
Pagination *paginator.Model
Count *int
Fcount int
Ftotal int
ActiveSyncScreen bool
ActiveFinderScreen bool
StatusSyncScreen bool
ProgressSync progress.Model
Finder textinput.Model
FinderFilter string
Store []SqliteCommand
}
36 changes: 36 additions & 0 deletions repositories/sqlite/sqliterepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"log"

"github.com/grrlopes/storydb/entity"
"github.com/grrlopes/storydb/repositories"
)

Expand Down Expand Up @@ -108,3 +109,38 @@ func (sql SQLiteRepository) InsertParsed(data string) (int64, error) {

return id, err
}

func (sql *SQLiteRepository) Search(filter string, limit int, skip int) ([]entity.SqliteCommand, int, error) {
var count int

stmt, err := sql.db.Prepare("SELECT * FROM command WHERE title LIKE ? limit ? offset ?")
if err != nil {
return []entity.SqliteCommand{}, count, err
}

result, err := stmt.Query("%"+filter+"%", limit, skip)
if err != nil {
return []entity.SqliteCommand{}, count, err
}

defer result.Close()

err = sql.db.QueryRow("SELECT COUNT(*) FROM command WHERE Title LIKE ?", "%"+filter+"").Scan(&count)

var data []entity.SqliteCommand

for result.Next() {
var command entity.SqliteCommand
if err := result.Scan(
&command.ID,
&command.EnTitle,
&command.Desc,
); err != nil {
return []entity.SqliteCommand{}, count, err
}

data = append(data, command)
}

return data, count, nil
}
30 changes: 25 additions & 5 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/grrlopes/storydb/usecase/count"
"github.com/grrlopes/storydb/usecase/fhistory"
"github.com/grrlopes/storydb/usecase/fhistorytotal"
"github.com/grrlopes/storydb/usecase/finder"
"github.com/grrlopes/storydb/usecase/pager"
)

Expand All @@ -26,6 +28,7 @@ var (
usecaseCount count.InputBoundary = count.NewCount(repository)
usecaseHistory fhistory.InputBoundary = fhistory.NewFHistory(frepository, repository)
usecaseHistoryTotal fhistorytotal.InputBoundary = fhistorytotal.NewFHistoryTotal(frepository, repository)
usecaseFinder finder.InputBoundary = finder.NewFinder(repository)
)

type ModelHome struct {
Expand All @@ -39,6 +42,12 @@ func NewHome(m *entity.Command) *ModelHome {
p.PerPage = 18
p.SetTotalPages(count)
pro := progress.New(progress.WithDefaultGradient())
txt := textinput.New()
txt.Placeholder = "type"
txt.Focus()
txt.CharLimit = 156
txt.Width = 20
txt.Prompt = "Finder: "

home := ModelHome{
home: entity.Command{
Expand All @@ -47,11 +56,12 @@ func NewHome(m *entity.Command) *ModelHome {
Viewport: viewport.Model{},
PageTotal: m.PageTotal,
Pagination: &p,
Count: count,
Count: &count,
ActiveSyncScreen: false,
StatusSyncScreen: false,
ProgressSync: pro,
Ftotal: ftotal,
Finder: txt,
},
}
return &home
Expand All @@ -74,6 +84,12 @@ 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
}

var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
Expand Down Expand Up @@ -101,6 +117,10 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
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 Down Expand Up @@ -136,7 +156,7 @@ func (m *ModelHome) GetSelected() string {
}

func (m *ModelHome) updatepagination() (int, int) {
start, end := m.home.Pagination.GetSliceBounds(m.home.Count)
start, end := m.home.Pagination.GetSliceBounds(*m.home.Count)
return start, end
}

Expand All @@ -146,14 +166,14 @@ func (m *ModelHome) GetDataView() string {
selecty = m.home.Content
)

data, _ := usecasePager.Execute(18, m.home.Start)
m.home.PageTotal = len(data)
m.home.Store, _ = usecasePager.Execute(18, m.home.Start)
m.home.PageTotal = len(m.home.Store)
var (
result []string
maxLen = m.home.Viewport.Width
)

for i, v := range data {
for i, v := range m.home.Store {
if m.home.Cursor == i && selecty == "arrow" {
m.home.Selected = v.EnTitle
v.EnTitle = SelecRow.Render(v.EnTitle)
Expand Down

0 comments on commit 6399eed

Please sign in to comment.