Skip to content

Commit

Permalink
feat: 'source_once' for modules to avoid re-calling src when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Jul 16, 2024
1 parent 2cd3e8c commit f9b7db3
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Definition for modules:
type Module struct {
Prefix string `json:"prefix,omitempty"`
Name string `json:"name,omitempty"`
SrcOnce string `json:"src_once,omitempty"`
SrcOnceRefresh bool `json:"src_once_refresh,omitempty"`
Src string `json:"src,omitempty"`
Cmd string `json:"cmd,omitempty"`
SpecialLabel string `json:"special_label,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type Runner struct {
type Module struct {
Prefix string `json:"prefix,omitempty"`
Name string `json:"name,omitempty"`
SrcOnce string `json:"src_once,omitempty"`
SrcOnceRefresh bool `json:"src_once_refresh,omitempty"`
Src string `json:"src,omitempty"`
Cmd string `json:"cmd,omitempty"`
SpecialLabel string `json:"special_label,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions modules/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (a Applications) Setup(cfg *config.Config) Workable {
return a
}

func (a Applications) Refresh() {}

func (a Applications) Name() string {
return ApplicationsName
}
Expand Down
2 changes: 2 additions & 0 deletions modules/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (c Clipboard) SwitcherExclusive() bool {
return c.switcherExclusive
}

func (c Clipboard) Refresh() {}

type ClipboardItem struct {
Content string `json:"content,omitempty"`
Time time.Time `json:"time,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions modules/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ func (cc Commands) Setup(cfg *config.Config) Workable {

return c
}

func (c Commands) Refresh() {}
55 changes: 40 additions & 15 deletions modules/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type External struct {
ModuleName string
src string
cmd string
cachedOutput []byte
refresh bool
switcherExclusive bool
recalculateScore bool
terminal bool
Expand All @@ -28,21 +30,34 @@ func (e External) SwitcherExclusive() bool {
return e.switcherExclusive
}

func (e External) Setup(cfg *config.Config) Workable {
func (e *External) Setup(cfg *config.Config) Workable {
module := Find(cfg.External, e.Name())
if module == nil {
return nil
}

e.prefix = module.Prefix
e.switcherExclusive = module.SwitcherExclusive
e.src = module.Src
e.cmd = module.Cmd
e.src = os.ExpandEnv(module.Src)
e.cmd = os.ExpandEnv(module.Cmd)
e.terminal = module.Terminal

if module.SrcOnce != "" {
e.src = os.ExpandEnv(module.SrcOnce)
e.cachedOutput = e.getSrcOutput(false, "")
}

e.refresh = module.SrcOnceRefresh

return e
}

func (e *External) Refresh() {
if e.refresh {
e.cachedOutput = e.getSrcOutput(false, "")
}
}

func (e External) Name() string {
return e.ModuleName
}
Expand Down Expand Up @@ -78,20 +93,13 @@ func (e External) Entries(ctx context.Context, term string) []Entry {
hasExplicitResult = true
}

e.src = os.ExpandEnv(e.src)

if e.cmd != "" {
name, args := util.ParseShellCommand(e.src)
cmd := exec.Command(name, args...)

if !hasExplicitTerm {
cmd.Stdin = strings.NewReader(term)
}
var out []byte

out, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
return entries
if e.cachedOutput != nil {
out = e.cachedOutput
} else {
out = e.getSrcOutput(hasExplicitTerm, term)
}

scanner := bufio.NewScanner(strings.NewReader(string(out)))
Expand Down Expand Up @@ -150,3 +158,20 @@ func (e External) Entries(ctx context.Context, term string) []Entry {

return entries
}

func (e External) getSrcOutput(hasExplicitTerm bool, term string) []byte {
name, args := util.ParseShellCommand(e.src)
cmd := exec.Command(name, args...)

if !hasExplicitTerm && term != "" {
cmd.Stdin = strings.NewReader(term)
}

out, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
return nil
}

return out
}
2 changes: 2 additions & 0 deletions modules/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Finder struct {
switcherExclusive bool
}

func (f Finder) Refresh() {}

func (f Finder) Entries(ctx context.Context, term string) []Entry {
e := []Entry{}

Expand Down
2 changes: 2 additions & 0 deletions modules/hyprland.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func (h Hyprland) Setup(cfg *config.Config) Workable {
return b
}

func (h Hyprland) Refresh() {}

func (h *Hyprland) monitorWindows() {
for {
clear(h.windows)
Expand Down
2 changes: 2 additions & 0 deletions modules/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (r Runner) Setup(cfg *config.Config) Workable {
return r
}

func (r Runner) Refresh() {}

func (r Runner) Prefix() string {
return r.prefix
}
Expand Down
2 changes: 2 additions & 0 deletions modules/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type SSH struct {
entries []Entry
}

func (s SSH) Refresh() {}

func (s SSH) Entries(ctx context.Context, term string) []Entry {
fields := strings.Fields(term)

Expand Down
2 changes: 2 additions & 0 deletions modules/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ func (s Switcher) Setup(cfg *config.Config) Workable {

return s
}

func (s Switcher) Refresh() {}
1 change: 1 addition & 0 deletions modules/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Workable interface {
Name() string
SwitcherExclusive() bool
Setup(cfg *config.Config) Workable
Refresh()
}

type MatchingType int
Expand Down
2 changes: 2 additions & 0 deletions modules/websearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (w Websearch) Setup(cfg *config.Config) Workable {
return w
}

func (w Websearch) Refresh() {}

func (w Websearch) Prefix() string {
return w.prefix
}
Expand Down
2 changes: 1 addition & 1 deletion ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func setupModules() {
}

for _, v := range cfg.External {
e := modules.External{
e := &modules.External{
ModuleName: v.Name,
}

Expand Down
6 changes: 6 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func Activate(state *state.AppState) func(app *gtk.Application) {
if appstate.HasUI {
ui.appwin.SetVisible(true)

for _, v := range procs {
for _, proc := range v {
proc.Refresh()
}
}

if !appstate.IsMeasured {
fmt.Printf("startup time: %s\n", time.Since(appstate.Started))
appstate.IsMeasured = true
Expand Down

0 comments on commit f9b7db3

Please sign in to comment.