about summary refs log tree commit diff
path: root/.local/bin/rofi-powermenu
blob: 0ff3f9c235815618324c09ef488db77963c7fb9b (plain)
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
#!/usr/bin/env lua

local function rofi(prompt, lines)
  local cmd = string.format(
    'printf "%s" | rofi -dmenu -i -p "%s"',
    table.concat(lines, "\n"),
    prompt
  )
  local handle = io.popen(cmd)
  local selection = handle:read("*a")
  handle:close()
  selection = selection:gsub("%s+$", "")
  if selection == "" then return nil end
  return selection
end

local function contains(tbl, val)
  for _, v in pairs(tbl) do
    if v == val then
      return true
    end
  end
  return false
end

-- TODO: Deduplicate This
local options = {
  "Log Out",
  "Shutdown",
  "Restart"
}

local option_pair = {
  ["Log Out"] = "swaymsg exit",
  ["Shutdown"] = "systemctl poweroff",
  ["Restart"] = "systemctl reboot"
}

local choice = rofi("Action:", options)
if (not choice) or (not contains(options,choice)) then os.exit() end

if choice == "Log Out" then
  io.popen(option_pair[choice])
else
  local confirm = rofi("Are you sure?:", {"Yes", "No"})
  if confirm == "Yes" then
    io.popen(option_pair[choice])
  end
end