Skip to content

Commit

Permalink
feat: dmenu-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Jul 18, 2024
1 parent 60044d7 commit 433f3d6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
_ "embed"
"fmt"
"log"
Expand All @@ -12,6 +13,7 @@ import (
"time"

"github.com/abenz1267/walker/config"
"github.com/abenz1267/walker/modules"
"github.com/abenz1267/walker/state"
"github.com/abenz1267/walker/ui"
"github.com/abenz1267/walker/util"
Expand Down Expand Up @@ -47,6 +49,20 @@ func main() {
case "-c", "--config":
case "-s", "--style":
case "-m", "--modules":
case "-d", "--dmenu":
forceNew = true

dmenu := modules.Dmenu{
Content: []string{},
}

scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
dmenu.Content = append(dmenu.Content, scanner.Text())
}

state.Dmenu = &dmenu
case "--version":
fmt.Println(version)
return
Expand Down Expand Up @@ -98,6 +114,7 @@ func main() {

app.AddMainOption("modules", 'm', glib.OptionFlagNone, glib.OptionArgString, "modules to be loaded", "the modules")
app.AddMainOption("new", 'n', glib.OptionFlagNone, glib.OptionArgNone, "start new instance ignoring service", "")
app.AddMainOption("dmenu", 'd', glib.OptionFlagNone, glib.OptionArgNone, "run in dmenu mode", "")
app.AddMainOption("config", 'c', glib.OptionFlagNone, glib.OptionArgString, "config file to use", "")
app.AddMainOption("style", 's', glib.OptionFlagNone, glib.OptionArgString, "style file to use", "")

Expand Down
44 changes: 44 additions & 0 deletions modules/dmenu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package modules

import (
"context"
"fmt"

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

type Dmenu struct {
Content []string
}

func (d Dmenu) Entries(ctx context.Context, term string) []Entry {
entries := []Entry{}

for _, v := range d.Content {
entries = append(entries, Entry{
Label: v,
Sub: "Dmenu",
Exec: fmt.Sprintf("echo '%s'", v),
})
}

return entries
}

func (Dmenu) Prefix() string {
return ""
}

func (Dmenu) Name() string {
return "dmenu"
}

func (Dmenu) SwitcherExclusive() bool {
return false
}

func (d Dmenu) Setup(cfg *config.Config, config *config.Module) Workable {
return d
}

func (Dmenu) Refresh() {}
1 change: 1 addition & 0 deletions state/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type AppState struct {
IsRunning bool
HasUI bool
Clipboard modules.Workable
Dmenu modules.Workable
ExplicitModules []string
ExplicitConfig string
ExplicitStyle string
Expand Down
71 changes: 45 additions & 26 deletions ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"bytes"
"context"
"fmt"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -59,28 +60,38 @@ func setupCommands() {
func setupModules() {
util.FromGob(filepath.Join(util.CacheDir(), "typeahead.gob"), &tah)

internals := []modules.Workable{
modules.Applications{},
modules.Runner{ShellConfig: cfg.ShellConfig},
modules.Websearch{},
modules.Commands{},
modules.Hyprland{},
modules.SSH{},
modules.Finder{},
emojis.Emojis{},
appstate.Clipboard,
var internals []modules.Workable

if appstate.Dmenu != nil {
internals = []modules.Workable{
appstate.Dmenu,
}
} else {
internals = []modules.Workable{
modules.Applications{},
modules.Runner{ShellConfig: cfg.ShellConfig},
modules.Websearch{},
modules.Commands{},
modules.Hyprland{},
modules.SSH{},
modules.Finder{},
emojis.Emojis{},
appstate.Clipboard,
}
}

externals := make(map[string]struct{})

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

externals[e.Name()] = struct{}{}
externals[e.Name()] = struct{}{}

internals = append(internals, e)
internals = append(internals, e)
}
}

clear(procs)
Expand All @@ -102,7 +113,7 @@ func setupModules() {
s = modules.Find(cfg.External, v.Name())
}

if s == nil {
if s == nil && appstate.Dmenu == nil {
continue
}

Expand All @@ -112,16 +123,18 @@ func setupModules() {
}
}

// setup switcher individually
switcher := modules.Switcher{Procs: procs}
if appstate.Dmenu == nil {
// setup switcher individually
switcher := modules.Switcher{Procs: procs}

module := modules.Find(cfg.Modules, switcher.Name())
module := modules.Find(cfg.Modules, switcher.Name())

if module != nil {
s := switcher.Setup(cfg, module)
if module != nil {
s := switcher.Setup(cfg, module)

if s != nil {
procs[s.Prefix()] = append(procs[s.Prefix()], s)
if s != nil {
procs[s.Prefix()] = append(procs[s.Prefix()], s)
}
}
}

Expand Down Expand Up @@ -454,6 +467,12 @@ func activateItem(keepOpen, selectNext, alt bool) {

entry := gioutil.ObjectValue[modules.Entry](ui.items.Item(ui.selection.Selected()))

if appstate.Dmenu != nil {
fmt.Println(entry.Label)
closeAfterActivation(keepOpen, selectNext)
return
}

if entry.Sub == "Walker" {
commands[entry.Exec]()
closeAfterActivation(keepOpen, selectNext)
Expand Down Expand Up @@ -603,15 +622,15 @@ func process() {

text := strings.TrimSpace(ui.search.Text())

if text == "" && cfg.ShowInitialEntries && singleProc == nil && len(appstate.ExplicitModules) == 0 {
if text == "" && cfg.ShowInitialEntries && singleProc == nil && len(appstate.ExplicitModules) == 0 && appstate.Dmenu == nil {
setInitials()
return
}

var ctx context.Context
ctx, cancel = context.WithCancel(context.Background())

if (ui.search.Text() != "" || singleProc != nil) || (len(appstate.ExplicitModules) > 0 && cfg.ShowInitialEntries) {
if (ui.search.Text() != "" || singleProc != nil || appstate.Dmenu != nil) || (len(appstate.ExplicitModules) > 0 && cfg.ShowInitialEntries) {
go processAsync(ctx)
} else {
ui.items.Splice(0, int(ui.items.NItems()))
Expand Down
4 changes: 2 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Activate(state *state.AppState) func(app *gtk.Application) {
}
}

if !appstate.IsMeasured {
if !appstate.IsMeasured && appstate.Dmenu == nil {
fmt.Printf("startup time: %s\n", time.Since(appstate.Started))
appstate.IsMeasured = true
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func setupUI(app *gtk.Application) {

fc := gtk.NewEventControllerFocus()
fc.Connect("enter", func() {
if !appstate.IsMeasured {
if !appstate.IsMeasured && appstate.Dmenu == nil {
fmt.Printf("startup time: %s\n", time.Since(appstate.Started))
appstate.IsMeasured = true
}
Expand Down

0 comments on commit 433f3d6

Please sign in to comment.