-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
functions.sh
173 lines (149 loc) · 4.51 KB
/
functions.sh
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
#!/bin/bash
# Functions
# copy authy token
auth() {
mambembe-cli get-token -s "$@" | fzf --reverse -0 -1 | rg -oP 'Token: "\K\d+' | $CLIPCOPY
}
# cd into dir and list contents
lc() {
cd "$1" && la "$2"
}
# Make directory and cd into it
mcd() {
mkdir -p -- "$1" && cd -P -- "$1"
}
# kill all tmux sessions
tmux-clean() {
tmux list-sessions | grep -E -v '\(attached\)$' | while IFS='\n' read line; do
tmux kill-session -t "${line%%:*}"
done
}
# Execute command in directory
xin() {
(cd "${1}" && shift && ${@})
}
# show files which contain a term
grep_open() {
local editor="$EDITOR"
if [ "$EDITOR" = "vim" ] || [ "$EDITOR" = "nvim" ]; then
local editor="$EDITOR +/$1 +'norm! n'"
fi
rg -l "$1" | fzf --bind "enter:execute($editor + {})"
}
# review changed files on this branch
review_changes() {
local base_branch="${1:-}"
if [ -z "$base_branch" ]; then
base_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
[ -z "$base_branch" ] && base_branch="main"
fi
git diff --name-only "$base_branch"...HEAD | fzf \
--preview "git diff $base_branch...HEAD -- {} | delta --width \$FZF_PREVIEW_COLUMNS" \
--bind "enter:execute($EDITOR {})"
}
# show staged and unstaged file changes
changed_files() {
git status --short | awk '{print $2}' | fzf \
--preview "git diff --cached -- {} | delta --width \$FZF_PREVIEW_COLUMNS && git diff -- {} | \
delta --width \$FZF_PREVIEW_COLUMNS && git diff --no-index -- /dev/null {} | delta --width \$FZF_PREVIEW_COLUMNS" \
--bind "enter:execute($EDITOR {})"
}
# Show diff for argument PR number for current repo
pr_diff() {
gh pr diff "$1" | delta
}
# Show PR files for argument PR number for current repo
pr_files() {
gh pr diff "$1" --name-only | fzf \
--bind "enter:execute($EDITOR {})"
}
# edit a binary file in path, useful for editing executables/symlinked scripts
binary_edit() {
local bin=""
bin=$(which "$1")
if [ -z "$bin" ]; then
echo "Binary not found in path"
return 1
fi
$EDITOR "$bin"
}
lvi() {
NVIM_APPNAME=lvim2k nvim "$@"
}
# ex - archive extractor
ex() {
if [ -f "$1" ]; then
case $1 in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via ex()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Runs when tab is pressed after ,
_fzf_comprun() {
local command=$1
shift
case "$command" in
cd) fzf "$@" --preview 'exa -TFl --group-directories-first --icons --git -L 2 --no-user {}' ;;
nvim) fzf --preview 'bat --color=always --style=numbers --line-range=:500 {}' ;;
vim) fzf --preview 'bat --color=always --style=numbers --line-range=:500 {}' ;;
*) fzf "$@" ;;
esac
}
# colorized man output
man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;36m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
PAGER="${commands[less]:-$PAGER}" \
_NROFF_U=1 \
PATH="$HOME/bin:$PATH" \
man "$@"
}
# show colored text
color_text() {
for code in {000..255}; do
printf "%s: \033[38;5;%smThis is how your text would look like\033[0m\n" "$code" "$code"
done
}
# show colors and codes
color_escape() {
local fgc bgc vals seq0
printf "Color escapes are %s\n" '\e[${value};...;${value}m'
printf "Values 30..37 are \e[33mforeground colors\e[m\n"
printf "Values 40..47 are \e[43mbackground colors\e[m\n"
printf "Value 1 gives a \e[1mbold-faced look\e[m\n\n"
# foreground colors
for fgc in {30..37}; do
# background colors
for bgc in {40..47}; do
fgc=${fgc#37} # white
bgc=${bgc#40} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}TEXT\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo
echo
done
}