Skip to content

Commit e3f4948

Browse files
jnjaeschkeahal
authored andcommitted
Add a python virtualenv style prompt prefix.
If the user sets the env variable `BUILDWITH_SHOW_PROMPT`, the shell prompt is prefixed by the currently activated mozconfig: ```sh cd ~/gecko buildwith release (release) ./mach build ``` This prefix is only shown if the shell is inside a gecko repository.
1 parent 668552d commit e3f4948

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ For example, if you put this in your ~/.bashrc file (or equivalent):
7272
buildwith will also set the MOZCONFIG environment variable in launchctl (useful when running Android Studio).
7373
All occurences of ``#1`` will be replaced by the path to the mozconfig file.
7474

75+
#### prompt prefix
76+
77+
Similar to Python virtualenvs, you can display the active mozconfig name as a prefix in your shell prompt.
78+
This prefix only appears when you are inside a gecko repository. To enable this feature, add to your ~/.bashrc file (or equivalent):
79+
80+
export BUILDWITH_SHOW_PROMPT=1
81+
82+
The default format is `(mozconfig_name) ` but you can customize it with:
83+
84+
export BUILDWITH_PROMPT_FORMAT="[%s] "
85+
86+
The `%s` will be replaced with the mozconfig name. For example, if you activate a config named "debug" while inside a gecko repository, your prompt will show `(debug) ` or `[debug] ` depending on your format setting.
87+
88+
The prompt automatically appears when you `cd` into a gecko repository and disappears when you leave it.
89+
Gecko repositories are detected by the presence of `mach` or `moz.configure` files.
90+
7591
#### mozconfig template
7692

7793
When you make a new mozconfig, it will be populated with some basic build commands and the name of the mozconfig

src/scripts/mozconfigwrapper.sh

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@ function mozconfigwrapper_buildwith_home {
99
fi
1010
}
1111

12+
# Detect if current directory is inside a gecko repository
13+
function mozconfigwrapper_is_in_gecko {
14+
# Search up the directory tree for gecko markers
15+
typeset current="$PWD"
16+
while [ "$current" != "/" ]; do
17+
if [ -f "$current/mach" ] || [ -f "$current/moz.configure" ]; then
18+
return 0
19+
fi
20+
current=$(dirname "$current")
21+
done
22+
23+
return 1
24+
}
25+
26+
# Generate prompt prefix if BUILDWITH_SHOW_PROMPT is enabled
27+
function mozconfigwrapper_prompt {
28+
# Only show prompt if explicitly enabled
29+
if [ -z "$BUILDWITH_SHOW_PROMPT" ]; then
30+
return
31+
fi
32+
33+
# Only show if we have an active mozconfig and we're in a gecko repo
34+
if [ -n "$MOZCONFIG" ] && mozconfigwrapper_is_in_gecko; then
35+
typeset mozconfig_name=$(basename "$MOZCONFIG")
36+
typeset format="${BUILDWITH_PROMPT_FORMAT:-(%s) }"
37+
# Replace %s with mozconfig name
38+
printf "$format" "$mozconfig_name"
39+
fi
40+
}
41+
1242
function buildwith {
1343
mozconfigwrapper_buildwith_home
1444
typeset name="$1"
@@ -75,7 +105,15 @@ function rmmozconfig {
75105
fi
76106

77107
mozconfig="$BUILDWITH_HOME/$name"
78-
rm $mozconfig
108+
109+
# If we're removing the active mozconfig, unset it
110+
if [ "$MOZCONFIG" = "$mozconfig" ]
111+
then
112+
unset MOZCONFIG
113+
rm -f "$BUILDWITH_HOME/.active"
114+
fi
115+
116+
rm "$mozconfig"
79117
echo "Removed: $mozconfig"
80118
}
81119

@@ -108,8 +146,46 @@ function mozconfigwrapper_list_mozconfigs {
108146
| (unset GREP_OPTIONS; command \egrep -v '^\*$') 2>/dev/null
109147
}
110148

149+
# Store original PS1 for zsh
150+
_MOZCONFIGWRAPPER_ORIGINAL_PS1=""
151+
152+
# Set up prompt integration if BUILDWITH_SHOW_PROMPT is enabled
153+
function mozconfigwrapper_setup_prompt {
154+
if [ -z "$BUILDWITH_SHOW_PROMPT" ]; then
155+
return
156+
fi
157+
158+
if [ -n "$BASH" ]; then
159+
# For bash, we modify PS1 to include our prompt function
160+
# Only modify if not already present
161+
if [[ "$PS1" != *'$(mozconfigwrapper_prompt)'* ]]; then
162+
export PS1="\$(mozconfigwrapper_prompt)$PS1"
163+
fi
164+
elif [ -n "$ZSH_VERSION" ]; then
165+
# For zsh, save the original PS1 and add to precmd_functions array
166+
if [ -z "$_MOZCONFIGWRAPPER_ORIGINAL_PS1" ]; then
167+
_MOZCONFIGWRAPPER_ORIGINAL_PS1="$PS1"
168+
fi
169+
if [[ ! " ${precmd_functions[@]} " =~ " mozconfigwrapper_precmd " ]]; then
170+
precmd_functions+=(mozconfigwrapper_precmd)
171+
fi
172+
fi
173+
}
174+
175+
# Zsh precmd hook to update PS1
176+
function mozconfigwrapper_precmd {
177+
# Reconstruct PS1 from the original, preserving prompt expansions
178+
typeset prefix=$(mozconfigwrapper_prompt)
179+
if [ -n "$prefix" ]; then
180+
PS1="${prefix}${_MOZCONFIGWRAPPER_ORIGINAL_PS1}"
181+
else
182+
PS1="$_MOZCONFIGWRAPPER_ORIGINAL_PS1"
183+
fi
184+
}
185+
111186
mozconfigwrapper_buildwith_home
112187
mozconfigwrapper_setup_tab_completion
188+
mozconfigwrapper_setup_prompt
113189

114190
if [ -f "$BUILDWITH_HOME/.active" ]
115191
then

0 commit comments

Comments
 (0)