Skip to content

Commit

Permalink
websearch: switch to more flexible solution, better control
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Nov 23, 2024
1 parent 2af17a3 commit 581f0d3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
2 changes: 1 addition & 1 deletion cmd/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.7-git
0.9.7
22 changes: 21 additions & 1 deletion internal/config/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,27 @@
"icon": "applications-internet",
"name": "websearch",
"placeholder": "Websearch",
"engines": ["google"]
"entries": [
{
"name": "Google",
"url": "https://www.google.com/search?q=%TERM%"
},
{
"name": "DuckDuckGo",
"url": "https://duckduckgo.com/?q=%TERM%",
"switcher_only": true
},
{
"name": "Ecosia",
"url": "https://www.ecosia.org/search?q=%TERM%",
"switcher_only": true
},
{
"name": "Yandex",
"url": "https://yandex.com/search/?text=%TERM%",
"switcher_only": true
}
]
},
"dmenu": {
"weight": 5,
Expand Down
9 changes: 8 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ type SSH struct {

type Websearch struct {
GeneralModule `mapstructure:",squash"`
Engines []string `mapstructure:"engines"`
Entries []WebsearchEntry `mapstructure:"entries"`
}

type WebsearchEntry struct {
Name string `mapstructure:"name"`
Url string `mapstructure:"url"`
Prefix string `mapstructure:"prefix"`
SwitcherOnly bool `mapstructure:"switcher_only"`
}

type Applications struct {
Expand Down
84 changes: 39 additions & 45 deletions internal/modules/websearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@ import (
"net/http"
"net/url"
"os/exec"
"slices"
"strings"
"time"

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

const (
GoogleURL = "https://www.google.com/search?q=%TERM%"
DuckDuckGoURL = "https://duckduckgo.com/?q=%TERM%"
EcosiaURL = "https://www.ecosia.org/search?q=%TERM%"
YandexURL = "https://yandex.com/search/?text=%TERM%"
)

type Websearch struct {
config config.Websearch
engineInfo map[string]EngineInfo
threshold int
config config.Websearch
threshold int
prefixes []string
}

type EngineInfo struct {
Expand All @@ -47,32 +39,14 @@ func (w *Websearch) Setup(cfg *config.Config) bool {
}

func (w *Websearch) SetupData(_ *config.Config, ctx context.Context) {
slices.Reverse(w.config.Engines)

w.engineInfo = make(map[string]EngineInfo)

w.engineInfo["google"] = EngineInfo{
Label: "Google",
URL: GoogleURL,
}

w.engineInfo["duckduckgo"] = EngineInfo{
Label: "DuckDuckGo",
URL: DuckDuckGoURL,
}
w.config.IsSetup = true
w.config.HasInitialSetup = true

w.engineInfo["ecosia"] = EngineInfo{
Label: "Ecosia",
URL: EcosiaURL,
}
w.prefixes = []string{}

w.engineInfo["yandex"] = EngineInfo{
Label: "Yandex",
URL: YandexURL,
for _, v := range w.config.Entries {
w.prefixes = append(w.prefixes, v.Prefix)
}

w.config.IsSetup = true
w.config.HasInitialSetup = true
}

func (w *Websearch) Refresh() {
Expand All @@ -90,20 +64,40 @@ func (w Websearch) Entries(ctx context.Context, term string) []util.Entry {

term = strings.TrimPrefix(term, w.config.Prefix)

for k, v := range w.config.Engines {
if val, ok := w.engineInfo[strings.ToLower(v)]; ok {
url := strings.ReplaceAll(val.URL, "%TERM%", url.QueryEscape(term))
prefix := ""

for _, v := range w.prefixes {
if strings.HasPrefix(term, v) {
prefix = v
break
}
}

n := util.Entry{
Label: fmt.Sprintf("Search with %s", val.Label),
Sub: "Websearch",
Exec: fmt.Sprintf("xdg-open %s", url),
Class: "websearch",
ScoreFinal: float64(k + 1 + w.threshold),
}
term = strings.TrimPrefix(term, prefix)

entries = append(entries, n)
for k, v := range w.config.Entries {
if prefix != "" && v.Prefix != prefix {
continue
}

url := strings.ReplaceAll(v.Url, "%TERM%", url.QueryEscape(term))

score := float64(k + 1 + w.threshold)

if prefix != "" {
score = 1000000
}

n := util.Entry{
Label: fmt.Sprintf("Search with %s", v.Name),
Sub: "Websearch",
Exec: fmt.Sprintf("xdg-open %s", url),
Class: "websearch",
ScoreFinal: score,
SingleModuleOnly: v.SwitcherOnly && prefix == "",
}

entries = append(entries, n)
}

if strings.ContainsAny(term, ".") && !strings.HasSuffix(term, ".") {
Expand Down

0 comments on commit 581f0d3

Please sign in to comment.