Only replace '~' with home if at start of path

Ref #757
This commit is contained in:
Zachary Yedidia
2017-09-23 20:56:08 -04:00
parent 5a7ddb8330
commit 12a4dd58f3
5 changed files with 23 additions and 22 deletions

View File

@@ -4,8 +4,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"github.com/mitchellh/go-homedir"
) )
var pluginCompletions []func(string) []string var pluginCompletions []func(string) []string
@@ -22,13 +20,9 @@ func FileComplete(input string) (string, []string) {
var files []os.FileInfo var files []os.FileInfo
var err error var err error
if len(dirs) > 1 { if len(dirs) > 1 {
home, _ := homedir.Dir()
directories := strings.Join(dirs[:len(dirs)-1], sep) + sep directories := strings.Join(dirs[:len(dirs)-1], sep) + sep
if strings.HasPrefix(directories, "~") { directories = ReplaceHome(directories)
directories = strings.Replace(directories, "~", home, 1)
}
files, err = ioutil.ReadDir(directories) files, err = ioutil.ReadDir(directories)
} else { } else {
files, err = ioutil.ReadDir(".") files, err = ioutil.ReadDir(".")

View File

@@ -16,7 +16,6 @@ import (
"time" "time"
"unicode/utf8" "unicode/utf8"
"github.com/mitchellh/go-homedir"
"github.com/zyedidia/micro/cmd/micro/highlight" "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 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
func (b *Buffer) SaveAs(filename string) error { func (b *Buffer) SaveAs(filename string) error {
b.UpdateRules() b.UpdateRules()
dir, _ := homedir.Dir()
if b.Settings["rmtrailingws"].(bool) { if b.Settings["rmtrailingws"].(bool) {
r, _ := regexp.Compile(`[ \t]+$`) r, _ := regexp.Compile(`[ \t]+$`)
for lineNum, line := range b.Lines(0, b.NumLines) { for lineNum, line := range b.Lines(0, b.NumLines) {
@@ -408,7 +406,7 @@ func (b *Buffer) SaveAs(filename string) error {
data := []byte(str) data := []byte(str)
err := ioutil.WriteFile(filename, data, 0644) err := ioutil.WriteFile(filename, data, 0644)
if err == nil { if err == nil {
b.Path = strings.Replace(filename, "~", dir, 1) b.Path = ReplaceHome(filename)
b.IsModified = false b.IsModified = false
b.ModTime, _ = GetModTime(filename) b.ModTime, _ = GetModTime(filename)
return b.Serialize() return b.Serialize()

View File

@@ -14,7 +14,6 @@ import (
"strings" "strings"
humanize "github.com/dustin/go-humanize" 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 // 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 // Cd changes the current working directory
func Cd(args []string) { func Cd(args []string) {
if len(args) > 0 { if len(args) > 0 {
home, _ := homedir.Dir() path := ReplaceHome(args[0])
path := strings.Replace(args[0], "~", home, 1)
os.Chdir(path) os.Chdir(path)
for _, tab := range tabs { for _, tab := range tabs {
for _, view := range tab.views { for _, view := range tab.views {
@@ -325,8 +323,7 @@ func VSplit(args []string) {
CurView().VSplit(NewBufferFromString("", "")) CurView().VSplit(NewBufferFromString("", ""))
} else { } else {
filename := args[0] filename := args[0]
home, _ := homedir.Dir() filename = ReplaceHome(filename)
filename = strings.Replace(filename, "~", home, 1)
file, err := os.Open(filename) file, err := os.Open(filename)
fileInfo, _ := os.Stat(filename) fileInfo, _ := os.Stat(filename)
@@ -355,8 +352,7 @@ func HSplit(args []string) {
CurView().HSplit(NewBufferFromString("", "")) CurView().HSplit(NewBufferFromString("", ""))
} else { } else {
filename := args[0] filename := args[0]
home, _ := homedir.Dir() filename = ReplaceHome(filename)
filename = strings.Replace(filename, "~", home, 1)
file, err := os.Open(filename) file, err := os.Open(filename)
fileInfo, _ := os.Stat(filename) fileInfo, _ := os.Stat(filename)
@@ -396,8 +392,7 @@ func NewTab(args []string) {
CurView().AddTab(true) CurView().AddTab(true)
} else { } else {
filename := args[0] filename := args[0]
home, _ := homedir.Dir() filename = ReplaceHome(filename)
filename = strings.Replace(filename, "~", home, 1)
file, err := os.Open(filename) file, err := os.Open(filename)
fileInfo, _ := os.Stat(filename) fileInfo, _ := os.Stat(filename)

View File

@@ -12,6 +12,7 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
homedir "github.com/mitchellh/go-homedir"
) )
// Util.go is a collection of utility functions that are used throughout // Util.go is a collection of utility functions that are used throughout
@@ -363,3 +364,18 @@ func JoinCommandArgs(args ...string) string {
return buf.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)
}

View File

@@ -6,7 +6,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/mitchellh/go-homedir"
"github.com/zyedidia/tcell" "github.com/zyedidia/tcell"
) )
@@ -244,8 +243,7 @@ func (v *View) OpenBuffer(buf *Buffer) {
// Open opens the given file in the view // Open opens the given file in the view
func (v *View) Open(filename string) { func (v *View) Open(filename string) {
home, _ := homedir.Dir() filename = ReplaceHome(filename)
filename = strings.Replace(filename, "~", home, 1)
file, err := os.Open(filename) file, err := os.Open(filename)
fileInfo, _ := os.Stat(filename) fileInfo, _ := os.Stat(filename)