aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/details.lua
blob: a1063a414abe2fd266ece536986abf12cc97f24a (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- Details: wind, humidity, UV and air quality, over a wind compass.
--
-- Adapted from the Conky-Mirage theme's details tiles by alexsson-xexpanderx
-- (see lib/weather.lua). UV and AQI are the measured quantities here, so they
-- carry the ok / warning / critical roles.

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

local M = {}

local function draw_compass(cr, x, y, w, h, colors, deg, S)
  local r = math.min(w, h) / 2 - S * 0.1
  if r < 24 then return end
  local cx, cy = x + w / 2, y + h / 2
  card.rgba(cr, colors.rule, 0.8)
  cairo_set_line_width(cr, 1)
  cairo_new_path(cr)
  cairo_arc(cr, cx, cy, r, 0, math.pi * 2)
  cairo_stroke(cr)

  local size = math.max(9, r * 0.2)
  card.font(cr, card.FONT_MONO, size, false)
  card.rgba(cr, colors.label)
  for i, p in ipairs({ 'N', 'E', 'S', 'W' }) do
    local a = (i - 1) * math.pi / 2
    card.text_centre(cr, cx + (r - size) * math.sin(a),
      cy - (r - size) * math.cos(a) + size * 0.35, p)
  end

  if type(deg) ~= 'number' then return end
  -- Open-Meteo gives where the wind comes FROM; the needle points where it goes.
  local a = math.rad(deg + 180)
  local tip = r - size * 2
  local tx, ty = cx + tip * math.sin(a), cy - tip * math.cos(a)
  card.rgba(cr, colors.highlight)
  cairo_set_line_width(cr, math.max(2, r * 0.04))
  cairo_move_to(cr, cx - tip * 0.6 * math.sin(a), cy + tip * 0.6 * math.cos(a))
  cairo_line_to(cr, tx, ty)
  cairo_stroke(cr)
  cairo_new_path(cr)
  cairo_arc(cr, tx, ty, math.max(3, r * 0.06), 0, math.pi * 2)
  cairo_fill(cr)
end

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, 'DETAILS', 'no weather data', 'bin/weather-fetch.sh')
    return
  end

  local c = w.current
  -- UV is the forecast for the current hour; Open-Meteo's current block has none.
  local uv = (w.hours[1] or {}).uv
  local aqi = w.air and w.air.aqi
  local wind = '--'
  if type(c.wind) == 'number' then
    wind = weather.num(c.wind) .. ' ' .. weather.wind_unit(w.units)
      .. ' ' .. (weather.compass(c.wind_deg) or '')
  end
  local rows = {
    { 'WIND', wind },
    { 'HUMIDITY', weather.num(c.humidity, '%') },
    { 'UV', type(uv) == 'number' and string.format('%.1f', uv) or '--',
      colors[weather.uv_role(uv)] },
    { 'AIR (EAQI)', weather.num(aqi), colors[weather.aqi_role(aqi)] },
  }
  local y, S = card.standard(cr, inner, colors, {
    icon = card.ICON.details, title = 'DETAILS', rows = rows, share = 0.6,
  })
  draw_compass(cr, inner.x, y, inner.w, inner.y + inner.h - y, colors, c.wind_deg, S)
end

return M