Skip to content

Commit

Permalink
Merge pull request #33 from IdlePhysicist/autocomp-names
Browse files Browse the repository at this point in the history
Autocompletion for Names

PR includes code for autocompleting caver names on trip creation & modification.

Closes #10

Also includes a go fmt.
  • Loading branch information
IdlePhysicist committed Aug 8, 2021
2 parents 9cb7c83 + 2e67fdf commit 4fea37c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 52 deletions.
3 changes: 1 addition & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"

"github.com/idlephysicist/cave-logger/internal/register"
"github.com/idlephysicist/cave-logger/internal/gui"
"github.com/idlephysicist/cave-logger/internal/model"
"github.com/idlephysicist/cave-logger/internal/register"
)

var version, date string
Expand Down Expand Up @@ -46,7 +46,6 @@ func main() {
}
log.SetLevel(level)


// Read config file
cfg := func(_file string) *model.Config {
var _cfg model.Config
Expand Down
139 changes: 118 additions & 21 deletions internal/gui/cud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package gui

import (
"fmt"
"time"
"strings"
"time"

tview "gitlab.com/tslocum/cview"
)
Expand Down Expand Up @@ -35,10 +35,51 @@ func (g *Gui) createTripForm() {
return
})

// Caver Field
caverField := tview.NewInputField().
SetLabel("Names").
SetFieldWidth(inputWidth)
caverField.SetAutocompleteFunc(func(current string) (matches []string) {
if len(current) == 0 {
return []string{``}
}

names := strings.Split(current, `,`)

// Process preceeding names
for _, n := range names {
n = strings.TrimSpace(n)
}

var lastPart string
if len(names) == 0 { // Then we're still on the first name
lastPart = current
} else {
lastPart = names[len(names)-1] // To get the last name
}

lastPart = strings.TrimPrefix(lastPart, ` `) // We should still trim whitespace

if len(lastPart) == 0 {
return []string{``}
}

for _, caver := range g.state.resources.people {
if strings.HasPrefix(strings.ToLower(caver.Name), strings.ToLower(lastPart)) {
matches = append(
matches,
fmt.Sprintf("%s%s", textFactory(names[0:len(names)-1], `, `), caver.Name),
)
}
}

return
})

form.
AddInputField("Date", time.Now().Format(`2006-01-02`), inputWidth, nil, nil).
AddFormItem(caveField).
AddInputField("Names", "", inputWidth, nil, nil).
AddFormItem(caverField).
AddInputField("Notes", "", inputWidth, nil, nil).
AddButton("Add", func() {
g.createTrip(form)
Expand All @@ -47,7 +88,8 @@ func (g *Gui) createTripForm() {
g.closeAndSwitchPanel("form", "trips")
})

g.pages.AddAndSwitchToPage("form", g.modal(form, 80, 29), true)
g.pages.AddAndSwitchToPage("form", g.modal(form, 80, 29), true) //.ShowPage("main")
//REVIEW: main or trips ? ^^
}

func (g *Gui) createTrip(form *tview.Form) {
Expand All @@ -58,7 +100,7 @@ func (g *Gui) createTrip(form *tview.Form) {
form.GetFormItemByLabel("Notes").(*tview.InputField).GetText(),
)
if err != nil { // NOTE: Needs fixing
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}

Expand Down Expand Up @@ -129,7 +171,7 @@ func (g *Gui) createLocation(form *tview.Form) {
form.GetFormItemByLabel("SRT").(*tview.CheckBox).IsChecked(),
)
if err != nil { // NOTE: Needs fixing
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}

Expand Down Expand Up @@ -181,7 +223,7 @@ func (g *Gui) createPerson(form *tview.Form) {
form.GetFormItemByLabel("Notes").(*tview.InputField).GetText(),
)
if err != nil { // NOTE: Needs fixing
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}

Expand Down Expand Up @@ -216,18 +258,60 @@ func (g *Gui) modifyTripForm() {
}
}

