From b658536ad13cde3861f1516c2b432fad60fc76e8 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Tue, 5 Mar 2019 10:44:26 +1100 Subject: [PATCH] Started working on opening down and right --- commandmap.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- session.go | 2 +- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/commandmap.go b/commandmap.go index c1d36b1..e8f4cb4 100644 --- a/commandmap.go +++ b/commandmap.go @@ -1,8 +1,10 @@ package main import ( - "github.com/lmika/ted/ui" + "errors" "fmt" + + "github.com/lmika/ted/ui" ) const ( @@ -97,13 +99,55 @@ func (cm *CommandMapping) RegisterViewCommands() { _, cellY := grid.CellPosition() grid.MoveTo(0, cellY) })) - cm.Define("col-right", "Moves the cursor to the right-most column", "", gridNavOperation(func(grid *ui.Grid) { _, cellY := grid.CellPosition() dimX, _ := grid.Model().Dimensions() grid.MoveTo(dimX-1, cellY) })) + cm.Define("open-right", "Inserts a column to the right of the curser", "", func(ctx *CommandContext) error { + grid := ctx.Frame().Grid() + cellX, _ := grid.CellPosition() + + if rwModel, isRwModel := ctx.Session().Model.(RWModel); isRwModel { + height, width := rwModel.Dimensions() + if cellX == width-1 { + rwModel.Resize(height, width+1) + } + return nil + } + + return errors.New("model is read-only") + }) + + cm.Define("open-down", "Inserts a row below the curser", "", func(ctx *CommandContext) error { + grid := ctx.Frame().Grid() + _, cellY := grid.CellPosition() + + if rwModel, isRwModel := ctx.Session().Model.(RWModel); isRwModel { + height, width := rwModel.Dimensions() + if cellY == height-1 { + rwModel.Resize(height+1, width) + } + return nil + } + + return errors.New("model is read-only") + }) + + cm.Define("append", "Inserts a row below the curser", "", func(ctx *CommandContext) error { + if err := ctx.Session().Commands.Eval(ctx, "open-down"); err != nil { + return err + } + if err := ctx.Session().Commands.Eval(ctx, "move-down"); err != nil { + return err + } + + ctx.Session().UIManager.Redraw() + + return ctx.Session().Commands.Eval(ctx, "set-cell") + }) + cm.Define("enter-command", "Enter command", "", func(ctx *CommandContext) error { ctx.Frame().Prompt(":", func(res string) { cm.DoEval(ctx, res) @@ -149,7 +193,6 @@ func (cm *CommandMapping) RegisterViewCommands() { return cm.Eval(ctx, "quit") }) - // Aliases cm.Commands["w"] = cm.Command("save") cm.Commands["q"] = cm.Command("quit") @@ -178,6 +221,8 @@ func (cm *CommandMapping) RegisterViewKeyBindings() { cm.MapKey('e', cm.Command("set-cell")) + cm.MapKey('a', cm.Command("append")) + cm.MapKey(':', cm.Command("enter-command")) } diff --git a/session.go b/session.go index bf8e7d0..f5693dd 100644 --- a/session.go +++ b/session.go @@ -15,7 +15,7 @@ type Session struct { func NewSession(uiManager *ui.Ui, frame *Frame, source ModelSource) *Session { session := &Session{ Model: NewSingleCellStdModel(), - Source: source, + Source: source, Frame: frame, Commands: NewCommandMapping(), UIManager: uiManager,