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