Add cd and pwd commands to change the working dir

Closes #451
This commit is contained in:
Zachary Yedidia 2016-11-18 16:48:08 -05:00
parent 7bc8d77387
commit 3ecdd96931
6 changed files with 75 additions and 10 deletions

View file

@ -81,12 +81,6 @@ func NewBuffer(txt []byte, path string) *Buffer {
b.Path = path
b.AbsPath = absPath
b.Name = path
// If the file doesn't have a path to disk then we give it no name
if path == "" {
b.Name = "No name"
}
// The last time this file was modified
b.ModTime, _ = GetModTime(b.Path)
@ -270,7 +264,6 @@ func (b *Buffer) Serialize() error {
func (b *Buffer) SaveAs(filename string) error {
b.FindFileType()
b.UpdateRules()
b.Name = filename
b.Path = filename
str := b.String()
if b.Settings["eofnewline"].(bool) {
@ -295,7 +288,6 @@ func (b *Buffer) SaveAs(filename string) error {
func (b *Buffer) SaveAsWithSudo(filename string) error {
b.FindFileType()
b.UpdateRules()
b.Name = filename
b.Path = filename
// The user may have already used sudo in which case we won't need the password

View file

@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"strings"
@ -46,6 +47,8 @@ func init() {
"ToggleLog": ToggleLog,
"Plugin": PluginCmd,
"Reload": Reload,
"Cd": Cd,
"Pwd": Pwd,
}
}
@ -95,6 +98,8 @@ func DefaultCommands() map[string]StrCommand {
"log": {"ToggleLog", []Completion{NoCompletion}},
"plugin": {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}},
"reload": {"Reload", []Completion{NoCompletion}},
"cd": {"Cd", []Completion{FileCompletion}},
"pwd": {"Pwd", []Completion{NoCompletion}},
}
}
@ -183,6 +188,32 @@ func PluginCmd(args []string) {
}
}
func Cd(args []string) {
if len(args) > 0 {
home, _ := homedir.Dir()
path := strings.Replace(args[0], "~", home, 1)
os.Chdir(path)
for _, tab := range tabs {
for _, view := range tab.views {
wd, _ := os.Getwd()
view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd)
if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) {
view.Buf.Path = view.Buf.AbsPath
}
}
}
}
}
func Pwd(args []string) {
wd, err := os.Getwd()
if err != nil {
messenger.Message(err.Error())
} else {
messenger.Message(wd)
}
}
func ToggleLog(args []string) {
buffer := messenger.getBuffer()
if CurView().Type != vtLog {

File diff suppressed because one or more lines are too long

View file

@ -18,6 +18,9 @@ func (sline *Statusline) Display() {
y := sline.view.height + sline.view.y
file := sline.view.Buf.Name
if file == "" {
file = sline.view.Buf.Path
}
// If the buffer is dirty (has been modified) write a little '+'
if sline.view.Buf.IsModified {

View file

@ -91,6 +91,18 @@ func Insert(str string, pos int, value string) string {
return string([]rune(str)[:pos]) + value + string([]rune(str)[pos:])
}
// MakeRelative will attempt to make a relative path between path and base
func MakeRelative(path, base string) (string, error) {
if len(path) > 0 {
rel, err := filepath.Rel(base, path)
if err != nil {
return path, err
}
return rel, nil
}
return path, nil
}
// GetLeadingWhitespace returns the leading whitespace of the given string
func GetLeadingWhitespace(str string) string {
ws := ""

View file

@ -64,6 +64,10 @@ Here are the possible commands that you can use.
* `reload`: reloads all runtime files.
* `cd path`: Change the working directory to the given `path`.
* `pwd`: Print the current working directory.
---
The following commands are provided by the default plugins: