Skip to content

Commit

Permalink
minor: expose matching to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Jul 22, 2024
1 parent b62fda9 commit da33d61
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 64 deletions.
21 changes: 11 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ type CustomCommand struct {
}

type GeneralModule struct {
IsSetup bool `mapstructure:"-"`
Placeholder string `mapstructure:"placeholder"`
SpecialLabel string `mapstructure:"special_label"`
Prefix string `mapstructure:"prefix"`
SpecialLabel string `mapstructure:"special_label"`
SwitcherOnly bool `mapstructure:"switcher_only"`
IsSetup bool `mapstructure:"-"`
}

type Finder struct {
Expand Down Expand Up @@ -124,14 +124,15 @@ type Runner struct {

type Plugin struct {
GeneralModule `mapstructure:",squash"`
Name string `mapstructure:"name"`
SrcOnce string `mapstructure:"src_once"`
SrcOnceRefresh bool `mapstructure:"src_once_refresh"`
Src string `mapstructure:"src"`
Cmd string `mapstructure:"cmd"`
CmdAlt string `mapstructure:"cmd_alt"`
Terminal bool `mapstructure:"terminal"`
KeepSort bool `mapstructure:"keep_sort"`
Name string `mapstructure:"name"`
SrcOnce string `mapstructure:"src_once"`
SrcOnceRefresh bool `mapstructure:"src_once_refresh"`
Src string `mapstructure:"src"`
Cmd string `mapstructure:"cmd"`
CmdAlt string `mapstructure:"cmd_alt"`
Terminal bool `mapstructure:"terminal"`
KeepSort bool `mapstructure:"keep_sort"`
Matching util.MatchingType `mapstructure:"matching"`
}

type Search struct {
Expand Down
2 changes: 1 addition & 1 deletion modules/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func parse(cache bool, actions bool) []Entry {
Generic: Entry{
Class: ApplicationsName,
History: true,
Matching: Fuzzy,
Matching: util.Fuzzy,
RecalculateScore: true,
},
Actions: []Entry{},
Expand Down
2 changes: 1 addition & 1 deletion modules/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c Clipboard) Entries(ctx context.Context, term string) []modules.Entry {
Piped: modules.Piped{Content: v.Content, Type: "string"},
Categories: []string{"clipboard"},
Class: "clipboard",
Matching: modules.Fuzzy,
Matching: util.Fuzzy,
LastUsed: v.Time,
}

Expand Down
3 changes: 2 additions & 1 deletion modules/customcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
)

type CustomCommands struct {
Expand Down Expand Up @@ -53,7 +54,7 @@ func (c *CustomCommands) SetupData(cfg *config.Config) {
Exec: v.Cmd,
ExecAlt: v.CmdAlt,
Terminal: v.Terminal,
Matching: Fuzzy,
Matching: util.Fuzzy,
RecalculateScore: true,
})
}
Expand Down
3 changes: 2 additions & 1 deletion modules/emojis/emojis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/modules"
"github.com/abenz1267/walker/util"
)

//go:embed list.csv
Expand Down Expand Up @@ -71,7 +72,7 @@ func (e *Emojis) SetupData(cfg *config.Config) {
Searchable: fields[5],
Categories: []string{fields[0], fields[1]},
Class: "emojis",
Matching: modules.Fuzzy,
Matching: util.Fuzzy,
RecalculateScore: true,
})
}
Expand Down
3 changes: 2 additions & 1 deletion modules/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
"github.com/boyter/gocodewalker"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ func (f Finder) Entries(ctx context.Context, term string) []Entry {
DragDropData: f.Location,
Categories: []string{"finder", "fzf"},
Class: "finder",
Matching: Fuzzy,
Matching: util.Fuzzy,
})
}

Expand Down
3 changes: 2 additions & 1 deletion modules/hyprland.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
)

type Hyprland struct {
Expand Down Expand Up @@ -173,7 +174,7 @@ func (Hyprland) Entries(ctx context.Context, term string) []Entry {
Categories: []string{"hyprland", "windows", fmt.Sprintf("workspace %s", v.workspace), fmt.Sprintf("ws %s", v.workspace), v.initialTitle},
Class: "hyprland",
History: false,
Matching: Fuzzy,
Matching: util.Fuzzy,
}

entries = append(entries, n)
Expand Down
1 change: 1 addition & 0 deletions modules/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (e Plugin) Entries(ctx context.Context, term string) []Entry {
Terminal: e.General.Terminal,
Exec: strings.ReplaceAll(e.General.Cmd, "%RESULT%", txt),
ExecAlt: strings.ReplaceAll(e.General.CmdAlt, "%RESULT%", txt),
Matching: e.General.Matching,
}

if !hasExplicitResult {
Expand Down
5 changes: 3 additions & 2 deletions modules/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
)

type SSH struct {
Expand Down Expand Up @@ -121,7 +122,7 @@ func getConfigFileEntries(sshCfg string) []Entry {
Terminal: true,
Categories: []string{"ssh"},
Class: "ssh",
Matching: Fuzzy,
Matching: util.Fuzzy,
RecalculateScore: true,
})
}
Expand Down Expand Up @@ -158,7 +159,7 @@ func getHostFileEntries(hosts string) []Entry {
Terminal: true,
Categories: []string{"ssh"},
Class: "ssh",
Matching: Fuzzy,
Matching: util.Fuzzy,
RecalculateScore: true,
})
}
Expand Down
3 changes: 2 additions & 1 deletion modules/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
)

type Switcher struct {
Expand Down Expand Up @@ -41,7 +42,7 @@ func (s Switcher) Entries(ctx context.Context, term string) []Entry {
Exec: "",
Categories: []string{"switcher"},
Class: "switcher",
Matching: Fuzzy,
Matching: util.Fuzzy,
}

entries = append(entries, e)
Expand Down
65 changes: 29 additions & 36 deletions modules/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
)

