From 12a4dd58f32f809641cf6faf25a2146fe8da83df Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 23 Sep 2017 20:56:08 -0400 Subject: [PATCH] Only replace '~' with home if at start of path Ref #757 --- cmd/micro/autocomplete.go | 8 +------- cmd/micro/buffer.go | 4 +--- cmd/micro/command.go | 13 ++++--------- cmd/micro/util.go | 16 ++++++++++++++++ cmd/micro/view.go | 4 +--- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/cmd/micro/autocomplete.go b/cmd/micro/autocomplete.go index 9fba8f53..8599c9ae 100644 --- a/cmd/micro/autocomplete.go +++ b/cmd/micro/autocomplete.go @@ -4,8 +4,6 @@ import ( "io/ioutil" "os" "strings" - - "github.com/mitchellh/go-homedir" ) var pluginCompletions []func(string) []string @@ -22,13 +20,9 @@ func FileComplete(input string) (string, []string) { var files []os.FileInfo var err error if len(dirs) > 1 { - home, _ := homedir.Dir() - directories := strings.Join(dirs[:len(dirs)-1], sep) + sep - if strings.HasPrefix(directories, "~") { - directories = strings.Replace(directories, "~", home, 1) - } + directories = ReplaceHome(directories) files, err = ioutil.ReadDir(directories) } else { files, err = ioutil.ReadDir(".") diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 776ee65d..25c0dc28 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -16,7 +16,6 @@ import ( "time" "unicode/utf8" - "github.com/mitchellh/go-homedir" "github.com/zyedidia/micro/cmd/micro/highlight" ) @@ -385,7 +384,6 @@ func (b *Buffer) Serialize() error { // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist func (b *Buffer) SaveAs(filename string) error { b.UpdateRules() - dir, _ := homedir.Dir() if b.Settings["rmtrailingws"].(bool) { r, _ := regexp.Compile(`[ \t]+$`) for lineNum, line := range b.Lines(0, b.NumLines) { @@ -408,7 +406,7 @@ func (b *Buffer) SaveAs(filename string) error { data := []byte(str) err := ioutil.WriteFile(filename, data, 0644) if err == nil { - b.Path = strings.Replace(filename, "~", dir, 1) + b.Path = ReplaceHome(filename) b.IsModified = false b.ModTime, _ = GetModTime(filename) return b.Serialize() diff --git a/cmd/micro/command.go b/cmd/micro/command.go index 0947cd77..3c5baa26 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -14,7 +14,6 @@ import ( "strings" humanize "github.com/dustin/go-humanize" - "github.com/mitchellh/go-homedir" ) // A Command contains a action (a function to call) as well as information about how to autocomplete the command @@ -230,8 +229,7 @@ func TabSwitch(args []string) { // Cd changes the current working directory func Cd(args []string) { if len(args) > 0 { - home, _ := homedir.Dir() - path := strings.Replace(args[0], "~", home, 1) + path := ReplaceHome(args[0]) os.Chdir(path) for _, tab := range tabs { for _, view := range tab.views { @@ -325,8 +323,7 @@ func VSplit(args []string) { CurView().VSplit(NewBufferFromString("", "")) } else { filename := args[0] - home, _ := homedir.Dir() - filename = strings.Replace(filename, "~", home, 1) + filename = ReplaceHome(filename) file, err := os.Open(filename) fileInfo, _ := os.Stat(filename) @@ -355,8 +352,7 @@ func HSplit(args []string) { CurView().HSplit(NewBufferFromString("", "")) } else { filename := args[0] - home, _ := homedir.Dir() - filename = strings.Replace(filename, "~", home, 1) + filename = ReplaceHome(filename) file, err := os.Open(filename) fileInfo, _ := os.Stat(filename) @@ -396,8 +392,7 @@ func NewTab(args []string) { CurView().AddTab(true) } else { filename := args[0] - home, _ := homedir.Dir() - filename = strings.Replace(filename, "~", home, 1) + filename = ReplaceHome(filename) file, err := os.Open(filename) fileInfo, _ := os.Stat(filename) diff --git a/cmd/micro/util.go b/cmd/micro/util.go index bbcc4f55..3cc842bd 100644 --- a/cmd/micro/util.go +++ b/cmd/micro/util.go @@ -12,6 +12,7 @@ import ( "unicode/utf8" "github.com/mattn/go-runewidth" + homedir "github.com/mitchellh/go-homedir" ) // Util.go is a collection of utility functions that are used throughout @@ -363,3 +364,18 @@ func JoinCommandArgs(args ...string) string { return buf.String() } + +// ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's +// home directory. Does nothing if the path does not start with '~'. +func ReplaceHome(path string) string { + if !strings.HasPrefix(path, "~") { + return path + } + + home, err := homedir.Dir() + if err != nil { + messenger.Error("Could not find home directory: ", err) + return path + } + return strings.Replace(path, "~", home, 1) +} diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 2fd3f3e4..bdcfdcf3 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/mitchellh/go-homedir" "github.com/zyedidia/tcell" ) @@ -244,8 +243,7 @@ func (v *View) OpenBuffer(buf *Buffer) { // Open opens the given file in the view func (v *View) Open(filename string) { - home, _ := homedir.Dir() - filename = strings.Replace(filename, "~", home, 1) + filename = ReplaceHome(filename) file, err := os.Open(filename) fileInfo, _ := os.Stat(filename)