Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
napalm255 committed Dec 12, 2014
0 parents commit 414e496
Show file tree
Hide file tree
Showing 16 changed files with 745 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# powertmux

# Screenshots

# Requirements

# Plugin Requirements

# Installation

# Configuration

# Debugging
140 changes: 140 additions & 0 deletions plugins/battery.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# LICENSE This code is not under the same license as the rest of the project as it's "stolen". It's cloned from https://github.com/richoH/dotfiles/blob/master/bin/battery and just some modifications are done so it works for my laptop. Check that URL for more recent versions.

POWERTMUX_SEG_BATTERY_TYPE_DEFAULT="percentage"
POWERTMUX_SEG_BATTERY_NUM_HEARTS_DEFAULT=5

HEART_FULL=""
HEART_EMPTY=""

run_plugin() {
__process_settings
if shell_is_osx; then
battery_status=$(__battery_osx)
else
battery_status=$(__battery_linux)
fi
[ -z "$battery_status" ] && return

case "$POWERTMUX_SEG_BATTERY_TYPE" in
"percentage")
output="${HEART_FULL} ${battery_status}%"
;;
"cute")
output=$(__cutinate $battery_status)
esac
if [ -n "$output" ]; then
echo "$output"
fi
}

__process_settings() {
if [ -z "$POWERTMUX_SEG_BATTERY_TYPE" ]; then
export POWERTMUX_SEG_BATTERY_TYPE="${POWERTMUX_SEG_BATTERY_TYPE_DEFAULT}"
fi
if [ -z "$POWERTMUX_SEG_BATTERY_NUM_HEARTS" ]; then
export POWERTMUX_SEG_BATTERY_NUM_HEARTS="${POWERTMUX_SEG_BATTERY_NUM_HEARTS_DEFAULT}"
fi
}

__battery_osx() {
ioreg -c AppleSmartBattery -w0 | \
grep -o '"[^"]*" = [^ ]*' | \
sed -e 's/= //g' -e 's/"//g' | \
sort | \
while read key value; do
case $key in
"MaxCapacity")
export maxcap=$value;;
"CurrentCapacity")
export curcap=$value;;
"ExternalConnected")
export extconnect=$value;;
"FullyCharged")
export fully_charged=$value;;
esac
if [[ -n $maxcap && -n $curcap && -n $extconnect ]]; then
if [[ "$curcap" == "$maxcap" || "$fully_charged" == "Yes" && $extconnect == "Yes" ]]; then
return
fi
charge=$(( 100 * $curcap / $maxcap ))
if [[ "$extconnect" == "Yes" ]]; then
echo "$charge"
else
if [[ $charge -lt 50 ]]; then
echo -n "#[fg=red]"
fi
echo "$charge"
fi
break
fi
done
}

__battery_linux() {
case "$SHELL_PLATFORM" in
"linux")
BATPATH=/sys/class/power_supply/BAT0
if [ ! -d $BATPATH ]; then
BATPATH=/sys/class/power_supply/BAT1
fi
STATUS=$BATPATH/status
BAT_FULL=$BATPATH/charge_full
if [ ! -r $BAT_FULL ]; then
BAT_FULL=$BATPATH/energy_full
fi
BAT_NOW=$BATPATH/charge_now
if [ ! -r $BAT_NOW ]; then
BAT_NOW=$BATPATH/energy_now
fi

if [ "$1" = `cat $STATUS` -o "$1" = "" ]; then
__linux_get_bat
fi
;;
"bsd")
STATUS=`sysctl -n hw.acpi.battery.state`
case $1 in
"Discharging")
if [ $STATUS -eq 1 ]; then
__freebsd_get_bat
fi
;;
"Charging")
if [ $STATUS -eq 2 ]; then
__freebsd_get_bat
fi
;;
"")
__freebsd_get_bat
;;
esac
;;
esac
}

__cutinate() {
perc=$1
inc=$(( 100 / $POWERTMUX_SEG_BATTERY_NUM_HEARTS ))


for i in `seq $POWERTMUX_SEG_BATTERY_NUM_HEARTS`; do
if [ $perc -lt 99 ]; then
echo -n $HEART_EMPTY
else
echo -n $HEART_FULL
fi
echo -n " "
perc=$(( $perc + $inc ))
done
}

__linux_get_bat() {
bf=$(cat $BAT_FULL)
bn=$(cat $BAT_NOW)
echo $(( 100 * $bn / $bf ))
}

__freebsd_get_bat() {
echo "$(sysctl -n hw.acpi.battery.life)"

}
15 changes: 15 additions & 0 deletions plugins/date.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Print the current date.

POWERTMUX_SEG_DATE_FORMAT_DEFAULT="%F"

__process_settings() {
if [ -z "$POWERTMUX_SEG_DATE_FORMAT" ]; then
export POWERTMUX_SEG_DATE_FORMAT="${POWERTMUX_SEG_DATE_FORMAT_DEFAULT}"
fi
}

run_plugin() {
__process_settings
date +"$POWERTMUX_SEG_DATE_FORMAT"
return 0
}
6 changes: 6 additions & 0 deletions plugins/date_day.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Prints the name of the current day.

