Skip to content

Commit bf921b0

Browse files
author
Jonathan Dahan
committed
Inline lots of environment variables
The biggest change is going from := to :- The downside is we dont have these nice list of environment variables at the top of each function. The upside is we are no longer making global assignments The main difference between := and :- is that :- just substitutes the word on the right hand side inline, while := assigns the variable on the left hand side to the value on the right. echo ${A:=something} # assign A to 'something', then expand $A echo ${A:-something} # expand to 'something' One reason this is hard to understand is the zsh manpages describe := and :- in an inverted manner - one description uses negatives and exceptions, while the other uses positives. I'm not sure if its more accurate but at least we aren't polluting variables as much. This rabbit hole happened when I came across `setopt warn_global_assign`
1 parent 16ebba4 commit bf921b0

File tree

6 files changed

+63
-81
lines changed

6 files changed

+63
-81
lines changed

functions/geometry_echo.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# geometry_echo - simple echo for a balanced terminal
22

3-
geometry_echo() { echo -n ${GEOMETRY_ECHO:=''}; }
3+
geometry_echo() { echo -n ${GEOMETRY_ECHO:-''}; }

functions/geometry_git.zsh

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,78 +13,64 @@ geometry_git_time() {
1313
local seconds_since_last_commit
1414
last_commit=$(git log -1 --pretty=format:'%at' 2> /dev/null)
1515

16-
[[ -z "$last_commit" ]] && ansi ${GEOMETRY_COLOR_NO_TIME:=default} ${GEOMETRY_GIT_NO_COMMITS_MESSAGE:=no-commits} && return
16+
[[ -z "$last_commit" ]] && ansi ${GEOMETRY_COLOR_NO_TIME:-default} ${GEOMETRY_GIT_NO_COMMITS_MESSAGE:-no-commits} && return
1717

1818
now=$(date +%s)
1919
seconds_since_last_commit=$((now - last_commit))
20-
geometry::time $seconds_since_last_commit ${GEOMETRY_GIT_TIME_DETAILED:=false}
20+
geometry::time $seconds_since_last_commit ${GEOMETRY_GIT_TIME_DETAILED:-false}
2121
}
2222

2323
geometry_git_branch() {
24-
ansi ${GEOMETRY_GIT_COLOR_BRANCH:=242} $(git symbolic-ref --short HEAD || git rev-parse --short HEAD)
24+
ansi ${GEOMETRY_GIT_COLOR_BRANCH:-242} $(git symbolic-ref --short HEAD || git rev-parse --short HEAD)
2525
}
2626

2727
geometry_git_status() {
28-
: ${GEOMETRY_GIT_COLOR_DIRTY:=red}
29-
: ${GEOMETRY_GIT_COLOR_CLEAN:=green}
30-
: ${GEOMETRY_GIT_SYMBOL_DIRTY:=""}
31-
: ${GEOMETRY_GIT_SYMBOL_CLEAN:=""}
32-
local _status
3328
command git rev-parse --git-dir > /dev/null 2>&1 || return
34-
_status=$([[ -z "$(git status --porcelain --ignore-submodules HEAD)" ]] && [[ -z "$(git ls-files --others --modified --exclude-standard $(git rev-parse --show-toplevel))" ]] && echo CLEAN || echo DIRTY)
35-
ansi ${(e):-\$GEOMETRY_GIT_COLOR_${_status}} ${(e):-\$GEOMETRY_GIT_SYMBOL_${_status}}
29+
30+
[[ -z "$(git status --porcelain --ignore-submodules HEAD)" ]] \
31+
&& [[ -z "$(git ls-files --others --modified --exclude-standard $(git rev-parse --show-toplevel))" ]] \
32+
&& ansi ${GEOMETRY_GIT_COLOR_DIRTY:-red} ${GEOMETRY_GIT_SYMBOL_DIRTY:-""} \
33+
|| ansi ${GEOMETRY_GIT_COLOR_CLEAN:-green} ${GEOMETRY_GIT_SYMBOL_CLEAN:-""}
3634
}
3735

