Skip to content

Instantly share code, notes, and snippets.

@sr
Created November 17, 2008 21:31
Show Gist options
  • Save sr/25914 to your computer and use it in GitHub Desktop.
Save sr/25914 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def truncate(text, length = 30, truncate_string = "...")
if text.nil? then return end
l = length - truncate_string.length
(text.length > length ? text[0..l] + truncate_string : text).to_s
end
def date
Time.now.strftime('%Y/%m/%d %H:%M')
end
def mpd
mpc = `mpc`
status = mpc =~ /\[(\w+)\]/ && $1
return 'blargh' unless status
return status.to_s unless status == 'playing'
truncate(mpc.split("\n")[0], 30)
end
def battery
state = File.readlines('/proc/acpi/battery/BAT0/state').map { |l| l.gsub(/.*:\s*/, '').chomp }
info = File.readlines('/proc/acpi/battery/BAT0/info').map { |l| l.gsub(/.*:\s*/, '').chomp }
percentage = ((state[4].chomp('mAh').to_f / info[2].chomp('mAh').to_f ) * 100).to_i
status = state[2]
indicator = case status
when 'charged' then '='
when 'charging' then '^'
when 'discharging' then 'v'
end
if status == 'discharging' && percentage <= 10
system("ssid echo 'Critical battery' | xmessage -buttons quit:0 -default quit -file -")
end
"#{indicator}#{percentage}%"
end
def wifi
(`iwconfig` =~ /Link Quality=(\d+)\/100/ && $1) || 0
end
puts "#{date} | [M: #{mpd}] | [B: #{battery}] | [W: #{wifi}]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment