Skip to content

Commit 78835d1

Browse files
committed
perf(shell): streamline startup configuration
1 parent 5ac38f4 commit 78835d1

6 files changed

Lines changed: 133 additions & 148 deletions

File tree

.pkgList

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ intel-ucode
5151
iwd # Daemon to manage network connections (make sure to configure it with systemd-networkd, systemd-resolved)
5252
iw # iw wifi thingy
5353
jq # Json parser
54-
keychain # manage SSH agent
5554
keyd # keyboard deamon
5655
lazygit
5756
libnotify # library for sending desktop notifications

Justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ mac-system:
152152
install-pi:
153153
cd "$HOME/.pi/agent/extensions" && npm install
154154

155+
# Format the pi extensions.
156+
fmt:
157+
cd {{ quote(repo_dir) }}/home/.pi/agent/extensions && npm run format
158+
155159
# Typecheck, lint, and test the pi extensions.
156160
check:
157161
cd {{ quote(repo_dir) }}/home/.pi/agent/extensions && npm run check

home/.aliasrc

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,10 @@ urlencode() { printf "%s" "$1" | jq -sRr @uri; }
154154

155155
dshell() { docker run --entrypoint /bin/bash --rm -it "$1" 2>/dev/null || docker run --entrypoint /bin/sh --rm -it "$1"; }
156156

