-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
210 lines (181 loc) · 5.45 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
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
210
# Get the git branch of the current folder (useful in PS1)
function parse_git_branch() {
git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# Kill the process that listens on specific port
function killport() {
lsof -i TCP:$1 | grep LISTEN | awk '{print $2}' | xargs kill -9
}
function _yarnhook() {
YARN_FILES=(yarn.lock package.json package-lock.json shrinkwrap.yaml)
# HAS_FOUND=0
for i in ${YARN_FILES[*]}; do
if [[ -s $i ]]; then
# HAS_FOUND=1;
yarn install
break
fi
done
# if [[ $HAS_FOUND == 0 ]]; then
# echo "Not a yarn project";
# fi
}
ssh_tmux() {
if [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then
tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
fi
}
stepper() {
# Usage guidelines
#
# - Wrap the step-prefix with brackets and show the progress: "[ 5/10]"
# - End step-messages with three dots (not an ellipsis)
# - End ending messages (error, success, info, warn) with '.'
# - 😃 Have an emoji in the beginning of the message
# - Use double-spaces after each emoji, so it looks better in the terminal
# - Start a stepper with `echo "$(tput bold)title$(tput sgr0)"`
# - End a stepper with `echo "✨ Done."`
#
# An example:
# echo "$(tput bold)title$(tput sgr0)"
# stepper default "[1/3]" "🚚 Fetching dependencies..." 'npx concurrently --names "1/3: brew fetch,1/3: cask fetch" --prefix-colors "grey" "brew fetch composer php-cs-fixer" "brew cask fetch docker";'
# stepper default "[2/3]" "🔗 Linking dependencies..." 'brew install composer php-cs-fixer; brew cask install docker; composer install;'
# stepper default "[3/3]" "🐳 Building docker image..." 'docker-compose build;'
# stepper success "success" "Success message."
#
# echo "✨ Done."
prefix="$2"
message="$3"
case "$1" in
default)
color="8"
;;
error)
color="1"
;;
success)
color="2"
;;
info)
color="4"
;;
warn)
color="3"
;;
*)
echo "Invalid color"
return 1
;;
esac
shift 3
echo "$(tput setaf ${color})$prefix$(tput sgr0) ${message}"
if ! SUPPRESSED=$( (eval $@) 2>&1); then
echo "$SUPPRESSED"
return 1
fi
}
function _alert() {
_ALERT_TYPE="${1:-success}"
_ALERT_TITLE="${2:-Alert}"
_ALERT_GROUP="${ALERT_GROUP_ID:-}"
TERMINAL_NOTIFIER_OPTIONS=()
_ALERT_MESSAGE="${3:-}"
case "${_ALERT_TYPE}" in
success)
_ALERT_TITLE="${2:-Success}"
TERMINAL_NOTIFIER_OPTIONS+=(-sound default)
TERMINAL_NOTIFIER_OPTIONS+=(-contentImage ~/dotfiles/assets/success.png)
;;
warn)
_ALERT_TITLE="${2:-Warning}"
TERMINAL_NOTIFIER_OPTIONS+=(-sound Funk)
TERMINAL_NOTIFIER_OPTIONS+=(-contentImage ~/dotfiles/assets/warn.png)
;;
error)
_ALERT_TITLE="${2:-Error}"
TERMINAL_NOTIFIER_OPTIONS+=(-sound Basso)
TERMINAL_NOTIFIER_OPTIONS+=(-contentImage ~/dotfiles/assets/error.png)
;;
esac
if [[ "${_ALERT_GROUP}" == "" ]]; then
TERMINAL_NOTIFIER_OPTIONS+=(-group "$(tty)")
else
TERMINAL_NOTIFIER_OPTIONS+=(-group "${_ALERT_GROUP}")
fi
if [[ "${_ALERT_MESSAGE}" != "" ]]; then
TERMINAL_NOTIFIER_OPTIONS+=(-title "${_ALERT_TITLE}")
TERMINAL_NOTIFIER_OPTIONS+=(-message "${_ALERT_MESSAGE}")
else
TERMINAL_NOTIFIER_OPTIONS+=(-message "${_ALERT_TITLE}")
fi
echo "${_ALERT_TITLE}: ${_ALERT_MESSAGE}"
/opt/homebrew/bin/terminal-notifier "${TERMINAL_NOTIFIER_OPTIONS[@]:-}"
}
function success() {
_alert "success" "$@"
}
function warn() {
_alert "warn" "$@"
}
function error() {
_alert "error" "$@"
}
function check_upstream() {
set -o pipefail
local -r _OLD_PWD="$(pwd)"
local -r _NEW_PWD="${1:-$(pwd)}"
local -r REPO_NAME=$(basename "${_NEW_PWD}")
local _RETURN_CODE=0
if ! cd "${_NEW_PWD}" || ! git fetch --all --prune >/dev/null; then
_RETURN_CODE=2
fi
if [[ $_RETURN_CODE -eq 0 && "$(git rev-parse HEAD)" != "$(git rev-parse @{u})" ]]; then
set +o errexit
git pull >/dev/null 2>&1
set -o errexit
if [[ $_RETURN_CODE -ne 0 ]]; then
echo "^ From repo at path $(pwd)"
fi
fi
if [[ $_RETURN_CODE -eq 0 && "$(git rev-parse HEAD)" != "$(git rev-parse @{u})" ]]; then
readonly ALERT_GROUP_ID=$(echo -n "${_NEW_PWD}" | md5sum | cut -f1 -d" ")
export ALERT_GROUP_ID
warn "${REPO_NAME}" "${REPO_NAME} is not in sync with upstream"
_RETURN_CODE=1
fi
cd "${_OLD_PWD}"
return "${_RETURN_CODE}"
}
function start() {
# Open all folders in .projects
PROJECTS_LIST_FILE="${HOME}"/dev/.projects
cd "$(dirname "${PROJECTS_LIST_FILE}")" || return 1
grep -E '^[^#]' <"${PROJECTS_LIST_FILE}" | xargs -n1 code
}
function wait_for_jobs() {
for job in $(jobs -p); do
wait "${job}"
done
}
function _open_project() {
local readonly DIR="$1"
{
open -a /Applications/iTerm.app "$1" &
code "$1" &
} >/dev/null
}
# Convert a hexadecimal color code to an ANSI color code.
# This function takes a hexadecimal color code as an argument,
# removes the leading '#', and calculates the equivalent ANSI color code.
# The resulting ANSI color code can be used for styling output in
# a Bash script or command-line environment that supports ANSI colors.
# The function returns the ANSI color code as a three-digit decimal number.
function fromhex() {
hex=${1#"#"}
r=$(printf '0x%0.2s' "$hex")
g=$(printf '0x%0.2s' ${hex#??})
b=$(printf '0x%0.2s' ${hex#????})
printf '%03d' "$(((r < 75 ? 0 : (r - 35) / 40) * 6 * 6 + (\
g < 75 ? 0 : (g - 35) / 40) * 6 + (\
b < 75 ? 0 : (b - 35) / 40) + 16))"
}