-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.lua
198 lines (168 loc) · 4.86 KB
/
helpers.lua
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
local awful = require("awful")
local gears = require("gears")
local beautiful = require("beautiful")
local xresources = require("beautiful.xresources")
local dpi = xresources.apply_dpi
local wibox = require("wibox")
local helpers = {}
function helpers.contains(_table, _c)
for _, c in ipairs(_table) do
if _c == c then
return true
end
end
return false
end
function helpers.find(rule)
local function matcher(c)
return awful.rules.match(c, rule)
end
local clients = client.get()
local findex = gears.table.hasitem(clients, client.focus) or 1
local start = gears.math.cycle(#clients, findex + 1)
local matches = {}
for c in awful.client.iterate(matcher, start) do
matches[#matches + 1] = c
end
return matches
end
-- Create rounded rectangle shape (in one line)
helpers.rrect = function(radius)
return function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, radius)
end
end
helpers.squircle = function(rate, delta)
return function(cr, width, height)
gears.shape.squircle(cr, width, height, rate, delta)
end
end
helpers.psquircle = function(rate, delta, tl, tr, br, bl)
return function(cr, width, height)
gears.shape.partial_squircle(cr, width, height, tl, tr, br, bl, rate, delta)
end
end
-- Create pi
helpers.pie = function(width, height, start_angle, end_angle, radius)
return function(cr)
gears.shape.pie(cr, width, height, start_angle, end_angle, radius)
end
end
-- Create parallelogram
helpers.prgram = function(height, base)
return function(cr, width)
gears.shape.parallelogram(cr, width, height, base)
end
end
-- Create partially rounded rect
helpers.prrect = function(radius, tl, tr, br, bl)
return function(cr, width, height)
gears.shape.partially_rounded_rect(cr, width, height, tl, tr, br, bl, radius)
end
end
-- Create rounded bar
helpers.rbar = function(width, height)
return function(cr)
gears.shape.rounded_bar(cr, width, height)
end
end
-- Markup helper
function helpers.colorize_text(txt, fg)
return "<span foreground='" .. fg .. "'>" .. txt .. "</span>"
end
function helpers.client_menu_toggle()
local instance = nil
return function()
if instance and instance.wibox.visible then
instance:hide()
instance = nil
else
instance = awful.menu.clients({ theme = { width = dpi(250) } })
end
end
end
-- Escapes a string so that it can be displayed inside pango markup
-- tags. Modified from:
-- https://github.com/kernelsauce/turbo/blob/master/turbo/escape.lua
function helpers.pango_escape(s)
return (string.gsub(s, "[&<>]", { ["&"] = "&", ["<"] = "<", [">"] = ">" }))
end
function helpers.vertical_pad(height)
return wibox.widget({
forced_height = height,
layout = wibox.layout.fixed.vertical,
})
end
function helpers.horizontal_pad(width)
return wibox.widget({
forced_width = width,
layout = wibox.layout.fixed.horizontal,
})
end
-- Maximizes client and also respects gaps
function helpers.maximize(c)
c.maximized = not c.maximized
if c.maximized then
awful.placement.maximize(c, {
honor_padding = true,
honor_workarea = true,
margins = beautiful.useless_gap * 2,
})
end
c:raise()
end
function helpers.add_hover_cursor(w, hover_cursor)
local original_cursor = "left_ptr"
w:connect_signal("mouse::enter", function()
local w = _G.mouse.current_wibox
if w then
w.cursor = hover_cursor
end
end)
w:connect_signal("mouse::leave", function()
local w = _G.mouse.current_wibox
if w then
w.cursor = original_cursor
end
end)
end
-- Rounds a number to any number of decimals
function helpers.round(number, decimals)
local power = 10 ^ decimals
return math.floor(number * power) / power
end
function helpers.run_or_raise(match, move, spawn_cmd, spawn_args)
local matcher = function(c)
return awful.rules.match(c, match)
end
-- Find and raise
local found = false
for c in awful.client.iterate(matcher) do
found = true
c.minimized = false
if move then
c:move_to_tag(mouse.screen.selected_tag)
client.focus = c
else
c:jump_to()
end
break
end
-- Spawn if not found
if not found then
awful.spawn(spawn_cmd, spawn_args)
end
end
function helpers.scratchpad(match, spawn_cmd, spawn_args)
local cf = client.focus
if cf and awful.rules.match(cf, match) then
cf.minimized = true
else
helpers.run_or_raise(match, true, spawn_cmd, spawn_args)
end
end
function helpers.filter_ampersand(text)
if not text then return end
return text:gsub("&", "and")
end
return helpers