3
# .shutil: some shared utilities used by all status scripts
5
# Copyright (C) 2011 Dustin Kirkland
7
# Authors: Dustin Kirkland <kirkland@ubuntu.com>
8
# Scott Moser <smoser@ubuntu.com>
10
# This program is free software: you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation, version 3 of the License.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
27
-) printf "$ESC{-}" ;;
28
--) printf "$ESC{-} " ;;
30
bold1) printf "$ESC{=b }" || printf "$ESC{= }" ;;
31
bold2) printf "$ESC{+b }" || printf "$ESC{= }" ;;
32
none) printf "$ESC{= }" ;;
33
invert) printf "$ESC{=r }" ;;
41
attr=$1 ; fg=$2 ; bg=$3
44
if [ "$MONOCHROME" = "1" ]; then
48
printf "$ESC{=$attr $fg$bg}"
79
u) _RET=,underscore ;;
94
-) printf "#[default]" ;;
95
--) printf "#[default] " ;;
97
bold*) printf "#[fg=bold]" ;;
98
none) printf "#[default]" ;;
99
invert) printf "#[reverse]" ;;
101
if [ "$#" = "2" ]; then
102
color_map "$1"; back="$_RET"
103
color_map "$2"; fore="$_RET"
105
attr_map "$1"; attr="$_RET"
106
color_map "$2"; back="$_RET"
107
color_map "$3"; fore="$_RET"
109
[ "$MONOCHROME" = "1" ] && printf "#[default]" || printf "#[fg=$fore$attr,bg=$back]"
115
case "$BYOBU_BACKEND" in
125
# uncommented_lines(char=#)
126
# does the standard input have lines that do not start with 'char'?
127
uncommented_lines() {
128
local line chr="${1:-#}"
130
[ "${line#${chr}}" = "${line}" ] && return 0;
135
# newest(file,file,file..)
136
# return the newest file in the list
139
for i in "$@"; do [ "$i" -nt "$c" ] && c="$i"; done
140
[ -e "$c" ] && _RET="$c"
144
echo "ERROR: " "$@" 1>&2
148
[ $# -eq 0 ] || error "$@"; exit 1;
152
# Allow for local status scripts
153
if [ -x "$BYOBU_CONFIG_DIR/bin/$1" ]; then
154
_RET="$BYOBU_CONFIG_DIR/bin/$1"
155
elif [ -x "$BYOBU_PREFIX/lib/$PKG/$1" ]; then
156
_RET="$BYOBU_PREFIX/lib/$PKG/$1"
157
elif [ -x "$BYOBU_PREFIX/libexec/$PKG/$1" ]; then
158
_RET="$BYOBU_PREFIX/libexec/$PKG/$1"
164
# divide 2 integers and return a floating point number
165
# third argument indicates how many digits after the decimal
167
local a=$1 b=$2 pres=${3:-3}
168
local i=0 mp="10" whole="" part="" chunk="" n=0
169
while i=$(($i+1)) && [ $i -le $pres ]; do
174
n=$(((${mp}*${a})/${b}))
176
# round up if necessary
177
[ $(($n%${mp})) -ge $((${mp}/2)) ] && n=$(($n+5))
179
# drop the last digit, which was only there for rounding
183
_RET=${whole}${part:+.${part}}
187
# rtrim(string,chars)
191
local cur="${1}" set="[${2:- ${tab}${cr}}]" n=""
192
while n=${cur%${set}} && [ "$n" != "$cur" ]; do cur=${n}; done
208
metadata_available() {
209
# This is really ugly. We need a reliable, fast way of determining
210
# if a metadata service is available, that does NOT slow down non-ec2
212
if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then
213
wget -q -O- --timeout=1 --tries=1 http://169.254.169.254 >/dev/null 2>&1
220
# Define status frequencies
221
# Use prime number intervals, to decrease collisions, which
222
# yields some less expensive status updates.
229
arch) _RET=9999999 ;;
231
color) _RET=9999999 ;;
236
date) [ "$BYOBU_BACKEND" = "tmux" ] && _RET=1 || _RET=28793 ;;
239
ec2_cost) _RET=601 ;;
240
fan_speed) _RET=23 ;;
241
hostname) _RET=607 ;;
242
ip_address) _RET=127 ;;
243
load_average) _RET=2 ;;
244
logo) _RET=9999999 ;;
247
menu) _RET=9999999 ;;
249
notify_osd) _RET=9999999 ;;
252
rcs_cost) _RET=613 ;;
253
reboot_required) _RET=5 ;;
257
time) [ "$BYOBU_BACKEND" = "tmux" ] && _RET=1 || _RET=9999999 ;;
258
time_binary) _RET=23 ;;
260
trash) _RET=9999999 ;;
261
updates_available) _RET=7 ;;
264
whoami) _RET=86029 ;;
265
wifi_quality) _RET=17 ;;
271
if [ -n "${BASH_VERSION}" ] && [ -n "${SECONDS}" ]; then
273
elif [ -r /proc/uptime ]; then
274
# return the integer part of the first item in /proc/uptime
276
read s c < /proc/uptime
283
get_network_interface() {
284
if [ -n "$MONITORED_NETWORK" ]; then
285
_RET="$MONITORED_NETWORK"
287
local Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
288
while read Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT; do
289
[ "$Mask" = "00000000" ] && break
290
done < /proc/net/route
295
# vi: syntax=sh ts=4 noexpandtab