even more refactoring! consolidate the command execution

This commit is contained in:
Felix Angell 2021-01-10 13:18:15 +00:00
parent 66473bb4b1
commit f26a93c1db
6 changed files with 20 additions and 22 deletions

View File

@ -455,11 +455,10 @@ func (b *Buffer) processTextInput(r rune) bool {
key = string(unicode.ToLower(r))
}
// FIXME(FELIX): what is source?
actionName, actionExists := source[key]
if actionExists {
if action, ok := register[actionName]; ok {
return bool(action.proc(b.parent, []*lex.Token{}))
}
return bool(ExecuteCommandIfExist(actionName, b.parent))
}
}

View File

@ -65,7 +65,7 @@ func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) {
ctx.SetColor(strife.HexRGB(conf.Suggestion.Foreground))
ctx.SetFont(b.font)
lastWidth, _ = ctx.Text(infoLine, ((x + w) - (lastWidth + (pad))), mpY+(pad/2))
lastWidth, _ = ctx.Text(infoLine, (x+w)-(lastWidth+(pad)), mpY+(pad/2))
}
{

View File

@ -72,11 +72,9 @@ func (s *suggestion) render(x, y int, ctx *strife.Renderer) {
ctx.SetColor(strife.HexRGB(conf.Suggestion.Foreground))
// FIXME strife library needs something to get
// text width and heights... for now we render offscreen to measure... lol
_, h := ctx.Text("foo", -500000, -50000)
_, textHeight := ctx.GetStringDimension("foo")
yOffs := (suggestionBoxHeight / 2) - (h / 2)
yOffs := (suggestionBoxHeight / 2) - (textHeight / 2)
ctx.Text(s.name, x+border, y+yOffs)
}
@ -148,16 +146,10 @@ func (b *CommandPalette) processCommand() {
}
if tokens[0].Equals("!") && tokens[1].IsType(lex.Word) {
command := tokens[1].Lexeme
action, exists := register[command]
if !exists {
log.Println("No such action ", command)
return
}
commandName := tokens[1].Lexeme
args := tokens[2:]
log.Println("executing action", command, "with arguments", args)
action.proc(b.parent, args)
log.Println("executing action", commandName, "with arguments", args)
ExecuteCommandIfExist(commandName, b.parent, args...)
}
if index, ok := b.pathToIndex[input]; ok {

View File

@ -1,5 +1,7 @@
package buff
import "github.com/felixangell/phi/internal/lex"
var register = map[string]BufferAction{
"page_down": NewBufferAction("page_down", pageDown),
"page_up": NewBufferAction("page_up", pageUp),
@ -17,3 +19,10 @@ var register = map[string]BufferAction{
"show_palette": NewBufferAction("show_palette", ShowPalette),
"exit": NewBufferAction("exit", ExitPhi),
}
func ExecuteCommandIfExist(command string, view *BufferView, tokens ...*lex.Token) BufferDirtyState {
if cmd, ok := register[command]; ok {
return cmd.proc(view, tokens)
}
return false
}

View File

@ -8,7 +8,6 @@ import (
"github.com/felixangell/phi/internal/cfg"
"github.com/felixangell/phi/internal/gui"
"github.com/felixangell/phi/internal/lex"
"github.com/felixangell/strife"
"github.com/fsnotify/fsnotify"
"github.com/veandco/go-sdl2/sdl"
@ -303,10 +302,7 @@ func (n *BufferView) OnUpdate() bool {
actionName, actionExists := source[key]
if actionExists {
if action, ok := register[actionName]; ok {
log.Println("Executing action '" + actionName + "'")
return bool(action.proc(n, []*lex.Token{}))
}
return bool(ExecuteCommandIfExist(actionName, n))
}
}

View File

@ -128,6 +128,8 @@ type shortcutConfig struct {
Controls map[string]string
}
// FIXME we should define some default shorcuts somehow.
// why are these a global and not stored in the config itself?
var Shortcuts = shortcutConfig{
Supers: map[string]string{},
Controls: map[string]string{},