Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 7, 2023
1 parent 90986c1 commit 7dd4391
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
10 changes: 9 additions & 1 deletion carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package carapace

import (
"os"
"sync"

"github.com/rsteube/carapace/internal/shell"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -70,9 +71,16 @@ func (c Carapace) DashAnyCompletion(action Action) {
storage.get(c.cmd).dashAny = action
}

// TODO move to storage
var flagMutex sync.RWMutex

Check failure on line 75 in carapace.go

View workflow job for this annotation

GitHub Actions / lint

var `flagMutex` is unused (unused)

// FlagCompletion defines completion for flags using a map consisting of name and Action.
func (c Carapace) FlagCompletion(actions ActionMap) {
if e := storage.get(c.cmd); e.flag == nil {
e := storage.get(c.cmd)
e.flagMutex.Lock()
defer e.flagMutex.Unlock()

if e.flag == nil {
e.flag = actions
} else {
for name, action := range actions {
Expand Down
7 changes: 5 additions & 2 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

type entry struct {
flag ActionMap
flagMutex sync.RWMutex
positional []Action
positionalAny Action
dash []Action
Expand All @@ -37,8 +38,10 @@ func (s _storage) get(cmd *cobra.Command) *entry {
if !ok {
storageMutex.Lock()
defer storageMutex.Unlock()
e = &entry{}
s[cmd] = e
if e, ok = s[cmd]; !ok {
e = &entry{}
s[cmd] = e
}
}
return e
}
Expand Down
12 changes: 6 additions & 6 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func BenchmarkStorage(b *testing.B) {
cmd2 := &cobra.Command{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
// Gen(cmd).FlagCompletion(ActionMap{
// "flag1": ActionValues("a", "b"),
// })
Gen(cmd).FlagCompletion(ActionMap{
"flag1": ActionValues("a", "b"),
})
Gen(cmd).PositionalCompletion(ActionValues("a", "b"))

// Gen(cmd2).FlagCompletion(ActionMap{
// "flag2": ActionValues("a", "b"),
// })
Gen(cmd2).FlagCompletion(ActionMap{
"flag2": ActionValues("a", "b"),
})
Gen(cmd2).PositionalCompletion(ActionValues("a", "b"))
}
})
Expand Down

0 comments on commit 7dd4391

Please sign in to comment.