Skip to content

Commit

Permalink
feat: added a medium seeder (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHope authored Jan 10, 2024
1 parent c6cc78c commit d105cd1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
20 changes: 16 additions & 4 deletions cmd/cli/tui/booksview/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type model struct {
height int // the current height of the screen
folder string // the current folder
query string // current search query
busy bool // used to limit writers
header header.HeaderModel // header for app
table scrolltable.ScrolltableModel[armaria.Book] // table of books
help help.HelpModel // help for the app
Expand Down Expand Up @@ -244,15 +245,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.inputMode = false
m.query = ""
m.inputType = inputNone
return m, m.inputEndCmd()
return m, tea.Batch(m.inputEndCmd(), m.updateFiltersCmd())

case "enter":
cmds := []tea.Cmd{m.inputEndCmd()}
if m.inputType == inputName {
m.busy = true
cmds = append(cmds, m.updateNameCmd(m.input.Text()))
} else if m.inputType == inputURL {
m.busy = true
cmds = append(cmds, m.updateURLCmd(m.input.Text()))
} else if m.inputType == inputFolder {
m.busy = true
cmds = append(cmds, m.addFolderCmd(m.input.Text()))
}

Expand Down Expand Up @@ -342,6 +346,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case "D":
if !m.table.Empty() {
m.busy = true
cmds = append(cmds, m.deleteBookCmd())
}

Expand Down Expand Up @@ -382,7 +387,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, m.inputStartCmd("Folder: ", ""))

case "ctrl+up":
if m.query == "" && !m.table.Empty() && m.table.Index() > 0 {
if m.query == "" && !m.table.Empty() && m.table.Index() > 0 && !m.busy {
m.busy = true

if m.table.Index() == 1 {
next := m.table.Data()[0].ID
cmds = append(cmds, m.moveToStartCmd(next, msgs.DirectionUp))
Expand All @@ -394,7 +401,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case "ctrl+down":
if m.query == "" && !m.table.Empty() && m.table.Index() < len(m.table.Data())-1 {
if m.query == "" && !m.table.Empty() && m.table.Index() < len(m.table.Data())-1 && !m.busy {
m.busy = true

if m.table.Index() == len(m.table.Data())-2 {
previous := m.table.Data()[len(m.table.Data())-1].ID
cmds = append(cmds, m.moveToEndCmd(previous, msgs.DirectionDown))
Expand Down Expand Up @@ -424,6 +433,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case msgs.ViewMsg:
m.activeView = msgs.View(msg)
return m, nil

case msgs.FreeMsg:
m.busy = false
}

return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -737,7 +749,7 @@ func (m model) moveToEndCmd(previous string, move msgs.Direction) tea.Cmd {
}
}

// moveToEndCmd moves a bookmark or folder to the end of the list.
// moveToStartCmd moves a bookmark or folder to the end of the list.
func (m model) moveToStartCmd(next string, move msgs.Direction) tea.Cmd {
return func() tea.Msg {
if m.table.Selection().IsFolder {
Expand Down
4 changes: 3 additions & 1 deletion cmd/cli/tui/errorview/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
if m.activeView == msgs.ViewError {
return m, tea.Quit
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/cli/tui/msgs/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ package msgs

// FolderMsg is a message that changes the current folder.
type FolderMsg string

// FreeMsg is a message that denotes the writer is free again.
type FreeMsg struct{}
2 changes: 1 addition & 1 deletion cmd/cli/tui/scrolltable/scrolltable.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (m ScrolltableModel[T]) Update(msg tea.Msg) (ScrolltableModel[T], tea.Cmd)
if msg.Name == m.name {
m.data = msg.Data
m.resetFrame(msg.Move)
return m, m.selectionChangedCmd()
return m, tea.Batch(m.selectionChangedCmd(), func() tea.Msg { return msgs.FreeMsg{} })
}

case tea.KeyMsg:
Expand Down
64 changes: 59 additions & 5 deletions cmd/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

// Used to seed data for testing.

const numTags = 3

type Context struct {
}

Expand Down Expand Up @@ -36,7 +38,7 @@ func (r *SmallCmd) Run(_ *Context) error {
DefaultAddBookOptions().
WithDescription(gofakeit.ProductDescription()).
WithName(gofakeit.ProductName()).
WithTags(tagsFactory(3))
WithTags(tagsFactory())
_, err := armariaapi.AddBook(gofakeit.URL(), bo)
if err != nil {
return err
Expand All @@ -51,7 +53,58 @@ func (r *SmallCmd) Run(_ *Context) error {
DefaultAddBookOptions().
WithDescription(gofakeit.ProductDescription()).
WithName(gofakeit.ProductName()).
WithTags(tagsFactory(3)).
WithTags(tagsFactory()).
WithParentID(f)
_, err := armariaapi.AddBook(gofakeit.URL(), bo)
if err != nil {
return err
}
}
}

return nil
}

type MediumCmd struct {
}

func (r *MediumCmd) Run(_ *Context) error {
// Add 50 folders.

folders := make([]string, 0)
for i := 0; i < 50; i++ {
fo := armariaapi.DefaultAddFolderOptions()
fr, err := armariaapi.AddFolder(gofakeit.ProductName(), fo)
if err != nil {
return err
}

folders = append(folders, fr.ID)
}

// Add 1000 top level bookmarks.

for i := 0; i < 1000; i++ {
bo := armariaapi.
DefaultAddBookOptions().
WithDescription(gofakeit.ProductDescription()).
WithName(gofakeit.ProductName()).
WithTags(tagsFactory())
_, err := armariaapi.AddBook(gofakeit.URL(), bo)
if err != nil {
return err
}
}

// Add 1000 bookmarks to each folder.

for _, f := range folders {
for i := 0; i < 1000; i++ {
bo := armariaapi.
DefaultAddBookOptions().
WithDescription(gofakeit.ProductDescription()).
WithName(gofakeit.ProductName()).
WithTags(tagsFactory()).
WithParentID(f)
_, err := armariaapi.AddBook(gofakeit.URL(), bo)
if err != nil {
Expand All @@ -63,10 +116,10 @@ func (r *SmallCmd) Run(_ *Context) error {
return nil
}

func tagsFactory(num int) []string {
func tagsFactory() []string {
tags := []string{}

for len(tags) < num {
for len(tags) < numTags {
tag := gofakeit.NounCommon()
count := lo.Count(tags, tag)
if tag != "" && count == 0 {
Expand All @@ -78,7 +131,8 @@ func tagsFactory(num int) []string {
}

type cli struct {
Small SmallCmd `cmd:"" help:"Seed a small amount of data."`
Small SmallCmd `cmd:"" help:"Seed a small amount of data."`
Medium MediumCmd `cmd:"" help:"Seed a medium amount of data."`
}

func main() {
Expand Down

0 comments on commit d105cd1

Please sign in to comment.