Implemented customizable shortcuts, fixes #11

pull/34/head v0.0.9
マリウス 2 years ago
parent 3205682db0
commit 30024d48c2
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F

@ -128,7 +128,7 @@ patient.
USAGE
-----
Keyboard shortcuts:
The default keyboard shortcuts are:
C-r: Refresh
C-h: Focus groups list
@ -145,6 +145,17 @@ C-l, C-k: Focus articles list
n: Publish new article
r: Reply to selected article
However, you are free to customize these within your configuration file, under
the section `Shortcuts`. The structure is as following:
`<key code> = "event"`
The key codes can be looked up under the following link:
https://pkg.go.dev/github.com/gdamore/tcell/v2#Key
For simple ASCII characters use their ASCII code, e.g. `114` for the character
`r`.
KNOWN LIMITATIONS

@ -7,9 +7,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/gdamore/tcell/v2"
)
type ConfigProfile struct {
@ -17,6 +19,18 @@ type ConfigProfile struct {
Organization string
}
type ConfigShortcuts struct {
Refresh int64
Quit int64
FocusGroups int64
FocusArticles int64
FocusPreviews int64
NewArticle int64
ReplyToArticle int64
}
type Config struct {
ConfigFile string `toml:"-"`
@ -29,6 +43,8 @@ type Config struct {
Logfile string
Profile ConfigProfile
Shortcuts map[string]string
}
func LoadConfig() (*Config, error) {
@ -56,12 +72,41 @@ func LoadConfig() (*Config, error) {
}
cfg := new(Config)
cfg.Shortcuts = make(map[string]string)
_, err = toml.Decode(string(configFileContent), &cfg)
cfg.ConfigFile = configFile
err = cfg.LoadDefaults()
if err != nil {
return nil, err
}
return cfg, nil
}
func (cfg *Config) LoadDefaults() (error) {
if len(cfg.Shortcuts) == 0 {
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyCtrlQ), 10)] = "quit"
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyCtrlR), 10)] = "refresh"
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyCtrlH), 10)] = "focus-groups"
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyCtrlL), 10)] = "focus-articles"
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyCtrlK), 10)] = "focus-articles"
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyCtrlJ), 10)] = "focus-preview"
cfg.Shortcuts[strconv.FormatInt(int64('n'), 10)] = "article-new"
cfg.Shortcuts[strconv.FormatInt(int64('r'), 10)] = "article-reply"
cfg.Shortcuts[strconv.FormatInt(int64('h'), 10)] = "additional-key-left"
cfg.Shortcuts[strconv.FormatInt(int64('j'), 10)] = "additional-key-down"
cfg.Shortcuts[strconv.FormatInt(int64('k'), 10)] = "additional-key-up"
cfg.Shortcuts[strconv.FormatInt(int64('l'), 10)] = "additional-key-right"
cfg.Shortcuts[strconv.FormatInt(int64('g'), 10)] = "additional-key-home"
cfg.Shortcuts[strconv.FormatInt(int64('G'), 10)] = "additional-key-end"
cfg.Shortcuts[strconv.FormatInt(int64(tcell.KeyF8), 10)] = "play"
}
return cfg.Persist()
}
func (cfg *Config) Persist() (error) {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(cfg); err != nil {

@ -6,7 +6,6 @@ import (
"sort"
"strings"
"time"
"unicode"
"github.com/gdamore/tcell/v2"
"github.com/mrusme/superhighway84/models"
@ -307,44 +306,39 @@ func(mainscreen *Mainscreen) Refresh() {
}
func (mainscreen *Mainscreen) HandleInput(event *tcell.EventKey) (*tcell.EventKey) {
switch event.Key() {
case tcell.KeyCtrlH:
action := mainscreen.T.getInputEvent(event)
switch action {
case "focus-groups":
mainscreen.T.App.SetFocus(mainscreen.Groups)
return nil
case tcell.KeyCtrlJ:
case "focus-preview":
mainscreen.T.App.SetFocus(mainscreen.Preview)
return nil
case tcell.KeyCtrlL, tcell.KeyCtrlK:
case "focus-articles":
mainscreen.T.App.SetFocus(mainscreen.Articles)
return nil
case tcell.KeyRune:
switch unicode.ToLower(event.Rune()) {
case 'n':
mainscreen.submitNewArticle(mainscreen.GroupsList[mainscreen.CurrentGroupSelected])
return nil
case 'r':
mainscreen.replyToArticle(mainscreen.ArticlesList[mainscreen.CurrentArticleSelected])
return nil
case 'j':
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone))
return nil
case 'k':
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone))
return nil
case 'h':
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModNone))
return nil
case 'l':
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone))
return nil
case 'g':
if event.Rune() == 'G' {
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyEnd, 0, tcell.ModNone))
} else {
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyHome, 0, tcell.ModNone))
}
}
return event
case "article-new":
mainscreen.submitNewArticle(mainscreen.GroupsList[mainscreen.CurrentGroupSelected])
return nil
case "article-reply":
mainscreen.replyToArticle(mainscreen.ArticlesList[mainscreen.CurrentArticleSelected])
return nil
case "additional-key-down":
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone))
return nil
case "additional-key-up":
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone))
return nil
case "additional-key-left":
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModNone))
return nil
case "additional-key-right":
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone))
return nil
case "additional-key-home":
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyEnd, 0, tcell.ModNone))
case "additional-key-end":
mainscreen.T.App.QueueEvent(tcell.NewEventKey(tcell.KeyHome, 0, tcell.ModNone))
}
return event

@ -3,6 +3,7 @@ package tui
import (
"embed"
"log"
"strconv"
"time"
"unicode"
@ -99,17 +100,35 @@ func Init(embedfs *embed.FS, cfg *config.Config, cch *cache.Cache, logger *zap.L
return t
}
func (t *TUI) getInputEvent(event *tcell.EventKey) (string) {
var action string
var exist bool
if event.Key() == tcell.KeyRune {
action, exist = t.Config.Shortcuts[strconv.FormatInt(int64(event.Rune()), 10)]
} else {
action, exist = t.Config.Shortcuts[strconv.FormatInt(int64(event.Key()), 10)]
}
if exist == false {
return ""
}
return action
}
func (t *TUI) initInput() {
t.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlR:
action := t.getInputEvent(event)
switch action {
case "refresh":
t.RefreshMainscreen()
t.SetInfo(true)
return nil
case tcell.KeyCtrlQ:
case "quit":
t.App.Stop()
return nil
case tcell.KeyF8:
case "play":
t.Player.Play()
return nil
default:

Loading…
Cancel
Save