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 { type BufferAction struct {
name string 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{ return BufferAction{
name: name, name: name,
proc: proc, 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{ var actions = map[string]BufferAction{
"new": NewBufferAction("new", NewFile),
"save": NewBufferAction("save", Save), "save": NewBufferAction("save", Save),
"delete_line": NewBufferAction("delete_line", DeleteLine), "delete_line": NewBufferAction("delete_line", DeleteLine),
"close_buffer": NewBufferAction("close_buffer", CloseBuffer), "close_buffer": NewBufferAction("close_buffer", CloseBuffer),
"paste": NewBufferAction("paste", Paste), "paste": NewBufferAction("paste", Paste),
"show_palette": NewBufferAction("show_palette", ShowPalette), "show_palette": NewBufferAction("show_palette", ShowPalette),
"exit": NewBufferAction("exit", func(*View) bool { "exit": NewBufferAction("exit", func(*View, []string) bool {
// TODO do this properly lol // TODO do this properly lol
os.Exit(0) os.Exit(0)
return false return false

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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