Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Andrew Wong
executable file 130 lines (114 sloc) 3.741 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#!/bin/bash
#
# ~/bin/dwm-statusbar
#
# Status bar for dwm. Expanded from:
# https://bitbucket.org/jasonwryan/eeepc/src/73dadb289dead8ef17ef29a9315ba8f1706927cb/Scripts/dwm-status

# Colour codes from dwm/config.h
colour_gry="\x01" # grey on black
colour_wht="\x02" # white on black
colour_dgry="\x04" # darkgrey on black
colour_blk="\x05" # black on darkgrey
colour_red="\x06" # colour_red on black
colour_grn="\x07" # green on black
colour_dylw="\x08" # orange on black
colour_ylw="\x09" # yellow on black
colour_blu="\x0A" # colour_blue on darkgrey
colour_mag="\x0B" # colour_magenta on darkgrey
colour_cyn="\x0C" # cyan on darkgrey

# Icon glyphs from font xbmicons.pcf
glyph_msc="\uE00E"
glyph_cpu="\uE00F"
glyph_mem="\uE010"
glyph_dl="\uE011"
glyph_ul="\uE012"
glyph_com="\uE013"
glyph_eml="\uE014"
glyph_vol="\uE015"
glyph_tim="\uE016"
glyph_tor="\uE017"
sep_solid="\uE01A"
sep_line="\uE01B"
sep_bar="\uE020"

print_song_info() {
  song_info="$(ncmpcpp --now-playing '{{{{%a - }%t}}|{%f}}' | head -c 75)"
  if [[ ! $song_info ]]; then
song_info="Off"
  fi
echo -ne "${colour_cyn} ${glyph_msc} ${song_info} "
}

print_torrent_status() {
  torrent_status="$(transmission-remote -l | awk -F " +" '$5 ~ /^[0-9]/ {print $5}')"
  if [[ ! $torrent_status ]]; then
torrent_status="Idle"
  fi
echo -ne "${colour_dylw} ${glyph_tor} ${torrent_status} "
}

print_email_unread() {
  echo -ne "${colour_ylw} ${glyph_eml} "
  for maildir in $(find $HOME/.mutt/maildir/*/inbox/new -type d); do
unread_count="$(ls "${maildir}" | wc -l)"
    echo -n "${unread_count} "
  done
}

print_last_msg() {
  last_msg="$(cat $HOME/.logs/irssi_pipe)"
  echo -ne "${colour_dylw} ${glyph_com} ${last_msg} "
}

print_mem_used() {
  mem_used="$(free -m | awk 'NR==3 {print $3}')"
  echo -ne "${sep_bar} ${glyph_mem} ${mem_used}M "
}

print_volume() {
  volume="$(amixer get PCM | tail -n1 | sed -r 's/.*\[(.*)%\].*/\1/')"
  echo -ne "${colour_mag} ${glyph_vol} ${volume}% "
}

print_datetime() {
  datetime="$(date "+%a %d %b ${sep_bar} %H:%M")"
  echo -ne "${colour_wht} ${glyph_tim} ${datetime} "
}

# network (from: http://dzen.geekmode.org/dwiki/doku.php?id=dzen:network-meter)
# cpu (from: https://bbs.archlinux.org/viewtopic.php?pid=661641#p661641)
rx_old=$(cat /sys/class/net/enp4s0/statistics/rx_bytes)
tx_old=$(cat /sys/class/net/enp4s0/statistics/tx_bytes)

while true; do
  # get new cpu idle and total usage
  eval $(awk '/^cpu /{print "cpu_idle_now=" $5 "; cpu_total_now=" $2+$3+$4+$5 }' /proc/stat)
  cpu_interval=$((cpu_total_now-${cpu_total_old:-0}))
  # calculate cpu usage (%)
  let cpu_used="100 * ($cpu_interval - ($cpu_idle_now-${cpu_idle_old:-0})) / $cpu_interval"

  # get new rx/tx counts
  rx_now=$(cat /sys/class/net/enp4s0/statistics/rx_bytes)
  tx_now=$(cat /sys/class/net/enp4s0/statistics/tx_bytes)
  # calculate the rate (K) and total (M)
  let rx_rate=($rx_now-$rx_old)/1024
  let tx_rate=($tx_now-$tx_old)/1024
  # let rx_total=$rx_now/1048576
  # let tx_total=$tx_now/1048576

  # output vars
  print_cpu_used() {
    printf "%-10b" "${colour_blu} ${glyph_cpu} ${cpu_used}% "
  }
  print_rx_rate() {
    printf "%-11b" "${colour_grn} ${glyph_dl} ${rx_rate}K "
  }
  print_tx_rate() {
    printf "%-15b" "${colour_gry}${sep_bar} ${colour_red}${glyph_ul} ${tx_rate}K"
  }

  # Pipe to status bar, not indented due to printing extra spaces/tabs
  xsetroot -name "$(print_song_info)\
$(print_torrent_status)\
$(print_email_unread)\
$(print_last_msg)\
$(print_cpu_used)$(print_mem_used)\
$(print_rx_rate)$(print_tx_rate)\
$(print_volume)\
$(print_datetime)"

  # reset old rates
  rx_old=$rx_now
  tx_old=$tx_now
  cpu_idle_old=$cpu_idle_now
  cpu_total_old=$cpu_total_now
  # loop stats every 1 second
  sleep 1
done
Something went wrong with that request. Please try again.