Advertisement
Guest User

g_SG

a guest
May 17th, 2009
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -- @file functions.lua
  3. -- @author Gigamo <gigamo@gmail.com>
  4. -------------------------------------------------------------------------------
  5.  
  6. -- {{{1 Environment
  7.  
  8. local os = os
  9. local io = io
  10. local math = math
  11. local string = string
  12. local type = type
  13. local tonumber = tonumber
  14. local spacer = ' '
  15. local awful = require('awful')
  16. local beautiful = require('beautiful')
  17.  
  18. module('functions')
  19.  
  20. -- {{{1 Markup
  21.  
  22. function set_bg(bgcolor, text)
  23.     if text then return '<span background="'..bgcolor..'">'..text..'</span>' end
  24. end
  25.  
  26. function set_fg(fgcolor, text)
  27.     if text then return '<span color="'..fgcolor..'">'..text..'</span>' end
  28. end
  29.  
  30. function set_font(font, text)
  31.     if text then return '<span font_desc="'..font..'">'..text..'</span>' end
  32. end
  33.  
  34. -- {{{1 Util
  35.  
  36. function escape(text)
  37.     return awful.util.escape(text or 'nil')
  38. end
  39.  
  40. -- Copy from awful.util
  41. function pread(cmd)
  42.     if cmd and cmd ~= '' then
  43.         local f, err = io.popen(cmd, 'r')
  44.         if f then
  45.             local s = f:read('*all')
  46.             f:close()
  47.             return s
  48.         else
  49.             print(err)
  50.         end
  51.     end
  52. end
  53.  
  54. -- Same as pread, but files instead of processes
  55. function fread(cmd)
  56.     if cmd and cmd ~= '' then
  57.         local f, err = io.open(cmd, 'r')
  58.         if f then
  59.             local s = f:read('*all')
  60.             f:close()
  61.             return s
  62.         else
  63.             print(err)
  64.         end
  65.     end
  66. end
  67.  
  68. -- {{{1 Clock
  69.  
  70. function clock(widget, format)
  71.     widget.text = spacer..os.date(format)..spacer
  72. end
  73.  
  74. -- {{{1 Battery
  75.  
  76. function battery(widget, adapter)
  77.     local index, color = 0, ''
  78.     local palette =
  79.     {
  80.         "#FF4444",
  81.         "#EE8888",
  82.         "#DD9988",
  83.         "#CCAA88",
  84.         "#CCBB88",
  85.         "#CCCC88",
  86.         "#BBBB88",
  87.         "#AAAA88",
  88.         "#999988",
  89.         "#888888",
  90.     }
  91.     local charge = pread('hal-get-property --udi /org/freedesktop/Hal/devices/computer_power_supply_battery_'..adapter..' --key battery.charge_level.percentage'):gsub("\n", '')
  92.     if tonumber(charge) > 10 then
  93.         index = math.min(math.floor(charge / 10), #palette)
  94.     else
  95.         index = 1
  96.     end
  97.     color = palette[index]
  98.  
  99.     widget.text = spacer..set_fg(color, charge..'%')..set_fg('#4C4C4C', ' |')
  100. end
  101.  
  102. -- {{{1 Memory
  103.  
  104. function memory(widget)
  105.     local memfile = io.open('/proc/meminfo')
  106.     if memfile then
  107.         for line in memfile:lines() do
  108.             if line:match("^MemTotal.*") then
  109.                 mem_total = math.floor(tonumber(line:match("(%d+)")) / 1024)
  110.             elseif line:match("^MemFree.*") then
  111.                 mem_free = math.floor(tonumber(line:match("(%d+)")) / 1024)
  112.             elseif line:match("^Buffers.*") then
  113.                 mem_buffers = math.floor(tonumber(line:match("(%d+)")) / 1024)
  114.             elseif line:match("^Cached.*") then
  115.                 mem_cached = math.floor(tonumber(line:match("(%d+)")) / 1024)
  116.             end
  117.         end
  118.     end
  119.     local mem_in_use = mem_total - (mem_free + mem_buffers + mem_cached)
  120.     local mem_usage_percentage = math.floor(mem_in_use / mem_total * 100)
  121.  
  122.     widget.text = spacer..mem_in_use..'Mb'..set_fg('#4C4C4C', ' |')
  123. end
  124.  
  125. -- {{{1 CPU
  126.  
  127. function cpu(widget)
  128.     local temperature, howmany = 0, 0
  129.     local sensors = io.popen('sensors')
  130.     if sensors then
  131.         for line in sensors:lines() do
  132.             if line:match(':%s+%+([.%d]+)') then
  133.                 howmany = howmany + 1
  134.                 temperature = temperature + tonumber(line:match(':%s+%+([.%d]+)'))
  135.             end
  136.         end
  137.         sensors:close()
  138.     end
  139.     temperature = temperature / howmany
  140.  
  141.     local freq, gov = {}, {}
  142.     for i = 0, 1 do
  143.         freq[i] = fread('/sys/devices/system/cpu/cpu'..i..'/cpufreq/scaling_cur_freq'):match('(.*)000')
  144.         gov[i] = fread('/sys/devices/system/cpu/cpu'..i..'/cpufreq/scaling_governor'):gsub("\n", '')
  145.     end
  146.  
  147.     widget.text = spacer..freq[0]..'/'..freq[1]..'MHz ('..gov[0]..') @ '..temperature..'C'..set_fg('#4C4C4C', ' |')
  148. end
  149.  
  150. -- {{{1 Load Average
  151.  
  152. function loadavg(widget)
  153.     local palette =
  154.     {
  155.         "#888888",
  156.         "#999988",
  157.         "#AAAA88",
  158.         "#BBBB88",
  159.         "#CCCC88",
  160.         "#CCBB88",
  161.         "#CCAA88",
  162.         "#DD9988",
  163.         "#EE8888",
  164.         "#FF4444",
  165.     }
  166.     local txt = fread('/proc/loadavg')
  167.     if type(txt) == 'string' then
  168.         local one, five, ten = txt:match('^([%d%.]+)%s+([%d%.]+)%s+([%d%.]+)%s+')
  169.         if type(one) == 'string' then
  170.             loadtext = string.format('%.2f %.2f %.2f', one, five, ten)
  171.         end
  172.         local current_avg = tonumber(one)
  173.         if type(current_avg) == 'number' then
  174.             local index = math.min(math.floor(current_avg * (#palette-1)) + 1, #palette)
  175.             color = palette[index]
  176.         end
  177.     end
  178.  
  179.     widget.text = spacer..set_fg(color, loadtext)..set_fg('#4C4C4C', ' |')
  180. end
  181.  
  182. -- {{{1 Volume
  183.  
  184. function volume(widget, mixer)
  185.     local vol = ''
  186.     local txt = pread('amixer get '..mixer)
  187.     if txt:match('%[off%]') then
  188.         vol = 'Mute'
  189.     else
  190.         vol = txt:match('%[(%d+%%)%]')
  191.     end
  192.  
  193.     widget.text = '['..vol..'] '
  194. end
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement