Merge pull request #233 from schollz/master

Flag to start at specified line/column number (#224)
This commit is contained in:
Zachary Yedidia
2016-08-31 16:39:30 -04:00
committed by GitHub
2 changed files with 40 additions and 2 deletions

View File

@@ -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 +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])
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,
}

View File

@@ -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]
// Check if +LINE,COL was passed, and send this to the flagLineColumn
if string(filename[0]) == "+" && strings.Contains(filename, ",") {
flagLineColumn = filename
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 +LINE,COL will start the cursor at position LINE,COL
var flagLineColumn = ""
func main() {
flag.Parse()
if *flagVersion {