diff --git a/cmd/docs.go b/cmd/docs.go index b3e55235b..5693d904f 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -175,11 +175,10 @@ func stringifySupportedActions(path string, entry apitypes.Entry) string { fmt.Sprintf("- ls %s", path), fmt.Sprintf(" Type 'docs ' to view an ls'ed child's documentation"), fmt.Sprintf("- cd %s", path), - fmt.Sprintf(" The 'W' environment variable contains the Wash root. Use 'cd $W' to"), - fmt.Sprintf(" return to it."), + fmt.Sprintf(" Use 'cd' to return to Wash's starting directory"), fmt.Sprintf("- stree %s", path), fmt.Sprintf(" Gives you a high-level overview of the kinds of things that you will"), - fmt.Sprintf(" encounter when you 'cd' and 'ls' through this entry."), + fmt.Sprintf(" encounter when you 'cd' and 'ls' through this entry"), fmt.Sprintf("- (anything else that works with directories [e.g. 'tree'])"), } case plugin.ReadAction().Name: diff --git a/cmd/internal/shell/bash.go b/cmd/internal/shell/bash.go index 211b4e929..b128abcde 100644 --- a/cmd/internal/shell/bash.go +++ b/cmd/internal/shell/bash.go @@ -47,18 +47,11 @@ func (b bash) Command(subcommands []string, rundir string) (*exec.Cmd, error) { ` // Re-add aliases in case .bashrc overrode them. content += common - content += ` -function prompter() { - local prompt_path - if [ -x "$(command -v realpath)" ]; then - prompt_path=$(realpath --relative-base=$W "$(pwd)") - else - prompt_path=$(basename "$(pwd)") - fi - export PS1="\e[0;36mwash ${prompt_path}\e[0;32m ❯\e[m " -} -export PROMPT_COMMAND=prompter + // Configure prompt and override `cd` + content += preparePrompt(`\e[0;36m`, `\e[0;32m`, `\e[m`, "export PS1") + ` +export PROMPT_COMMAND=prompter +` + prepareCd() + ` [[ -s ~/.washrc ]] && source ~/.washrc ` if err := ioutil.WriteFile(rcpath, []byte(content), 0644); err != nil { diff --git a/cmd/internal/shell/core.go b/cmd/internal/shell/core.go index bb1199cd4..b9b8ecd90 100644 --- a/cmd/internal/shell/core.go +++ b/cmd/internal/shell/core.go @@ -21,7 +21,9 @@ type Shell interface { // 1. if ~/.washenv exists, load it // Additionally for interactive invocations they should: // 1. if ~/.washrc does not exist, load the shell's default interactive config - // 1. reconfigure subcommand aliases (in case they were overridden), configure the prompt + // 1. reconfigure subcommand aliases (in case they were overridden) + // 1. configure the prompt to show your location within the Wash hierarchy (use preparePrompt) + // 1. override cd so `cd` without arguments changes directory to $W (use prepareCd) // 1. if ~/.washrc exists, load it Command(subcommands []string, rundir string) (*exec.Cmd, error) } @@ -42,3 +44,28 @@ func Get() Shell { return basic{sh: sh} } } + +// Create the declaration for a `prompter` function that generates the prompt +// `%F{cyan}wash ${prompt_path}%F{green} ❯%f ` +// with substitution for shell-specific portions of the function. +func preparePrompt(cyan, green, reset, assign string) string { + return ` +function prompter() { + local prompt_path + if [ -x "$(command -v realpath)" ]; then + prompt_path=$(realpath --relative-base=$W "$(pwd)") + else + prompt_path=$(basename "$(pwd)") + fi + ` + assign + `="` + cyan + `wash ${prompt_path}` + green + ` ❯` + reset + ` " +} +` +} + +// Create the declaration for a `cd` function that returns to the Wash root when no arguments are +// supplied. +func prepareCd() string { + return ` +function cd { if (( $# == 0 )); then builtin cd $W; else builtin cd $*; fi } +` +} diff --git a/cmd/internal/shell/zsh.go b/cmd/internal/shell/zsh.go index a501c5ed2..ee6c5d520 100644 --- a/cmd/internal/shell/zsh.go +++ b/cmd/internal/shell/zsh.go @@ -57,20 +57,12 @@ fi ` // Re-add aliases in case .zprofile or .zshrc overrode them. content += common - content += ` -function prompter() { - local prompt_path - if [ -x "$(command -v realpath)" ]; then - prompt_path=$(realpath --relative-base=$W "$(pwd)") - else - prompt_path=$(basename "$(pwd)") - fi - PROMPT="%F{cyan}wash ${prompt_path}%F{green} ❯%f " -} + // Configure prompt and override `cd` + content += preparePrompt("%F{cyan}", "%F{green}", "%f", "PROMPT") + ` autoload -Uz add-zsh-hook add-zsh-hook precmd prompter - +` + prepareCd() + ` if [[ -s ~/.washrc ]]; then source ~/.washrc; fi ` if err := ioutil.WriteFile(filepath.Join(rundir, ".zshrc"), []byte(content), 0644); err != nil {