~ubuntu-branches/debian/wheezy/byobu/wheezy

« back to all changes in this revision

Viewing changes to usr/lib/byobu/.shutil

  • Committer: Package Import Robot
  • Author(s): Alexander Chernyakhovsky
  • Date: 2011-10-02 19:04:37 UTC
  • mfrom: (0.5.2 upstream) (0.1.145 oneiric)
  • Revision ID: package-import@ubuntu.com-20111002190437-apn9z04063rh052s
Tags: 4.37-1
* Syncing from Ubuntu.
* Switch to dh short syntax
* Change maintainer (adopting this package)
* Added a substvar ${perl:Depends} dependency into debian/control
* Bump standards version to 3.9.2, no changes 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
#    .shutil: some shared utilities used by all status scripts
 
4
#
 
5
#    Copyright (C) 2011 Dustin Kirkland
 
6
#
 
7
#    Authors: Dustin Kirkland <kirkland@ubuntu.com>
 
8
#             Scott Moser <smoser@ubuntu.com>
 
9
#
 
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.
 
13
#
 
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.
 
18
#
 
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/>.
 
21
 
 
22
# Define colors
 
23
color_screen() {
 
24
        ESC="\005"
 
25
        case "$1" in
 
26
                "") return 0 ;;
 
27
                -)   printf "$ESC{-}" ;;
 
28
                --)   printf "$ESC{-} " ;;
 
29
                esc)    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 }" ;;
 
34
                *)
 
35
                        local attr fg bg
 
36
                        case $# in
 
37
                                2)
 
38
                                        attr= ; fg=$1 ; bg=$2
 
39
                                ;;
 
40
                                3)
 
41
                                        attr=$1 ; fg=$2 ; bg=$3
 
42
                                ;;
 
43
                        esac
 
44
                        if [ "$MONOCHROME" = "1" ]; then
 
45
                                fg=
 
46
                                bg=
 
47
                        fi
 
48
                        printf "$ESC{=$attr $fg$bg}"
 
49
                ;;
 
50
        esac
 
51
}
 
52
 
 
53
color_map() {
 
54
        case "$1" in
 
55
                k) _RET="black" ;;
 
56
                r) _RET="red" ;;
 
57
                g) _RET="green" ;;
 
58
                y) _RET="yellow" ;;
 
59
                b) _RET="blue" ;;
 
60
                m) _RET="magenta" ;;
 
61
                c) _RET="cyan" ;;
 
62
                w) _RET="white" ;;
 
63
                d) _RET="color0" ;;
 
64
                K) _RET="#555555" ;;
 
65
                R) _RET="#FF0000" ;;
 
66
                G) _RET="#00FF00" ;;
 
67
                Y) _RET="#FFFF00" ;;
 
68
                B) _RET="#0000FF" ;;
 
69
                M) _RET="#FF00FF" ;;
 
70
                C) _RET="#00FFFF" ;;
 
71
                W) _RET="#FFFFFF" ;;
 
72
                *) _RET= ;;
 
73
        esac
 
74
}
 
75
 
 
76
attr_map() {
 
77
        case "$1" in
 
78
                d) _RET=,dim ;;
 
79
                u) _RET=,underscore ;;
 
80
                b) _RET=,bold ;;
 
81
                r) _RET=,reverse ;;
 
82
                s) _RET=,standout ;;
 
83
                B) _RET=,blinking ;;
 
84
                h) _RET=,hidden ;;
 
85
                i) _RET=,italics ;;
 
86
                *) _RET= ;;
 
87
        esac
 
88
}
 
89
 
 
90
color_tmux() {
 
91
        local back fore attr
 
92
        case "$1" in
 
93
                "") return 0 ;;
 
94
                -)   printf "#[default]" ;;
 
95
                --)   printf "#[default] " ;;
 
96
                esc)    printf "" ;;
 
97
                bold*)  printf "#[fg=bold]" ;;
 
98
                none)   printf "#[default]" ;;
 
99
                invert) printf "#[reverse]" ;;
 
100
                *)
 
101
                        if [ "$#" = "2" ]; then
 
102
                                color_map "$1"; back="$_RET"
 
103
                                color_map "$2"; fore="$_RET"
 
104
                        else
 
105
                                attr_map "$1";  attr="$_RET"
 
106
                                color_map "$2"; back="$_RET"
 
107
                                color_map "$3"; fore="$_RET"
 
108
                        fi
 
109
                        [ "$MONOCHROME" = "1" ] && printf "#[default]" || printf "#[fg=$fore$attr,bg=$back]"
 
110
                ;;
 
111
        esac
 
112
}
 
113
 
 
114
color() {
 
115
        case "$BYOBU_BACKEND" in
 
116
                tmux)
 
117
                        color_tmux "$@"
 
118
                ;;
 
119
                *)
 
120
                        color_screen "$@"
 
121
                ;;
 
122
        esac
 
123
}
 
124
 
 
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:-#}"
 
129
        while read line; do
 
130
                [ "${line#${chr}}" = "${line}" ] && return 0;
 
131
        done
 
132
        return 1
 
133
}
 
134
 
 
135
# newest(file,file,file..)
 
136
# return the newest file in the list
 
