File: init.lua

package info (click to toggle)
awesome-extra 2023010601
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,340 kB
  • sloc: cpp: 112; sh: 84; makefile: 25; python: 11
file content (163 lines) | stat: -rw-r--r-- 4,686 bytes parent folder | download | duplicates (3)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
------------------------------------------
-- Author: Andrei "Garoth" Thorp        --
-- Copyright 2009 Andrei "Garoth" Thorp --
------------------------------------------
-- Awful Timer Wrapper With Speed Controls

local pairs = pairs
local gears_timer = require('gears').timer

local timer = {}
local hooks = {
   timer = timer
}

local registry = {}

-- Register a timer just as you would with awful.hooks.timer.register, but
-- with one slight twist: you set your regular speed, your slow
-- speed, and then your function.
-- @param reg_time Regular speed for the widget
-- @param slow_time (Optional) Slow time for the widget
-- @param fn The function that the timer should call
-- @param descr (Optional) The description of this function
function hooks.timer.register(reg_time, slow_time, fn, descr)
  if not slow_time then slow_time = reg_time * 4 end
  registry[fn] = {
    regular=reg_time,
    slow=slow_time,
    speed="regular",
    description=descr or "Undescribed timer",
    timer=gears_timer({ timeout = 600 })
  }
  registry[fn].timer:connect_signal("timeout", fn)
  -- set_speed() will :stop() again and start with the real timeout
  registry[fn].timer:start()
  timer.set_speed(registry[fn].speed, fn)
end

-- Unregister the timer altogether
-- Note: It's possible to pause the timer with timer.stop() instead.
-- @param fn The function you want to unregister.
function hooks.timer.unregister(fn)
  if not registry[fn] then
    return
  end
  if registry[fn].timer.started then
    registry[fn].timer:stop()
  end
  registry[fn] = nil
end

-- Switch timers between "slow" and "regular" speeds.
-- @param speed The speed at which you want the function(s) to run: one of
-- "regular" or "slow"
-- @param fn (Optional) Function that you want to set to some speed. If not
-- not specified, set all timers to the given speed.
function hooks.timer.set_speed(speed, fn)
  if fn then
    local obj = registry[fn]
    obj.speed = speed
    if not registry[fn].timer.started then
      return
    end
    obj.timer:stop()
    local timeout = nil
    if speed == "regular" then
      timeout = obj.regular
    elseif speed == "slow" then
      timeout = obj.slow
    end
    if timeout then
      obj.timer.timeout = timeout
      obj.timer:start()
    end
  else
    for key, value in pairs(registry) do
      timer.set_speed(speed, key)
    end
  end
end

-- Edit a function's speeds.
-- @param reg_time Regular speed for the function
-- @param slow_time Slow speed for the function
-- @param fn Function that you want to alter the speed of
function hooks.timer.set_speeds(reg_time, slow_time, fn)
  registry[fn] = {
    regular=reg_time,
    slow=slow_time,
    speed=registry[fn].speed,
    timer=registry[fn].timer,
  }
  timer.set_speed(registry[fn].speed, fn)
end

-- Returns the speeds for the timer(s).
-- @param fn (Optional) Function that you want to know the data for. If not
-- specified, this returns a table of all registered timers with their data.
-- @return A table with the regular speed, the slow speed, the currently used
-- speed, whether the timer is running or not, and the description.
-- Note: This function returns a copy of the internal registry, so assigning to
-- it doesn't work.
function hooks.timer.get_speeds(fn)
  local copy = {}
  if fn then
    for key, value in pairs(registry[fn]) do
      copy[key] = value
    end
  else
    for key, value in pairs(registry) do
      local subcopy = {}
      for subkey, subvalue in pairs(registry[key]) do
        subcopy[subkey] = subvalue
      end
      copy[key] = subcopy
    end
  end
  return copy
end

-- Pause timer(s)
-- @param fn (Optional) Function to pause the timer for. If none is specified,
-- this pauses all registered timers.
function hooks.timer.stop(fn)
  if fn then
    registry[fn].timer:stop()
  else
    for key, value in pairs(registry) do
      registry[fn].timer:stop()
    end
  end
end

-- Start timer(s)
-- @param fn (Optional) Function to start the timer for. If none is specified,
-- this starts all registered timers.
function hooks.timer.start(fn)
  if fn then
    if not registry[fn].timer.started then
      registry[fn].timer:start()
    end
    timer.set_speed(registry[fn].speed, fn)
  else
    for key, value in pairs(registry) do
      if not registry[fn].timer.started then
        registry[fn].timer:start()
      end
      timer.set_speed(registry[key].speed, fn)
    end
  end
end

-- Checks whether the given function is registered
-- @return boolean true/false of whether the function is registered
function hooks.timer.has(fn)
  if registry[fn] then
    return true
  else
    return false
  end
end
return hooks
-- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et