3836
geometry_git_rebase() {
39-
: ${GEOMETRY_GIT_SYMBOL_REBASE:="®"}
4037
local git_dir
4138
git_dir=$(git rev-parse --git-dir)
4239
[[ -d "$git_dir/rebase-merge" ]] || [[ -d "$git_dir/rebase-apply" ]] || return
43-
echo "$GEOMETRY_GIT_SYMBOL_REBASE"
40+
echo ${GEOMETRY_GIT_SYMBOL_REBASE:-"®"}
4441
}
4542

4643
geometry_git_remote() {
47-
: ${GEOMETRY_GIT_SYMBOL_UNPUSHED:=""}
48-
: ${GEOMETRY_GIT_SYMBOL_UNPULLED:=""}
49-
: ${GEOMETRY_GIT_SYMBOL_CONFLICTS_SOLVED:=""}
50-
: ${GEOMETRY_GIT_SYMBOL_CONFLICTS_UNSOLVED:=""}
51-
52-
local common_base
53-
local local_commit
54-
local remote_commit
55-
local_commit=$(git rev-parse "@" 2>/dev/null)
56-
remote_commit=$(git rev-parse "@{u}" 2>/dev/null)
44+
local unpushed=${GEOMETRY_GIT_SYMBOL_UNPUSHED:-""}
45+
local unpulled=${GEOMETRY_GIT_SYMBOL_UNPULLED:-""}
46+
local local_commit && local_commit=$(git rev-parse "@" 2>/dev/null)
47+
local remote_commit && remote_commit=$(git rev-parse "@{u}" 2>/dev/null)
5748

5849
[[ $local_commit == "@" || $local_commit == $remote_commit ]] && return
5950

60-
common_base=$(git merge-base "@" "@{u}" 2>/dev/null) # last common commit
61-
[[ $common_base == $remote_commit ]] && echo $GEOMETRY_GIT_SYMBOL_UNPUSHED && return
62-
[[ $common_base == $local_commit ]] && echo $GEOMETRY_GIT_SYMBOL_UNPULLED && return
51+
local common_base && common_base=$(git merge-base "@" "@{u}" 2>/dev/null) # last common commit
52+
[[ $common_base == $remote_commit ]] && echo $unpushed && return
53+
[[ $common_base == $local_commit ]] && echo $unpulled && return
6354

64-
echo "$GEOMETRY_GIT_SYMBOL_UNPUSHED $GEOMETRY_GIT_SYMBOL_UNPULLED"
55+
echo "$unpushed $unpulled"
6556
}
6657

6758
geometry_git_symbol() { echo ${(j: :):-$(geometry_git_rebase) $(geometry_git_remote)}; }
6859

