-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sh
More file actions
101 lines (89 loc) · 2.7 KB
/
Copy pathfunctions.sh
File metadata and controls
101 lines (89 loc) · 2.7 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
#!/usr/bin/env bash
source $(dirname $BASH_SOURCE)/colors.sh
source $(dirname $BASH_SOURCE)/icons.sh
# trim function, from dylanaraps
# https://github.com/dylanaraps/pure-sh-bible#trim-all-white-space-from-string-and-truncate-spaces
trim_all() {
set -f
set -- $*
printf '%s\n' "$*"
set +f
}
# These two functions deal with current directory.
shrink_path() {
local path=$1
path="${path/"$HOME"/\~}"
# shrink paths to one letter each
# inspiration from powerlevel10k
if [ ${#path} -gt 32 ]; then
IFS='/' read -r -a path_array <<< "${path}"
for entry in "${path_array[@]::${#path_array[@]}-1}"; do
path="${path/"/$entry/"/"/${entry:0:1}/"}"
done
fi
_color green
printf "${path}"
_reset
}
# Implements lazy loading on path
# Produce path again only when it changes
get_path() {
CURRENT_REAL_PATH=$(pwd)
if [ ! "${CACHED_REAL_PATH}" == "${CURRENT_REAL_PATH}" ]; then
CACHED_SHRINK_PATH=$(shrink_path "${CURRENT_REAL_PATH}")
CACHED_REAL_PATH=${CURRENT_REAL_PATH}
fi
printf "${CACHED_SHRINK_PATH}"
}
get_status() {
[[ -n "${IN_NIX_SHELL}" && -z "${DIRENV_FILE}" ]] && printf "${ICON_NIX}"
[[ -n "${DIRENV_FILE}" ]] && printf "${ICON_DIRENV}"
[[ -n "${IN_DOCKER}" ]] && printf "${ICON_DOCKER}"
[[ -n "${SSH_CLIENT}" ]] && printf "${ICON_SSH}"
}
get_hostname() {
if [[ -n "${SSH_CLIENT}" ]]; then
_color purple
printf "\h"
_reset
fi
}
get_arrow() {
if [[ $1 == 0 ]]; then
_color teal
else
_color red
fi
printf "${ICON_ARROW}"
_reset
}
get_gitstatus() {
if [[ ${HAS_GITSTATUS} == 1 ]]; then
local output='';
if gitstatus_query && [[ "$VCS_STATUS_RESULT" == ok-sync ]]; then
if [[ -n "$VCS_STATUS_LOCAL_BRANCH" ]]; then
output+="${VCS_STATUS_LOCAL_BRANCH//\\/\\\\}" # escape backslash
else
output+="@${VCS_STATUS_COMMIT//\\/\\\\}" # escape backslash
fi
(( VCS_STATUS_NUM_STAGED )) && output+="${ICON_TICK}${VCS_STATUS_NUM_STAGED}"
(( VCS_STATUS_NUM_UNSTAGED )) && output+="${ICON_PENCIL}${VCS_STATUS_NUM_UNSTAGED}"
(( VCS_STATUS_NUM_UNTRACKED )) && output+="${ICON_WARN}${VCS_STATUS_NUM_UNTRACKED}"
(( VCS_STATUS_COMMITS_AHEAD )) && output+="${ICON_UPARROW}${VCS_STATUS_COMMITS_AHEAD}"
(( VCS_STATUS_COMMITS_BEHIND )) && output+="${ICON_DOWNARROW}${VCS_STATUS_COMMITS_BEHIND}"
fi
_color blue
printf "${ICON_GIT} ${output}"
_reset
fi
}
if command -v gitstatusd 2>&1 >/dev/null && [ -z ${GITSTATUS_PLUGIN_PATH} ]; then
GITSTATUS_PLUGIN_PATH=$(realpath "$(dirname $(which gitstatusd))/../share/gitstatus")
fi
if [ -d "${GITSTATUS_PLUGIN_PATH}" ]; then
source ${GITSTATUS_PLUGIN_PATH}/gitstatus.plugin.sh
gitstatus_stop && gitstatus_start
HAS_GITSTATUS=1
fi
[[ ${PATH} == /nix/store/* ]] && IN_NIX_SHELL=1
grep -q docker /proc/1/cgroup && IN_DOCKER=1