Initial commit

This commit is contained in:
Trevor Slocum 2019-06-26 09:00:25 -07:00
commit 6c49005342
12 changed files with 469 additions and 0 deletions

27
.builds/amd64_freebsd.yml Normal file
View File

@ -0,0 +1,27 @@
arch: amd64
environment:
PROJECT_DIR: '~/go/src/git.sr.ht/~tslocum'
PROJECT_NAME: 'gmenu'
CGO_ENABLED: '0'
GO111MODULE: 'on'
image: freebsd/latest
packages:
- go
sources:
- https://git.sr.ht/~tslocum/gmenu
tasks:
- setup:
- mkdir -p $PROJECT_DIR
- mv $PROJECT_NAME $PROJECT_DIR/$PROJECT_NAME
- test-gmenu:
- cd $PROJECT_DIR/$PROJECT_NAME/cmd/gmenu
- go test
- test-gtkmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gtkmenu
go test
- build-gmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gmenu
go build
- build-gtkmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gtkmenu
go build

View File

@ -0,0 +1,27 @@
arch: x86_64
environment:
PROJECT_DIR: '~/go/src/git.sr.ht/~tslocum'
PROJECT_NAME: 'gmenu'
CGO_ENABLED: '0'
GO111MODULE: 'on'
image: alpine/edge
packages:
- go
sources:
- https://git.sr.ht/~tslocum/gmenu
tasks:
- setup: |
mkdir -p $PROJECT_DIR
mv $PROJECT_NAME $PROJECT_DIR/$PROJECT_NAME
- test-gmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gmenu
go test
- test-gtkmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gtkmenu
go test
- build-gmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gmenu
go build
- build-gtkmenu: |
cd $PROJECT_DIR/$PROJECT_NAME/cmd/gtkmenu
go build

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.idea/
dist/
*.sh
gmenu
!cmd/gmenu/
gtkmenu
vendor/

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Trevor Slocum <trevor@rocketnine.space>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16
README.md Normal file
View File

