Skip to content

Commit

Permalink
Add hotkey to edit snippets.yml, make hotkeys configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro-h committed May 19, 2021
1 parent 74c3dd0 commit ba82940
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BASE_VERSION=0.3.0
BASE_VERSION=0.3.1
REPO_OWNER=sandro-h
BUILD_CENTOS_IMAGE_VERSION=0.0.1
BUILD_CENTOS_IMAGE_TAG=ghcr.io/${REPO_OWNER}/snippet-centos-build:${BUILD_CENTOS_IMAGE_VERSION}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Tiny cross-platform widget to insert snippets into the terminal, editor, etc.
5. Press `escape` to cancel search and hide widget again.
6. Press `Alt + F4` while widget is active to close it for good.

Snippets are stored in `snippet.yml` file, see [snippet_sample.yml](snippet_sample.yml).
Snippets are stored in `snippet.yml` file, see [snippet_sample.yml](snippet_sample.yml).
Snippets are automatically reloaded when `snippet.yml` changes.
If `editor_cmd` is configured, `Alt + e` will open the `snippet.yml` with the given editor command.

Optional configuration is stored in `config.yml` file, see [config_sample.yml](config_sample.yml).

Expand Down
9 changes: 9 additions & 0 deletions config_sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ special_chars:
# Duration until an unlocked secret snippet is locked again.
# Duration is in Golang format: https://golang.org/pkg/time/#ParseDuration
secret_ttl: 10m

# Hotkey combination to activate and show the snippet window.
activate_hotkeys: [q, alt]

# Hotkey combination to show snippets.yml in the editor.
editor_hotkeys: [e, alt]

# Command with which to open snippets.yml when Alt + e is pressed. Empty by default.
editor_cmd: vim
62 changes: 54 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"image/color"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"fyne.io/fyne/v2"
Expand All @@ -25,19 +27,34 @@ import (
"gopkg.in/yaml.v2"
)

type hotkeyConfig struct {
activateHotkeys []string
editorHotkeys []string
editorCmd string
}

type config struct {
typing.Config
secretTTL time.Duration
hotkeyConfig
}

const defaultSecretTTL = 10 * time.Minute

var defaultActivateHotkeys = []string{"q", "alt"}

var defaultEditorHotkeys = []string{"e", "alt"}

var cfg *config = &config{
typing.Config{
Config: typing.Config{
SpecialChars: map[string]typing.SpecialChar{},
SpecialCharList: "",
},
defaultSecretTTL,
secretTTL: defaultSecretTTL,
hotkeyConfig: hotkeyConfig{
activateHotkeys: defaultActivateHotkeys,
editorHotkeys: defaultEditorHotkeys,
},
}

type appState struct {
Expand Down Expand Up @@ -121,7 +138,7 @@ func main() {
w.Canvas().Focus(search.Entry)
w.CenterOnScreen()

go listenForHotkeys(w)
go listenForHotkeys(w, snippetsFile, cfg.hotkeyConfig)
go periodicallyEvictSecrets(state, cfg.secretTTL)

w.ShowAndRun()
Expand Down Expand Up @@ -163,18 +180,36 @@ func loadConfig(configFile string) (*config, error) {
var rawCfg struct {
SpecialCharList []typing.SpecialChar `yaml:"special_chars"`
SecretTTL string `yaml:"secret_ttl"`
EditorCmd string `yaml:"editor_cmd"`
ActivateHotkeys []string `yaml:"activate_hotkeys"`
EditorHotkeys []string `yaml:"editor_hotkeys"`
}
err = yaml.Unmarshal(bytes, &rawCfg)
if err != nil {
return nil, err
}

cfg := config{
typing.Config{
Config: typing.Config{
SpecialChars: map[string]typing.SpecialChar{},
SpecialCharList: "",
},
defaultSecretTTL,
secretTTL: defaultSecretTTL,
hotkeyConfig: hotkeyConfig{
editorCmd: rawCfg.EditorCmd,
},
}

if rawCfg.ActivateHotkeys != nil {
cfg.activateHotkeys = rawCfg.ActivateHotkeys
} else {
cfg.activateHotkeys = defaultActivateHotkeys
}

if rawCfg.EditorHotkeys != nil {
cfg.editorHotkeys = rawCfg.EditorHotkeys
} else {
cfg.editorHotkeys = defaultEditorHotkeys
}

if rawCfg.SecretTTL != "" {
Expand Down Expand Up @@ -223,12 +258,23 @@ func watchSnippets(snippetsFile string, onModified func()) {
}
}

func listenForHotkeys(w fyne.Window) {
robotgo.EventHook(hook.KeyDown, []string{"q", "alt"}, func(e hook.Event) {
func listenForHotkeys(w fyne.Window, snippetsFile string, hotkeyCfg hotkeyConfig) {
robotgo.EventHook(hook.KeyDown, hotkeyCfg.activateHotkeys, func(e hook.Event) {
w.Show()
// robotgo.EventEnd()
})

if hotkeyCfg.editorCmd != "" {
editorCmdParts := strings.Split(hotkeyCfg.editorCmd, " ")
editorCmdParts = append(editorCmdParts, snippetsFile)
robotgo.EventHook(hook.KeyDown, hotkeyCfg.editorHotkeys, func(e hook.Event) {
cmd := exec.Command(editorCmdParts[0], editorCmdParts[1:]...)
err := cmd.Start()
if err != nil {
log.Printf("Could not run %s: %s", cmd, err)
}
})
}

s := robotgo.EventStart()
<-robotgo.EventProcess(s)
}
Expand Down

0 comments on commit ba82940

Please sign in to comment.