summaryrefslogtreecommitdiff
path: root/scroll/scripts/scratchpad.lua
blob: 697284387c52a53562bca14bc4b8beada8b275a9 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
local args, _ = ...
local id = ""
local terminal = false
local command = nil

for i, arg in ipairs(args) do
  if i <= 1 then
    id = arg
  elseif i == 2 then
    if (arg == "kitty") then
      terminal = true
    else
      command = arg
      --terminal = true
    end
  elseif (terminal == true) then
    command = arg
    break
  end
end

--scroll.command(nil, "exec notify-send \"Spawn command\" \"" .. command .. "\"")

local function exists(id)
  local is_in_array = false
  local cons = scroll.scratchpad_get_containers()
  for _, con in ipairs(cons) do
    local views = scroll.container_get_views(con)
    for _, view in ipairs(views) do
      local app_id = scroll.view_get_app_id(view)
      if (app_id == id) then
        is_in_array = true
      end
    end
  end
  return is_in_array
end

local function is_focused(id)
  local view = scroll.focused_view()
  local app_id = scroll.view_get_app_id(view)
  return app_id == id
end

local function spawn(term, comm)
  if (term) then
    if (comm) then
      scroll.command(nil, "exec kitty --class " .. id .. " -e " .. comm)
    else
      scroll.command(nil, "exec kitty --class " .. id)
    end
  else
    scroll.command(nil, "exec " .. comm)
  end
end

if (not exists(id)) then
  spawn(terminal, command)
end
if (is_focused(id)) then
  scroll.command(nil, "scratchpad show")
else
  scroll.command(nil, "[app_id=\"" .. id .. "\"] scratchpad show")
end