Skip to content

Instantly share code, notes, and snippets.

@metalefty
Last active December 18, 2015 15:18
Show Gist options
  • Save metalefty/5803006 to your computer and use it in GitHub Desktop.
Save metalefty/5803006 to your computer and use it in GitHub Desktop.
九州電力の電力使用率を tmux のステータスバーに出力する
#!/bin/sh
# Usage:
# ~/.tmux.conf に以下の4行を書く(:より後ろ)
# 色は好みに合わせて変更すること
# .tmux.conf: set -g status-bg colour17 # navy
# .tmux.conf: set -g status-fg colour15 # white
# .tmux.conf: set -g status-interval 60
# .tmux.conf: set -g status-right '#(~/scripts/kyuden_tmux.sh)'
#
# 東京電力対応にしたい場合は下の API_URI を使う
# API_URI=http://tepco-usage-api.appspot.com/quick.txt
#
# ※Linux や Mac だと1行目を /bin/bash にしたほうがいいかもしれない
API_URI=http://www.club.kyutech.ac.jp/~meta/kyuden/quick.cgi?kyuden_tmux
TMPDIR=/tmp
__kyuden_tmux()
{
RESULT=$(cat ${TMPDIR}/$(basename ${API_URI}))
if [ -z "${RESULT}" ]; then echo "--%"; return; fi
PERCENTAGE=$(echo "scale=1; $(echo ${RESULT} | cut -f2,3 -d, | sed -e 's/,/ \* 100 \/ /')" | bc)
LASTUPDATE=$(echo ${RESULT}|cut -f1 -d,)
if [ $(echo "${PERCENTAGE} >= 95" | bc) -eq 1 ]; then
echo -n "#[fg=colour0,bg=colour203,blink]${PERCENTAGE}%#[default]";
elif [ $(echo "${PERCENTAGE} >= 90" | bc) -eq 1 ]; then
echo -n "#[fg=colour203]${PERCENTAGE}%#[default]";
elif [ $(echo "${PERCENTAGE} >= 80" | bc) -eq 1 ]; then
echo -n "#[fg=colour190]${PERCENTAGE}%#[default]";
elif [ $(echo "${PERCENTAGE} < 80" | bc) -eq 1 ]; then
echo -n "#[fg=colour118]${PERCENTAGE}%#[default]";
fi
}
__get_usage()
{
wget --quiet --timestamping --directory-prefix=${TMPDIR} ${API_URI}
}
# 最新の情報を取ってくるが使われるのは次回かもしれない
__get_usage &
# tmux 用の色付きステータスを出力
__kyuden_tmux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment