Skip to content

Commit

Permalink
typeahead: preparation for something that doesn't work rn
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Aug 16, 2024
1 parent c5f8297 commit bcd5fd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
24 changes: 17 additions & 7 deletions internal/history/inputhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,40 @@ import (
"github.com/abenz1267/walker/internal/util"
)

type InputHistory map[string][]string
type InputHistoryItem struct {
Term string
Identifier string
}

type InputHistory map[string][]InputHistoryItem

var inputhstry InputHistory

func SaveInputHistory(module string, input string) {
func SaveInputHistory(module string, input string, identifier string) {
if inputhstry == nil {
inputhstry = make(InputHistory)
}

if _, ok := inputhstry[module]; !ok {
inputhstry[module] = []string{}
inputhstry[module] = []InputHistoryItem{}
}

n := InputHistoryItem{
Term: input,
Identifier: identifier,
}

inputhstry[module] = append([]string{input}, inputhstry[module]...)
inputhstry[module] = append([]InputHistoryItem{n}, inputhstry[module]...)

util.ToGob(&inputhstry, filepath.Join(util.CacheDir(), "inputhistory_0.3.8.gob"))
util.ToGob(&inputhstry, filepath.Join(util.CacheDir(), "inputhistory_0.7.6.gob"))
}

func GetInputHistory(module string) []string {
func GetInputHistory(module string) []InputHistoryItem {
if inputhstry != nil {
return inputhstry[module]
}

file := filepath.Join(util.CacheDir(), "inputhistory_0.3.8.gob")
file := filepath.Join(util.CacheDir(), "inputhistory_0.7.6.gob")

if inputhstry == nil {
inputhstry = make(InputHistory)
Expand Down
15 changes: 9 additions & 6 deletions internal/ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func handleGlobalKeysPressed(val uint, code uint, modifier gdk.ModifierType) boo
return true
}

var inputhstry []string
var inputhstry []history.InputHistoryItem

if len(explicits) == 1 {
inputhstry = history.GetInputHistory(explicits[0].General().Name)
Expand All @@ -298,7 +298,7 @@ func handleGlobalKeysPressed(val uint, code uint, modifier gdk.ModifierType) boo
}

glib.IdleAdd(func() {
elements.input.SetText(inputhstry[historyIndex])
elements.input.SetText(inputhstry[historyIndex].Term)
elements.input.SetPosition(-1)
})
}
Expand Down Expand Up @@ -444,14 +444,16 @@ func activateItem(keepOpen, selectNext, alt bool) {
setStdin(cmd, &entry.PipedAlt)
}

identifier := entry.Identifier()

if entry.History {
hstry.Save(entry.Identifier(), strings.TrimSpace(elements.input.Text()))
hstry.Save(identifier, strings.TrimSpace(elements.input.Text()))
}

module := findModule(entry.Module, toUse, explicits)

if module != nil && (module.General().History || module.General().Typeahead) {
history.SaveInputHistory(module.General().Name, elements.input.Text())
history.SaveInputHistory(module.General().Name, elements.input.Text(), identifier)
}

err := cmd.Start()
Expand Down Expand Up @@ -754,8 +756,8 @@ func setTypeahead(modules []modules.Workable) {

if trimmed != "" {
for _, v := range tah {
if strings.HasPrefix(v, trimmed) {
toSet = v
if strings.HasPrefix(v.Term, trimmed) {
toSet = v.Term
}
}

Expand Down Expand Up @@ -839,6 +841,7 @@ func quit() {

appstate.IsRunning = false
appstate.IsSingle = false
// typeaheadSuggestionAccepted = ""
historyIndex = 0

for _, v := range toUse {
Expand Down

0 comments on commit bcd5fd6

Please sign in to comment.