-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
128 lines (121 loc) · 3.87 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
#!/usr/bin/env bash
# shellcheck shell=sh disable=SC2312,SC2015
# print log messages to STDOUT if $DEBUG is true
# for info how to format check out the manpage of printf
debuglog() {
if [ -n "${BASH_VERSION}" ]; then
__source="${BASH_SOURCE[1]:-${BASH_SOURCE[0]:-${0}}}"
elif [ -n "${ZSH_VERSION}" ]; then
__source="${funcstack:+${funcstack[2]}}"
else
__source="${FUNCNAME:-${0}}"
fi
if [ "${DEBUG:+${DEBUG}}" = "true" ]; then
# shellcheck disable=SC2059
__logmsg="$(printf "$@")"
__date=$(date "+%T")
printf "[%s][%s] %s\n" "${__date}" "${__source##*/}" "${__logmsg}"
unset -v __source
fi
}
# prepend dir to fpath if dir exists
prepend_fpath() {
if [ $# -ne 1 ]; then
printf "Usage: %s <path>" "${BASH_SOURCE:-$0}"
return 1
elif [ ! -d "$1" ]; then
printf "%s Error:\n\t%s is not a directory" "${BASH_SOURCE:-$0}" "${1}"
return 1
elif [ -d "${1}" ] && echo "${SHELL}" | grep -q "zsh"; then
_cleaned=$(echo "$FPATH" | sed "s#:${1}:#:#g")
export FPATH="${1}${_cleaned:+:$_cleaned}" \
&& debuglog "prepend_fpath: %s" "${1}"
unset _escaped _cleaned
fi
}
# prepend dir to path if dir exists
prepend_path() {
if [ $# -ne 1 ]; then
printf "Usage: %s <path>" "${0}"
return 1
elif [ ! -d "${1}" ]; then
printf "%s Error:\n\t%s is not a directory" "${BASH_SOURCE:-$0}" "${1}"
return 1
else
_cleaned=$(echo "$PATH" | sed "s#:${1}:#:#g")
export PATH="${1}${_cleaned:+:$_cleaned}" \
&& debuglog "prepend_path: %s" "${1}"
unset _escaped _cleaned
fi
}
# initialize brew
brew_init() {
# fail if no or to many arguments are provided
if [ $# -ne 1 ]; then
printf "Usage: %s <brew_prefix>\n" "${BASH_SOURCE:-$0}"
return 1
# check if brew is available and initialize
elif [ -x "${1}/bin/brew" ]; then
# add brew to shell environment
eval "$("${1}/bin/brew" shellenv)" \
&& debuglog "brew_init: brew initialized" \
|| true
# set brew bundle file
export HOMEBREW_BUNDLE_FILE="${HOME}/.local/Brewfile"
# enable autoremove
export HOMEBREW_AUTOREMOVE=1
# enable bat
if command -v bat >/dev/null 2>&1; then
export HOMEBREW_BAT=1
fi
# add fpaths if dirs exist
if [ -d "${1}/share/zsh/site-functions" ]; then
prepend_fpath "${1}/share/zsh/site-functions"
fi
if [ -d "${1}/share/zsh-completions" ]; then
prepend_fpath "${1}/share/zsh-completions"
fi
if validate_command pyenv; then
# remove pyenv shims from PATH before calling brew
alias brew='env PATH="${PATH//$(pyenv root)\/shims:/}" brew'
fi
fi
}
# validate command exists
validate_command() {
if [ $# -ne 1 ]; then
printf "Usage: validate_command <command>\n"
return 1
elif ! command -v "$1" >/dev/null 2>&1; then
debuglog "%s not found" "$1"
return 1
else
return 0
fi
}
# view markdown files
viewmd() {
if [ $# -ne 1 ]; then
printf "Usage: viewmd <path to md file>"
return 1
elif ! validate_command pandoc; then
printf "viewmd: Error: pandoc not found"
elif ! validate_command w3m; then
printf "viewmd: Error: w3m not found"
else
pandoc -f markdown -t html "$1" | w3m -T text/html -o display_image=true
fi
}
# view git flavored markdown files
viewgitmd() {
if [ $# -ne 1 ]; then
printf "Usage: viewgitmd <path to md file>"
return 1
elif ! validate_command pandoc; then
printf "Error: pandoc not found"
elif ! validate_command w3m; then
printf "Error: w3m not found"
else
pandoc -f gfm -t html "$1" | w3m -T text/html -o display_image=true
fi
}