mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-29 06:12:35 +09:00
Allows opening files using full path on Windows (#1126)
* Now can open Windows full-path from command line arg Example that now works: micro.exe D:\myfile.txt * Now correctly retrieves the path from the input path string. Except for single-letter filenames * Fixed line/cols, need to make the code prettier * Fixed path matching with regex by @Pariador * Fixed not stripping the line/col args from file path * Added tests for ParseCursorLocation
This commit is contained in:
committed by
Zachary Yedidia
parent
d7f7d845b9
commit
efe343b37c
@@ -12,6 +12,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/mattn/go-runewidth"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Util.go is a collection of utility functions that are used throughout
|
||||
@@ -356,11 +357,43 @@ func ReplaceHome(path string) string {
|
||||
return strings.Replace(path, homeString, home, 1)
|
||||
}
|
||||
|
||||
// GetPath returns a filename without everything following a `:`
|
||||
// GetPathAndCursorPosition returns a filename without everything following a `:`
|
||||
// This is used for opening files like util.go:10:5 to specify a line and column
|
||||
func GetPath(path string) string {
|
||||
if strings.Contains(path, ":") {
|
||||
path = strings.Split(path, ":")[0]
|
||||
// Special cases like Windows Absolute path (C:\myfile.txt:10:5) are handled correctly.
|
||||
func GetPathAndCursorPosition(path string) (string, []string) {
|
||||
re := regexp.MustCompile(`([\s\S]+?)(?::(\d+))(?::(\d+))?`)
|
||||
match := re.FindStringSubmatch(path)
|
||||
// no lines/columns were specified in the path, return just the path with cursor at 0, 0
|
||||
if len(match) == 0 {
|
||||
return path, []string{"0", "0"}
|
||||
} else if match[len(match)-1] != "" {
|
||||
// if the last capture group match isn't empty then both line and column were provided
|
||||
return match[1], match[2:]
|
||||
}
|
||||
return path
|
||||
// if it was empty, then only a line was provided, so default to column 0
|
||||
return match[1], []string{match[2], "0"}
|
||||
}
|
||||
|
||||
func ParseCursorLocation(cursorPositions []string) (Loc, error) {
|
||||
startpos := Loc{0, 0}
|
||||
var err error
|
||||
|
||||
// if no positions are available exit early
|
||||
if len(cursorPositions) == 0 {
|
||||
return startpos, err
|
||||
}
|
||||
|
||||
startpos.Y, err = strconv.Atoi(cursorPositions[0])
|
||||
if err != nil {
|
||||
messenger.Error("Error parsing cursor position: ", err)
|
||||
} else {
|
||||
if len(cursorPositions) > 1 {
|
||||
startpos.X, err = strconv.Atoi(cursorPositions[1])
|
||||
if err != nil {
|
||||
messenger.Error("Error parsing cursor position: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return startpos, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user