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

Commit ec65f2f

Browse files
authored
Merge pull request #746 from MikaelSmith/cd-to-wash
Make 'cd' go to Wash's root
2 parents 46ad9f0 + 6d2a13e commit ec65f2f

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

cmd/docs.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,10 @@ func stringifySupportedActions(path string, entry apitypes.Entry) string {
175175
fmt.Sprintf("- ls %s", path),
176176
fmt.Sprintf(" Type 'docs <child>' to view an ls'ed child's documentation"),
177177
fmt.Sprintf("- cd %s", path),
178-
fmt.Sprintf(" The 'W' environment variable contains the Wash root. Use 'cd $W' to"),
179-
fmt.Sprintf(" return to it."),
178+
fmt.Sprintf(" Use 'cd' to return to Wash's starting directory"),
180179
fmt.Sprintf("- stree %s", path),
181180
fmt.Sprintf(" Gives you a high-level overview of the kinds of things that you will"),
182-
fmt.Sprintf(" encounter when you 'cd' and 'ls' through this entry."),
181+
fmt.Sprintf(" encounter when you 'cd' and 'ls' through this entry"),
183182
fmt.Sprintf("- (anything else that works with directories [e.g. 'tree'])"),
184183
}
185184
case plugin.ReadAction().Name:

cmd/internal/shell/bash.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,11 @@ func (b bash) Command(subcommands []string, rundir string) (*exec.Cmd, error) {
4747
`
4848
// Re-add aliases in case .bashrc overrode them.
4949
content += common
50-
content += `
51-
function prompter() {
52-
local prompt_path
53-
if [ -x "$(command -v realpath)" ]; then
54-
prompt_path=$(realpath --relative-base=$W "$(pwd)")
55-
else
56-
prompt_path=$(basename "$(pwd)")
57-
fi
58-
export PS1="\e[0;36mwash ${prompt_path}\e[0;32m ❯\e[m "
59-
}
60-
export PROMPT_COMMAND=prompter
6150

51+
// Configure prompt and override `cd`
52+
content += preparePrompt(`\e[0;36m`, `\e[0;32m`, `\e[m`, "export PS1") + `
53+
export PROMPT_COMMAND=prompter
54+
` + overrideCd() + `
6255
[[ -s ~/.washrc ]] && source ~/.washrc
6356
`
6457
if err := ioutil.WriteFile(rcpath, []byte(content), 0644); err != nil {

cmd/internal/shell/core.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ type Shell interface {
2121
// 1. if ~/.washenv exists, load it
2222
// Additionally for interactive invocations they should:
2323
// 1. if ~/.washrc does not exist, load the shell's default interactive config
24-
// 1. reconfigure subcommand aliases (in case they were overridden), configure the prompt
24+
// 1. reconfigure subcommand aliases (in case they were overridden)
25+
// 1. configure the prompt to show your location within the Wash hierarchy (use preparePrompt)
26+
// 1. override cd so `cd` without arguments changes directory to $W (use overrideCd)
2527
// 1. if ~/.washrc exists, load it
2628
Command(subcommands []string, rundir string) (*exec.Cmd, error)
2729
}
@@ -42,3 +44,28 @@ func Get() Shell {
4244
return basic{sh: sh}
4345
}
4446
}
47+
48+
// Create the declaration for a `prompter` function that generates the prompt
49+
// `%F{cyan}wash ${prompt_path}%F{green} ❯%f `
50+
// with substitution for shell-specific portions of the function.
51+
func preparePrompt(cyan, green, reset, assign string) string {
52+
return `
53+
function prompter() {
54+
local prompt_path
55+
if [ -x "$(command -v realpath)" ]; then
56+
prompt_path=$(realpath --relative-base=$W "$(pwd)")
57+
else
58+
prompt_path=$(basename "$(pwd)")
59+
fi
60+
` + assign + `="` + cyan + `wash ${prompt_path}` + green + ` ❯` + reset + ` "
61+
}
62+
`
63+
}
64+
65+
// Create the declaration for a `cd` function that returns to the Wash root when no arguments are
66+
// supplied.
67+
func overrideCd() string {
68+
return `
69+
function cd { if (( $# == 0 )); then builtin cd $W; else builtin cd $*; fi }
70+
`
71+
}

cmd/internal/shell/zsh.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,12 @@ fi
5757
`
5858
// Re-add aliases in case .zprofile or .zshrc overrode them.
5959
content += common
60-
content += `
61-
function prompter() {
62-
local prompt_path
63-
if [ -x "$(command -v realpath)" ]; then
64-
prompt_path=$(realpath --relative-base=$W "$(pwd)")
65-
else
66-
prompt_path=$(basename "$(pwd)")
67-
fi
68-
PROMPT="%F{cyan}wash ${prompt_path}%F{green} ❯%f "
69-
}
7060

61+
// Configure prompt and override `cd`
62+
content += preparePrompt("%F{cyan}", "%F{green}", "%f", "PROMPT") + `
7163
autoload -Uz add-zsh-hook
7264
add-zsh-hook precmd prompter
73-
65+
` + overrideCd() + `
7466
if [[ -s ~/.washrc ]]; then source ~/.washrc; fi
7567
`
7668
if err := ioutil.WriteFile(filepath.Join(rundir, ".zshrc"), []byte(content), 0644); err != nil {

0 commit comments

Comments
 (0)