aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/now.lua
blob: f00c46e86397e0963fe7868a1430a0d2c2dadead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- Now: the current conditions. Replaces the OpenWeatherMap weather card; the
-- daylight arc it carried moved to the sky card.
--
-- One of the forecast cards adapted from the Conky-Mirage theme by
-- alexsson-xexpanderx (see lib/weather.lua).

local card = require 'lib.card'
local data = require 'lib.data'
local weather = require 'lib.weather'

local M = {}

function M.draw(cr, rect, colors)
  local inner = card.card(cr, rect, colors)
  local w = weather.parse(data.slurp(weather.CACHE))
  if not w then
    card.notice(cr, inner, colors, 'WEATHER', 'no weather data', 'bin/weather-fetch.sh')
    return
  end

  local c, today, now = w.current, w.days[1] or {}, os.time()
  local rows = {
    { 'FEELS LIKE', weather.deg(c.feels) },
    { 'HIGH / LOW', weather.deg(today.max) .. ' / ' .. weather.deg(today.min) },
    { 'RAIN', weather.num(today.precip, '%') },
    { 'UPDATED', weather.age_str(c.dt, now),
      weather.is_stale(c.dt, now) and colors.warning or nil },
  }
  local y, S = card.standard(cr, inner, colors, {
    icon = weather.icon(c.code, c.is_day), title = 'WEATHER',
    big = weather.deg(c.temp), rows = rows, share = 0.85,
  })

  -- The condition as a large glyph, the card's variable band: the type is
  -- capped to match the other cards, so a tall cell leaves height over, and the
  -- glyph takes it. Dropped when there is no room for one bigger than a row.
  local size = math.min(inner.y + inner.h - y, inner.w * 0.45)
  if size > S * 0.6 then
    card.font(cr, card.FONT_MONO, size, false)
    card.rgba(cr, colors.highlight)
    card.text_centre(cr, inner.x + inner.w / 2, inner.y + inner.h - size * 0.15,
      weather.icon(c.code, c.is_day))
  end
end

return M