Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Make 'cd' go to Wash's root
Browse files Browse the repository at this point in the history
One challenge new users run into is how to get back to Wash's root. Make
`cd` without arguments take them there.

Fixes #716.

Signed-off-by: Michael Smith <[email protected]>
  • Loading branch information
MikaelSmith committed Mar 2, 2020
1 parent e7a358f commit 3bfc3c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
5 changes: 2 additions & 3 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,10 @@ func stringifySupportedActions(path string, entry apitypes.Entry) string {
fmt.Sprintf("- ls %s", path),
fmt.Sprintf(" Type 'docs <child>' 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:
Expand Down
15 changes: 4 additions & 11 deletions cmd/internal/shell/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 28 additions & 1 deletion cmd/internal/shell/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 }
`
}
14 changes: 3 additions & 11 deletions cmd/internal/shell/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3bfc3c1

Please sign in to comment.