xdbe replaced with more generic pixmap based buffering but there are 2 bugs:
[monky] / lua_scripts / misc.lua
1 --\r
2 -- Conky Lua scripting example\r
3 --\r
4 -- Copyright (c) 2009 Brenden Matthews, all rights reserved.\r
5 --\r
6 -- This program is free software: you can redistribute it and/or modify\r
7 -- it under the terms of the GNU General Public License as published by\r
8 -- the Free Software Foundation, either version 3 of the License, or\r
9 -- (at your option) any later version.\r
10 --\r
11 \r
12 function components_to_colour(r, g, b)\r
13         -- Take the RGB components r, g, b, and return an RGB integer\r
14         return ((math.floor(r + 0.5) * 0x10000) + (math.floor(g + 0.5) * 0x100) + math.floor(b + 0.5)) % 0xffffff -- no bit shifting operator in Lua afaik\r
15 end\r
16 \r
17 function colour_to_components(colour)\r
18         -- Take the RGB components r, g, b, and return an RGB integer\r
19         return (colour / 0x10000) % 0x100, (colour / 0x100) % 0x100, colour % 0x100\r
20 end\r
21 \r
22 function conky_top_colour(value, default_colour, lower_thresh, upper_thresh)\r
23         --[[\r
24         This function returns a colour based on a threshold, by adding more of\r
25         the red component and reducing the other components.  ``value'' is the\r
26         value we're checking the thresholds against, ``default_colour'' is the\r
27         original colour (before adjusting), and the ``lower_thresh'' and\r
28         ``upper_thresh'' parameters are the low and high values for which we\r
29         start applying redness.\r
30         ]]\r
31         local r, g, b = colour_to_components(default_colour)\r
32         local colour = 0\r
33         if value ~= nil and (value - lower_thresh) > 0 then\r
34                 if value > upper_thresh then value = upper_thresh end\r
35                 local perc = (value - lower_thresh) / (upper_thresh - lower_thresh)\r
36                 if perc > 1 then perc = 1 end\r
37                 -- add some redness, depending on where ``value'' lies within the\r
38                 -- threshhold range\r
39                 r = r + perc * (0xff - r)\r
40                 b = b - perc * b\r
41                 g = g - perc * g\r
42         end\r
43         colour = components_to_colour(r, g, b)\r
44         return string.format("${color #%06x}", colour)\r
45 end\r
46 \r
47 -- parses the output from top and calls the colour function\r
48 function conky_top_cpu_colour(arg)\r
49         -- input is the top var number we want to use\r
50         local str = conky_parse(string.format('${top name %i}${top cpu %i}${top mem %i}', tonumber(arg), tonumber(arg), tonumber(arg)))\r
51         local cpu = tonumber(string.match(str, '(%d+%.%d+)'))\r
52         return conky_top_colour(cpu, 0xd3d3d3, 25, 70) .. str\r
53 end\r
54 \r
55 function conky_top_mem_colour(arg)\r
56         -- input is the top var number we want to use\r
57         local str = conky_parse(string.format('${top_mem name %i}${top_mem mem_res %i}  ${top_mem mem_vsize %i}', tonumber(arg), tonumber(arg), tonumber(arg)))\r
58         local mem = tonumber(string.match(str, '%w+%s+(%d+%.%d+)%w%s%s'))\r
59         -- tweak the last 3 parameters to your liking\r
60         -- my machine has ~8GiB of ram, so an upper thresh of 15% seemed appropriate\r
61         return conky_top_colour(mem, 0xd3d3d3, 10, 24) .. str\r
62 end\r
63 \r
64 function conky_top_io_colour(arg)\r
65         -- input is the top var number we want to use\r
66         local str = conky_parse(string.format('${top_io name %i}${top_io io_read %i} ${top_io io_write %i} ${top_io io_perc %i}', tonumber(arg), tonumber(arg), tonumber(arg), tonumber(arg)))\r
67         local ioR,ioW = string.match(str, '%w+%s+(%d+%.*%d*%w)%s+(%d+%.*%d*%w)%s+')\r
68         local tot = conky_parse("${to_bytes "..ioR.."}") + conky_parse("${to_bytes "..ioW.."}") --these can be bytes or mb :(\r
69         -- tweak the last 3 parameters to your liking\r
70         -- my machine has ~8GiB of ram, so an upper thresh of 15% seemed appropriate\r
71         str = string.gsub(str," 0B", " 0.00B")\r
72         return conky_top_colour(tot, 0xd3d3d3, 100, 200) .. str\r
73 end\r
74 \r
75 function colour_transition(start, stop, position)\r
76         --[[\r
77         Transition from one colour to another based on the value of\r
78         ``position'', which should be a number between 0 and 1.\r
79         ]]\r
80         local rs, gs, bs = colour_to_components(start) -- start components\r
81         local re, ge, be = colour_to_components(stop) -- end components\r
82         local function tr(s, e, p)\r
83                 return e + (e - s) * p\r
84         end\r
85         local rr, gr, br = tr(rs, re, position), tr(gs, ge, position), tr(bs, be, position) -- result components\r
86         return components_to_colour(rr, gr, br)\r
87 end\r
88 \r
89 function get_timezone_offset()\r
90         -- returns the number of seconds of timezone offset\r
91         local tz = tonumber(os.date('%z'))\r
92         local tzh = math.floor(tz / 100 + 0.5)\r
93         local tzm = math.abs(tz) % 100 / 60.\r
94         if tzh < 0 then tzm = -tzm end\r
95         return (tzh + tzm) * 3600\r
96 end\r
97 \r
98 function julian_to_unix(J)\r
99         -- converts a julian date into unit time\r
100         return (J - 2440588) * 86400\r
101 end\r
102 \r
103 function get_julian_now()\r
104         -- returns the current time in julian date format\r
105         local now = os.time()\r
106         return now / 86400. + 2440588\r
107 end\r
108 \r
109 function calculate_sunrise_sunset(latitude, longitude)\r
110         --[[\r
111         This function returns the unix timestamps in the local time for sunrise and\r
112         sunset times, according to ``latitude'' and ``longitude''.  For the\r
113         latitude, north is positive and south is negative.  For the longitude, west\r
114         is negative, and east is positive.  You can usually determine the lat/long\r
115         for your location from Wikipedia or using some mapping tool.\r
116 \r
117         In my case (Calgary, AB) the lat/long are 51.045 and -114.057222\r
118 \r
119         Reference: http://en.wikipedia.org/wiki/Sunrise_equation\r
120         ]]\r
121 \r
122         -- Negate longitude, west is positive and east is negative\r
123         longitude = -longitude\r
124 \r
125         --  Calculate current Julian Cycle\r
126         local n = math.floor(get_julian_now() - 2451545 - 0.0009 - longitude / 360 + 0.5)\r
127 \r
128         -- Approximate Solar Noon\r
129         local Js = 2451545 + 0.0009 + longitude / 360 + n\r
130 \r
131         -- Solar Mean Anomaly\r
132         local M = (357.5291 + 0.98560028 * (Js - 2451545)) % 360\r
133 \r
134         -- Equation of Center\r
135         local C = (1.9148 * math.deg(math.sin(math.rad(M)))) + (0.0200 * math.deg(math.sin(math.rad(2 * M)))) + (0.0003 * math.deg(math.sin(math.rad(3 * M))))\r
136 \r
137         -- Ecliptic Longitude\r
138         local lam = (M + 102.9372 + C + 180) % 360\r
139 \r
140         -- Solar Transit\r
141         local Jt = Js + (0.0053 * math.deg(math.sin(math.rad(M)))) - (0.0069 * math.deg(math.sin(math.rad(2 * lam))))\r
142 \r
143         -- Declination of the Sun\r
144         local delta = math.deg(math.asin(math.sin(math.rad(lam)) * math.sin(math.rad(23.45))))\r
145 \r
146         -- Hour Angle\r
147         local w = math.deg(math.acos((math.sin(math.rad(-0.83)) - math.sin(math.rad(delta)) * math.sin(math.rad(latitude))) / (math.cos(math.rad(latitude)) * math.cos(math.rad(delta)))))\r
148 \r
149         local J_set = 2451545 + 0.0009 + ((w + longitude)/360 + n + (0.0053 * math.deg(math.sin(math.rad(M)))) - (0.0069 * math.deg(math.sin(math.rad(2 * lam)))))\r
150         local J_rise = Jt - (J_set - Jt)\r
151 \r
152 \r
153         local rising_t, setting_t = julian_to_unix(J_rise), julian_to_unix(J_set)\r
154 \r
155         -- apply timezone offset\r
156         local tz_offset = get_timezone_offset()\r
157         rising_t = rising_t + tz_offset\r
158         setting_t = setting_t + tz_offset\r
159 \r
160         return rising_t, setting_t\r
161 end\r
162 \r
163 local last_sunrise_set_check = 0\r
164 local sunrise, sunset = 0\r
165 \r
166 function conky_datey(latitude, longitude, change)\r
167         --[[\r
168         Returns a colour at or between day_sky and night_sky (see below) depending on the\r
169         time of day.  You must provide the ``latitude'' and ``longitude''\r
170         parameters for your location (see the comments for\r
171         calculate_sunrise_sunset() above for more info).  The ``change'' parameter\r
172         is the number of hours we want to start and have a transition, so a value\r
173         of 1 will mean the transition starts 30 minutes before, and ends 30 minutes\r
174         after.\r
175         ]]\r
176         local function to_hours(t)\r
177                 return tonumber(os.date('%k', t)) + (tonumber(os.date('%M', t)) / 60) + (tonumber(os.date('%S', t)) / 3600)\r
178         end\r
179         if last_sunrise_set_check < os.time() - 86400 then\r
180                 sunrise, sunset = calculate_sunrise_sunset(tonumber(latitude), tonumber(longitude))\r
181                 -- convert unix times into hours\r
182                 sunrise, sunset = to_hours(sunrise), to_hours(sunset)\r
183         end\r
184         local day_sky = 0x6698FF -- colour to use during daytime\r
185         local night_sky = 0x342D7E -- colour to use during nighttime\r
186         local hour = to_hours(os.time())\r
187         if hour > sunrise + change / 2 and hour < sunset - change / 2 then\r
188                 -- midday\r
189                 sky = day_sky\r
190         elseif hour > sunset + change / 2 or hour < sunrise - change / 2 then\r
191                 -- midnight\r
192                 sky = night_sky\r
193         elseif hour > sunset - change / 2 then\r
194                 -- sunset time\r
195                 sky = colour_transition(day_sky, night_sky, (hour - sunset - change / 2) / change)\r
196         elseif hour < sunrise + change / 2 then\r
197                 -- sunrise time\r
198                 sky = colour_transition(night_sky, day_sky, (hour - sunrise - change / 2) / change)\r
199         end\r
200         return string.format('${color #%6x}', sky)\r
201 end\r
202 \r
203 require 'imlib2'\r
204 \r