repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

hypr/scripts/common.lua

Raw
File outline
local M = {}

function M.trim(value)
  return (value or ""):gsub("^%s+", ""):gsub("%s+$", "")
end

function M.shell_quote(value)
  value = tostring(value)
  return "'" .. value:gsub("'", "'\\''") .. "'"
end

function M.read_command(command)
  local pipe = io.popen(command .. " 2>/dev/null")
  if not pipe then return "" end
  local output = pipe:read("*a") or ""
  pipe:close()
  return output
end

function M.choose(lines)
  if #lines == 0 then return nil end

  local path = os.tmpname()
  local file = assert(io.open(path, "w"))
  for _, line in ipairs(lines) do
    file:write(line, "\n")
  end
  file:close()

  local selected = M.trim(M.read_command("hyprlauncher --dmenu < " .. M.shell_quote(path)))
  os.remove(path)

  if selected == "" then return nil end
  return selected
end

return M