137
newest() {
 
138
        local c="$1" i
 
139
        for i in "$@"; do [ "$i" -nt "$c" ] && c="$i"; done
 
140
        [ -e "$c" ] && _RET="$c"
 
141
}
 
142
 
 
143
error() {
 
144
        echo "ERROR: " "$@" 1>&2
 
145
}
 
146
 
 
147
fail() {
 
148
        [ $# -eq 0 ] || error "$@"; exit 1;
 
149
}
 
150
 
 
151
find_script() {
 
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"
 
159
        else
 
160
                _RET="/dev/null"
 
161
        fi
 
162
}
 
163
 
 
164
# divide 2 integers and return a floating point number
 
165
# third argument indicates how many digits after the decimal
 
166
fpdiv() {
 
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
 
170
                mp="${mp}0"
 
171
                chunk="${chunk}?"
 
172
        done
 
173
 
 
174
        n=$(((${mp}*${a})/${b}))
 
175
 
 
176
        # round up if necessary
 
177
        [ $(($n%${mp})) -ge $((${mp}/2)) ] && n=$(($n+5))
 
178
 
 
179
        # drop the last digit, which was only there for rounding
 
180
        n=${n%?}
 
181
        whole=${n%${chunk}}
 
182
        part=${n#${whole}}
 
183
        _RET=${whole}${part:+.${part}}
 
184
        return
 
185
}
 
186
 
 
187
# rtrim(string,chars)
 
188
rtrim() {
 
189
        local tab=' ' cr="
 
190
"
 
191
        local cur="${1}" set="[${2:- ${tab}${cr}}]" n=""
 
192
        while n=${cur%${set}} && [ "$n" != "$cur" ]; do cur=${n}; done
 
193
        _RET=${cur}
 
194
}
 
195
 
 
196
readfile() {
 
197
        local c="" r="" cr="
 
198
"
 
199
        OIFS="$IFS"; IFS="";
 
200
        while read c; do
 
201
                r="$r${cr}$c"
 
202
        done
 
203
        IFS=$OIFS
 
204
        _RET=${r}
 
205
        return 0
 
206
}
 
207
 
 
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
 
211
        # machines.
 
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
 
214
        else
 
215
                false
 
216
        fi
 
217
}
 
218
 
 
219
status_freq() {
 
220
# Define status frequencies
 
221
#   Use prime number intervals, to decrease collisions, which
 
222
#   yields some less expensive status updates.
 
223
#   ~86000 ~1 day
 
224
#   ~600   ~10 minutes
 
225
#   ~180   ~3 minutes
 
226
#   ~60    ~1 minute
 
227
        case "$1" in
 
228
                apport)         _RET=67 ;;
 
229
                arch)           _RET=9999999 ;;
 
230
                battery)        _RET=61 ;;
 
231
                color)          _RET=9999999 ;;
 
232
                cpu_count)      _RET=5 ;;
 
233
                cpu_freq)       _RET=2 ;;
 
234
                cpu_temp)       _RET=19 ;;
 
235
                custom)         _RET=5 ;;
 
236
                date)           [ "$BYOBU_BACKEND" = "tmux" ] && _RET=1 || _RET=28793 ;;
 
237
                disk)           _RET=13 ;;
 
238
                disk_io)        _RET=3 ;;
 
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 ;;
 
245
                mail)           _RET=5 ;;
 
246
                memory)         _RET=13 ;;
 
247
                menu)           _RET=9999999 ;;
 
248
                network)        _RET=3 ;;
 
249
                notify_osd)     _RET=9999999 ;;
 
250
                processes)      _RET=7 ;;
 
251
                raid)           _RET=59 ;;
 
252
                rcs_cost)       _RET=613 ;;
 
253
                reboot_required) _RET=5 ;;
 
254
                release)        _RET=599 ;;
 
255
                services)       _RET=53 ;;
 
256
                swap)           _RET=19 ;;
 
257
                time)           [ "$BYOBU_BACKEND" = "tmux" ] && _RET=1 || _RET=9999999 ;;
 
258
                time_binary)    _RET=23 ;;
 
259
                time_utc)       _RET=11 ;;
 
260
                trash)          _RET=9999999 ;;
 
261
                updates_available) _RET=7 ;;
 
262
                uptime)         _RET=29 ;;
 
263
                users)          _RET=11 ;;
 
264
                whoami)         _RET=86029 ;;
 
265
                wifi_quality)   _RET=17 ;;
 
266
                *)              _RET=9999999 ;;
 
267
        esac
 
268
}
 
269
 
 
270
get_now() {
 
271
        if [ -n "${BASH_VERSION}" ] && [ -n "${SECONDS}" ]; then
 
272
                _RET=${SECONDS}
 
273
        elif [ -r /proc/uptime ]; then
 
274
                # return the integer part of the first item in /proc/uptime
 
275
                local s c
 
276
                read s c < /proc/uptime
 
277
                _RET=${s%.*}
 
278
        else
 
279
                _RET=$(date +%s);
 
280
        fi
 
281
}
 
282
 
 
283
get_network_interface() {
 
284
        if [ -n "$MONITORED_NETWORK" ]; then
 
285
                _RET="$MONITORED_NETWORK"
 
286
        else
 
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
 
291
                _RET="$Iface"
 
292
        fi
 
293
}
 
294
 
 
295
# vi: syntax=sh ts=4 noexpandtab