Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- # A simple statusbar script for awesome
- # I know my code sucks, thank you :)
- # Nathan LaFreniere <quitlahok@gmail.com>
- def awesome_running(screen = 0)
- return File.exist?("/home/nathan/.awesome_ctl." + screen.to_s)
- end
- def battery_status(widget, interval, bat, screen = 0)
- Thread.new {
- while true
- info_file = File.new("/proc/acpi/battery/" + bat + "/info", "r")
- state_file = File.new("/proc/acpi/battery/" + bat + "/state", "r")
- info_lines = info_file.readlines
- state_lines = state_file.readlines
- info_file.close
- state_file.close
- remaining = state_lines.grep(/remaining capacity/).to_s.split(" ")[2].to_i
- last_full = info_lines.grep(/last full/).to_s.split(" ")[3].to_i
- percentage = ((remaining * 100) / last_full.to_f)
- printf "%d widget_tell %s %3.2f\n\n" % [screen, widget, percentage]
- sleep(interval)
- end
- }
- end
- def ac_status(widget, interval, acdev, screen = 0)
- Thread.new {
- while true
- state_file = File.new("/proc/acpi/ac_adapter/" + acdev + "/state", "r")
- state_lines = state_file.readlines
- state_file.close
- status = state_lines.grep(/state/).to_s.split(" ")[1]
- printf "%d widget_tell %s %s\n" % [screen, widget, status]
- sleep(interval)
- end
- }
- end
- def cpu_speed(widget, interval, cpunum, screen = 0)
- Thread.new {
- while true
- cpuinfo_file = File.new("/proc/cpuinfo", "r")
- cpuinfo_lines = cpuinfo_file.readlines
- cpuinfo_file.close
- speed_lines = cpuinfo_lines.grep(/cpu MHz/)
- speed = speed_lines[cpunum].to_s.split(" ")[3].to_i
- printf "%d widget_tell %s %d\n" % [screen, widget, speed]
- sleep(interval)
- end
- }
- end
- def mem_usage(widget, interval, screen = 0)
- Thread.new {
- while true
- meminfo_file = File.new("/proc/meminfo", "r")
- meminfo_lines = meminfo_file.readlines
- meminfo_file.close
- total = meminfo_lines.grep(/^MemTotal/).to_s.split(" ")[1].to_i
- free = meminfo_lines.grep(/^MemFree/).to_s.split(" ")[1].to_i
- buffers = meminfo_lines.grep(/^Buffers/).to_s.split(" ")[1].to_i
- cached = meminfo_lines.grep(/^Cached/).to_s.split(" ")[1].to_i
- used = (((total - free - cached - buffers) * 100) / total.to_f)
- printf "%d widget_tell %s %3.2f\%\n" % [screen, widget, used]
- sleep(interval)
- end
- }
- end
- def swap_usage(widget, interval, screen = 0)
- Thread.new {
- while true
- meminfo_file = File.new("/proc/meminfo", "r")
- meminfo_lines = meminfo_file.readlines
- meminfo_file.close
- cached = meminfo_lines.grep(/SwapCached/).to_s.split(" ")[1].to_i
- total = meminfo_lines.grep(/SwapTotal/).to_s.split(" ")[1].to_i
- free = meminfo_lines.grep(/SwapFree/).to_s.split(" ")[1].to_i
- used = ((( total - free - cached) * 100) / total.to_f)
- printf "%d widget_tell %s %3.2f\%\n" % [screen, widget, used]
- sleep(interval)
- end
- }
- end
- def audio_volume(widget, interval, mixer, screen = 0)
- Thread.new {
- while true
- amix_pipe = IO.popen("amixer get " + mixer)
- amix_string = amix_pipe.readlines
- amix_string.to_s.split("\n").grep(/%/).first =~ /\[(\d+)/
- volume = $1.to_i
- printf "%d widget_tell %s %d\%\n" % [screen, widget, volume]
- sleep(interval)
- end
- }
- end
- def cpu_usage(widget, interval, cpunum, screen = 0)
- Thread.new {
- while true
- stat_file = File.new("/proc/stat", "r")
- stat_lines_1 = stat_file.readlines
- stat_file.close
- cpu_lines_1 = stat_lines_1.grep(/^cpu/)
- cpu_idle_1 = cpu_lines_1[cpunum+1].split(" ")[4].to_i
- cpu_sum_1 = cpu_lines_1[cpunum+1].split(" ")[1].to_i + cpu_lines_1[cpunum+1].split(" ")[2].to_i + cpu_lines_1[cpunum+1].split(" ")[3].to_i + cpu_lines_1[cpunum+1].split(" ")[4].to_i
- sleep(interval)
- stat_file = File.new("/proc/stat", "r")
- stat_lines_2 = stat_file.readlines
- stat_file.close
- cpu_lines_2 = stat_lines_2.grep(/^cpu/)
- cpu_idle_2 = cpu_lines_2[cpunum+1].split(" ")[4].to_i
- cpu_sum_2 = cpu_lines_2[cpunum+1].split(" ")[1].to_i + cpu_lines_2[cpunum+1].split(" ")[2].to_i + cpu_lines_2[cpunum+1].split(" ")[3].to_i + cpu_lines_2[cpunum+1].split(" ")[4].to_i
- cpu_idle = cpu_idle_2 - cpu_idle_1
- cpu_sum = cpu_sum_2 - cpu_sum_1
- cpu_usage = 100 - ((cpu_idle * 100) / cpu_sum.to_f)
- printf "%d widget_tell %s %3.2f\n" % [screen, widget, cpu_usage]
- sleep(interval)
- end
- }
- end
- def wifi_essid(widget, interval, iface, screen = 0)
- Thread.new {
- while true
- iwconfig_out = IO.popen("iwconfig " + iface)
- iwconfig_lines = iwconfig_out.readlines
- iwconfig_lines.grep(/ESSID/).to_s.split(" ")[3] =~ /ESSID:"([^"]*)"/
- essid = $1.to_s
- printf "%d widget_tell %s %s\n" % [screen, widget, essid]
- sleep(interval)
- end
- }
- end
- def wifi_strength(widget, interval, iface, screen = 0)
- Thread.new {
- while true
- iwconfig_out = IO.popen("iwconfig " + iface)
- iwconfig_lines = iwconfig_out.readlines
- quality_line = iwconfig_lines.grep(/Quality/).to_s.split(" ")
- quality_current = quality_line[1].split("=")[1].split("/")[0]
- quality_max = quality_line[1].split("=")[1].split("/")[1]
- quality = ((quality_current.to_i * 100) / quality_max.to_f)
- printf "%d widget_tell %s %3.2f\%\n" % [screen, widget, quality]
- sleep(interval)
- end
- }
- end
- # all functions are called in the form of function(widget_name, run_interval)
- # any function that needs extra data will have it specified after those two
- # if you run multiple screens, you can add a last parameter for the screen
- # below are some examples, uncomment/modify as needed
- battery_status("batbar", 5.0, "BAT1")
- #ac_status("ac", 5.0, "ACAD")
- #cpu_speed("cpu0", 5.0, 0)
- #cpu_speed("cpu1", 5.0, 1)
- #mem_usage("mem", 10.0)
- #swap_usage("swap", 10.0)
- #audio_volume("sound", 5.0, "Front")
- #cpu_usage("cpu0_usage", 1.0, 0)
- #cpu_usage("cpu1_usage", 1.0, 1)
- #wifi_essid("essid", 1.0, "ath0")
- #wifi_strength("qual", 1.0, "ath0")
- # please do not touch this, it's what keeps the script running :)
- main_thread = Thread.new {
- while awesome_running
- sleep(1.0)
- end
- }
- main_thread.join()
Add Comment
Please, Sign In to add comment