#!/bin/sh -e
#
# byobu-select-profile
# Copyright (C) 2008 Canonical Ltd.
#
# Authors: Dustin Kirkland
# Nick Barcet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# If you change any strings, please generate localization information with:
# ./debian/rules get-po
PKG="byobu"
[ -r "$HOME/.byoburc" ] && . "$HOME/.byoburc"
[ -z "${BYOBU_PREFIX}" ] && export BYOBU_PREFIX="/usr" || export BYOBU_PREFIX
. "${BYOBU_PREFIX}/lib/${PKG}/include/common"
TEXTDOMAIN="$PKG"
COLORS="default_light \
default_dark \
white \
black \
grey \
dark_grey \
light_grey \
blue \
light_blue \
cyan \
light_cyan \
green \
light_green \
purple \
light_purple \
red \
light_red \
yellow \
brown"
# Find a hash utility
for i in md5sum md5 sha512sum sha256sum sha1sum shasum shasum5.12 shasum5.10; do
if eval $BYOBU_TEST $i >/dev/null 2>&1; then
HASH="$i"
break
fi
done
usage () {
cat < $FILE
touch "$BYOBU_RUN_DIR/reload-required"
$BYOBU_BACKEND -X at 0 source "$BYOBU_CONFIG_DIR/profile"
}
get_contrast() {
# See section 2.2: http://www.w3.org/TR/AERT#color-contrast
local awk="awk"
if eval $BYOBU_TEST gawk >/dev/null 2>&1; then
awk="gawk"
fi
local hex="$(echo "$1" | sed -e "s/\(..\)\(..\)\(..\)/\1 \2 \3/")"
local bright=$(echo $hex | $awk --non-decimal-data '{printf "%0.0f",(("0x"$1)*299+("0x"$2)*587+("0x"$3)*114)/1000}')
if [ $bright -ge 130 ]; then
_RET="black"
else
_RET="white"
fi
}
setcolor_tmux() {
if [ "$BYOBU_BACKEND" != "tmux" ]; then
echo "WARNING: This functionality is only supported in Byobu with the tmux backend" 1>&2
fi
dark="$1"
get_contrast "$dark"
light="$_RET"
accent="magenta"
highlight="red"
monochrome="0"
printf "BYOBU_DARK=\"$dark\"\nBYOBU_LIGHT=$light\nBYOBU_ACCENT=$accent\nBYOBU_HIGHLIGHT=$highlight\nMONOCHROME=$MONOCHROME" > "$FILE".tmux
tmux source "$BYOBU_PREFIX/share/byobu/profiles/tmuxrc"
}
if [ $# -eq 0 ]; then
usage
else
while true; do
case "$1" in
-b|--background)
setcolor_screen "background" "$2"
shift 2
;;
-f|--foreground)
setcolor_screen "foreground" "$2"
shift 2
;;
-h|--hostname)
color=$(hostname | $HASH | head -c 6)
setcolor_tmux "$color"
shift
break
;;
-i|--ip)
[ -r "$BYOBU_CONFIG_DIR/statusrc" ] && . "$BYOBU_CONFIG_DIR/statusrc"
. $BYOBU_PREFIX/lib/$PKG/ip_address
color=$(__ip_address t| $HASH | head -c 6)
setcolor_tmux "$color"
shift
break
;;
-r|--random)
color=$(head -c 10 /dev/urandom | $HASH | head -c 6)
setcolor_tmux "\#$color"
shift
break
;;
*)
usage
exit 1
;;
--)
shift
break
;;
esac
[ $# -eq 0 ] && break
done
fi
exit 0
# vi: syntax=sh ts=4 noexpandtab