if len(matches) <= 1 || matches[0] == current {
if len(matches) <= 1 || matches[0] == current {
matches = nil
}

return
})

// Caver Field
caverField := tview.NewInputField().
SetLabel("Names").
SetFieldWidth(inputWidth).
SetText(selectedTrip.Names)
caverField.SetAutocompleteFunc(func(current string) (matches []string) {
if len(current) == 0 {
return []string{``}
}

names := strings.Split(current, `,`)

// Process preceeding names
for _, n := range names {
n = strings.TrimSpace(n)
}

var lastPart string
if len(names) == 0 { // Then we're still on the first name
lastPart = current
} else {
lastPart = names[len(names)-1] // To get the last name
}

lastPart = strings.TrimPrefix(lastPart, ` `) // We should still trim whitespace

if len(lastPart) == 0 {
return []string{``}
}

for _, caver := range g.state.resources.people {
if strings.HasPrefix(strings.ToLower(caver.Name), strings.ToLower(lastPart)) {
matches = append(
matches,
fmt.Sprintf("%s%s", textFactory(names[0:len(names)-1], `, `), caver.Name),
)
}
}

return
})

form.
AddInputField("Date", selectedTrip.Date, inputWidth, nil, nil).
AddFormItem(caveField).
AddInputField("Names", selectedTrip.Names, inputWidth, nil, nil).
AddInputField("Notes", selectedTrip.Notes, inputWidth, nil, nil).
AddFormItem(caverField).
AddInputField("Notes", selectedTrip.Notes, inputWidth, nil, nil).
AddButton("Apply", func() {
g.modifyTrip(selectedTrip.ID, form)
}).
Expand All @@ -247,15 +331,14 @@ func (g *Gui) modifyTrip(id string, form *tview.Form) {
form.GetFormItemByLabel("Notes").(*tview.InputField).GetText(),
)
if err != nil {
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}

g.closeAndSwitchPanel(`form`, `trips`)
g.tripsPanel().updateEntries(g)
}


func (g *Gui) modifyPersonForm() {
// First - what trip is selected?
selectedPerson := g.selectedPerson()
Expand Down Expand Up @@ -287,7 +370,7 @@ func (g *Gui) modifyPersonForm() {
form.
AddInputField("Name", selectedPerson.Name, inputWidth, nil, nil).
AddFormItem(clubField).
AddInputField("Notes", selectedPerson.Notes, inputWidth, nil, nil).
AddInputField("Notes", selectedPerson.Notes, inputWidth, nil, nil).
AddButton("Apply", func() {
g.modifyPerson(selectedPerson.ID, form)
}).
Expand All @@ -306,15 +389,14 @@ func (g *Gui) modifyPerson(id string, form *tview.Form) {
form.GetFormItemByLabel("Notes").(*tview.InputField).GetText(),
)
if err != nil {
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}

g.closeAndSwitchPanel(`form`, `cavers`)
g.caversPanel().updateEntries(g)
}


func (g *Gui) modifyLocationForm() {
// First - what trip is selected?
selectedLocation := g.selectedLocation()
Expand Down Expand Up @@ -359,13 +441,12 @@ func (g *Gui) modifyLocationForm() {
return
})


form.
AddInputField("Name", selectedLocation.Name, inputWidth, nil, nil).
AddFormItem(regionField).
AddFormItem(countryField).
AddCheckBox("SRT", "", selectedLocation.SRT, nil).
AddInputField("Notes", selectedLocation.Notes, inputWidth, nil, nil).
AddInputField("Notes", selectedLocation.Notes, inputWidth, nil, nil).
AddButton("Apply", func() {
g.modifyLocation(selectedLocation.ID, form)
}).
Expand All @@ -386,15 +467,14 @@ func (g *Gui) modifyLocation(id string, form *tview.Form) {
form.GetFormItemByLabel("SRT").(*tview.CheckBox).IsChecked(),
)
if err != nil {
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}

g.closeAndSwitchPanel(`form`, `caves`)
g.cavesPanel().updateEntries(g)
}


//
// DELETE FUNCS
func (g *Gui) deleteTrip() {
Expand All @@ -407,7 +487,7 @@ func (g *Gui) deleteTrip() {

g.warning(message, `trips`, []string{`Yes`, `No`}, func() {
if err := g.reg.RemoveTrip(selectedTrip.ID); err != nil {
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}
g.tripsPanel().updateEntries(g)
Expand All @@ -424,7 +504,7 @@ func (g *Gui) deleteLocation() {

g.warning(message, `locations`, []string{`Yes`, `No`}, func() {
if err := g.reg.RemoveCave(selectedLocation.ID); err != nil {
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}
g.cavesPanel().updateEntries(g)
Expand All @@ -441,9 +521,26 @@ func (g *Gui) deletePerson() {

g.warning(message, `people`, []string{`Yes`, `No`}, func() {
if err := g.reg.RemoveCaver(selectedPerson.ID); err != nil {
g.warning(err.Error(), `form`, []string{`OK`}, func() {return})
g.warning(err.Error(), `form`, []string{`OK`}, func() { return })
return
}
g.caversPanel().updateEntries(g)
})
}

func textFactory(sl []string, sep string) string {
s := ``
numEl := len(sl)
if numEl == 0 {
return s
}

for i, el := range sl {
el = strings.TrimSpace(el)
s += el
if i < numEl {
s += sep
}
}
return s
}
31 changes: 14 additions & 17 deletions internal/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strconv"
"strings"

tview "gitlab.com/tslocum/cview"
"github.com/gdamore/tcell/v2"
tview "gitlab.com/tslocum/cview"

"github.com/idlephysicist/cave-logger/internal/register"
"github.com/idlephysicist/cave-logger/internal/model"
"github.com/idlephysicist/cave-logger/internal/register"
)

type panels struct {
Expand All @@ -18,9 +18,9 @@ type panels struct {
}

type resources struct {
trips []*model.Log
people []*model.Caver
locations []*model.Cave
trips []*model.Log
people []*model.Caver
locations []*model.Cave
//statsLocations []*model.Statistic
}

Expand All @@ -39,19 +39,19 @@ func newState() *state {
}

type Gui struct {
app *tview.Application
pages *tview.Pages
state *state
reg *register.Register
app *tview.Application
pages *tview.Pages
state *state
reg *register.Register
//statsLocations *statsLocations
}

func New(reg *register.Register) *Gui {
return &Gui{
app: tview.NewApplication(),
app: tview.NewApplication(),
pages: tview.NewPages(),
state: newState(),
reg: reg,
reg: reg,
}
}

Expand Down Expand Up @@ -143,17 +143,16 @@ func (g *Gui) caversPanel() *cavers {
}
*/


func (g *Gui) initPanels() {

g.state.tabBar = newTabBar(g)

// Page definitions
trips := newTrips(g)
trips := newTrips(g)
cavers := newCavers(g)
caves := newCaves(g)
caves := newCaves(g)

/*
/*
// NOTE: I would really like to get this working as it would be far neater.
// The issue is with the three pages being of different types.
// cannot use pg (type panel) as type tview.Primitive in argument to g.pages.AddPage:
Expand Down Expand Up @@ -252,7 +251,6 @@ func (g *Gui) warning(message, page string, labels []string, doneFunc func()) {
g.pages.AddAndSwitchToPage("modal", g.modal(modal, 80, 29), true)
}


//
// Functions for returning the selected item in the table
// REVIEW: There might be better ways of doing this.
Expand Down Expand Up @@ -291,4 +289,3 @@ func (g *Gui) selectedPerson() *model.Caver {

return g.state.resources.people[row-1]
}

Loading

0 comments on commit 4fea37c

Please sign in to comment.