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 (86 lines) | stat: -rw-r--r-- 2,074 bytes parent folder | download | duplicates (2)
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
-----------------------------------
-- Author: Uli Schlachter        --
-- Copyright 2009 Uli Schlachter --
-----------------------------------

local widgets_by_type = {
  graph       = require 'obvious.lib.widget.graph',
  progressbar = require 'obvious.lib.widget.progressbar',
  textbox     = require 'obvious.lib.widget.textbox',
}

local setmetatable = setmetatable
local getmetatable = getmetatable
local pairs = pairs
local type = type
local lib = {
  hooks = require("obvious.lib.hooks")
}

local defaults = { }

-- The functions each object from from_data_source will get
local funcs = { }

funcs.set_type = function (obj, widget_type)
  local widget_type = widgets_by_type[widget_type]
  if not widget_type or not widget_type.create then
    return
  end

  local meta = getmetatable(obj)

  local widget = widget_type.create(meta.data)
  obj[1] = widget
  obj.update()
  return obj
end

local function from_data_source(data)
  local ret = { }

  for k, v in pairs(funcs) do
    ret[k] = v
  end

  -- We default to graph since progressbars can't handle sources without an
  -- upper bound on their value
  ret[1] = widgets_by_type.graph.create(data)

  ret.update = function()
    -- because this uses ret, if ret[1] is changed this automatically
    -- picks up the new widget
    ret[1]:update()
  end

  -- Fire up the timer which keeps this widget up-to-date
  lib.hooks.timer.register(10, 60, ret.update)
  lib.hooks.timer.start(ret.update)
  ret.update()

  local meta = { }

  meta.data = data

  -- This is called when an unexesting key is accessed
  meta.__index = function (obj, key)
    local ret = obj[1][key]
    if key ~= "layout" and type(ret) == "function" then
      -- Ugly hack: this function wants to be called on the right object
      return function(_, ...)
        -- Ugly hack: this function wants to be called on the right object
        return ret(obj[1], ...)
      end
    end
    return ret
  end

  setmetatable(ret, meta)
  return ret
end

return {
  from_data_source = from_data_source,
}

-- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et