Desktop application launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.0 KiB

package main
import (
"flag"
"fmt"
"strings"
"code.rocketnine.space/tslocum/cview"
"code.rocketnine.space/tslocum/desktop"
"code.rocketnine.space/tslocum/gmenu/pkg/gmenu"
"github.com/kballard/go-shellquote"
)
func updateEntries(input string) {
gmenu.FilterEntries()
entryList.Clear()
entryList.SetCurrentItem(0)
entryList.SetOffset(0, 0)
for _, entry := range gmenu.FilteredEntries {
entryList.AddItem(cview.NewListItem(entry.Label))
}
if input != "" {
entryList.AddItem(cview.NewListItem(input))
}
app.QueueUpdateDraw(updateEntryInfo)
}
func selectedIndex() int {
if entryList == nil {
return -1
}
return entryList.GetCurrentItemIndex()
}
func selectedEntry() *desktop.Entry {
i := selectedIndex()
if len(gmenu.FilteredEntries) == 0 || i < 0 || i > len(gmenu.FilteredEntries)-1 {
return nil
}
return gmenu.FilteredEntries[i].Entry
}
func updateEntryInfo() {
if config.HideAppDetails {
return
}
var exLine, comLine string
entry := selectedEntry()
if entry != nil {
if entry.Type == desktop.Application {
exLine = entry.Exec
} else { // Type == desktop.Link
exLine = config.BrowserCommand() + " " + entry.URL
}
comLine = entry.Comment
} else {
exLine = fmt.Sprintf("bash -c %s", strings.TrimSpace(inputView.GetText()))
comLine = "Shell command"
}
detailView.SetText(exLine + "\n\n" + comLine)
}
func listSelect(runInTerminal bool) error {
defer closeTUI()
var (
execute string
waitUntilFinished bool
)
entry := selectedEntry()
if entry == nil {
waitUntilFinished = true
execute = inputView.GetText()
} else if entry.Type == desktop.Application {
if entry.Terminal {
runInTerminal = true
}
execute = entry.ExpandExec(shellquote.Join(flag.Args()...))
} else { // Type == desktop.Link
execute = shellquote.Join(config.BrowserCommand(), entry.URL)
}
closeTUI()
path := ""
if entry != nil {
path = entry.Path
}
return gmenu.Run(&config.Config, execute, path, runInTerminal, waitUntilFinished)
}