From 4499228cdbe68690d0ff23aeb70adc1d3ad83fc6 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 30 Aug 2016 11:28:28 -0400 Subject: [PATCH 1/2] Added flag to introduce cursor starting positions --- cmd/micro/buffer.go | 32 ++++++++++++++++++++++++++++++-- cmd/micro/micro.go | 10 ++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index e019b424..4a77963d 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -8,6 +8,8 @@ import ( "os/exec" "os/signal" "path/filepath" + "strconv" + "strings" "time" "unicode/utf8" ) @@ -85,10 +87,36 @@ func NewBuffer(txt []byte, path string) *Buffer { } // Put the cursor at the first spot + cursorStartX := 0 + cursorStartY := 0 + // If -cursor LINE,COL was passed, use start position LINE,COL + if len(*flagLineColumn) > 0 { + positions := strings.Split(*flagLineColumn, ",") + if len(positions) == 2 { + lineNum, errPos1 := strconv.Atoi(positions[0]) + colNum, errPos2 := strconv.Atoi(positions[1]) + if errPos1 == nil && errPos2 == nil { + cursorStartX = colNum + cursorStartY = lineNum - 1 + // Check to avoid line overflow + if cursorStartY > b.NumLines { + cursorStartY = b.NumLines - 1 + } else if cursorStartY < 0 { + cursorStartY = 0 + } + // Check to avoid column overflow + if cursorStartX > len(b.Line(cursorStartY)) { + cursorStartX = len(b.Line(cursorStartY)) + } else if cursorStartX < 0 { + cursorStartX = 0 + } + } + } + } b.Cursor = Cursor{ Loc: Loc{ - X: 0, - Y: 0, + X: cursorStartX, + Y: cursorStartY, }, buf: b, } diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index aa301e72..dac8df8d 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -87,6 +87,13 @@ func LoadInput() []*Buffer { // We go through each file and load it for i := 1; i < len(os.Args); i++ { filename = os.Args[i] + + // Need to skip arguments that are not filenames + if filename == "-cursor" { + i++ // also skip the LINE,COL for -cursor + continue + } + // Check that the file exists if _, e := os.Stat(filename); e == nil { // If it exists we load it into a buffer @@ -197,6 +204,9 @@ func RedrawAll() { // Passing -version as a flag will have micro print out the version number var flagVersion = flag.Bool("version", false, "Show the version number") +// Passing -cursor LINE,COL will start the cursor at position LINE,COL +var flagLineColumn = flag.String("cursor", "", "Start the cursor at position `LINE,COL`") + func main() { flag.Parse() if *flagVersion { From 62ac9f79f23a7ba3caee4c3d7511640ca7c4a575 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Wed, 31 Aug 2016 10:10:54 -0400 Subject: [PATCH 2/2] Switched to +LINE,COL Previously the flag was parsed for `-cursor LINE,COL` However, emacs and nano both us `+LINE,COL` and this also makes it easier to ignore the `+` as a filename. --- cmd/micro/buffer.go | 6 +++--- cmd/micro/micro.go | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 4a77963d..2046c729 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -89,9 +89,9 @@ func NewBuffer(txt []byte, path string) *Buffer { // Put the cursor at the first spot cursorStartX := 0 cursorStartY := 0 - // If -cursor LINE,COL was passed, use start position LINE,COL - if len(*flagLineColumn) > 0 { - positions := strings.Split(*flagLineColumn, ",") + // If +LINE,COL was passed, use start position LINE,COL + if len(flagLineColumn) > 0 { + positions := strings.Split(flagLineColumn[1:], ",") if len(positions) == 2 { lineNum, errPos1 := strconv.Atoi(positions[0]) colNum, errPos2 := strconv.Atoi(positions[1]) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index dac8df8d..f19afdf6 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -88,9 +88,9 @@ func LoadInput() []*Buffer { for i := 1; i < len(os.Args); i++ { filename = os.Args[i] - // Need to skip arguments that are not filenames - if filename == "-cursor" { - i++ // also skip the LINE,COL for -cursor + // Check if +LINE,COL was passed, and send this to the flagLineColumn + if string(filename[0]) == "+" && strings.Contains(filename, ",") { + flagLineColumn = filename continue } @@ -204,8 +204,8 @@ func RedrawAll() { // Passing -version as a flag will have micro print out the version number var flagVersion = flag.Bool("version", false, "Show the version number") -// Passing -cursor LINE,COL will start the cursor at position LINE,COL -var flagLineColumn = flag.String("cursor", "", "Start the cursor at position `LINE,COL`") +// Passing +LINE,COL will start the cursor at position LINE,COL +var flagLineColumn = "" func main() { flag.Parse()