run_plugin() {
date +"%a"
return 0
}
23 changes: 23 additions & 0 deletions plugins/hostname.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Prints the hostname.

POWERTMUX_SEG_HOSTNAME_FORMAT_DEFAULT="short"

__process_settings() {
if [ -z "$POWERTMUX_SEG_HOSTNAME_FORMAT" ]; then
export POWERTMUX_SEG_HOSTNAME_FORMAT="${POWERTMUX_SEG_HOSTNAME_FORMAT_DEFAULT}"
fi
}

run_plugin() {
__process_settings
local opts=""
if [ "$POWERTMUX_SEG_HOSTNAME_FORMAT" == "short" ]; then
if shell_is_osx || shell_is_bsd; then
opts="-s"
else
opts="--short"
fi
fi
hostname ${opts}
return 0
}
31 changes: 31 additions & 0 deletions plugins/lan_ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Prints the local network IPv4 address for a statically defined NIC or search for an IPv4 address on all active NICs.

run_plugin() {
if shell_is_bsd || shell_is_osx ; then
all_nics=$(ifconfig 2>/dev/null | awk -F':' '/^[a-z]/ && !/^lo/ { print $1 }')
for nic in ${all_nics[@]}; do
ipv4s_on_nic=$(ifconfig ${nic} 2>/dev/null | awk '$1 == "inet" { print $2 }')
for lan_ip in ${ipv4s_on_nic[@]}; do
[[ -n "${lan_ip}" ]] && break
done
[[ -n "${lan_ip}" ]] && break
done
else
# Get the names of all attached NICs.
all_nics="$(ip addr show | cut -d ' ' -f2 | tr -d :)"
all_nics=(${all_nics[@]//lo/}) # Remove lo interface.
for nic in "${all_nics[@]}"; do
# Parse IP address for the NIC.
lan_ip="$(ip addr show ${nic} | grep '\<inet\>' | tr -s ' ' | cut -d ' ' -f3)"
# Trim the CIDR suffix.
lan_ip="${lan_ip%/*}"
# Only display the last entry
lan_ip="$(echo "$lan_ip" | tail -1)"

[ -n "$lan_ip" ] && break
done
fi

echo "${lan_ip-N/a}"
return 0
}
6 changes: 6 additions & 0 deletions plugins/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Print the average load.

run_plugin() {
uptime | cut -d "," -f 3- | cut -d ":" -f2 | sed -e "s/^[ \t]*//"
exit 0
}
15 changes: 15 additions & 0 deletions plugins/time.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Prints the current time.

POWERTMUX_SEG_TIME_FORMAT_DEFAULT="%H:%M"

__process_settings() {
if [ -z "$POWERTMUX_SEG_TIME_FORMAT" ]; then
export POWERTMUX_SEG_TIME_FORMAT="${POWERTMUX_SEG_TIME_FORMAT_DEFAULT}"
fi
}

run_plugin() {
__process_settings
date +"$POWERTMUX_SEG_TIME_FORMAT"
return 0
}
7 changes: 7 additions & 0 deletions plugins/tmux_session_info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Prints tmux session info.
# Assuems that [ -n "$TMUX"].

run_plugin() {
tmux display-message -p '#S:#I.#P'
return 0
}
6 changes: 6 additions & 0 deletions plugins/uptime.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Prints the uptime.

run_plugin() {
uptime | grep -PZo "(?<=up )[^,]*"
return 0
}
6 changes: 6 additions & 0 deletions plugins/utc_time.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Prints the current time in UTC.

run_plugin() {
date -u +"%H:%M"
return 0
}
42 changes: 42 additions & 0 deletions plugins/wan_ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Prints the WAN IP address. The result is cached and updated according to $update_period.

run_plugin() {
local tmp_file="${POWERTMUX_DIR_TEMPORARY}/wan_ip.txt"
local wan_ip

if [ -f "$tmp_file" ]; then
if shell_is_osx || shell_is_bsd; then
stat >/dev/null 2>&1 && is_gnu_stat=false || is_gnu_stat=true
if [ "$is_gnu_stat" == "true" ];then
last_update=$(stat -c "%Y" ${tmp_file})
else
last_update=$(stat -f "%m" ${tmp_file})
fi
elif shell_is_linux || [ -z $is_gnu_stat]; then
last_update=$(stat -c "%Y" ${tmp_file})
fi

time_now=$(date +%s)
update_period=900
up_to_date=$(echo "(${time_now}-${last_update}) < ${update_period}" | bc)

if [ "$up_to_date" -eq 1 ]; then
wan_ip=$(cat ${tmp_file})
fi
fi

if [ -z "$wan_ip" ]; then
wan_ip=$(curl --max-time 2 -s http://whatismyip.akamai.com/)
if [ "$?" -eq "0" ]; then
echo "${wan_ip}" > $tmp_file
elif [ -f "${tmp_file}" ]; then
wan_ip=$(cat "$tmp_file")
fi
fi

if [ -n "$wan_ip" ]; then
echo "${wan_ip}"
fi

return 0
}
Loading

0 comments on commit 414e496

Please sign in to comment.