-
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.
Implemented finder Screen scaffolder
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 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,87 @@ | ||
package ui | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func finderUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyEsc, tea.KeyCtrlC: | ||
if m.home.ActiveFinderScreen { | ||
m.home.ActiveFinderScreen = false | ||
m.home.Finder.Reset() | ||
m.home.Viewport.SetContent(m.GetDataView()) | ||
return &m, nil | ||
} | ||
} | ||
switch msg.String() { | ||
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++ | ||
} | ||
} | ||
} | ||
m.home.Finder, cmd = m.home.Finder.Update(msg) | ||
m.home.FinderFilter = m.home.Finder.Value() | ||
m.home.Viewport.SetContent(finderView(&m)) | ||
return &m, cmd | ||
} | ||
|
||
func finderView(m *ModelHome) string { | ||
return view.Render( | ||
m.home.Finder.View(), | ||
finderDataView(m, m.home.FinderFilter), | ||
) | ||
} | ||
|
||
func finderDataView(m *ModelHome, filter string) string { | ||
m.home.Start, _ = strconv.Atoi(filter) | ||
*m.home.Count = 2 | ||
var ( | ||
pagey = m.home.PageTotal - 1 | ||
selecty = m.home.Content | ||
) | ||
|
||
m.home.Store, _, _ = usecaseFinder.Execute(filter, 18, 1) | ||
m.home.PageTotal = len(m.home.Store) | ||
var ( | ||
result []string | ||
maxLen = m.home.Viewport.Width | ||
) | ||
|
||
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) | ||
} | ||
|
||
if pagey == i && selecty == "window" { | ||
v.EnTitle = SelecRow.Render(v.EnTitle) | ||
} | ||
|
||
if len(v.EnTitle) > maxLen { | ||
title := ShrinkWordMiddle(v.EnTitle, maxLen) | ||
v.EnTitle = title | ||
} | ||
|
||
result = append(result, fmt.Sprintf("\n%s", v.EnTitle)) | ||
} | ||
|
||
rowData := strings.Trim(fmt.Sprintf("%s", result), "[]") | ||
|
||
return rowData | ||
} |
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,31 @@ | ||
package finder | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/grrlopes/storydb/entity" | ||
"github.com/grrlopes/storydb/repositories" | ||
) | ||
|
||
type InputBoundary interface { | ||
Execute(string, int, int) ([]entity.SqliteCommand, int, error) | ||
} | ||
|
||
type execute struct { | ||
repository repositories.ISqliteRepository | ||
} | ||
|
||
func NewFinder(repo repositories.ISqliteRepository) InputBoundary { | ||
return execute{ | ||
repository: repo, | ||
} | ||
} | ||
|
||
func (e execute) Execute(filter string, limit int, skip int) ([]entity.SqliteCommand, int, error) { | ||
result, count, err := e.repository.Search(filter, limit, skip) | ||
if err != nil { | ||
log.Fatal("Search:", err) | ||
} | ||
|
||
return result, count, err | ||
} |