type Workable interface {
Expand All @@ -30,43 +31,35 @@ type Workable interface {
KeepSort() bool
}

type MatchingType int

const (
Fuzzy MatchingType = iota
AlwaysTop
AlwaysBottom
)

type Entry struct {
Label string `json:"label,omitempty"`
Sub string `json:"sub,omitempty"`
Exec string `json:"exec,omitempty"`
ExecAlt string `json:"exec_alt,omitempty"`
Terminal bool `json:"terminal,omitempty"`
Piped Piped `json:"-"`
PipedAlt Piped `json:"-"`
Icon string `json:"icon,omitempty"`
IconIsImage bool `json:"icon_is_image,omitempty"`
DragDrop bool `json:"drag_drop,omitempty"`
DragDropData string `json:"drag_drop_data,omitempty"`
Image string `json:"image,omitempty"`
HideText bool `json:"hide_text,omitempty"`
Categories []string `json:"categories,omitempty"`
Searchable string `json:"searchable,omitempty"`
MatchFields int `json:"match_fields,omitempty"`
Class string `json:"class,omitempty"`
History bool `json:"history,omitempty"`
Matching MatchingType `json:"matching,omitempty"`
RecalculateScore bool `json:"recalculate_score,omitempty"`
ScoreFinal float64 `json:"score_final,omitempty"`
ScoreFuzzy float64 `json:"score_fuzzy,omitempty"`
Used int `json:"-"`
DaysSinceUsed int `json:"-"`
SpecialLabel string `json:"special_label,omitempty"`
LastUsed time.Time `json:"-"`
InitialClass string `json:"initial_class,omitempty"`
OpenWindows uint `json:"-"`
Label string `json:"label,omitempty"`
Sub string `json:"sub,omitempty"`
Exec string `json:"exec,omitempty"`
ExecAlt string `json:"exec_alt,omitempty"`
Terminal bool `json:"terminal,omitempty"`
Piped Piped `json:"-"`
PipedAlt Piped `json:"-"`
Icon string `json:"icon,omitempty"`
IconIsImage bool `json:"icon_is_image,omitempty"`
DragDrop bool `json:"drag_drop,omitempty"`
DragDropData string `json:"drag_drop_data,omitempty"`
Image string `json:"image,omitempty"`
HideText bool `json:"hide_text,omitempty"`
Categories []string `json:"categories,omitempty"`
Searchable string `json:"searchable,omitempty"`
MatchFields int `json:"match_fields,omitempty"`
Class string `json:"class,omitempty"`
History bool `json:"history,omitempty"`
Matching util.MatchingType `json:"matching,omitempty"`
RecalculateScore bool `json:"recalculate_score,omitempty"`
ScoreFinal float64 `json:"score_final,omitempty"`
ScoreFuzzy float64 `json:"score_fuzzy,omitempty"`
Used int `json:"-"`
DaysSinceUsed int `json:"-"`
SpecialLabel string `json:"special_label,omitempty"`
LastUsed time.Time `json:"-"`
InitialClass string `json:"initial_class,omitempty"`
OpenWindows uint `json:"-"`
}

func (e Entry) Identifier() string {
Expand Down
3 changes: 2 additions & 1 deletion modules/websearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/util"
)

const (
Expand Down Expand Up @@ -140,7 +141,7 @@ func (w Websearch) Entries(ctx context.Context, term string) []Entry {
Sub: "Websearch",
Exec: "xdg-open https://" + term,
Class: "websearch",
Matching: AlwaysTop,
Matching: util.AlwaysTop,
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions ui/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"

"github.com/abenz1267/walker/modules"
"github.com/abenz1267/walker/util"
"github.com/diamondburned/gotk4/pkg/glib/v2"
)

Expand Down Expand Up @@ -53,19 +54,19 @@ func (h *Handler) handle() {

func sortEntries(entries []modules.Entry) {
slices.SortFunc(entries, func(a, b modules.Entry) int {
if a.Matching == modules.AlwaysTop && b.Matching != modules.AlwaysTop {
if a.Matching == util.AlwaysTop && b.Matching != util.AlwaysTop {
return -1
}

if b.Matching == modules.AlwaysTop && a.Matching != modules.AlwaysTop {
if b.Matching == util.AlwaysTop && a.Matching != util.AlwaysTop {
return 1
}

if a.Matching == modules.AlwaysBottom && b.Matching != modules.AlwaysBottom {
if a.Matching == util.AlwaysBottom && b.Matching != util.AlwaysBottom {
return 1
}

if b.Matching == modules.AlwaysBottom && a.Matching != modules.AlwaysBottom {
if b.Matching == util.AlwaysBottom && a.Matching != util.AlwaysBottom {
return -1
}

Expand Down
6 changes: 3 additions & 3 deletions ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,13 @@ func processAsync(ctx context.Context, text string) {

if e[k].ScoreFinal == 0 {
switch e[k].Matching {
case modules.Fuzzy:
case util.Fuzzy:
e[k].ScoreFinal = fuzzyScore(e[k], toMatch, hyprland)
case modules.AlwaysTop:
case util.AlwaysTop:
if e[k].ScoreFinal == 0 {
e[k].ScoreFinal = 1000
}
case modules.AlwaysBottom:
case util.AlwaysBottom:
if e[k].ScoreFinal == 0 {
e[k].ScoreFinal = 1
}
Expand Down
9 changes: 9 additions & 0 deletions util/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package util

type MatchingType int

const (
Fuzzy MatchingType = iota
AlwaysTop
AlwaysBottom
)
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.6-git
0.3.6

0 comments on commit da33d61

Please sign in to comment.