]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/command.go
Add cd and pwd commands to change the working dir
[micro.git] / cmd / micro / command.go
index 7193e56a3fe00559a3b8845d7ac06d735b41dd80..b36df74bd46cfcfc4b2a5f961778f62b9fd75fef 100644 (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 {