-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.lua
215 lines (177 loc) · 5.03 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
local spawn = awful.spawn
local async = awful.spawn.easy_async
function out(s, t)
naughty.notify({ text=s, timeout=t })
end
function rout(s, t)
naughty.notify({ text=s, timeout=t, replaces_id=0 })
end
function urgent(s1, s2)
naughty.notify({ title=s1, text=s2, bg="#ff0000", fg="#000000"})
end
function program_exists(cmd)
-- Returns: (integer) The forked PID.
-- Or (string) Error message.
return type(spawn(cmd) == "number")
end
-- Spawn program and show output in notification
function spawn_out(cmd, timeout)
async(cmd, function(output)
rout(output, timeout)
end)
end
-- Simplify calls to external programs in places where a function is expected
function lspawn(s)
return function() awful.spawn(s) end
end
function t(s)
return terminal.." -e "..s
end
function mkprompt(p, f, c)
return function ()
awful.prompt.run({
prompt = p,
textbox = awful.screen.focused().promptbox.widget,
exe_callback = f,
history_path = c
})
end
end
function round(what, precision)
return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
end
-- Show notification with layout information
function layoutinfo()
rout( "Masters: " .. awful.tag.getnmaster() ..
"\nColumns: " .. awful.tag.getncol() ..
"\nFactor: " .. awful.tag.getmwfact(), 2)
end
-- Move windows to adjacent desktops
function shift_to_tag(n)
return function(c)
local screen = awful.screen.focused()
local current_id = awful.tag.getidx(screen.selected_tag);
local next_id = ((current_id + n - 1) % NUM_TAGS) + 1
local next_screen = screen.tags[next_id]
awful.client.movetotag(next_screen)
awful.tag.viewonly(next_screen)
end
end
last_focus = {}
-- Hide/Show all windows matching some property
function toggle_hidden(prop, ...)
local args = {...}
local windows = {}
local all_hidden = false
local focus = false
local id = args[1] -- Use the first name as the identifier for a group
for i, c in ipairs(client.get()) do
for j, value in ipairs(args) do
if c[prop] == value then
table.insert(windows, c)
all_hidden = all_hidden or c.hidden
if c == client.focus then
last_focus[id] = c
focus = true
end
end
end
end
if all_hidden then
for i, c in ipairs(windows) do
c.hidden = false
c:raise()
end
if last_focus[id] and last_focus[id].valid then
client.focus = last_focus[id]
end
elseif next(windows) then
if focus then
for i, c in ipairs(windows) do
c.hidden = true
end
else
if not last_focus[id] or not last_focus[id].valid then
last_focus[id] = windows[1]
end
client.focus = last_focus[id]
last_focus[id]:raise()
end
else
rout("No hidden windows found")
end
end
-- Get the client with some property, like instance of value conky
function getclient(prop, val)
for i, c in ipairs(client.get()) do
if c[prop] == val then
return c
end
end
end
-- Set the factor being always multiple of 5
function incmwfact5(factor)
local val = math.ceil(awful.tag.getmwfact() * 100) -- math.ceil is to correct a weird bug
val = ((val - (val%5)) / 100) + factor -- if getmwfact() seems to return 0.65, that value is different from 0.65
awful.tag.setmwfact(val) -- so 65%5 return 5 instead of 0
end
-- Returns a copy of the table without the entries rejected by the filter function
function filter_table(tbl, filter)
local ret = {}
for _, v in pairs(tbl) do
if not filter(v) then
table.insert(ret, v)
end
end
return ret
end
-- Swap all normal windows between the visible clients in each screen
function swap_screens()
if screen.count() ~= 2 then -- not implemented
return
end
local clients = {}
for s = 1, 2 do
local filter = function(c)
return c.screen.index == s
and c.type == "normal"
and not c.hidden
and not c.sticky
and c:isvisible()
end
clients[s] = {}
for c in awful.client.iterate(filter) do
table.insert(clients[s], c)
end
end
for i, c in ipairs(clients[1]) do
awful.client.movetoscreen(c, 2)
end
for i, c in ipairs(clients[2]) do
awful.client.movetoscreen(c, 1)
end
end
-- Show notification with all the info about the focused window
function client_info()
local v = ""
-- object
local c = client.focus
v = v .. tostring(c)
-- geometry
local cc = c:geometry()
local signx = cc.x >= 0 and "+"
local signy = cc.y >= 0 and "+"
v = v .. " @ " .. cc.width .. 'x' .. cc.height .. signx .. cc.x .. signy .. cc.y .. "\n\n"
local inf = {
"window", "name", "skip_taskbar", "type", "class", "instance", "pid", "role",
"machine", "icon_name", "icon", "screen", "hidden", "minimized", "size_hints_honor",
"border_width", "border_color", "titlebar", "urgent", "content", "focus", "opacity",
"ontop", "above", "below", "fullscreen", "maximized_horizontal", "maximized_vertical",
"transient_for", "group_window", "leader_id", "size_hints", "sticky", "modal", "focusable",
}
for i = 1, #inf do
v = v .. string.format("%2s: %-20s = %s\n", i, inf[i], tostring(c[inf[i]]))
end
naughty.notify{ text = v:sub(1,#v-1), timeout = 0, margin = 10 }
end
-- vim:ts=4:sw=4