Skip to content

Commit ea5af20

Browse files
committed
updates
1 parent 4079439 commit ea5af20

1 file changed

Lines changed: 46 additions & 31 deletions

File tree

.bashrc

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@ alias duck='{ du -ha | sort -rh | head -20;} 2> /dev/null'
1818
alias common="history | awk '{CMD[\$2]++;count++;}END { for (a in CMD)print CMD[a] \" \" CMD[a]/count*100 \"% \" a;}' |\
1919
grep -v \"./\" | column -c3 -s \" \" -t | sort -nr | nl | head"
2020

21-
# This makes some assumptions about the ordering of arguments that may not be true
2221
function brew() {
2322
if [[ $1 == "rm" || $1 == "uninstall" ]]; then
24-
local pkg="$2" # Save package name before shift
23+
local pkg="$2"
2524
shift
2625
command brew rm "$@"
2726

2827
# Only check deps if we actually removed something successfully
2928
if [[ $? -eq 0 && -n "$pkg" ]]; then
3029
local -a deps
31-
mapfile -t deps < <(join <(brew leaves) <(brew deps "$pkg") 2>/dev/null)
30+
mapfile -t deps < <(join <(command brew leaves) <(command brew deps "$pkg") 2>/dev/null)
3231
if (( ${#deps[@]} > 0 )); then
3332
echo "Removing unused dependencies: ${deps[*]}"
3433
command brew rm "${deps[@]}"
@@ -41,10 +40,16 @@ function brew() {
4140

4241
function git() {
4342
if [[ "$1" == "push" && "$*" == *"--force"* ]]; then
44-
# Replace --force with --force-with-lease in the command line
45-
local cmd_line="$*"
46-
cmd_line="${cmd_line// --force/ --force-with-lease}"
47-
eval "command git $cmd_line"
43+
local -a args=()
44+
local arg
45+
for arg in "$@"; do
46+
if [[ "$arg" == "--force" ]]; then
47+
args+=("--force-with-lease")
48+
else
49+
args+=("$arg")
50+
fi
51+
done
52+
command git "${args[@]}"
4853
else
4954
command git "$@"
5055
fi
@@ -59,11 +64,7 @@ function ls() {
5964
fi
6065
done
6166
if (( long )); then
62-
if [[ "$(uname)" == "Darwin" ]]; then
63-
command ls -GhLa "$@"
64-
else
65-
command ls -GhLa --color=auto "$@"
66-
fi
67+
command ls -GhLa "$@"
6768
else
6869
command ls "$@"
6970
fi
@@ -84,11 +85,16 @@ function rm() {
8485
for target in "$@"; do
8586
[[ $target == -* ]] && continue
8687
[[ ! -e $target ]] && continue
87-
abs_target=$(cd -- "$(dirname -- "$target")" 2>/dev/null && printf '%s/%s' "$(pwd)" "$(basename -- "$target")") || continue
88+
if [[ $target == /* ]]; then
89+
abs_target=$target
90+
else
91+
abs_target=$PWD/$target
92+
fi
8893
if [[ -d $abs_target ]]; then
8994
check_dir=$abs_target
9095
else
91-
check_dir=$(dirname -- "$abs_target")
96+
check_dir=${abs_target%/*}
97+
[[ -z $check_dir ]] && check_dir=/
9298
fi
9399
repo_root=$(command git -C "$check_dir" rev-parse --show-toplevel 2>/dev/null) || continue
94100
if [[ -n "$(command git -C "$repo_root" status --porcelain -- "$abs_target" 2>/dev/null)" ]]; then
@@ -114,12 +120,14 @@ function rm() {
114120
return $rc
115121
}
116122

117-
function cat() {
118-
command bat --style=plain --paging=never "$@" 2>/dev/null || command cat "$@"
119-
}
123+
if command -v bat >/dev/null; then
124+
function cat() {
125+
command bat --style=plain --paging=never "$@"
126+
}
127+
fi
120128

121129
function extract() {
122-
if [ -f "$1" ]; then
130+
if [[ -f "$1" ]]; then
123131
case $1 in
124132
*.tar.bz2) tar xjf "$1" ;;
125133
*.tar.gz) tar xzf "$1" ;;
@@ -135,7 +143,7 @@ function extract() {
135143
*) echo "'$1' cannot be extracted via extract()" ;;
136144
esac
137145
else
138-
echo "$1" is not a valid file
146+
echo "'$1' is not a valid file"
139147
fi
140148
}
141149

@@ -145,27 +153,34 @@ function gh() {
145153
local exit_code=$?
146154

147155
if [[ $exit_code -eq 0 ]]; then
148-
# First non-flag arg after "clone" is the repo identifier
149-
local repo_arg=""
156+
# First non-flag arg after "clone" is the repo identifier; the second
157+
# (if present, before "--") is an explicit target directory.
158+
local repo_arg="" target_arg="" arg
150159
for arg in "${@:3}"; do
151-
if [[ "$arg" != -* ]]; then
160+
[[ "$arg" == "--" ]] && break
161+
[[ "$arg" == -* ]] && continue
162+
if [[ -z "$repo_arg" ]]; then
152163
repo_arg="$arg"
164+
else
165+
target_arg="$arg"
153166
break
154167
fi
155168
done
156169

157170
if [[ -n "$repo_arg" ]]; then
158-
local repo_name
159-
repo_name=$(basename "$repo_arg" .git)
160-
if [[ -d "$repo_name" ]] && cd "$repo_name"; then
171+
local clone_dir
172+
if [[ -n "$target_arg" ]]; then
173+
clone_dir="$target_arg"
174+
else
175+
clone_dir=$(basename "$repo_arg" .git)
176+
fi
177+
if [[ -d "$clone_dir" ]] && cd "$clone_dir"; then
161178
# Derive owner/repo from origin so set-default works regardless
162179
# of which form the user passed to clone (owner/repo, https://, git@…)
163180
local origin_url
164181
origin_url=$(git remote get-url origin 2>/dev/null)
165-
if [[ "$origin_url" == *github.com* ]]; then
166-
local repo_path
167-
repo_path=$(echo "$origin_url" | sed -E 's|.*github\.com[:/]([^/]+/[^/]+)(\.git)?.*|\1|')
168-
[[ -n "$repo_path" ]] && command gh repo set-default "$repo_path" 2>/dev/null || true
182+
if [[ "$origin_url" =~ github\.com[:/]([^/]+/[^/]+?)(\.git)?$ ]]; then
183+
command gh repo set-default "${BASH_REMATCH[1]}" 2>/dev/null
169184
fi
170185
fi
171186
fi
@@ -180,5 +195,5 @@ function gh() {
180195
# -------------- Per-shell tool initialization --------------
181196
# These hook into PROMPT_COMMAND / keybindings / PS1, which are shell-local —
182197
# they must run in every interactive shell, not just login shells. Hence .bashrc.
183-
[ -x "$(command -v starship)" ] && eval "$(starship init bash)"
184-
[ -x "$(command -v atuin)" ] && eval "$(atuin init bash)"
198+
command -v starship >/dev/null && eval "$(starship init bash)"
199+
command -v atuin >/dev/null && eval "$(atuin init bash)"

0 commit comments

Comments
 (0)