-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetaround.sh
More file actions
230 lines (199 loc) · 6.3 KB
/
getaround.sh
File metadata and controls
230 lines (199 loc) · 6.3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# getaround.sh
# helpers
_ga_warn() { printf '%s\n' "$*" >&2; }
# resolve absolute path of this script
_ga_script_path() {
local src dir base
if [ -n "$BASH_SOURCE" ]; then
src="${BASH_SOURCE[0]}"
elif [ -n "$ZSH_VERSION" ]; then
src="${(%):-%x}"
else
src="$0"
fi
if command -v realpath >/dev/null 2>&1; then
realpath "$src"
else
dir="$(cd "$(dirname "$src")" >/dev/null 2>&1 && pwd -P)"
base="$(basename "$src")"
printf '%s/%s\n' "$dir" "$base"
fi
}
# env
GETAROUND_VERSION="1.0.2"
# set EDITOR dynamically: nvim > vim > vi > nano (fallback vi)
for candidate in nvim vim vi nano; do
if command -v "$candidate" >/dev/null 2>&1; then
export EDITOR="$candidate"
break
fi
done
: "${EDITOR:=vi}"
# set GETAROUND_ROOT dynamically (to script's directory)
if [ -z "$GETAROUND_ROOT" ]; then
script_path="$(_ga_script_path)"
export GETAROUND_ROOT="$(cd "$(dirname "$script_path")" && pwd -P)"
fi
# optional bin on PATH
if [ -d "$GETAROUND_ROOT/bin" ]; then
case ":$PATH:" in
*":$GETAROUND_ROOT/bin:"*) : ;;
*) PATH="$GETAROUND_ROOT/bin:$PATH" ;;
esac
fi
# terminal flow control
stty -ixon 2>/dev/null || true
# functions
ga-welcome() { printf 'Welcome to getaround (%s). EDITOR=%s\n' "$GETAROUND_VERSION" "$EDITOR"; }
getaround() { ga-welcome; }
ga-save() {
if [ -z "$1" ]; then
echo "Missing commit message."
else
git commit -a -m "$1" && git push
fi
}
ga-rcg() {
if ! command -v git >/dev/null 2>&1; then
_ga_warn "git not found; cannot configure."
return 127
fi
echo "Setting up default global git configuration for Daniel Goude."
git config --global user.name 'Daniel Goude'
git config --global user.email 'daniel@goude.se'
git config --global push.default simple
git config --global pull.rebase false
git config --global core.editor "${EDITOR:-vi}"
git config --global init.defaultBranch main
git config --global diff.tool vimdiff
git config --global merge.tool vimdiff
git config --global core.excludesfile ~/.gitignore
[ -f ~/.gitignore ] || touch ~/.gitignore
git config --list
}
# fzf init portability
_ga_init_fzf() {
if [ -n "$BASH_VERSION" ] && [ -f "$HOME/.fzf.bash" ]; then
# shellcheck disable=SC1090
source "$HOME/.fzf.bash"
elif [ -n "$ZSH_VERSION" ] && [ -f "$HOME/.fzf.zsh" ]; then
# shellcheck disable=SC1090
source "$HOME/.fzf.zsh"
elif [ -n "$ZSH_VERSION" ] && command -v fzf-share >/dev/null 2>&1; then
# shellcheck disable=SC1090
source "$(fzf-share)/key-bindings.zsh"
# shellcheck disable=SC1090
source "$(fzf-share)/completion.zsh"
elif [ -d /usr/share/doc/fzf/examples ]; then
if [ -n "$BASH_VERSION" ] && [ -f /usr/share/doc/fzf/examples/key-bindings.bash ]; then
# shellcheck disable=SC1091
source /usr/share/doc/fzf/examples/key-bindings.bash
# shellcheck disable=SC1091
source /usr/share/doc/fzf/examples/completion.bash
elif [ -n "$ZSH_VERSION" ] && [ -f /usr/share/doc/fzf/examples/key-bindings.zsh ]; then
# shellcheck disable=SC1091
source /usr/share/doc/fzf/examples/key-bindings.zsh
# shellcheck disable=SC1091
source /usr/share/doc/fzf/examples/completion.zsh
fi
fi
}
# help
ga-help() {
cat <<'EOF'
getaround.sh installed.
Aliases:
rm/mv/cp (interactive), ls (color), df, .., ..., ta, n
gs, ga, gca, gp, sau
Functions:
getaround - prints welcome with version and current $EDITOR
ga-save "msg" - git commit -a -m "msg" && git push
ga-rcg - set Git globals (core.editor mirrors $EDITOR)
install-getaround - add/update source block in ~/.zshrc or ~/.bashrc
Clipboard:
pbcopy/pbpaste - macOS native; Linux via xclip/xsel; WSL via Windows clipboard
Notes:
GETAROUND_ROOT set dynamically to this script’s directory.
GETAROUND_ROOT/bin is prepended to PATH if it exists.
EDITOR preference: nvim > vim > vi > nano.
EOF
}
# bash-specific
if [ -n "$BASH_VERSION" ]; then
set -o vi
set bell-style none
bind 'set show-mode-in-prompt on'
bind 'set vi-ins-mode-string "\1\e[32m\2[INS]\1\e[0m\2 "'
bind 'set vi-cmd-mode-string "\1\e[31m\2[NORMAL]\1\e[0m\2 "'
bind '"\C-f": menu-complete'
bind -m vi-insert '"\C-l": clear-screen'
_ga_init_fzf
fi
# zsh-specific
if [ -n "$ZSH_VERSION" ]; then
bindkey -v
setopt NO_BEEP
setopt PROMPT_SUBST
zle-keymap-select() {
case $KEYMAP in
vicmd) RPROMPT='%F{red}[NORMAL]%f ' ;;
main|viins) RPROMPT='%F{green}[INS]%f ' ;;
*) RPROMPT='' ;;
esac
zle reset-prompt
}
zle -N zle-keymap-select
zle-line-init() { zle-keymap-select; }
zle -N zle-line-init
bindkey -M viins '^F' menu-complete
bindkey -M vicmd '^F' menu-complete
bindkey -M viins '^L' clear-screen
autoload -Uz complist 2>/dev/null || true
zmodload zsh/complist 2>/dev/null || true
zstyle ':completion:*' menu select
_ga_init_fzf
fi
# fallback for other shells
if [ -z "$BASH_VERSION$ZSH_VERSION" ]; then
_ga_warn "getaround.sh: non-bash/zsh shell detected; skipped shell-specific keybindings."
fi
# auto-run welcome when sourced (interactive only)
case $- in
*i*) getaround ;;
esac
# ------------------------------------------------------------------------------
# install helper (idempotent rewrite of block) — kept last
install-getaround() {
local target_rc block_start block_end src_path tmp
block_start="# >>> getaround init >>>"
block_end="# <<< getaround init <<<"
if [ -n "$1" ]; then
target_rc="$1"
elif [ -n "$ZSH_VERSION" ]; then
target_rc="$HOME/.zshrc"
else
target_rc="$HOME/.bashrc"
fi
src_path="$(_ga_script_path)"
[ -f "$target_rc" ] || touch "$target_rc"
tmp="$(mktemp "${TMPDIR:-/tmp}/getaround.XXXXXX")" || { _ga_warn "mktemp failed"; return 1; }
if grep -Fq "$block_start" "$target_rc"; then
awk -v start="$block_start" -v end="$block_end" -v path="$src_path" '
BEGIN { inblk=0 }
$0 ~ start { print start; print "source \"" path "\""; print end; inblk=1; next }
$0 ~ end && inblk==1 { inblk=0; next }
inblk==0 { print }
' "$target_rc" > "$tmp"
mv "$tmp" "$target_rc"
echo "Updated: source block in $target_rc"
else
{
echo ""
echo "$block_start"
echo "source \"$src_path\""
echo "$block_end"
} >> "$target_rc"
echo "Installed: added source block to $target_rc"
fi
echo "Reload with: source \"$target_rc\""
}