-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
662 lines (578 loc) · 19.3 KB
/
Copy pathutil.lua
File metadata and controls
662 lines (578 loc) · 19.3 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
local awful = require("awful")
local beautiful = require("beautiful")
local gears = require("gears")
local naughty = require("naughty")
local cairo = require("lgi").cairo
local wibox = require("wibox")
local config = require("config")
local dpi = require("beautiful.xresources").apply_dpi
local config_dir = gears.filesystem.get_configuration_dir()
local theme = dofile(config_dir .. "theme.lua")
local util = {} -- Create the module table
do
local in_error = false
awesome.connect_signal("debug::error", function (err)
-- Make sure we don't go into an endless error loop
if in_error then return end
in_error = true
debug_log("[Error]: " .. tostring(err))
in_error = false
end)
end
function load_util(name)
return dofile(string.format("%s%s.lua", config_dir, name))
end
function load_widget(name)
if table_contains(config.disabled_widgets, name) then
return nil
end
return dofile(string.format("%swidgets/%s.lua", config_dir, name))
end
function debug_log(message)
-- Get home directory
local home = os.getenv("HOME")
-- Create log file path in /tmp
local log_file = home .. "/.cache/awesome/debug.log"
-- Format message with timestamp
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
local formatted_message = string.format("[%s] %s\n", timestamp, tostring(message))
-- Open file in append mode
local file = io.open(log_file, "a")
if file then
file:write(formatted_message)
file:close()
end
end
function take_screenshot()
os.execute('path="' .. screenshot_path .. '$(date +%s).png" && maim -s -u "$path" && xclip -selection clipboard -t image/png "$path"')
end
function table_to_string(tbl, indent)
indent = indent or 0
local string = string.rep(" ", indent) .. "{\n"
for k, v in pairs(tbl) do
local key = type(k) == "number" and "[" .. k .. "]" or k
local value = type(v) == "table" and table_to_string(v, indent + 1) or tostring(v)
string = string .. string.rep(" ", indent + 1) .. key .. " = " .. value .. ",\n"
end
return string .. string.rep(" ", indent) .. "}"
end
function clip_text(text, length)
if not text then return "" end
-- Remove newlines and extra spaces
text = text:gsub("\n", " "):gsub("%s+", " "):gsub("^%s*(.-)%s*$", "%1")
-- Truncate if too long
if #text > length then
return text:sub(1, length) .. "..."
end
return text
end
function is_string_empty(str)
return str == nil or str == "" or string.match(str, "^%s*$") ~= nil
end
function escape_string(str)
if not str then return '""' end
-- Replace backslashes first
str = str:gsub('\\', '\\\\')
-- Replace double quotes
str = str:gsub('"', '\\"')
return '"' .. str .. '"'
end
-- Makes a vertical line widget with the specified width and margin
function create_divider(width, margin)
return wibox.widget {
{
color = beautiful.taglist_shape_border_color,
orientation = "vertical",
forced_width = width,
widget = wibox.widget.separator
},
top = margin,
bottom = margin,
widget = wibox.container.margin
}
end
function create_image_button(args)
-- Default values merged with provided args
args = args or {}
local widget = args.widget
local image_path = args.image_path
local fallback_text = args.fallback_text or "⬡"
local image_size = args.image_size or dpi(24)
local padding = args.padding or dpi(6)
local opacity = args.opacity or 1.0
local opacity_hover = args.opacity_hover or opacity or 1
local bg_color = args.bg_color or theme.bg_normal
local border_color = args.border_color or theme.border_normal
local hover_bg = args.hover_bg or args.bg_color
local hover_border = args.hover_border or args.border_color
local button_size = args.button_size
local shape_radius = args.shape_radius or dpi(6)
local on_click = args.on_click
local on_right_click = args.on_right_click
local on_ctrl_click = args.on_ctrl_click
local on_release = args.on_release
local id = args.id or ""
-- Create the image or fallback text widget
local content_widget
if widget then
content_widget = wibox.widget {
widget,
margins = padding,
widget = wibox.container.margin
}
elseif image_path then
content_widget = wibox.widget {
{
{
image = image_path,
resize = true,
forced_width = image_size,
forced_height = image_size,
opacity = opacity,
widget = wibox.widget.imagebox,
id = 'icon'
},
widget = wibox.container.place,
halign = 'center',
valign = 'center'
},
margins = padding,
widget = wibox.container.margin
}
else
content_widget = wibox.widget {
{
text = fallback_text,
font = font_with_size(math.floor(image_size * 0.83)),
forced_width = image_size,
forced_height = image_size,
align = 'center',
valign = 'center',
widget = wibox.widget.textbox
},
margins = padding,
widget = wibox.container.margin
}
end
-- Create the button container
local button = wibox.widget {
content_widget,
bg = bg_color,
shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, shape_radius)
end,
shape_border_width = dpi(1),
shape_border_color = border_color,
widget = wibox.container.background,
id = args.id
}
-- Add size constraints if specified
if button_size then
button.forced_width = button_size
button.forced_height = button_size
end
-- Hover effects
button:connect_signal("button::focus", function()
-- Change button colors
button.bg = hover_bg
button.shape_border_color = hover_border
-- Change icon opacity
local imagebox = button.widget:get_children_by_id('icon')[1]
if imagebox then imagebox.opacity = opacity_hover end
end)
button:connect_signal("button::unfocus", function()
-- Reset button colors
button.bg = bg_color
button.shape_border_color = border_color
-- Reset icon opacity
local imagebox = button.widget:get_children_by_id('icon')[1]
if imagebox then imagebox.opacity = opacity end
end)
button:connect_signal("mouse::enter", function()
button:emit_signal("button::focus")
end)
button:connect_signal("mouse::leave", function()
button:emit_signal("button::unfocus")
end)
add_hover_cursor(button)
-- Click handlers
local click_handlers = {}
-- Use press/release handlers
if on_click then
button:connect_signal("button::press", function(self, lx, ly, button_id, mods)
if button_id == 1 then
-- Check for ctrl modifier
if on_ctrl_click and mods.Control then
return on_ctrl_click(self, lx, ly)
else
return on_click(self, lx, ly)
end
elseif button_id == 3 and on_right_click then
return on_right_click(self, lx, ly)
end
end)
end
if on_release then
button:connect_signal("button::release", function(self, lx, ly, button_id, mods)
if button_id == 1 and not mods.Control then
return on_release(self, lx, ly)
end
end)
end
-- Add methods to update button properties
function button:update_image(new_image)
local imagebox = content_widget:get_children_by_id('icon')[1]
if imagebox then
imagebox.image = new_image
end
end
function button:update_text(new_text)
if content_widget.widget == wibox.widget.textbox then
content_widget.widget.text = new_text
end
end
function button:update_colors(new_bg, new_border)
button.bg = new_bg or bg_color
button.shape_border_color = new_border or border_color
end
return button
end
function create_labeled_image_button(args)
-- Default values merged with provided args
args = args or {}
local image_path = args.image_path
local fallback_text = args.fallback_text or "⬡"
local image_size = args.image_size or dpi(24)
local label_text = args.label_text or ""
local text_size = args.text_size or math.floor(image_size * 0.75)
local padding = args.padding or dpi(6)
local opacity = args.opacity or 1.0
local opacity_hover = args.opacity_hover or opacity or 1
local bg_color = args.bg_color or theme.bg_normal
local border_color = args.border_color or theme.border_normal
local fg_color = args.fg_color or theme.fg_normal
local hover_bg = args.hover_bg or args.bg_color
local hover_border = args.hover_border or args.border_color
local hover_fg = args.hover_fg or args.fg_color
local button_size = args.button_size
local shape_radius = args.shape_radius or dpi(6)
local on_click = args.on_click
local on_right_click = args.on_right_click
local on_ctrl_click = args.on_ctrl_click
-- Create the image or fallback text widget
local content_widget
if image_path then
content_widget = wibox.widget {
image = image_path,
resize = true,
forced_width = image_size,
forced_height = image_size,
opacity = opacity,
widget = wibox.widget.imagebox,
valign = 'center',
id = 'icon'
}
else
content_widget = wibox.widget {
text = fallback_text,
font = font_with_size(math.floor(image_size * 0.83)),
forced_width = image_size,
forced_height = image_size,
align = 'center',
valign = 'center',
widget = wibox.widget.textbox
}
end
-- Create label textbox widget
local label_textbox = wibox.widget {
text = label_text,
font = font_with_size(text_size),
align = 'left',
valign = 'center',
widget = wibox.widget.textbox,
id = 'label'
}
-- Create label widget container
local label_widget = wibox.widget {
label_textbox,
fg = fg_color,
widget = wibox.container.background
}
-- Create horizontal layout for image and label
local layout = wibox.widget {
{
content_widget,
margins = padding,
widget = wibox.container.margin
},
{
label_widget,
right = padding,
top = -dpi(2),
widget = wibox.container.margin
},
align = 'center',
layout = wibox.layout.fixed.horizontal
}
-- Create the button container
local button = wibox.widget {
layout,
bg = bg_color,
shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, shape_radius)
end,
shape_border_width = dpi(1),
shape_border_color = border_color,
widget = wibox.container.background
}
-- Add size constraints if specified
if button_size then
button.forced_width = button_size
end
-- Hover effects
button:connect_signal("button::focus", function()
button.bg = hover_bg
button.shape_border_color = hover_border
label_widget.fg = hover_fg
if content_widget and content_widget.opacity ~= nil then
content_widget.opacity = opacity_hover
end
end)
button:connect_signal("button::unfocus", function()
button.bg = bg_color
button.shape_border_color = border_color
label_widget.fg = fg_color
if content_widget and content_widget.opacity ~= nil then
content_widget.opacity = opacity
end
end)
button:connect_signal("mouse::enter", function()
button:emit_signal("button::focus")
end)
button:connect_signal("mouse::leave", function()
button:emit_signal("button::unfocus")
end)
add_hover_cursor(button)
-- Click handlers
local click_handlers = {}
if on_click then
table.insert(click_handlers, awful.button({}, 1, on_click))
end
if on_ctrl_click then
table.insert(click_handlers, awful.button({ "Control" }, 1, on_ctrl_click))
end
if on_right_click then
table.insert(click_handlers, awful.button({}, 3, on_right_click))
end
button:buttons(gears.table.join(table.unpack(click_handlers)))
-- Add methods to update button properties using direct references
-- (more reliable than searching by ID through nested widget hierarchy)
button.update_image = function(self, new_image)
if content_widget and content_widget.image ~= nil then
content_widget.image = new_image
end
end
button.update_text = function(self, new_text)
if label_textbox then
label_textbox.text = new_text
end
end
button.update_fallback_text = function(self, new_fallback_text)
if content_widget and content_widget.text ~= nil then
content_widget.text = new_fallback_text
end
end
button.update_colors = function(self, new_bg, new_border)
self.bg = new_bg or bg_color
self.shape_border_color = new_border or border_color
end
return button
end
function add_hover_cursor(widget)
-- Store cursor state for this specific widget
local widget_state = {
old_cursor = nil,
old_wibox = nil
}
widget:connect_signal("mouse::enter", function(w)
if w == widget and mouse.current_wibox then
local wibox = mouse.current_wibox
widget_state.old_cursor = wibox.cursor
widget_state.old_wibox = wibox
wibox.cursor = "hand2"
end
end)
widget:connect_signal("mouse::leave", function(w)
if w == widget then
if widget_state.old_wibox then
widget_state.old_wibox.cursor = widget_state.old_cursor
widget_state.old_wibox = nil
end
end
end)
end
function math.clamp(val, lower, upper)
if lower > upper then lower, upper = upper, lower end
return math.max(lower, math.min(upper, val))
end
-- {{{ Window management functions
function get_focused_client()
return client.focus
end
function clamp_window_bounds(c)
local geo = c.screen:get_bounding_geometry({honor_padding = true, honor_workarea = true})
local winWidth = c.width
local winHeight = c.height
c.x = math.clamp(c.x, geo.x, geo.width - winWidth + geo.x)
c.y = math.clamp(c.y, geo.y, geo.height - winHeight + geo.y)
end
-- Focuses the window currently under the mouse
-- Called when the window layout changes (window spawned/killed, tag changed, etc.)
function set_focus_to_mouse()
if not (focusing or switcher_open) then
local focus_timer = timer({ timeout = 0.1 })
focus_timer:connect_signal("timeout", function()
local c = awful.mouse.client_under_pointer()
if not (c == nil) then
client.focus = c
c:raise()
end
focus_timer:stop()
end)
focus_timer:start()
end
end
-- }}}
function execute_keybind(key, mod)
for _, binding in ipairs(root.keys()) do
if awful.key.match(binding, mod, key) then
-- Get the Lua wrapper object via the private reference
local lua_key = binding._private._legacy_convert_to
if lua_key and lua_key.trigger then
lua_key:trigger()
return
end
end
end
end
function jump_to_client(client)
local current_pos = mouse.coords()
client:jump_to()
mouse.coords{x = current_pos.x, y = current_pos.y}
end
function table_contains(table, value)
for i = 1, #table do
if (table[i] == value) then
return true
end
end
return false
end
function table_remove(table, value)
for i,v in pairs(table) do
if v == value then
table.remove(table,i)
break
end
end
end
local function serialize_value(v)
local t = type(v)
if t == "string" then
return string.format("%q", v)
elseif t == "number" then
return tostring(v)
elseif t == "boolean" then
return tostring(v)
elseif t == "table" then
-- Simple table serialization for arrays
local result = "{"
local first = true
for _, item in ipairs(v) do
if not first then
result = result .. ","
end
result = result .. "\n " .. serialize_value(item)
first = false
end
if not first then
result = result .. "\n "
end
result = result .. "}"
return result
else
return "nil"
end
end
-- Generic function to update a config value in the config file
local function update_config_file(key, value)
local config_path = gears.filesystem.get_configuration_dir() .. "config.lua"
-- Read the current config file
local file = io.open(config_path, "r")
if not file then
naughty.notify({
preset = naughty.config.presets.critical,
title = "Config Error",
text = "Could not open config.lua for reading"
})
return false
end
local lines = {}
for line in file:lines() do
table.insert(lines, line)
end
file:close()
-- Find and update the specific config line
local pattern = "^(%s*config%." .. key .. "%s*=%s*)"
local updated = false
for i, line in ipairs(lines) do
local prefix = line:match(pattern)
if prefix then
-- Found the line, replace it with the new value
lines[i] = prefix .. serialize_value(value)
updated = true
break
end
end
if not updated then
-- If the key doesn't exist, add it before the return statement
for i = #lines, 1, -1 do
if lines[i]:match("^return config") then
table.insert(lines, i, "config." .. key .. " = " .. serialize_value(value))
updated = true
break
end
end
end
if not updated then
naughty.notify({
preset = naughty.config.presets.critical,
title = "Config Error",
text = "Could not find location to update config." .. key
})
return false
end
-- Write the updated config back
file = io.open(config_path, "w")
if not file then
naughty.notify({
preset = naughty.config.presets.critical,
title = "Config Error",
text = "Could not open config.lua for writing"
})
return false
end
for _, line in ipairs(lines) do
file:write(line .. "\n")
end
file:close()
return true
end
-- Helper function to save a config value both in memory and to file
function save_config(key, value)
-- Update in memory
config[key] = value
-- Update in file
update_config_file(key, value)
end
return util