-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdownbars.lua
More file actions
234 lines (208 loc) · 9.02 KB
/
updownbars.lua
File metadata and controls
234 lines (208 loc) · 9.02 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require 'cairo'
require 'cairo_xlib'
-- CONFIG
local COLORS = {
bg = {0.251, 0.251, 0.251, 0.8},
green = {0, 1, 0, 1},
yellow = {1, 1, 0, 1},
red = {1, 0, 0, 1},
white = {1, 1, 1, 1},
bar_bg = {0.431, 0.133, 0.710, 0.3},
}
local BAR_CONFIG = {
{
xb = 60, yb = 220, name = 'upspeedf', arg = 'enp4s0', max = 20000, nb_blocks = 48,
cap = CAIRO_LINE_CAP_SQUARE, w = 9, h = 4, space = 1,
warning = 75, alarm = 90,
led_effect = true, led_alpha = 0.8, rotation = 90
},
{
xb = 60, yb = 238, name = 'downspeedf', arg = 'enp4s0', max = 100000, nb_blocks = 48,
cap = CAIRO_LINE_CAP_SQUARE, w = 9, h = 4, space = 1,
warning = 75, alarm = 90,
led_effect = true, led_alpha = 0.8, rotation = 90
}
}
local BG_STRIPE = {start_x = 10, start_y = 266, pair_height = 30, total_width = 290, max_pairs = 25}
-- Set your preferred connection limits here:
local MAX_IN = 10 -- maximum inbound connections shown (used if you want per-direction limits)
local MAX_TOTAL = 25 -- maximum total connections shown
-- Precompute
for _, params in ipairs(BAR_CONFIG) do
params._log_max = math.log(params.max + 1)
params._angle = (params.rotation or 0) * math.pi / 180
params._cos_angle = math.cos(params._angle)
params._sin_angle = math.sin(params._angle)
end
-- Per-update connection table cache
local conn_cache = {
last_update = -1,
in_table = nil,
in_order = nil,
out_table = nil,
out_order = nil
}
local function get_update_number()
return tonumber(conky_parse("${updates}")) or 0
end
local function get_cached_connections(max_in, max_total)
local upd = get_update_number()
if conn_cache.last_update ~= upd then
conn_cache.in_table, conn_cache.in_order = collect_connections(1, 32767, max_total)
conn_cache.out_table, conn_cache.out_order = collect_connections(32768, 61000, max_total)
conn_cache.last_update = upd
end
return conn_cache.in_table, conn_cache.in_order, conn_cache.out_table, conn_cache.out_order
end
-- NETWORK CONNECTED
local function is_network_connected()
local ip = conky_parse('${addr enp4s0}')
return (ip and ip ~= '' and ip ~= '0.0.0.0')
end
-- DRAW BLOCK: NO CACHE
local function draw_block(cr, x2, y2, w, cos_angle, sin_angle, color, led_effect, led_alpha)
local r, g, b, a = table.unpack(color)
local xx0, xx1 = x2, x2 + w * cos_angle
local yy0, yy1 = y2, y2 + w * sin_angle
local pat = cairo_pattern_create_linear(xx0, yy0, xx0, yy1)
cairo_pattern_add_color_stop_rgba(pat, 0, r, g, b, a * 0.4)
cairo_pattern_add_color_stop_rgba(pat, 0.5, r, g, b, a)
cairo_pattern_add_color_stop_rgba(pat, 1, r, g, b, a * 0.4)
cairo_set_source(cr, pat)
cairo_move_to(cr, xx0, yy0)
cairo_line_to(cr, xx1, yy1)
cairo_stroke(cr)
cairo_pattern_destroy(pat)
if led_effect then
local xc, yc = (xx0 + xx1)/2, (yy0 + yy1)/2
local led_pat = cairo_pattern_create_radial(xc, yc, 0, xc, yc, w/2)
cairo_pattern_add_color_stop_rgba(led_pat, 0, r, g, b, led_alpha)
cairo_pattern_add_color_stop_rgba(led_pat, 1, r, g, b, a)
cairo_set_source(cr, led_pat)
cairo_stroke(cr)
cairo_pattern_destroy(led_pat)
end
end
-- Connection condensation logic: always scan ALL connections for the range, fill up to max_display unique
function collect_connections(start_port, end_port, max_display)
local total_conns = tonumber(conky_parse("${tcp_portmon " .. start_port .. " " .. end_port .. " count}")) or 0
local ip_table = {}
local order = {}
for i = 0, total_conns - 1 do
if #order >= max_display then break end
local rip = conky_parse("${tcp_portmon " .. start_port .. " " .. end_port .. " rip " .. i .. "}")
if not rip or rip == "" then break end
if not ip_table[rip] then
local rservice = conky_parse("${tcp_portmon " .. start_port .. " " .. end_port .. " rservice " .. i .. "}")
local rhost = conky_parse("${tcp_portmon " .. start_port .. " " .. end_port .. " rhost " .. i .. "}")
ip_table[rip] = {count = 1, service = rservice, host = rhost}
table.insert(order, rip)
else
ip_table[rip].count = ip_table[rip].count + 1
end
end
return ip_table, order
end
-- New: Get the displayed connections as a single ordered list (for stripes and display)
local function get_displayed_connections(max_in, max_total)
local in_table, in_order, out_table, out_order = get_cached_connections(max_in, max_total)
local entries = {}
local total = 0
for _, rip in ipairs(in_order) do
if total >= max_total then break end
table.insert(entries, {direction="in", rip=rip, info=in_table[rip]})
total = total + 1
end
for _, rip in ipairs(out_order) do
if total >= max_total then break end
table.insert(entries, {direction="out", rip=rip, info=out_table[rip]})
total = total + 1
end
return entries
end
-- BACKGROUND STRIPES: Shade every other displayed connection row, not just visible slots
local function draw_background_stripes(cr, display_entries)
cairo_set_source_rgba(cr, table.unpack(COLORS.bar_bg))
for idx, _ in ipairs(display_entries) do
if idx % 2 == 0 then -- shade every other row; Lua arrays are 1-based
local y_pos = BG_STRIPE.start_y + ((idx-1) * BG_STRIPE.pair_height)
cairo_rectangle(cr, BG_STRIPE.start_x, y_pos, BG_STRIPE.total_width, BG_STRIPE.pair_height)
cairo_fill(cr)
end
end
end
-- Global for synced display entries
last_display_entries = nil
function conky_limit_connections(max_in, max_total)
max_in = tonumber(max_in) or 0
max_total = tonumber(max_total) or 0
last_display_entries = get_displayed_connections(max_in, max_total) -- Sync for this update!
local out = ""
for _, entry in ipairs(last_display_entries) do
local info = entry.info
local display_ip = info.count > 1 and (entry.rip .. " (" .. info.count .. ")") or entry.rip
if entry.direction == "in" then
out = out
.. "${goto 4}${color6}${voffset -1}${template4}├${color yellow}${template2}In${template4} ←${color2} ${template2}"
.. display_ip .. "${alignr 4}" .. info.service .. "\n"
.. "${voffset -1}${goto 4}${template4}└ ${color3}${template3}" .. info.host .. "\n"
else
out = out
.. "${color6}${voffset -1}${template4}├${color5}${template2}Out${template4} →${color2} ${template2}"
.. display_ip .. "${alignr 4}" .. info.service .. "\n"
.. "${voffset -1}${template4}└ ${color3}${template3}" .. info.host .. "\n"
end
end
return out
end
function conky_draw_pre()
if conky_window == nil then return end
if not is_network_connected() then return end
if not last_display_entries then return end -- Use last set from conky_limit_connections
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)
draw_background_stripes(cr, last_display_entries)
cairo_destroy(cr)
cairo_surface_destroy(cs)
end
function conky_draw_post()
if conky_window == nil then return end
if not is_network_connected() then return end
-- You can use last_display_entries here for consistency, but bars are not affected by the sync issue
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)
for _, params in ipairs(BAR_CONFIG) do
local value = tonumber(conky_parse(string.format('${%s %s}', params.name, params.arg))) or 0
local log_value = (value > 0) and math.log(value + 1) or 0
local pct = 100 * log_value / params._log_max
local pcb = 100 / params.nb_blocks
local y_step = params.h + params.space
cairo_set_line_width(cr, params.h)
cairo_set_line_cap(cr, params.cap)
for pt = 1, params.nb_blocks do
local blockStartPercentage = (pt - 1) * pcb
local color
if pct >= blockStartPercentage then
if blockStartPercentage < params.warning then
color = COLORS.green
elseif blockStartPercentage < params.alarm then
color = COLORS.yellow
else
color = COLORS.red
end
else
color = COLORS.bg
end
local y1 = params.yb - pt * y_step
local radius0 = params.yb - y1
local x2 = params.xb + radius0 * params._sin_angle
local y2 = params.yb - radius0 * params._cos_angle
draw_block(cr, x2, y2, params.w, params._cos_angle, params._sin_angle, color,
params.led_effect, params.led_alpha)
end
end
cairo_destroy(cr)
cairo_surface_destroy(cs)
end