Guest User

Untitled

a guest
Mar 13th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. # A simple statusbar script for awesome
  3. # I know my code sucks, thank you :)
  4. # Nathan LaFreniere <quitlahok@gmail.com>
  5.  
  6. def awesome_running(screen = 0)
  7. return File.exist?("/home/nathan/.awesome_ctl." + screen.to_s)
  8. end
  9.  
  10. def battery_status(widget, interval, bat, screen = 0)
  11. Thread.new {
  12. while true
  13. info_file = File.new("/proc/acpi/battery/" + bat + "/info", "r")
  14. state_file = File.new("/proc/acpi/battery/" + bat + "/state", "r")
  15. info_lines = info_file.readlines
  16. state_lines = state_file.readlines
  17. info_file.close
  18. state_file.close
  19. remaining = state_lines.grep(/remaining capacity/).to_s.split(" ")[2].to_i
  20. last_full = info_lines.grep(/last full/).to_s.split(" ")[3].to_i
  21. percentage = ((remaining * 100) / last_full.to_f)
  22. printf "%d widget_tell %s %3.2f\n\n" % [screen, widget, percentage]
  23. sleep(interval)
  24. end
  25. }
  26. end
  27.  
  28. def ac_status(widget, interval, acdev, screen = 0)
  29. Thread.new {
  30. while true
  31. state_file = File.new("/proc/acpi/ac_adapter/" + acdev + "/state", "r")
  32. state_lines = state_file.readlines
  33. state_file.close
  34. status = state_lines.grep(/state/).to_s.split(" ")[1]
  35. printf "%d widget_tell %s %s\n" % [screen, widget, status]
  36. sleep(interval)
  37. end
  38. }
  39. end
  40.  
  41. def cpu_speed(widget, interval, cpunum, screen = 0)
  42. Thread.new {
  43. while true
  44. cpuinfo_file = File.new("/proc/cpuinfo", "r")
  45. cpuinfo_lines = cpuinfo_file.readlines
  46. cpuinfo_file.close
  47. speed_lines = cpuinfo_lines.grep(/cpu MHz/)
  48. speed = speed_lines[cpunum].to_s.split(" ")[3].to_i
  49. printf "%d widget_tell %s %d\n" % [screen, widget, speed]
  50. sleep(interval)
  51. end
  52. }
  53. end
  54.  
  55. def mem_usage(widget, interval, screen = 0)
  56. Thread.new {
  57. while true
  58. meminfo_file = File.new("/proc/meminfo", "r")
  59. meminfo_lines = meminfo_file.readlines
  60. meminfo_file.close
  61. total = meminfo_lines.grep(/^MemTotal/).to_s.split(" ")[1].to_i
  62. free = meminfo_lines.grep(/^MemFree/).to_s.split(" ")[1].to_i
  63. buffers = meminfo_lines.grep(/^Buffers/).to_s.split(" ")[1].to_i
  64. cached = meminfo_lines.grep(/^Cached/).to_s.split(" ")[1].to_i
  65. used = (((total - free - cached - buffers) * 100) / total.to_f)
  66. printf "%d widget_tell %s %3.2f\%\n" % [screen, widget, used]
  67. sleep(interval)
  68. end
  69. }
  70. end
  71.  
  72. def swap_usage(widget, interval, screen = 0)
  73. Thread.new {
  74. while true
  75. meminfo_file = File.new("/proc/meminfo", "r")
  76. meminfo_lines = meminfo_file.readlines
  77. meminfo_file.close
  78. cached = meminfo_lines.grep(/SwapCached/).to_s.split(" ")[1].to_i
  79. total = meminfo_lines.grep(/SwapTotal/).to_s.split(" ")[1].to_i
  80. free = meminfo_lines.grep(/SwapFree/).to_s.split(" ")[1].to_i
  81. used = ((( total - free - cached) * 100) / total.to_f)
  82. printf "%d widget_tell %s %3.2f\%\n" % [screen, widget, used]
  83. sleep(interval)
  84. end
  85. }
  86. end
  87.  
  88. def audio_volume(widget, interval, mixer, screen = 0)
  89. Thread.new {
  90. while true
  91. amix_pipe = IO.popen("amixer get " + mixer)
  92. amix_string = amix_pipe.readlines
  93. amix_string.to_s.split("\n").grep(/%/).first =~ /\[(\d+)/
  94. volume = $1.to_i
  95. printf "%d widget_tell %s %d\%\n" % [screen, widget, volume]
  96. sleep(interval)
  97. end
  98. }
  99. end
  100.  
  101. def cpu_usage(widget, interval, cpunum, screen = 0)
  102. Thread.new {
  103. while true
  104. stat_file = File.new("/proc/stat", "r")
  105. stat_lines_1 = stat_file.readlines
  106. stat_file.close
  107. cpu_lines_1 = stat_lines_1.grep(/^cpu/)
  108. cpu_idle_1 = cpu_lines_1[cpunum+1].split(" ")[4].to_i
  109. 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
  110. sleep(interval)
  111. stat_file = File.new("/proc/stat", "r")
  112. stat_lines_2 = stat_file.readlines
  113. stat_file.close
  114. cpu_lines_2 = stat_lines_2.grep(/^cpu/)
  115. cpu_idle_2 = cpu_lines_2[cpunum+1].split(" ")[4].to_i
  116. 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
  117. cpu_idle = cpu_idle_2 - cpu_idle_1
  118. cpu_sum = cpu_sum_2 - cpu_sum_1
  119. cpu_usage = 100 - ((cpu_idle * 100) / cpu_sum.to_f)
  120. printf "%d widget_tell %s %3.2f\n" % [screen, widget, cpu_usage]
  121. sleep(interval)
  122. end
  123. }
  124. end
  125.  
  126. def wifi_essid(widget, interval, iface, screen = 0)
  127. Thread.new {
  128. while true
  129. iwconfig_out = IO.popen("iwconfig " + iface)
  130. iwconfig_lines = iwconfig_out.readlines
  131. iwconfig_lines.grep(/ESSID/).to_s.split(" ")[3] =~ /ESSID:"([^"]*)"/
  132. essid = $1.to_s
  133. printf "%d widget_tell %s %s\n" % [screen, widget, essid]
  134. sleep(interval)
  135. end
  136. }
  137. end
  138.  
  139. def wifi_strength(widget, interval, iface, screen = 0)
  140. Thread.new {
  141. while true
  142. iwconfig_out = IO.popen("iwconfig " + iface)
  143. iwconfig_lines = iwconfig_out.readlines
  144. quality_line = iwconfig_lines.grep(/Quality/).to_s.split(" ")
  145. quality_current = quality_line[1].split("=")[1].split("/")[0]
  146. quality_max = quality_line[1].split("=")[1].split("/")[1]
  147. quality = ((quality_current.to_i * 100) / quality_max.to_f)
  148. printf "%d widget_tell %s %3.2f\%\n" % [screen, widget, quality]
  149. sleep(interval)
  150. end
  151. }
  152. end
  153.  
  154. # all functions are called in the form of function(widget_name, run_interval)
  155. # any function that needs extra data will have it specified after those two
  156. # if you run multiple screens, you can add a last parameter for the screen
  157. # below are some examples, uncomment/modify as needed
  158.  
  159. battery_status("batbar", 5.0, "BAT1")
  160. #ac_status("ac", 5.0, "ACAD")
  161. #cpu_speed("cpu0", 5.0, 0)
  162. #cpu_speed("cpu1", 5.0, 1)
  163. #mem_usage("mem", 10.0)
  164. #swap_usage("swap", 10.0)
  165. #audio_volume("sound", 5.0, "Front")
  166. #cpu_usage("cpu0_usage", 1.0, 0)
  167. #cpu_usage("cpu1_usage", 1.0, 1)
  168. #wifi_essid("essid", 1.0, "ath0")
  169. #wifi_strength("qual", 1.0, "ath0")
  170.  
  171.  
  172. # please do not touch this, it's what keeps the script running :)
  173. main_thread = Thread.new {
  174. while awesome_running
  175. sleep(1.0)
  176. end
  177. }
  178. main_thread.join()
Add Comment
Please, Sign In to add comment