about summary refs log tree commit diff
path: root/.local/bin/rofi-ppd
blob: 5a4f13b6013a5e57d882cce6820f0a5dbc8d66f3 (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
#!/usr/bin/env lua

local function trim(s)
  return s:gsub("^%s+", ""):gsub("%s+$", "")
end

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 = trim(selection or "")
  if selection == "" then return nil end
  return selection
end

local modes = {}

local handle = io.popen('powerprofilesctl list')
local profile_list = handle:read("*a")
handle:close()

local handle = io.popen('powerprofilesctl get')
local current_profile = trim(handle:read("*a") or "")
handle:close()

for line in profile_list:gmatch("[^\r\n]+") do
  line = trim(line)
  local mode = line:match("^%*?%s*([%w%-_]+)%s*:$")
  if mode then
    if mode == current_profile then
      table.insert(modes, "* " .. mode)
    else
      table.insert(modes, "  " .. mode)
    end
  end
end

local selected_profile = rofi('󰓅', modes)
if not selected_profile then os.exit() end

selected_profile = selected_profile:gsub("^%*%s*", ""):gsub("^%s+", "")

io.popen('powerprofilesctl set ' .. selected_profile)