summaryrefslogtreecommitdiffstats
path: root/bin/functions.sh
blob: f872c776e17067620eab1732d68de9054aad006f (plain) (blame)
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
# output handling
coco() {
	[[ -z "$1" ]] && echo -ne "\033[0m" || echo -ne "\033[${1}m"
}

# name all colors to get rid of hieroglyphics
color() {
	case $1 in
		black)    coco '0;30' ;;
		dgray)    coco '1;30' ;;
		red)      coco '0;31' ;;
		lred)     coco '1;31' ;;
		green)    coco '0;32' ;;
		lgreen)   coco '1;32' ;;
		brown)    coco '0;33' ;;
		yellow)   coco '1;33' ;;
		blue)     coco '0;34' ;;
		lblue)    coco '1;34' ;;
		purple)   coco '0;35' ;;
		lpurple)  coco '1;35' ;;
		cyan)     coco '0;36' ;;
		lcyan)    coco '1;36' ;;
		lgray)    coco '0;37' ;;
		white)    coco '1;37' ;;
		*)        coco '0' ;;
	esac
}

_einfon() {
	echo -ne " ${GOOD}*${NORMAL} $*"
	return 0
}

_einfo() {
	echo -e " ${GOOD}*${NORMAL} $*"
	return 0
}

_ewarn() {
	echo -e " ${WARN}*${NORMAL} $*"
	return 0
}

_eerror() {
	echo -e " ${BAD}*${NORMAL} $*"
	return 0
}

_ewrap() {
	local cmd="$1"
	shift

	local c="${ENDCOL}"
	local p="$* "
	local max=$((COLS - 11))

	# Only use what we have space for
	local d="${p%%\\n*}"
	if [[ ${d} == "${p}" ]] ; then
		d="${p:0:${max}}"
	else
		d="${d} "
	fi

	# Grab the last whole word
	[[ ${#d} == "${max}" ]] && d="${d% *}"
	
	# Print that
	_${cmd} "${d}"

	# Recurse on the rest
	local r="$*"
	d="${d} "
	r="${r:${#d}}"
	[[ -n ${r} ]] && ${cmd} "${r}"

	return 0
}

einfo() {
	_ewrap einfo "$*"
}

ewarn() {
	_ewrap ewarn "$*"
}

eerror() {
	echo
	_ewrap eerror "$*"
	return 1
}

eheading() {
	echo
	echo -n "$(color green)>>> "
	echo -n "$@"
	echo " <<<$(color)"
}

# input handling
eask_num() {
	local -i reply=
	_einfon
	read -p "Please enter ${1:-Number} or press CTRL-C to abort> " reply
	REPLY=${reply}
}

eask_str() {
	local reply=
	_einfon
	read -p "Please enter ${1:-String} or press CTRL-C to abort> " reply
	REPLY=${reply}
}

eask_bool() {
	local reply= default=${2} str="[y/n]"
	
	if [[ ${default} == y || ${default} == Y || ${default} == true ]]
	then
		str="[Y/n]"
	elif [[ ${default} == n || ${default} == N || ${default} == false ]]
	then
		str="[y/N]"
	fi

	while [[ "${reply}" != y && "${reply}" != n ]]; do
		_einfon
		read -p "${1} ${str} " reply
		
		if [[ -z ${reply} ]]
		then 
			reply=${default}
			break
		fi
	done

	REPLY=${reply}
}


echoose() {
	einfo ${1:-"Choose something from the following list"}
	einfo
	shift

	local -a items=

	while true; do
		items=()

		for i in "$@"; do
			items=( "${items[@]}" "${i}" )
			einfo "${#items[@]}) ${i}"
		done

		eask_num

		if [[ ${REPLY} -le 0 || ${REPLY} -gt ${#items[@]} ]]; then
			eerror "\nInvalid selection. Try again.\n " || :
		else
			break
		fi
	done

	REPLY=${items[$(( ${REPLY} - 1 ))]}
}

# make-like exec wrapper
eexec() {
	einfo "$@"
	"$@"
}

# alway use the right thing, even if EDITOR is not set
editor() {
	if [[ -z "${EDITOR}" || ! -x "${EDITOR}" ]]; then
		if [[ -x /usr/bin/vim ]]; then
			EDITOR=/usr/bin/vim
		elif [[ -x /usr/bin/nano ]]; then
			EDITOR=/usr/bin/nano
		fi
	fi

	[[ -x ${EDITOR} ]] && ${EDITOR} "$@"
}

# Setup a secure $PATH. Just add system defaults.
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

if [[ -z ${COLS} || -n ${COLUMNS} ]] ; then
	COLS="${COLUMNS:-0}"
	(( COLS == 0 )) && COLS=$(set -- `stty size 2>/dev/null` ; echo "$2")
	(( COLS > 0 )) || (( COLS = 80 ))
fi

ENDCOL=$'\e[A\e['$(( COLS - 9 ))'C'
GOOD=$'\e[32;01m'
WARN=$'\e[33;01m'
BAD=$'\e[31;01m'
NORMAL=$'\e[0m'

# vim: set ts=4 :