レポジトリ種類: Mercurial

#!/bin/sh

# Define colors
color_screen() {
  ESC="\005"
  case "$1" in
    "") return 0 ;;
    -)   printf "$ESC{-}" ;;
    --)   printf "$ESC{-} " ;;
    esc)    printf "$ESC" ;;
    bold1)  printf "$ESC{=b }" || printf "$ESC{= }" ;;
    bold2)  printf "$ESC{+b }" || printf "$ESC{= }" ;;
    none)   printf "$ESC{= }" ;;
    invert) printf "$ESC{=r }" ;;
    *)
      local attr fg bg
      case $# in
        2)
          attr= ; fg=$1 ; bg=$2
          ;;
        3)
          attr=$1 ; fg=$2 ; bg=$3
          ;;
      esac
      if [ "$MONOCHROME" = "1" ]; then
        fg=
        bg=
      fi
      printf "$ESC{=$attr $fg$bg}"
    ;;
  esac
}

color_map() {
  case "$1" in
    "k") _RET="black" ;;
    "r") _RET="red" ;;
    "g") _RET="green" ;;
    "y") _RET="yellow" ;;
    "b") _RET="blue" ;;
    "m") _RET="magenta" ;;
    "c") _RET="cyan" ;;
    "w") _RET="white" ;;
    "d") _RET="black" ;;
    "K") _RET="brightblack" ;;
    "R") _RET="brightred" ;;
    "G") _RET="brightgreen" ;;
    "Y") _RET="brightyellow" ;;
    "B") _RET="brightblue" ;;
    "M") _RET="brightmagenta" ;;
    "C") _RET="brightcyan" ;;
    "W") _RET="brightwhite" ;;
    *) _RET="$1" ;;
  esac
}

attr_map() {
  case "$1" in
    "d") _RET=,dim ;;
    "u") _RET=,underscore ;;
    "b") _RET=,bold ;;
    "r") _RET=,reverse ;;
    "s") _RET=,standout ;;
    "B") _RET=,blinking ;;
    "h") _RET=,hidden ;;
    "i") _RET=,italics ;;
    *) _RET= ;;
  esac
}

color_tmux() {
  local back fore attr
  case "$1" in
    "") return 0 ;;
    -)      printf "#[default]#[fg=$BYOBU_LIGHT,bg=$BYOBU_DARK]" ;;
    --)     printf "#[default]#[fg=$BYOBU_LIGHT]#[bg=$BYOBU_DARK] " ;;
    esc)    printf "" ;;
    bold*)  printf "#[default]#[fg=bold]" ;;
    none)   printf "#[default]#[fg=$BYOBU_LIGHT,bg=$BYOBU_DARK]" ;;
    invert) printf "#[default]#[reverse]" ;;
    *)
      if [ "$#" = "2" ]; then
        color_map "$1"; back="$_RET"
	color_map "$2"; fore="$_RET"
      else
        attr_map "$1";  attr="$_RET"
	color_map "$2"; back="$_RET"
	color_map "$3"; fore="$_RET"
      fi
      [ "$MONOCHROME" = "1" ] && printf "#[default]" || printf "#[default]#[fg=$fore$attr,bg=$back]"
    ;;
  esac
}

color() {
  case "$BYOBU_BACKEND" in
    tmux)
      color_tmux "$@"
    ;;
    screen)
      color_screen "$@"
    ;;
  esac
}

uncommented_lines() {
  local line chr="${1:-#}"
  while read line; do
    [ "${line#${chr}}" = "${line}" ] && return 0;
  done
  return 1
}

newest() {
  local c="$1" i
  for i in "$@"; do [ "$i" -nt "$c" ] && c="$i"; done
  [ -e "$c" ] && _RET="$c"
}

error() {
  printf "%s\n" "ERROR: " "$@" 1>&2
}