6960
geometry_git_conflicts() {
70-
: ${GEOMETRY_GIT_COLOR_CONFLICTS_UNSOLVED:=red}
71-
: ${GEOMETRY_GIT_COLOR_CONFLICTS_SOLVED:=green}
72-
local conflicts
7361
local _grep
74-
local conflict_list
75-
local raw_file_count
76-
local file_count
77-
local raw_total
78-
local total
62+
local conflicts conflict_list
63+
local file_count raw_file_count
64+
local total raw_total
7965
conflicts=$(git diff --name-only --diff-filter=U)
8066

8167
[[ -z "$conflicts" ]] && return
8268

8369
pushd -q $(git rev-parse --show-toplevel)
8470

8571
_grep=${GEOMETRY_GIT_GREP:=${commands[rg]:=${commands[ag]:=${commands[grep]}}}}
86-
8772
conflict_list=$($_grep -cH '^=======$' $conflicts)
73+
8874
popd -q
8975

9076
raw_file_count="${#${(@f)conflict_list}}"
@@ -93,9 +79,11 @@ geometry_git_conflicts() {
9379
raw_total=$(echo $conflict_list | cut -d ':' -f2 | paste -sd+ - | bc)
9480
total=${raw_total##*( )}
9581

96-
[[ -z "$total" ]] && ansi $GEOMETRY_GIT_COLOR_CONFLICTS_SOLVED $GEOMETRY_GIT_SYMBOL_CONFLICTS_SOLVED && return
82+
[[ -z "$total" ]] && ansi ${GEOMETRY_GIT_COLOR_CONFLICTS_SOLVED:-green} ${GEOMETRY_GIT_SYMBOL_CONFLICTS_SOLVED:-""} && return
83+
84+
count="(${file_count}f|${total}c)"
9785

98-
ansi $GEOMETRY_GIT_COLOR_CONFLICTS_UNSOLVED "$GEOMETRY_GIT_SYMBOL_CONFLICTS_UNSOLVED (${file_count}f|${total}c)"
86+
ansi ${GEOMETRY_GIT_COLOR_CONFLICTS_UNSOLVED:-red} "${GEOMETRY_GIT_SYMBOL_CONFLICTS_UNSOLVED:-''} $count"
9987
}
10088

10189
geometry_git() {
@@ -105,16 +93,13 @@ geometry_git() {
10593
&& ansi ${GEOMETRY_GIT_COLOR_BARE:=blue} ${GEOMETRY_GIT_SYMBOL_BARE:=""} \
10694
&& return
10795

108-
echo -n $(geometry_git_symbol) $(geometry_git_branch) \
109-
$(geometry::git_wrapper \
110-
$(geometry_git_conflicts) \
111-
$(geometry_git_time) \
112-
$(geometry_git_stashes) \
113-
$(geometry_git_status)
114-
)
115-
}
96+
local geometry_git_details && geometry_git_details=(
97+
$(geometry_git_conflicts)
98+
$(geometry_git_time)
99+
$(geometry_git_stashes)
100+
$(geometry_git_status)
101+
)
116102

117-
geometry::git_wrapper() {
118-
: ${GEOMETRY_GIT_SEPARATOR:=" :: "}
119-
echo -n ${(pj.$GEOMETRY_GIT_SEPARATOR.)$(print -r "$@")}
103+
local separator=${GEOMETRY_GIT_SEPARATOR:-" :: "}
104+
echo -n $(geometry_git_symbol) $(geometry_git_branch) ${(pj.$separator.)geometry_git_details}
120105
}

functions/geometry_hostname.zsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
geometry_hostname() {
44
local _host=${HOST:-$HOSTNAME}
5-
[[ "$_host" = "${GEOMETRY_HOSTNAME_HIDE_ON:=localhost}" ]] && return
6-
echo -n "${USER}${GEOMETRY_HOSTNAME_SEPARATOR:=@}${_host}"
5+
[[ "$_host" = "${GEOMETRY_HOSTNAME_HIDE_ON:-localhost}" ]] && return
6+
echo -n "${USER}${GEOMETRY_HOSTNAME_SEPARATOR:-@}${_host}"
77
}

functions/geometry_path.zsh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# geometry_path - show the current path
22

33
geometry_path() {
4-
local dir=${GEOMETRY_PATH_SYMBOL_HOME:="%3~"} # symbol representing the home directory
5-
( ${GEOMETRY_PATH_SHOW_BASENAME:=false} ) && dir=${PWD:t}
6-
ansi ${GEOMETRY_PATH_COLOR:=blue} $dir
4+
local dir=${GEOMETRY_PATH_SYMBOL_HOME:-"%3~"} # symbol representing the home directory
5+
( ${GEOMETRY_PATH_SHOW_BASENAME:-false} ) && dir=${PWD:t}
6+
ansi ${GEOMETRY_PATH_COLOR:-blue} $dir
77
}

functions/geometry_status.zsh

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
# geometry_status - show a symbol with error/success and root/non-root information
22

33
geometry_status() {
4-
: ${GEOMETRY_STATUS_COLOR:=default}
5-
: ${GEOMETRY_STATUS_COLOR_ERROR:=red}
6-
: ${GEOMETRY_STATUS_SYMBOL:=▲}
7-
: ${GEOMETRY_STATUS_SYMBOL_ERROR:=△}
8-
: ${GEOMETRY_STATUS_SYMBOL_ROOT:=▼}
9-
: ${GEOMETRY_STATUS_SYMBOL_ROOT_ERROR:=▽}
4+
local root; [[ $UID = 0 || $EUID = 0 ]] && root=true || root=false
5+
local error; (( $GEOMETRY_LAST_STATUS )) && error=true || error=false
106

11-
( ${GEOMETRY_STATUS_SYMBOL_COLOR_HASH:=false} ) && {
12-
local colors=({1..9})
7+
local color=${GEOMETRY_STATUS_COLOR:-default}
138

14-
(($(echotc Co) == 256)) && colors+=({17..230})
9+
if ( $error ); then color=${GEOMETRY_STATUS_COLOR_ERROR:-red}; else
10+
( ${GEOMETRY_STATUS_SYMBOL_COLOR_HASH:=false} ) && {
11+
local colors=({1..9})
1512

16-
if (( ${+GEOMETRY_STATUS_SYMBOL_COLOR_HASH_COLORS} )); then
17-
colors=(${GEOMETRY_STATUS_SYMBOL_COLOR_HASH_COLORS})
18-
fi
13+
(($(echotc Co) == 256)) && colors+=({17..230})
1914

20-
local sum=0; for c in ${(s::)^HOST}; do ((sum += $(print -f '%d' "'$c"))); done
15+
if (( ${+GEOMETRY_STATUS_SYMBOL_COLOR_HASH_COLORS} )); then
16+
colors=(${GEOMETRY_STATUS_SYMBOL_COLOR_HASH_COLORS})
17+
fi
2118

22-
local index=$(($sum % ${#colors}))
19+
local sum=0; for c in ${(s::)^HOST}; do ((sum += $(print -f '%d' "'$c"))); done
2320

24-
[[ "$index" -eq 0 ]] && index=1
21+
local index=$(($sum % ${#colors}))
2522

26-
GEOMETRY_STATUS_COLOR=${colors[${index}]}
27-
}
23+
[[ "$index" -eq 0 ]] && index=1
2824

29-
local color=GEOMETRY_STATUS_COLOR symbol=GEOMETRY_STATUS_SYMBOL
30-
[[ $UID = 0 || $EUID = 0 ]] && symbol+=_ROOT
31-
(( $GEOMETRY_LAST_STATUS )) && symbol+=_ERROR && color+=_ERROR
25+
color=${colors[${index}]}
26+
}
27+
fi
3228

33-
ansi ${(P)color} ${(P)symbol}
29+
( $root && $error ) && ansi $color ${GEOMETRY_STATUS_SYMBOL_ROOT_ERROR:=▽} && return
30+
( $root ) && ansi $color ${GEOMETRY_STATUS_SYMBOL_ROOT:-▼} && return
31+
( $error ) && ansi $color ${GEOMETRY_STATUS_SYMBOL_ERROR:-△} && return
32+
ansi $color ${GEOMETRY_STATUS_SYMBOL:-▲}
3433
}

geometry.zsh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ add-zsh-hook precmd geometry::clear_title
5353
# join outputs of functions - pwd first
5454
geometry::wrap() {
5555
GEOMETRY_LAST_STATUS="$status"
56-
local -a outputs
57-
local cmd
58-
local output
5956
setopt localoptions noautopushd; builtin cd -q $1
57+
local -a outputs
58+
local cmd output
6059
shift
6160
for cmd in $@; do output=$($cmd); (( $? )) || outputs+=$output; done
6261
echo "${(ps.${GEOMETRY_SEPARATOR}.)outputs}"
@@ -85,10 +84,9 @@ geometry::prompt() {
8584
add-zsh-hook precmd geometry::prompt
8685

8786
geometry::info() { # draw info if no command is given
88-
local info
8987
[[ -n "$BUFFER" ]] && { zle accept-line && return; }
90-
info="$(geometry::wrap $PWD $GEOMETRY_INFO)"
91-
echo "${(%)info}" && geometry::prompt
88+
echo ${(%):-$(geometry::wrap $PWD $GEOMETRY_INFO)}
89+
geometry::prompt
9290
}
9391
zle -N buffer-empty geometry::info
9492
bindkey '^M' buffer-empty

0 commit comments

Comments
 (0)