-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunctions
125 lines (107 loc) · 3.48 KB
/
functions
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
# vim: ft=bash
# Git functions
function _master_branch() {
if git rev-parse --quiet --verify master > /dev/null; then
echo 'master'
else
echo 'main';
fi
}
function git_branch_fzf() {
git rev-parse HEAD > /dev/null 2>&1 || return
local branch_format='%(refname:short)|%(color:green)%(committerdate:relative)%(color:reset)|%(color:yellow)%(committername)%(color:reset)'
git for-each-ref --color --sort='-committerdate:iso8601' --format=${branch_format} refs/heads | column -s '|' -t |
grep -v HEAD |
fzf --height 50% --ansi --no-multi --preview-window right:55% \
--preview 'git log -n 20 --graph --color=always --date=short --pretty="format:%C(auto)%cd %h%d %s" master..$(cut -f1 -d" " <<< {})' |
cut -f1 -d' '
}
function git_checkout_fzf() {
git checkout "$(git_branch_fzf)"
}
function git_diff_fzf () {
PREVIEW_PAGER="less --tabs=2 -Rc"
ENTER_PAGER=${PREVIEW_PAGER}
if [ -x "$(command -v delta)" ]; then
PREVIEW_PAGER="delta | ${PREVIEW_PAGER}"
ENTER_PAGER="delta | sed -e '1,4d' | ${ENTER_PAGER}"
fi
# Don't just diff the selected file alone, get related files first using
# '--name-status -R' in order to include moves and renames in the diff.
# See for reference: https://stackoverflow.com/q/71268388/3018229
PREVIEW_COMMAND='git diff --color=always '$@' -- \
$(echo $(git diff --name-status -R '$@' | grep {}) | cut -d" " -f 2-) \
| '$PREVIEW_PAGER
# Show additional context compared to preview
ENTER_COMMAND='git diff --color=always '$@' -U10000 -- \
$(echo $(git diff --name-status -R '$@' | grep {}) | cut -d" " -f 2-) \
| '$ENTER_PAGER
git diff $1 --name-only $@ | \
fzf --ansi --height=100% --exit-0 --preview "${PREVIEW_COMMAND}" \
--preview-window=top:85% --bind "enter:execute:${ENTER_COMMAND}"
}
# Tmux Functions
function tmux_create_or_attach(){
session="$1"
if ! $(tmux has-session -t "=$session" &>/dev/null)
then
tmux new -s "$session" -d
fi
tmux attach -t "$session"
}
function tmux_new_attach_to_session(){
session="$1"
counter=0
while (tmux has-session -t "${session}_${counter}") ; do
counter=$[$counter +1]
echo $counter
done
tmux new-session -s "${session}_${counter}" -t "${session}"
}
# Extra Useful Functions
function file_composition {
find . -type f | perl -n -e'/(\.\w+$)/ && print "$1\n"' | sort | uniq -c | sort -n
}
function git_update {
local updir=$1
echo "git pull (in $updir)"
if pushd $updir >/dev/null ; then
git pull
popd >/dev/null
fi
}
function seconds_to_time_str {
local secs="$1"
printf "%dh %dm %ds" $((${secs}/3600)) $((${secs}%3600/60)) $((${secs}%60)) | grep -o "[1-9].*$"
}
function seconds_to_minutes {
local secs="$1"
echo "$((${secs}/60))"
}
function attach_shell_to_docker_container {
docker exec -i -t $1 /bin/sh
}
function test_24bit_colour {
awk 'BEGIN{
s="/\\/\\/\\/\\/\\"; s=s s s s s s s s s s s s s s s s s s s s s s s;
for (colnum = 0; colnum<256; colnum++) {
r = 255-(colnum*255/255);
g = (colnum*510/255);
b = (colnum*255/255);
if (g>255) g = 510-g;
printf "\033[48;2;%d;%d;%dm", r,g,b;
printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
printf "%s\033[0m", substr(s,colnum+1,1);
}
printf "\n";
}'
}
function tmuxinator_list_and_start {
project=`tmuxinator list --newline | grep -v "tmuxinator projects" | fzf --no-preview`
if [[ -n $project ]]; then
tmuxinator start $project
fi
}
function detect_zoom_meeting() {
ps -ef | grep -Eq 'zoom.*\-key [0-9]+'
}