Advertisement
Haron_Prime

quake.lua

Sep 13th, 2015
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.21 KB | None | 0 0
  1. -- Quake like console on top
  2. -- Similar to:
  3. --   http://git.sysphere.org/awesome-configs/tree/scratch/drop.lua
  4.  
  5. -- But uses a different implementation. The main difference is that we
  6. -- are able to detect the Quake console from its name
  7. -- (QuakeConsoleNeedsUniqueName by default).
  8.  
  9. -- Use:
  10.  
  11. -- local quake = require("quake")
  12. -- local quakeconsole = {}
  13. -- for s = 1, screen.count() do
  14. --    quakeconsole[s] = quake({ terminal = config.terminal,
  15. --                  height = 0.3,
  16. --                              screen = s })
  17. -- end
  18.  
  19. -- config.keys.global = awful.util.table.join(
  20. --    config.keys.global,
  21. --    awful.key({ modkey }, "`",
  22. --       function () quakeconsole[mouse.screen]:toggle() end)
  23.  
  24. -- If you have a rule like "awful.client.setslave" for your terminals,
  25. -- ensure you use an exception for
  26. -- QuakeConsoleNeedsUniqueName. Otherwise, you may run into problems
  27. -- with focus.
  28.  
  29. local setmetatable = setmetatable
  30. local string = string
  31. local awful  = require("awful")
  32. local capi   = { mouse = mouse,
  33.          screen = screen,
  34.          client = client,
  35.          timer = timer }
  36.  
  37. -- I use a namespace for my modules...
  38. module("quake")
  39.  
  40. local QuakeConsole = {}
  41.  
  42. -- Display
  43. function QuakeConsole:display()
  44.    -- First, we locate the terminal
  45.    local client = nil
  46.    local i = 0
  47.    for c in awful.client.iterate(function (c)
  48.                   -- c.name may be changed!
  49.                   return c.instance == self.name
  50.                    end,
  51.                    nil, self.screen) do
  52.       i = i + 1
  53.       if i == 1 then
  54.      client = c
  55.       else
  56.      -- Additional matching clients, let's remove the sticky bit
  57.      -- which may persist between awesome restarts. We don't close
  58.      -- them as they may be valuable. They will just turn into a
  59.      -- classic terminal.
  60.      c.sticky = false
  61.      c.ontop = false
  62.      c.above = false
  63.       end
  64.    end
  65.  
  66.    if not client and not self.visible then
  67.       -- The terminal is not here yet but we don't want it yet. Just do nothing.
  68.       return
  69.    end
  70.  
  71.    if not client then
  72.       -- The client does not exist, we spawn it
  73.       awful.util.spawn(self.terminal .. " " .. string.format(self.argname, self.name),
  74.                false, self.screen)
  75.       return
  76.    end
  77.  
  78.    -- Comptute size
  79.    local geom = capi.screen[self.screen].workarea
  80.    local width, height = self.width, self.height
  81.    if width  <= 1 then width = geom.width * width end
  82.    if height <= 1 then height = geom.height * height end
  83.    local x, y
  84.    if     self.horiz == "left"  then x = geom.x
  85.    elseif self.horiz == "right" then x = geom.width + geom.x - width
  86.    else   x = geom.x + (geom.width - width)/2 end
  87.    if     self.vert == "top"    then y = geom.y
  88.    elseif self.vert == "bottom" then y = geom.height + geom.y - height
  89.    else   y = geom.y + (geom.height - height)/2 end
  90.  
  91.    -- Resize
  92.    awful.client.floating.set(client, true)
  93.    client.border_width = 0
  94.    client.size_hints_honor = false
  95.    client:geometry({ x = x, y = y, width = width, height = height })
  96.  
  97.    -- Sticky and on top
  98.    client.ontop = true
  99.    client.above = true
  100.    client.skip_taskbar = true
  101.    client.sticky = true
  102.  
  103.    -- This is not a normal window, don't apply any specific keyboard stuff
  104.    client:buttons({})
  105.    client:keys({})
  106.  
  107.    -- Toggle display
  108.    if self.visible then
  109.       client.hidden = false
  110.       client:raise()
  111.       capi.client.focus = client
  112.    else
  113.       client.hidden = true
  114.    end
  115. end
  116.  
  117. -- Create a console
  118. function QuakeConsole:new(config)
  119.    -- The "console" object is just its configuration.
  120.  
  121.    -- The application to be invoked is:
  122.    --   config.terminal .. " " .. string.format(config.argname, config.name)
  123.    config.terminal = config.terminal or "xterm" -- application to spawn
  124.    config.name     = config.name     or "QuakeConsoleNeedsUniqueName" -- window name
  125.    config.argname  = config.argname  or "-name %s"     -- how to specify window name
  126.  
  127.    -- If width or height <= 1 this is a proportion of the workspace
  128.    config.height   = config.height   or 0.25           -- height
  129.    config.width    = config.width    or 1          -- width
  130.    config.vert     = config.vert     or "top"          -- top, bottom or center
  131.    config.horiz    = config.horiz    or "center"       -- left, right or center
  132.  
  133.    config.screen   = config.screen or capi.mouse.screen
  134.    config.visible  = config.visible or false -- Initially, not visible
  135.  
  136.    local console = setmetatable(config, { __index = QuakeConsole })
  137.    capi.client.connect_signal("manage",
  138.               function(c)
  139.                  if c.instance == console.name and c.screen == console.screen then
  140.                 console:display()
  141.                  end
  142.               end)
  143.    capi.client.connect_signal("unmanage",
  144.               function(c)
  145.                  if c.instance == console.name and c.screen == console.screen then
  146.                 console.visible = false
  147.                  end
  148.               end)
  149.  
  150.    -- "Reattach" currently running QuakeConsole. This is in case awesome is restarted.
  151.    local reattach = capi.timer { timeout = 0 }
  152.    reattach:connect_signal("timeout",
  153.                function()
  154.               reattach:stop()
  155.               console:display()
  156.                end)
  157.    reattach:start()
  158.    return console
  159. end
  160.  
  161. -- Toggle the console
  162. function QuakeConsole:toggle()
  163.    self.visible = not self.visible
  164.    self:display()
  165. end
  166.  
  167. setmetatable(_M, { __call = function(_, ...) return QuakeConsole:new(...) end })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement