-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_functions
209 lines (174 loc) · 4.53 KB
/
.bash_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
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
#!/usr/bin/env bash
# meleu's .bash_function
########################
# shellcheck disable=1091
# private stuff
if [ -f "${HOME}/.bash_functions_private" ]; then
source "${HOME}/.bash_functions_private"
fi
# functions for colorized output
###############################################################################
# ANSI escape color codes
if [[ -z "${ansiRed}" ]]; then
readonly ansiRed='\e[1;31m'
readonly ansiGreen='\e[1;32m'
readonly ansiYellow='\e[1;33m'
readonly ansiNoColor='\e[0m'
fi
echoRed() {
echo -e "${ansiRed}$*${ansiNoColor}"
}
echoGreen() {
echo -e "${ansiGreen}$*${ansiNoColor}"
}
echoYellow() {
echo -e "${ansiYellow}$*${ansiNoColor}"
}
err() {
echoRed "$*" >&2
}
warn() {
echoYellow "$*" >&2
}
###############################################################################
# .files(): update the dotfiles repo
# NOTE: the use of (parentheses) rather than {curly-brackets} is to prevent
# the need to cd back.
.files() (
local dotfilesDir="${HOME}/dotfiles"
local gitStatus
cd "${dotfilesDir}" || return 1
gitStatus="$(git status --porcelain)"
if [[ -z "${gitStatus}" ]]; then
warn "dotfiles: nothing to update"
return 0
fi
git add --all &&
git status &&
git commit -m "update $(date +'%Y-%m-%d %R'): ${gitStatus}" &&
git pull --rebase &&
git push
)
# launch(): Open the file/URL with the default application.
launch() {
case "$OSTYPE" in
"cygwin"*)
cygstart "$@"
;;
"darwin"*) # MacOS
open "$@"
;;
*)
xdg-open "$@"
;;
esac
}
# google(): Open google.com in the default browser, arguments are used as search terms.
google() {
local terms
terms="$(urlencode "$@")"
launch "https://www.google.com/search?q=${terms}"
}
# 0x0(): Upload file or URL shortener. See more info at https://0x0.st/
0x0() {
local arg="$1"
local curlArg
if [[ -f "${arg}" ]]; then
curlArg="file=@${arg}"
elif [[ ${arg} =~ ^https?://.* ]]; then
curlArg="shorten=${arg}"
else
echo "error: '${arg}': invalid argument (not a file neither a URL)" >&2
return 1
fi
curl -F"${curlArg}" https://0x0.st
}
# transfer(): upload file to https://transfer.sh
transfer() {
local url='https://transfer.sh'
local file
local file_name
uploadFile() {
curl --progress-bar --upload-file "-" "${url}/${file_name}" |
tee /dev/null
}
# usage
if [ $# -eq 0 ]; then
echo -e "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2
return 1
fi
# check if there's a terminal connected to the standard input
if tty -s; then
file="$1"
file_name=$(basename "$file")
if [ ! -e "$file" ]; then
echo "$file: No such file or directory" >&2
return 1
fi
# if it's a dir, create a zip
if [ -d "$file" ]; then
# shellcheck disable=2288
file_name="$file_name.zip" ,
(cd "$file" && zip -r -q - .) | uploadFile
else
uploadFile <"${file}"
fi
else
file_name=$1
uploadFile
fi
echo
}
# dud(): get the disk usage of a directory and its subdirs
dud() {
du --max-depth 1 --human-readable "${@:-.}" |
sort --human-numeric-sort
}
getGithubLatestVersion() {
local repo="$1"
local regexUserSlashRepo='^[^/]+/[^/]+$'
if [[ ! "${repo}" =~ $regexUserSlashRepo ]]; then
err "ERROR: invalid argument '${repo}'"
echo "Usage: ${FUNCNAME[0]} user/repo" >&2
return 1
fi
curl \
--silent \
--location \
"https://api.github.com/repos/${repo}/releases/latest" |
jq -e --raw-output '.tag_name'
}
################################################################################
# dolarhoje(): gets, from dolarhoje.com, the dollar value
# compared with brazilian real
# dependencies:
# - lynx: sudo apt install lynx
# - pup: https://github.com/ericchiang/pup
dolarhoje() {
local currency="$1"
local htmlFile
local url
[[ "${currency}" == 'dolar' ]] && currency=
htmlFile="${TMPDIR:-/tmp}/dolarhoje${currency}.html"
url="https://dolarhoje.com/${currency}"
# Check if ${htmlFile} is older than 2 hours
# see: https://stackoverflow.com/a/2005658/6354514
touch -d '2 hours ago' /tmp/2h-ago
# if ${htmlFile} doesn't exist or is older than 2h, recreate it
if [[ ! -e "${htmlFile}" || "${htmlFile}" -ot "/tmp/2h-ago" ]]; then
curl --fail -sL "${url}" |
pup 'div#cotacao' >"${htmlFile}" 2>/dev/null ||
{
echo "ERROR: unable to get data from ${url}" >&2
rm -f "${htmlFile}"
return 1
}
fi
lynx -dump "${htmlFile}" | sed 's/\(_\|hoje\)//g'
}
alias cotacao='dolarhoje'
################################################################################
# a find that ignores the '.git/' directory
gfind() {
find "$@" -not -iwholename '*/\.git/*'
}