-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
232 lines (195 loc) · 5.66 KB
/
Copy pathutils.lua
File metadata and controls
232 lines (195 loc) · 5.66 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
-- SPDX-FileCopyrightText: Robert Ryszard Paciorek <rrp@opcode.eu.org>
-- SPDX-License-Identifier: MIT
local time = require "posix.time"
---------------------------------------------
--- Utils functions for Cairo widgets ---
---------------------------------------------
function init_graph(cr, x, y, width, graph_h, text_offset)
cairo_rectangle(cr, x, y, width+1, graph_h+text_offset)
cairo_set_source_rgba(cr, 0.15, 0.15, 0.15, 0.6)
cairo_fill(cr)
cairo_set_source_rgba(cr, 1, 1, 1, 1)
cairo_set_line_width(cr, 1)
cairo_rectangle(cr, x, y, NET.W, graph_h)
cairo_stroke(cr)
return y + graph_h - 1
end
function draw_graph(cr, x, ybase, start_x_offset, graph_h, data, data_ptr, max_val, graph_step, line)
if not graph_step then
graph_step = 1
end
local scale = 1
if max_val > 0 then
scale = graph_h / max_val
end
local min_x_offset = math.max(start_x_offset - #data*graph_step + 1, 1)
for i = start_x_offset, min_x_offset, -graph_step do
local v = data[data_ptr] * scale
if line and v > 0 then
cairo_move_to(cr, x+i, ybase - v + 1)
else
cairo_move_to(cr, x+i, ybase)
end
cairo_line_to(cr, x+i, ybase - v)
data_ptr = data_ptr - 1
if data_ptr < 1 then data_ptr = #data end
end
cairo_stroke(cr)
end
tooltip_visible = nil
tooltip_timer = -1
tooltip_x = 0
tooltip_y = 0
function is_tooltip_visible(id)
if (tooltip_visible ~= id) then
return false
end
local t = get_time()
if (tooltip_timer + TOOLTIP_DELAY > t) then
return false
end
if (tooltip_timer + id.TOOLTIP_TIME < t) then
tooltip_visible = nil
return false
end
return true
end
function tooltip_show(id, x, y)
tooltip_x = x
tooltip_y = y
if (tooltip_visible ~= id) then
tooltip_visible = id
tooltip_timer = get_time()
return true
end
return false
end
function tooltip_draw(cr, str, colors, maxwidth, columns)
local padding_h = 3
local padding_w = 5
local line_height = 16
local line_margin = 4
set_default_font(cr)
if not maxwidth then
maxwidth = 0
for i, s in ipairs(str) do
cairo_text_extents(cr, s, te)
if te.width > maxwidth then maxwidth = te.width end
end
end
local total_w = maxwidth + padding_w * 2
local lines_count = #str
if columns then lines_count = math.ceil(lines_count/columns) end
local total_h = lines_count * line_height + padding_h * 2
local x = math.max(2, tooltip_x - total_w)
local y = math.max(1, tooltip_y - total_h/2)
y = math.min(tooltip_visible.Y + tooltip_visible.H - total_h - 1, y)
-- frame
cairo_set_source_rgba(cr, 1, 1, 1, 0.8)
cairo_set_line_width(cr, 2)
draw_rounded_rect(cr, x, y, total_w, total_h, 5)
cairo_stroke(cr)
-- background
cairo_set_source_rgba(cr, 0, 0, 0, 0.8)
draw_rounded_rect(cr, x, y, total_w, total_h, 5)
cairo_fill(cr)
-- text
cairo_set_source_rgba(cr, 1, 1, 1, 1)
y = y + padding_h - line_margin
for i, s in ipairs(str) do
if colors then
cairo_set_source_rgba(cr, colors[i][1], colors[i][2], colors[i][3], colors[i][4])
end
if not columns or i%columns == 1 then
y = y + line_height
cairo_move_to(cr, x + padding_w, y)
else
cairo_move_to(cr, x + padding_w + ((i-1)%columns) * maxwidth/columns, y)
end
cairo_show_text(cr, s)
end
end
function draw_rounded_rect(cr, x, y, w, h, r)
cairo_move_to(cr, x + r, y)
cairo_line_to(cr, x + w - r, y)
cairo_arc(cr, x + w - r, y + r, r, -math.pi/2, 0)
cairo_line_to(cr, x + w, y + h - r)
cairo_arc(cr, x + w - r, y + h - r, r, 0, math.pi/2)
cairo_line_to(cr, x + r, y + h)
cairo_arc(cr, x + r, y + h - r, r, math.pi/2, math.pi)
cairo_line_to(cr, x, y + r)
cairo_arc(cr, x + r, y + r, r, math.pi, 3*math.pi/2)
end
function cairo_show_text_on_center(cr, x, y, width, text)
cairo_text_extents(cr, text, te)
local align = math.max(0, math.abs(math.floor((width - te.width) / 2)))
cairo_move_to(cr, x + align, y)
cairo_show_text(cr, text)
end
----------------------
--- Mics utils ---
----------------------
SURFIXES = {B=1, K=1024, M=1048576, G=1073741824}
function human_to_number(v)
local s = v.sub(v, -1)
return tonumber(v.sub(v, 1, -2)) * SURFIXES[s]
end
function format_float(f, p)
s = string.sub(string.format("%f", f), 0, p)
if string.sub(s, -1) == "." then s = string.sub(s, 1, -2) end
return s
end
function number_to_human(v, s, p)
if v > 1073741824 then
v = v / 1073741824
return format_float(v, p) .. "G"
elseif v > 1048576 then
v = v / 1048576
return format_float(v, p) .. "M"
elseif v > 1024 then
v = v / 1024
return format_float(v, p) .. "k"
else
return format_float(v, p) .. s
end
end
function min_max_avg(data, data_ptr, data_len)
local min = data[data_ptr]
local max = data[data_ptr]
local avg = 0
for i = 1, data_len do
if data[data_ptr] > max then max = data[data_ptr] end
if data[data_ptr] < min then min = data[data_ptr] end
avg = avg + data[data_ptr]
data_ptr = data_ptr - 1
if data_ptr < 1 then data_ptr = #data end
end
avg = avg / data_len
return min, max, avg
end
function get_time()
local tt = time.clock_gettime(time.CLOCK_REALTIME)
return tt.tv_sec + tt.tv_nsec/1000000000
end
function resize_window(width, height)
win_width = width
win_height = height
os.execute(
"win_width=" .. tostring(win_width) .. "; win_height=" .. tostring(win_height) .. [[;
xdotool getdisplaygeometry | (
read dw dh
w=$(xdotool search --class ConkyBottomPanel)
xdotool windowsize $w $win_width $win_height
xdotool windowmove $w $(($dw - $win_width)) $(($dh - $win_height))
)
]]
)
end
---------------------------------
--- Conky text mode utils ---
---------------------------------
function conky_oncenter(size, ...)
local out = conky_parse(table.concat({...}, " "))
local pad = math.floor((size - out:len()) / 2)
return string.rep(" ", math.max(0, pad)) .. out
end