-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt
More file actions
executable file
·103 lines (93 loc) · 3.25 KB
/
prompt
File metadata and controls
executable file
·103 lines (93 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Selects a colour for the prompt that wasn't used by the previous terminal
function init_prompt() {
# 0=black, 1=red, 2=green, 3=yellow, 4=blue, 5=magenta, 6=cyan, 7=grey, 8-15=bright variants
if [[ $SHELL == "/bin/zsh" ]]; then
if [[ -f ~/.zsh_colour ]]; then
export ZSH_COLOUR=$(cat ~/.zsh_colour)
else
export ZSH_COLOUR=0
fi
let 'ZSH_COLOUR = (ZSH_COLOUR + 1) % 4'
echo "$ZSH_COLOUR" > ~/.zsh_colour
if [[ "ZSH_COLOUR" -ge 2 ]]; then
let 'ZSH_COLOUR += 3'
else
let 'ZSH_COLOUR += 1'
fi
precmd_functions+=(update_prompt)
elif [[ $SHELL == "/bin/bash" ]]; then
if [[ -f ~/.bash_colour ]]; then
export BASH_COLOUR=$(cat ~/.bash_colour)
else
export BASH_COLOUR=0
fi
let 'BASH_COLOUR = (BASH_COLOUR + 1) % 4'
echo $BASH_COLOUR > ~/.bash_colour
if [[ "$BASH_COLOUR" -ge 2 ]]; then
let 'BASH_COLOUR += 33'
else
let 'BASH_COLOUR += 31'
fi
export PROMPT_COMMAND=update_prompt
else
echo "Unknown shell. Will not configure prompt colour."
fi
}
# Sets the prompt with the current directory, git branch and git status
function update_prompt() {
local host=${HOSTNAME%.local}
local user=$USER
if [[ $SHELL == "/bin/zsh" ]]; then
local colour="%F{$ZSH_COLOUR}"
local reset="%f"
else
local colour="\[\e[${BASH_COLOUR}m\]"
local reset="\[\e[0m\]"
fi
local repo=$(git rev-parse --show-toplevel 2> /dev/null)
if [[ ! -z "$repo" ]]; then
if [[ $SHELL == "/bin/zsh" ]]; then
local red='%F{1}'
local green='%F{2}'
local yellow='%F{3}'
else
local red='\[\e[31m\]'
local green='\[\e[32m\]'
local yellow='\[\e[33m\]'
fi
local branch='HEAD'
if [[ ! -z $(git branch) ]]; then
# Get the current branch name or use "HEAD" if repo is newly initialized
branch=$(git rev-parse --abbrev-ref HEAD)
fi
local stat=$(git status --porcelain)
local warn=$green
if [[ ! -z $(echo "$stat" | cut -c1-2 | grep 'U') ]]; then
# Contains conflicts
warn=$red
elif [[ ! -z "$stat" ]]; then
# Contains modifications
warn=$yellow
fi
local pwd_phys=$(pwd -P)
local base=$(basename "$repo")
local path=$(echo "${pwd_phys#$repo}" | $MISC_ROOT/abbrev_path)
if [[ $SHELL == "/bin/zsh" ]]; then
export PROMPT="${colour}%n@%m${reset} ${base}${warn}(${branch})${reset}${path} ${colour}%#${reset} "
else
export PS1="${colour}${host}:${reset}${base}${warn}(${branch})${reset}${path} ${colour}${user}\$${reset} "
fi
else
local path=$PWD
if [[ "$path" =~ ^"$HOME"(/|$) ]]; then
path="~${path#$HOME}"
fi
path=$(echo "$path" | $MISC_ROOT/abbrev_path)
if [[ $SHELL == "/bin/zsh" ]]; then
export PROMPT="${colour}%n@%m${reset} %1~ ${colour}%#${reset} "
else
export PS1="${colour}${host}:${reset}${path} ${colour}${user}\$${reset} "
fi
fi
}