-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashrc
More file actions
175 lines (154 loc) · 4.32 KB
/
bashrc
File metadata and controls
175 lines (154 loc) · 4.32 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
#!/bin/bash
set -o emacs
shopt -s histappend
shopt -s expand_aliases
stty -ixon
function orgflowy {
case $# in
0) nvim -c "NifflerOrgFlowy" ;;
1) nvim -c "OrgFlowy $1" ;;
*) __error 'Usage: orgflowy [NOTE_NAME]' ;;
esac
}
function jenv {
if [[ -x /usr/libexec/java_home ]]; then
local version
case $1 in
# Java 9 and later version strings dropped the 1.X format
[0-8] ) version="1.$1";;
* ) version=$1;;
esac
if /usr/libexec/java_home -F -v "$version"; then
export JAVA_VERSION=$version
fi
else
echo 'Cannot find /usr/libexec/java_home executable'
exit 1
fi
}
# Private utility functions
function __prompt {
prompt=${1:-'Do you wish to proceed [yes/no]?'}
while true; do
read -p "$prompt" response
case $response in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
function __error {
echo "$@" 1>&2
return 1
}
function __archive_name {
printf '%s' "$(basename "$1").tar"
}
function __is_tar_file {
tar -tvf "$1" &> /dev/null
}
function __gpg {
if command -v gpg2 &>/dev/null; then
gpg2 "$@"
elif command -v gnupg2 &>/dev/null; then
gnupg2 "$@"
elif command -v gpg &>/dev/null; then
gpg "$@"
elif command -v gnupg &>/dev/null; then
gnupg "$@"
else
__error 'Unable to locate GPG program. Aborting...'
fi
}
# Public functions
function cdl {
cd "$@" && ls
}
function cpstat {
rsync -vrltD --stats --human-readable "$1" "$2" | pv -lep -s $((2 + $(find "$1" | wc -l)))
}
function check-aliases {
echo 'Possible alias conflicts...'
alias | while read alias_line; do
alias=$(sed 's/alias \([^=]*\).*/\1/' <<< "$alias_line")
if [[ -n $(which "$alias") ]]; then
echo "$alias"
fi
done
}
function vs {
session_home="$HOME/vim-sessions/"
session=$(find "$session_home" -type f -print0 | xargs -0 -n1 basename | fzf)
[[ -n $session ]] && nvim -S "${session_home}${session}"
}
function archive {
for file in "$@"; do
local archive_name
archive_name=$(__archive_name "$file")
if tar -cvf "$archive_name" "$file"; then
rm -rf "$file"
fi
done
}
function unarchive {
for tar_file in "$@"; do
if tar -xvf "$tar_file"; then
rm -rf "$tar_file"
fi
done
}
function encrypt {
if ! command -v pbpaste &>/dev/null; then
echo 'pbpaste command required. Aborting...'
return 1
elif (( $# == 0 )); then
echo 'Usage: encrypt FILE...'
return 2
fi
echo "Please copy passphrase to the system clipboard."
if __prompt "Are you ready to continue [yes/no]?"; then
for target in "$@"; do
if [[ -d $target ]]; then
archive "$target"
target=$(__archive_name "$target")
fi
if __gpg --batch --symmetric --passphrase "$(pbpaste)" "$target"; then
rm -f "$target"
fi
done
fi
}
function decrypt {
if ! command -v pbpaste &>/dev/null; then
echo 'pbpaste command required. Aborting...'
return 1
elif (( $# == 0 )); then
echo 'Usage: decrypt FILE...'
return 2
fi
echo "Please copy passphrase to the system clipboard."
if __prompt "Are you ready to continue [yes/no]?"; then
for gpg_file in "$@"; do
local decrypted_file
decrypted_file=$(sed 's/\.gpg$//' <<< "$gpg_file")
if __gpg --batch --passphrase "$(pbpaste)" "$gpg_file"; then
if __is_tar_file "$decrypted_file"; then
unarchive "$decrypted_file"
fi
fi
done
fi
}
function __source_if_present {
# ignore non-constant source check
# shellcheck disable=SC1090
[[ -f $1 ]] && source "$1"
}
# XXX bash_completion is slow to load
__source_if_present "$HOMEBREW_PREFIX"/etc/profile.d/bash_completion.sh
__source_if_present "$HOMEBREW_PREFIX"/etc/profile.d/autojump.sh
__source_if_present ~/.bashrc.local
__source_if_present "$XDG_DATA_HOME/fzf/fzf.bash"
__source_if_present "$XDG_DATA_HOME/git-prompt.sh"
__source_if_present "$XDG_CONFIG_HOME"/bash/aliases