new file + open file commands; commands can take args

This commit is contained in:
Felix Angell 2018-04-29 19:03:24 +01:00
parent ac351ec9db
commit bcab1eee76
8 changed files with 26 additions and 11 deletions

1
foo_bar_baz Normal file
View File

@ -0,0 +1 @@
this is the file new_bar_baz!

View File

@ -4,23 +4,37 @@ import "os"
type BufferAction struct {
name string
proc func(*View) bool
proc func(*View, []string) bool
}
func NewBufferAction(name string, proc func(*View) bool) BufferAction {
func NewBufferAction(name string, proc func(*View, []string) bool) BufferAction {
return BufferAction{
name: name,
proc: proc,
}
}
func NewFile(v *View, commands []string) bool {
// TODO some nice error stuff
// have an error roll thing in the view?
buff := v.AddBuffer()
buff.OpenFile(commands[0])
buff.SetFocus(true)
v.focusedBuff = buff.index
return false
}
var actions = map[string]BufferAction{
"new": NewBufferAction("new", NewFile),
"save": NewBufferAction("save", Save),
"delete_line": NewBufferAction("delete_line", DeleteLine),
"close_buffer": NewBufferAction("close_buffer", CloseBuffer),
"paste": NewBufferAction("paste", Paste),
"show_palette": NewBufferAction("show_palette", ShowPalette),
"exit": NewBufferAction("exit", func(*View) bool {
"exit": NewBufferAction("exit", func(*View, []string) bool {
// TODO do this properly lol
os.Exit(0)
return false

View File

@ -268,7 +268,7 @@ func (b *Buffer) processTextInput(r rune) bool {
actionName, actionExists := cfg.Shortcuts.Supers[string(unicode.ToLower(r))]
if actionExists {
if action, ok := actions[actionName]; ok {
return action.proc(b.parent)
return action.proc(b.parent, []string{})
}
} else {
log.Println("warning, unimplemented shortcut ctrl+", unicode.ToLower(r), actionName)

View File

@ -1,6 +1,6 @@
package gui
func CloseBuffer(v *View) bool {
func CloseBuffer(v *View, commands []string) bool {
b := v.getCurrentBuff()
if b == nil {
return false

View File

@ -1,6 +1,6 @@
package gui
func DeleteLine(v *View) bool {
func DeleteLine(v *View, commands []string) bool {
b := v.getCurrentBuff()
if b == nil {
return false

View File

@ -130,7 +130,7 @@ func (b *CommandPalette) processCommand() {
return
}
action.proc(b.parent)
action.proc(b.parent, tokenizedLine[1:])
}
func (b *CommandPalette) calculateSuggestions() {

View File

@ -7,14 +7,14 @@ import (
"log"
)
func ShowPalette(v *View) bool {
func ShowPalette(v *View, commands []string) bool {
b := v.getCurrentBuff()
v.UnfocusBuffers()
v.focusPalette(b)
return true
}
func Paste(v *View) bool {
func Paste(v *View, commands []string) bool {
b := v.getCurrentBuff()
if b == nil {
return false
@ -38,7 +38,7 @@ func Paste(v *View) bool {
// if the buffer is modified it will be
// re-rendered.
func Save(v *View) bool {
func Save(v *View, commands []string) bool {
b := v.getCurrentBuff()
if b == nil {
return false

View File

@ -137,7 +137,7 @@ func (n *View) OnUpdate() bool {
if actionExists {
if action, ok := actions[actionName]; ok {
log.Println("Executing action '" + actionName + "'")
return action.proc(n)
return action.proc(n, []string{})
}
} else {
log.Println("warning, unimplemented shortcut ctrl +", string(unicode.ToLower(r)), actionName)