fail() {
  [ $# -eq 0 ] || error "$@"; exit 1;
}

find_script() {
  if [ -x "$BYOBU_CONFIG_DIR/bin/$1" ]; then
    _RET="$BYOBU_CONFIG_DIR/bin/$1"
  elif [ -x "$BYOBU_PREFIX/lib/$PKG/$1" ]; then
    _RET="$BYOBU_PREFIX/lib/$PKG/$1"
  elif [ -x "$BYOBU_PREFIX/libexec/$PKG/$1" ]; then
    _RET="$BYOBU_PREFIX/libexec/$PKG/$1"
  else
    _RET="/dev/null"
  fi
}

fpdiv() {
  local a=$1 b=$2 pres=${3:-3}
  local i=0 mp="10" whole="" part="" chunk="" n=0
  while i=$(($i+1)) && [ $i -le $pres ]; do
    mp="${mp}0"
    chunk="${chunk}?"
  done

  n=$(((${mp}*${a})/${b}))

  [ $((($n-5)/10)) = $(($n/10)) ] && n=$(($n+5))

  n=${n%?}
  whole=${n%${chunk}}
  part=${n#${whole}}
  _RET=${whole:-0}${part:+.${part}}
  return
}

rtrim() {
  local tab=' ' cr=""
  local cur="${1}" set="[${2:- ${tab}${cr}}]" n=""
  while n=${cur%${set}} && [ "$n" != "$cur" ]; do cur=${n}; done
  _RET=${cur}
}

readfile() {
  local c="" r="" cr=""
  OIFS="$IFS"; IFS="";
  while read c; do
    r="$r${cr}$c"
  done
  IFS=$OIFS
  _RET=${r}
  return 0
}

metadata_available() {
  local x=0 cache="$BYOBU_CONFIG_DIR/.metadata_available"
  if [ -s "$cache" ]; then
    x=1
  else
    if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then
      timeout 1 wget -q -O- --tries=1 http://169.254.169.254 </dev/null >"$cache" 2>/dev/null &
      sleep 0.02
      [ -s "$cache" ] && x=1
    fi
  fi
  [ "$x" = "1" ]
}

status_freq() {
  case "$1" in
    apport)		_RET=67 ;;
    arch)		_RET=9999991 ;;
    bat)	_RET=61 ;;
    battery)	_RET=61 ;;
    color)		_RET=9999991 ;;
    cpu_count)	_RET=5 ;;
    cpu_freq)	_RET=2 ;;
    cpu_temp)	_RET=19 ;;
    custom)		_RET=5 ;;
    date)		_RET=9999991 ;;
    disk)		_RET=13 ;;
    disk_io)	_RET=3 ;;
    distro)		_RET=9999991 ;;
    entropy)	_RET=5 ;;
    fan_speed)	_RET=23 ;;
    hostname)	_RET=607 ;;
    ip_address)	_RET=127 ;;
    load_average)	_RET=2 ;;
    logo)		_RET=9999991 ;;
    mac_audio)  _RET=5 ;;
    mail)		_RET=5 ;;
    memory)		_RET=13 ;;
    menu)		_RET=9999991 ;;
    nbsd_audio)  _RET=4 ;;
    network)	_RET=3 ;;
    notify_osd)	_RET=9999991 ;;
    processes)	_RET=7 ;;
    raid)		_RET=59 ;;
    reboot_required) _RET=5 ;;
    release)	_RET=599 ;;
    services)	_RET=53 ;;
    session)	_RET=9999991 ;;
    sndio)  _RET=5 ;;
    swap)		_RET=19 ;;
    time)		_RET=9999991 ;;
    time_binary)	_RET=23 ;;
    time_utc)	_RET=11 ;;
    trash)		_RET=9999991 ;;
    updates_available) _RET=7 ;;
    uptime)		_RET=29 ;;
    users)		_RET=11 ;;
    whoami)		_RET=86029 ;;
    wifi_quality)	_RET=17 ;;
    *)		_RET=9999991 ;;
  esac
}

get_now() {
  if [ -r /proc/uptime ]; then
    local s c
    read s c < /proc/uptime
    _RET=${s%.*}
  else
    _RET=$(date +%s);
  fi
}

get_network_interface() {
  if [ -n "$MONITORED_NETWORK" ]; then
    _RET="$MONITORED_NETWORK"
  elif [ -e /proc/net/route ]; then
    local Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
    while read Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT; do
      [ "$Mask" = "00000000" ] && break
    done < /proc/net/route
    _RET="$Iface"
  elif eval $BYOBU_TEST route >/dev/null 2>&1; then
    _RET=$(route get default|grep interface:|awk '{print $2}')
  elif [ -x "/sbin/route" ]; then
    _RET=$(/sbin/route get default|grep interface:|awk '{print $2}')
  fi
}

get_distro() {
  local distro="${DISTRO}"
  if [ -n "$DISTRO" ]; then
    true
  elif [ -r "/etc/os-release" ]; then
    distro=$(. /etc/os-release && echo "$NAME")
    case "$distro" in
      Debian*)
        [ -r /etc/lsb-release ] && distro=$(. /etc/lsb-release && [ -n "$GOOGLE_ID" ] && echo "gLinux" || echo "Debian")
      ;;
    esac
  elif [ -r "/etc/issue" ]; then
    local issue
    IFS="" read issue < /etc/issue
    case "$issue" in
      Ubuntu*) distro="Ubuntu" ;;
      Debian*) distro="Debian" ;;
      Red\ Hat\ Enterprise*) distro="RHEL" ;;
      *) distro="${issue%% *}";;
    esac
    elif eval $BYOBU_TEST lsb_release >/dev/null 2>&1; then
    local r=$(lsb_release -s -d)
    case "$r" in
      Ubuntu*) distro="Ubuntu" ;;
      *) distro=$(lsb_release -s -i) ;;
    esac
  elif eval $BYOBU_TEST sw_vers >/dev/null 2>&1; then
    distro="$(sw_vers -productName)"
  elif eval $BYOBU_TEST uname >/dev/null 2>&1; then
    distro="$(uname -s)"
  else
    distro="Byobu"
  fi
  _RET="$distro"
}