-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc
executable file
·127 lines (108 loc) · 2.51 KB
/
misc
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
#!/usr/bin/env bash
# OS specific support.
darwin=false;
case "$(uname -s)" in
Darwin*)
darwin=true
;;
esac
RED='\033[0;31m'; B_RED='\033[1;31m'
GREEN='\033[0;32m'; B_GREEN='\033[1;32m'
YELLOW='\033[0;33m'; B_YELLOW='\033[1;33m'
BLUE='\033[0;34m'; B_BLUE='\033[1;34m'
MAGENTA='\033[0;35m'; B_MAGENTA='\033[1;35m'
CYAN='\033[0;36m'; B_CYAN='\033[1;36m'
WHITE='\033[0;37m'; B_WHITE='\033[1;37m'
NC='\033[0m' # Return to default colour
export WORKDIR="$HOME/projects"
export COLUMNS
export LINES
if [ "$darwin" = "false" ]; then
alias open='xdg-open'
fi
# make sure WORKDIR exists
mkdir -p "$WORKDIR"
usagesort() {
du -h -d 1 "$1" | sort -k 1,1 -g
}
is_app_installed() {
command -v "$1" &>/dev/null
}
check_dotfile() {
# if file is a symbolic link but broken,
# delete it so it can be recreated
local dotfile="$1"
local src="$2"
[[ ! -r $dotfile && -L $dotfile ]] && rm "$dotfile"
[[ -r $dotfile ]] || ln -s "$src" "$dotfile"
}
die() {
echo -e "${RED}$@${NC}" >&2
exit 1
}
error() {
echo -e "${B_RED}[ERROR]${NC} ${RED}$@${NC}"
}
warn() {
echo -e "${B_YELLOW}[WARN]${NC} ${YELLOW}$@${NC}"
}
info() {
echo -e "${B_GREEN}[INFO]${NC} ${GREEN}$@${NC}"
}
debug() {
echo -e "${B_CYAN}[DEBUG]${NC} ${CYAN}$@${NC}"
}
if ! is_app_installed realpath; then
realpath() {
OURPWD=${PWD}
cd "$(dirname "${1}")"
LINK=$(readlink "$(basename "${1}")")
while [ "$LINK" ]; do
cd "$(dirname "$LINK")"
LINK=$(readlink "$(basename "${1}")")
done
REALPATH="${PWD}/$(basename "${1}")"
cd "$OURPWD"
echo "$REALPATH"
}
fi
relPath() {
local common path up
common=$(realpath "${1}")
path=$(realpath "${2}")
while test "${path#"$common"/}" = "$path"; do
common=${common%/*}
up=../$up
done
path=$up${path#"$common"/}
path=${path%/}; printf %s "${path:-.}"
}
try-lock() {
__LOCKFILE="${TMPDIR:-/tmp}/$1"
if ( set -o noclobber; : > "$__LOCKFILE" ) &>/dev/null; then
trap 'rm -f "${__LOCKFILE}" &>/dev/null' INT TERM EXIT RETURN
return 0
fi
return 1
}
function pathmunge() {
# prevent duplicate directories in your PATH variable
if [[ -d "${1:-}" && ! $PATH =~ (^|:)"${1}"($|:) ]]; then
export PATH="${1}:$PATH"
fi
}
# from https://github.com/robhague/dotfiles/
function _ensure_cwd {
# If the working directory doesn't exist, try and find one that does
if [ ! -d "$PWD" ]
then
warn "$PWD no longer exists, "
D=$PWD
while [[ $D && ! -d $D && $D = */* ]]
do
D=${D%/*}
done
builtin cd "$D"
info "moved to $PWD"
fi
}