~kris/dots

srice

ref: 3bdfd3e0c26b75a939850355c30bee6fa9cc9abd srice/.config/conky/clock.lua -rw-r--r-- 3.5 KiB
3bdfd3e0 — Kris Yotam sync: lock down plan9 theme, wallpapers, nvim configs 3 months ago
                                                                                
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require "cairo"

-- Plan 9 style analog clock
-- Cream face, cyan border, simple black hands

local border_width = 8
local border_color = { 0.918, 1.0, 1.0, 1.0 }   -- #EAFFFF
local face_color   = { 1.0, 1.0, 0.918, 1.0 }    -- #FFFFEA (Plan 9 cream)
local hand_color   = { 0.05, 0.05, 0.05, 1.0 }   -- near black
local tick_color   = { 0.3, 0.3, 0.3, 1.0 }       -- dark grey
local second_color = { 0.83, 0.42, 0.42, 1.0 }    -- #D46A6A muted red
local center_color = { 0.3, 0.3, 0.3, 1.0 }

function conky_draw_clock()
    if conky_window == nil then return end

    local cs = cairo_xlib_surface_create(
        conky_window.display,
        conky_window.drawable,
        conky_window.visual,
        conky_window.width,
        conky_window.height
    )
    local cr = cairo_create(cs)

    local w = conky_window.width
    local h = conky_window.height
    local cx = w / 2
    local cy = h / 2
    local radius = math.min(w, h) / 2 - border_width - 8

    -- Border (4 filled rectangles)
    local bw = border_width
    cairo_set_source_rgba(cr, table.unpack(border_color))
    cairo_rectangle(cr, 0, 0, w, bw)
    cairo_rectangle(cr, 0, h - bw, w, bw)
    cairo_rectangle(cr, 0, 0, bw, h)
    cairo_rectangle(cr, w - bw, 0, bw, h)
    cairo_fill(cr)

    -- Clock face
    cairo_set_source_rgba(cr, table.unpack(face_color))
    cairo_arc(cr, cx, cy, radius, 0, 2 * math.pi)
    cairo_fill(cr)

    -- Face outline
    cairo_set_source_rgba(cr, table.unpack(tick_color))
    cairo_set_line_width(cr, 1.5)
    cairo_arc(cr, cx, cy, radius, 0, 2 * math.pi)
    cairo_stroke(cr)

    -- Hour ticks
    for i = 0, 11 do
        local angle = (i * 30 - 90) * math.pi / 180
        local inner = radius * 0.85
        local outer = radius * 0.95
        local tick_w = 2.0
        if i % 3 == 0 then
            inner = radius * 0.78
            tick_w = 3.0
        end
        cairo_set_line_width(cr, tick_w)
        cairo_set_source_rgba(cr, table.unpack(tick_color))
        cairo_move_to(cr, cx + inner * math.cos(angle), cy + inner * math.sin(angle))
        cairo_line_to(cr, cx + outer * math.cos(angle), cy + outer * math.sin(angle))
        cairo_stroke(cr)
    end

    -- Get time
    local hours = tonumber(os.date("%I"))
    local mins  = tonumber(os.date("%M"))
    local secs  = tonumber(os.date("%S"))

    -- Hour hand
    local hour_angle = ((hours + mins / 60) * 30 - 90) * math.pi / 180
    cairo_set_source_rgba(cr, table.unpack(hand_color))
    cairo_set_line_width(cr, 4)
    cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND)
    cairo_move_to(cr, cx, cy)
    cairo_line_to(cr, cx + radius * 0.5 * math.cos(hour_angle), cy + radius * 0.5 * math.sin(hour_angle))
    cairo_stroke(cr)

    -- Minute hand
    local min_angle = ((mins + secs / 60) * 6 - 90) * math.pi / 180
    cairo_set_source_rgba(cr, table.unpack(hand_color))
    cairo_set_line_width(cr, 2.5)
    cairo_move_to(cr, cx, cy)
    cairo_line_to(cr, cx + radius * 0.72 * math.cos(min_angle), cy + radius * 0.72 * math.sin(min_angle))
    cairo_stroke(cr)

    -- Second hand
    local sec_angle = (secs * 6 - 90) * math.pi / 180
    cairo_set_source_rgba(cr, table.unpack(second_color))
    cairo_set_line_width(cr, 1.2)
    cairo_move_to(cr, cx, cy)
    cairo_line_to(cr, cx + radius * 0.78 * math.cos(sec_angle), cy + radius * 0.78 * math.sin(sec_angle))
    cairo_stroke(cr)

    -- Center dot
    cairo_set_source_rgba(cr, table.unpack(center_color))
    cairo_arc(cr, cx, cy, 4, 0, 2 * math.pi)
    cairo_fill(cr)

    cairo_destroy(cr)
    cairo_surface_destroy(cs)
end