Display the current wit branch in your terminal prompt
Display your current wit branch directly in your terminal prompt, just like you would with Git. This helps you always know which branch you’re working on.
wit provides plumbing commands that output the current branch name, which can be used in your shell prompt:
Copy
# Get the current branch name (returns "HEAD" if detached)wit rev-parse --abbrev-ref HEAD# Alternative: Get the symbolic ref (fails silently if detached)wit symbolic-ref --short HEAD 2>/dev/null
# wit branch in prompt__wit_branch() { local branch # Try to get the current branch name branch=$(wit rev-parse --abbrev-ref HEAD 2>/dev/null) if [ -n "$branch" ]; then echo " ($branch)" fi}# Customize your prompt - add the branch after the current directoryPS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[33m\]$(__wit_branch)\[\033[00m\]\$ '
# Enable command substitution in promptssetopt PROMPT_SUBST# wit branch function__wit_branch() { local branch branch=$(wit rev-parse --abbrev-ref HEAD 2>/dev/null) if [[ -n "$branch" ]]; then echo " ($branch)" fi}# Add to your promptPROMPT='%F{green}%n@%m%f:%F{blue}%~%f%F{yellow}$(__wit_branch)%f$ '
If you use Oh My Zsh, you can create a custom plugin. Create the file ~/.oh-my-zsh/custom/plugins/wit/wit.plugin.zsh:
Copy
# wit integration for Oh My Zsh# Get current wit branchwit_prompt_info() { local branch branch=$(wit rev-parse --abbrev-ref HEAD 2>/dev/null) if [[ -n "$branch" ]]; then echo "%{$fg[yellow]%}($branch)%{$reset_color%}" fi}# Add to right promptRPROMPT='$(wit_prompt_info)'
__wit_branch() { local branch branch=$(wit symbolic-ref --short HEAD 2>/dev/null) if [ -n "$branch" ]; then echo " ($branch)" else # Detached HEAD - show short commit hash local commit commit=$(wit rev-parse --short HEAD 2>/dev/null) if [ -n "$commit" ]; then echo " (detached:$commit)" fi fi}
__wit_branch() { local branch branch=$(wit symbolic-ref --short HEAD 2>/dev/null) if [[ -n "$branch" ]]; then echo " ($branch)" else # Detached HEAD - show short commit hash local commit commit=$(wit rev-parse --short HEAD 2>/dev/null) if [[ -n "$commit" ]]; then echo " (detached:$commit)" fi fi}
__wit_branch() { local branch dirty="" branch=$(wit rev-parse --abbrev-ref HEAD 2>/dev/null) if [ -n "$branch" ]; then # Check for uncommitted changes if ! wit diff --quiet HEAD 2>/dev/null; then dirty="*" fi echo " ($branch$dirty)" fi}
__wit_branch() { local branch dirty="" branch=$(wit rev-parse --abbrev-ref HEAD 2>/dev/null) if [[ -n "$branch" ]]; then # Check for uncommitted changes if ! wit diff --quiet HEAD 2>/dev/null; then dirty="*" fi echo " ($branch$dirty)" fi}
The prompt function runs every time you press Enter, so it’s important to keep it fast.
Use 2>/dev/null to suppress error messages when not in a wit repository
Avoid multiple wit calls - each command spawns a new process
Consider caching for very large repositories:
Copy
# Cache the branch name for 1 second__wit_branch_cached() { local cache_file="/tmp/.wit_branch_cache_$$" local cache_age=1 if [ -f "$cache_file" ]; then local mod_time=$(stat -c %Y "$cache_file" 2>/dev/null || stat -f %m "$cache_file" 2>/dev/null) local now=$(date +%s) if [ $((now - mod_time)) -lt $cache_age ]; then cat "$cache_file" return fi fi local result=$(__wit_branch) echo "$result" > "$cache_file" echo "$result"}
For Fish shell users, add this to ~/.config/fish/functions/fish_prompt.fish:
Copy
function fish_prompt set -l branch (wit rev-parse --abbrev-ref HEAD 2>/dev/null) set_color green echo -n (whoami)"@"(hostname) set_color normal echo -n ":" set_color blue echo -n (prompt_pwd) if test -n "$branch" set_color yellow echo -n " ($branch)" end set_color normal echo -n '$ 'end