157-
ksc() { set -euo pipefail; s="$(klc | fzf)"; yq e -i ".current-context = \"$s\"" ~/.kube/config; }
158-
159-
# Download a directory from a GitHub repo via sparse checkout.
160-
# Usage: yoink <github-url-or-owner/repo> <dest> [branch] [subpath]
161-
# yoink foo/bar ./out -> default branch, full repo
162-
# yoink foo/bar ./out main -> branch main, full repo
163-
# yoink foo/bar ./out main src/pkg -> branch main, subpath src/pkg
164-
yoink() {
165-
local p=${1#https://github.com/} dest=$2
166-
local repo=${p%%/tree/*} spec=${p#*/tree/}
167-
local branch subpath=""
168-
[[ $spec != "$p" ]] && { branch=${spec%%/*}; subpath=${spec#*/}; [[ $subpath == "$spec" ]] && subpath=""; }
169-
[[ -e $dest ]] && { echo "yoink: $dest exists" >&2; return 1; }
170-
local tmp
171-
tmp=$(mktemp -d) || return
172-
gh repo clone "$repo" "$tmp/r" -- --depth 1 --filter=blob:none --sparse ${branch:+--branch "$branch"} &&
173-
(cd "$tmp/r" && git sparse-checkout set "$subpath") &&
174-
mkdir -p "$dest" && cp -r "$tmp/r/${subpath:-.}"/. "$dest/"
175-
rm -rf "$tmp"
157+
ksc() {
158+
set -euo pipefail
159+
s="$(klc | fzf)"
160+
yq e -i ".current-context = \"$s\"" ~/.kube/config
176161
}
177162

178163
_fzf_file_insert() {
@@ -259,8 +244,4 @@ bbcomp() {
259244
_fzf_dirs=(/opt/homebrew/Cellar/fzf/*/shell)
260245
[ -f "${_fzf_dirs[-1]}/key-bindings.$_sh" ] && . "${_fzf_dirs[-1]}/key-bindings.$_sh"
261246

262-
# keychain
263-
command -v keychain >/dev/null 2>&1 && eval "$(keychain --eval --quiet --noask)"
264-
[ -r "$HOME/.keychain/$HOSTNAME-sh" ] && . "$HOME/.keychain/$HOSTNAME-sh" 2>/dev/null
265-
266247
unset _sh _fzf_dirs

home/.bashrc

Lines changed: 82 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,102 +17,127 @@ HISTSIZE=-1
1717
HISTFILESIZE=-1
1818
HISTCONTROL=ignoredups
1919

20-
__prompt_is_unmerged_status() {
21-
case "$1" in
22-
DD | AU | UD | UA | DU | AA | UU) return 0 ;;
23-
*) return 1 ;;
24-
esac
25-
}
26-
2720
__prompt_git_operation() {
2821
local git_dir=$1
2922

3023
if [[ -f $git_dir/MERGE_HEAD ]]; then
31-
printf '%s\n' merge
24+
__prompt_git_operation_result=merge
3225
elif [[ -d $git_dir/rebase-merge || -d $git_dir/rebase-apply ]]; then
33-
printf '%s\n' rebase
26+
__prompt_git_operation_result=rebase
3427
elif [[ -f $git_dir/BISECT_LOG ]]; then
35-
printf '%s\n' bisect
28+
__prompt_git_operation_result=bisect
3629
elif [[ -f $git_dir/CHERRY_PICK_HEAD ]]; then
37-
printf '%s\n' cherry-pick
30+
__prompt_git_operation_result=cherry-pick
31+
else
32+
__prompt_git_operation_result=
3833
fi
3934
}
4035

41-
__prompt_git_info() {
42-
local branch_color=$1
43-
local staged_color=$2
44-
local unstaged_color=$3
45-
local untracked_color=$4
46-
local operation_color=$5
47-
local conflict_color=$6
48-
local reset=$7
49-
local branch git_dir operation line status index_status worktree_status
50-
local staged=0 unstaged=0 untracked=0 conflicts=0
51-
52-
git_dir=$(git rev-parse --git-dir 2>/dev/null) || return
53-
operation=$(__prompt_git_operation "$git_dir")
54-
55-
while IFS= read -r line; do
56-
if [[ $line == '## '* ]]; then
57-
branch=${line#'## '}
58-
branch=${branch#'No commits yet on '}
59-
branch=${branch%%...*}
60-
branch=${branch%% \[*}
61-
[[ $branch == HEAD\ * ]] && branch=HEAD
62-
continue
63-
fi
36+
__prompt_git_cache_key=
37+
__prompt_git_dir=
6438

65-
status=${line:0:2}
66-
index_status=${line:0:1}
67-
worktree_status=${line:1:1}
39+
__prompt_find_git_dir() {
40+
local cache_key candidate dir git_file parent target
6841

69-
if [[ $status == '??' ]]; then
70-
((untracked++))
71-
elif __prompt_is_unmerged_status "$status"; then
72-
((conflicts++))
42+
cache_key="${PWD}"$'\034'"${GIT_DIR-}"$'\034'"${GIT_WORK_TREE-}"$'\034'"${GIT_COMMON_DIR-}"
43+
[[ $cache_key == "$__prompt_git_cache_key" ]] && return
44+
45+
__prompt_git_cache_key=$cache_key
46+
__prompt_git_dir=
47+
48+
if [[ -n ${GIT_DIR:-} ]]; then
49+
if [[ $GIT_DIR == /* ]]; then
50+
candidate=$GIT_DIR
7351
else
74-
[[ $index_status != ' ' ]] && ((staged++))
75-
[[ $worktree_status != ' ' ]] && ((unstaged++))
52+
candidate=$PWD/$GIT_DIR
7653
fi
77-
done < <(git --no-optional-locks status --porcelain=v1 --branch 2>/dev/null)
54+
[[ -f $candidate/HEAD ]] && __prompt_git_dir=$candidate
55+
return
56+
fi
7857

58+
dir=$PWD
59+
while :; do
60+
candidate=$dir/.git
61+
if [[ -d $candidate ]]; then
62+
__prompt_git_dir=$candidate
63+
return
64+
elif [[ -f $candidate ]]; then
65+
IFS= read -r git_file < "$candidate" || return
66+
git_file=${git_file%$'\r'}
67+
if [[ $git_file == gitdir:* ]]; then
68+
target=${git_file#gitdir:}
69+
target="${target#"${target%%[![:space:]]*}"}"
70+
if [[ $target == /* ]]; then
71+
candidate=$target
72+
else
73+
candidate=$dir/$target
74+
fi
75+
[[ -f $candidate/HEAD ]] && __prompt_git_dir=$candidate
76+
fi
77+
return
78+
elif [[ -f $dir/HEAD && -d $dir/objects && -d $dir/refs ]]; then
79+
__prompt_git_dir=$dir
80+
return
81+
fi
82+
83+
[[ $dir == / ]] && return
84+
parent=${dir%/*}
85+
dir=${parent:-/}
86+
done
87+
}
88+
89+
__prompt_git_info() {
90+
local branch_color=$1
91+
local operation_color=$2
92+
local reset=$3
93+
local branch head
94+
95+
__prompt_git_segment=
96+
__prompt_find_git_dir
97+
[[ -n $__prompt_git_dir ]] || return
98+
IFS= read -r head < "$__prompt_git_dir/HEAD" || return
99+
head=${head%$'\r'}
100+
101+
if [[ $head == 'ref: refs/heads/'* ]]; then
102+
branch=${head#'ref: refs/heads/'}
103+
elif [[ $head == 'ref: '* ]]; then
104+
branch=${head#'ref: '}
105+
else
106+
branch=HEAD
107+
fi
79108
[[ -n $branch ]] || return
80109

81-
printf '%s%s%s' "$branch_color" "$branch" "$reset"
82-
[[ $staged -gt 0 ]] && printf ' %s+%d%s' "$staged_color" "$staged" "$reset"
83-
[[ $unstaged -gt 0 ]] && printf ' %s~%d%s' "$unstaged_color" "$unstaged" "$reset"
84-
[[ $untracked -gt 0 ]] && printf ' %s?%d%s' "$untracked_color" "$untracked" "$reset"
85-
[[ -n $operation ]] && printf ' %s%s%s' "$operation_color" "$operation" "$reset"
86-
[[ $conflicts -gt 0 ]] && printf ' %sconflict:%d%s' "$conflict_color" "$conflicts" "$reset"
87-
printf '\n'
110+
__prompt_git_segment="${branch_color}${branch}${reset}"
111+
__prompt_git_operation "$__prompt_git_dir"
112+
if [[ -n $__prompt_git_operation_result ]]; then
113+
__prompt_git_segment+=" ${operation_color}${__prompt_git_operation_result}${reset}"
114+
fi
88115
}
89116

90117
__prompt_render() {
91118
local exit_status=$1
92119
local cwd=${PWD##*/}
93-
local host git_info segments=()
120+
local host segments=()
94121
local reset='\[\e[0m\]'
95122
local bold='\[\e[1m\]'
96123
local dim='\[\e[2m\]'
97124
local slate='\[\e[38;5;245m\]'
98125
local red='\[\e[38;5;203m\]'
99126
local green='\[\e[38;5;114m\]'
100-
local yellow='\[\e[38;5;179m\]'
101127
local blue='\[\e[38;5;75m\]'
102128
local purple='\[\e[38;5;141m\]'
103-
local cyan='\[\e[38;5;109m\]'
104129

105130
[[ -n $cwd ]] || cwd=/
106131

107132
if [[ -n ${SSH_CLIENT:-} ]]; then
108-
host=$(hostname -s 2>/dev/null || hostname)
133+
host=${HOSTNAME%%.*}
109134
segments+=("${slate}${USER}@${host}${reset}")
110135
fi
111136

112137
segments+=("${bold}${blue}${cwd}${reset}")
113138

114-
git_info=$(__prompt_git_info "$green" "$cyan" "$yellow" "$purple" "$purple" "$red" "$reset")
115-
[[ -n $git_info ]] && segments+=("$git_info")
139+
__prompt_git_info "$green" "$purple" "$reset"
140+
[[ -n $__prompt_git_segment ]] && segments+=("$__prompt_git_segment")
116141
[[ -n ${VIRTUAL_ENV:-} ]] && segments+=("${purple}venv${reset}")
117142
[[ $exit_status -ne 0 ]] && segments+=("${red}$exit_status${reset}")
118143

@@ -145,8 +170,6 @@ PROMPT_COMMAND=__prompt_command
145170
# huponexit send SIGHUP to all jobs when an interactive login shell exits
146171
shopt -s autocd cdspell dirspell histappend checkjobs direxpand checkwinsize cmdhist huponexit
147172

148-
stty -ixon # Disable ctrl-s and ctrl-q.
149-
150173
# Load aliases
151174
if [ -f "$HOME/.aliasrc" ]; then
152175
# shellcheck disable=SC1091

home/.bashrc.d/envload.bash

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ __dotenv_active_names=()
66
__dotenv_active_values=()
77
__dotenv_active_had_values=()
88
__dotenv_active_exported=()
9+
__dotenv_last_pwd=
910

1011
__dotenv_find() {
1112
local __dotenv__dir=$PWD
@@ -99,7 +100,8 @@ __dotenv_expand() {
99100
}
100101

101102
__dotenv_load() {
102-
local __dotenv__file=$1 __dotenv__line __dotenv__key __dotenv__value __dotenv__expand_value
103+
local __dotenv__file=$1 __dotenv__signature=$2
104+
local __dotenv__line __dotenv__key __dotenv__value __dotenv__expand_value
103105
local __dotenv__declaration __dotenv__old_value __dotenv__line_number=0 __dotenv__was_exported
104106

105107
while IFS= read -r __dotenv__line || [[ -n $__dotenv__line ]]; do
@@ -182,13 +184,18 @@ __dotenv_load() {
182184
done < "$__dotenv__file"
183185

184186
__dotenv_active_file=$__dotenv__file
185-
__dotenv_active_signature=$(__dotenv_signature "$__dotenv__file")
187+
__dotenv_active_signature=$__dotenv__signature
186188
printf 'envload: loaded %s\n' "$__dotenv__file"
187189
}
188190

189191
__dotenv_update() {
190192
local __dotenv__force=${1:-} __dotenv__signature
191193

194+
if [[ -z $__dotenv__force && $__dotenv_last_pwd == "$PWD" ]]; then
195+
return
196+
fi
197+
__dotenv_last_pwd=$PWD
198+
192199
__dotenv_find
193200
if [[ -n $__dotenv_found_file ]]; then
194201
__dotenv__signature=$(__dotenv_signature "$__dotenv_found_file")
@@ -199,7 +206,7 @@ __dotenv_update() {
199206
fi
200207

201208
[[ -n $__dotenv_active_file ]] && __dotenv_unload
202-
[[ -n $__dotenv_found_file ]] && __dotenv_load "$__dotenv_found_file"
209+
[[ -n $__dotenv_found_file ]] && __dotenv_load "$__dotenv_found_file" "$__dotenv__signature"
203210
}
204211

205212
envload() {

0 commit comments

Comments
 (0)