@ -0,0 +1,16 @@
# gmenu
[![GoDoc](https://godoc.org/git.sr.ht/~tslocum/gmenu?status.svg)](https://godoc.org/git.sr.ht/~tslocum/gmenu)
[![builds.sr.ht status](https://builds.sr.ht/~tslocum/gmenu.svg)](https://builds.sr.ht/~tslocum/gmenu)
[![Donate](https://img.shields.io/liberapay/receives/rocketnine.space.svg?logo=liberapay)](https://liberapay.com/rocketnine.space)
Desktop application launcher
## Warning: Experimental
The only platform currently supported is Linux. Windows support is planned.
Only a terminal interface has been implemented. A GTK interface is planned.
## Installation
```go get git.sr.ht/~tslocum/gmenu/cmd/gmenu```

119
cmd/gmenu/main.go Normal file
View File

@ -0,0 +1,119 @@
// gmenu - Desktop application launcher
// https://git.sr.ht/~tslocum/gmenu
package main
import (
"flag"
"log"
"os"
"os/exec"
"path"
"strings"
"git.sr.ht/~tslocum/gmenu/pkg/dmenu"
"github.com/tslocum/promptui"
)
func init() {
log.SetFlags(0)
log.SetPrefix("gmenu: ")
promptui.SearchPrompt = ""
}
func main() {
flag.Parse()
var dataDirs []string
dataDirsSetting := strings.Split(os.Getenv("XDG_DATA_DIRS"), ":")
for _, dataDir := range dataDirsSetting {
dataDir = strings.TrimSpace(dataDir)
if dataDir == "" {
continue
}
dataDirs = append(dataDirs, dataDir)
}
if len(dataDirs) == 0 {
dataDirs = []string{"/usr/local/share", "/usr/share"}
}
homeDir := os.Getenv("HOME")
if strings.TrimSpace(homeDir) == "" {
homeDir = "~/"
}
dataHomeSetting := os.Getenv("XDG_DATA_HOME")
if dataHomeSetting == "" {
dataHomeSetting = path.Join(homeDir, ".local/share")
}
dataDirs = append(dataDirs, dataHomeSetting)
desktopEntries, err := dmenu.ScanEntries(dataDirs)
if err != nil {
log.Fatal(err)
}
searcher := func(input string, index int) bool {
entry := desktopEntries[index]
name := strings.Replace(strings.ToLower(entry.Name), " ", "", -1)
genericName := strings.Replace(strings.ToLower(entry.GenericName), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input) || strings.Contains(genericName, input)
}
templates := &promptui.SelectTemplates{
Label: "Launch:",
Active: ">{{ .Name }}",
Inactive: " {{ .Name }}",
Selected: "gmenu: Launching {{ .Name }}...",
Details: `{{ "Name(s)" | faint }} {{ .Name }}{{ if .GenericName }}, {{ .GenericName }}{{ end }}
{{ "Comment" | faint }} {{ .Comment }}
{{ "Exec" | faint }} {{ .Exec }}`,
Help: "",
}
prompt := promptui.Select{
Items: desktopEntries,
IsVimMode: false,
Templates: templates,
Searcher: searcher,
StartInSearchMode: true,
}
selected, _, err := prompt.Run()
if err != nil {
log.Fatal(err)
}
if selected < 0 {
return
}
entry := desktopEntries[selected]
// TODO: Support field codes
execSplit := strings.Split(entry.Exec, " ")
for i, arg := range execSplit {
if arg == "%F" || arg == "%f" || arg == "%U" || arg == "%u" {
execSplit[i] = ""
}
}
var args []string
if len(execSplit) > 1 {
args = execSplit[1:]
}
cmd := exec.Command(execSplit[0], args...)
if entry.Terminal {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
} else {
err = cmd.Start()
}
if err != nil {
log.Fatal(err)
}
}

25
cmd/gmenu/readline.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"os"
"github.com/chzyer/readline"
)
type filteredStdOut struct{}
func (s *filteredStdOut) Write(b []byte) (int, error) {
if len(b) == 1 && b[0] == 7 {
// ignore terminal bell from readline
return 0, nil
}
return os.Stdout.Write(b)
}
func (s *filteredStdOut) Close() error {
return os.Stdout.Close()
}
func init() {
readline.Stdout = &filteredStdOut{}
}

10
go.mod Normal file
View File

@ -0,0 +1,10 @@
module git.sr.ht/~tslocum/gmenu
go 1.12
require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/kr/pty v1.1.5 // indirect
github.com/mattn/go-isatty v0.0.8 // indirect
github.com/tslocum/promptui v0.3.3-0.20190626143017-6cce979adef8
)

41
go.sum Normal file
View File

@ -0,0 +1,41 @@
github.com/alecthomas/gometalinter v2.0.11+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk=
github.com/alecthomas/gometalinter v2.0.12+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk=
github.com/alecthomas/gometalinter v3.0.0+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc/go.mod h1:cuNKsD1zp2v6XfE/orVX2QE1LC+i254ceGcVeDT3pTU=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/manifoldco/promptui v0.3.2 h1:rir7oByTERac6jhpHUPErHuopoRDvO3jxS+FdadEns8=
github.com/manifoldco/promptui v0.3.2/go.mod h1:8JU+igZ+eeiiRku4T5BjtKh2ms8sziGpSYl1gN8Bazw=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYvZ+fpjMXqs+XEriussHjSYqeXVnAdSV1tkMYk=
github.com/tslocum/promptui v0.3.3-0.20190626142258-8da0c0f6cdc6 h1:ApJCfIVMWJ1AwC0IkFO6vx7B2pkGkaearWQdkZtb/5s=
github.com/tslocum/promptui v0.3.3-0.20190626142258-8da0c0f6cdc6/go.mod h1:ib2idig9p59EUDQJwRIN3/JHD8ThqsUu9fnPuA4zspA=
github.com/tslocum/promptui v0.3.3-0.20190626143017-6cce979adef8 h1:OkqQt5hcU507iYyocFea4Rz18xuPoKdUOUD7SQMtc1s=
github.com/tslocum/promptui v0.3.3-0.20190626143017-6cce979adef8/go.mod h1:ib2idig9p59EUDQJwRIN3/JHD8ThqsUu9fnPuA4zspA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/tools v0.0.0-20181122213734-04b5d21e00f1/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

34
goreleaser.yml Normal file
View File

@ -0,0 +1,34 @@
project_name: gophast
builds:
-
main: ./cmd/gmenu/main.go
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X git.sr.ht/~tslocum/gmenu/pkg/config.Version={{.Version}}
goos:
# - darwin
# - freebsd
- linux
- windows
goarch:
- 386
- amd64
# - arm
# - arm64
# - ppc64
# - ppc64le
# goarm:
# - 6
# - 7
archive:
replacements:
386: i386
format_overrides:
- goos: windows
format: zip
files:
- LICENSE
- README.md
checksum:
name_template: 'checksums.txt'

3
pkg/config/config.go Normal file
View File

@ -0,0 +1,3 @@
package config
var Version string

139
pkg/dmenu/dmenu.go Normal file
View File

@ -0,0 +1,139 @@
package dmenu
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"unicode"
)
// TODO: Support Type=URL
var (
scannedEntries []*DesktopEntry
scannedBytes []byte
scannedBytesLen int
entryHeader = []byte("[desktop entry]")
entryName = []byte("name=")
entryGenericName = []byte("genericname=")
entryComment = []byte("comment=")
entryIcon = []byte("icon=")
entryExec = []byte("exec=")
entryTerminal = []byte("terminal=true")
entryNoDisplay = []byte("nodisplay=true")
)
type DesktopEntry struct {
Name string
GenericName string
Comment string
Icon string
Exec string
Terminal bool
}
func (e *DesktopEntry) String() string {
name := ""
if e.Name != "" {
name = e.Name
}
if e.GenericName != "" {
if name != "" {
name += " / "
}
name += e.GenericName
}
comment := "no comment"
if e.Comment != "" {
comment = e.Comment
}
return fmt.Sprintf("{%s - %s}", name, comment)
}
func ScanEntries(dirs []string) ([]*DesktopEntry, error) {
var entries []*DesktopEntry
for _, dir := range dirs {
err := filepath.Walk(filepath.Join(dir, "applications"), scanDirectory)
if err != nil {
return nil, err
}
entries = append(entries, scannedEntries...)
scannedEntries = nil
}
return entries, nil
}
func scanDirectory(path string, f os.FileInfo, err error) error {
if f.IsDir() || !strings.HasSuffix(strings.ToLower(path), ".desktop") {
return nil
}
entry := &DesktopEntry{}
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var foundHeader bool
for scanner.Scan() {
scannedBytes = bytes.TrimRightFunc(scanner.Bytes(), unicode.IsSpace)
scannedBytesLen = len(scannedBytes)
if scannedBytesLen == 0 || scannedBytes[0] == byte('#') {
continue
} else if scannedBytes[0] == byte('[') {
if !foundHeader {
if scannedBytesLen < 15 || !bytes.EqualFold(scannedBytes[0:15], entryHeader) {
log.Printf("Warning: invalid desktop entry %s", path)
return nil
} else {
foundHeader = true
continue
}
} else {
break // Start of new section
}
} else if scannedBytesLen >= 5 && bytes.EqualFold(scannedBytes[0:5], entryName) {
entry.Name = string(scannedBytes[5:])
} else if scannedBytesLen >= 12 && bytes.EqualFold(scannedBytes[0:12], entryGenericName) {
entry.GenericName = string(scannedBytes[12:])
} else if scannedBytesLen >= 8 && bytes.EqualFold(scannedBytes[0:8], entryComment) {
entry.Comment = string(scannedBytes[8:])
} else if scannedBytesLen >= 5 && bytes.EqualFold(scannedBytes[0:5], entryIcon) {
entry.Icon = string(scannedBytes[5:])
} else if scannedBytesLen >= 5 && bytes.EqualFold(scannedBytes[0:5], entryExec) {
entry.Exec = string(scannedBytes[5:])
} else if scannedBytesLen >= 13 && bytes.EqualFold(scannedBytes, entryTerminal) {
entry.Terminal = true
} else if scannedBytesLen >= 14 && bytes.EqualFold(scannedBytes, entryNoDisplay) {
return nil // NoDisplay=true
} else if len(bytes.TrimSpace(scannedBytes)) > 0 {
//log.Printf("invalid row %s", scannedBytes)
}
}
if err := scanner.Err(); err != nil {
return err
}
if entry.Name == "" && entry.GenericName != "" {
entry.Name = entry.GenericName
entry.GenericName = ""
} else if entry.Name == entry.GenericName {
entry.GenericName = ""
}
scannedEntries = append(scannedEntries, entry)
return nil
}