-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Uli Schlachter wrote:
> Hi list,
> 
> if you don't use obvious stop here.
> 
> This patch series adds the concept of general data sources to obvious. This
> splits the widget-part from the collect-data-about-the-system-part. Any data
> source is a table with two entries:

Whoops, some debugging "code" left in (timer fires each second instead of every
10 seconds).

New stuff attached

- --
"Do you know that books smell like nutmeg or some spice from a foreign land?"
                                                  -- Faber in Fahrenheit 451
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQEcBAEBCAAGBQJKeZztAAoJECLkKOvLj8sGbdkH+gOD5IHVtWyX+Ni1cwkXNzL2
+rEgjFZqNUef58vF0BSV2+URbmUz6DRoVmDM2kRe/TRpklLOt7NMD956Fl0rcn7h
6jppJ46//+25/aEu4OwQ8de516OqWE+yNhbsCdBIhU7wC668QJJZscTh/8VjnXQZ
rhbAfADYisumWcMo04Nc/KCSr9vHQiQRB4Rue8fQX/AddJXP4yq8nNX8bRbISGn+
x4G1C2TDenEwDGb86sklyRjn0UOA1jxur7ww2VJ7hnftSItKviyX8gOyYyvLoHRJ
ijwBcvQs6UPC5uYPVfEwq8ZIQeoM6eIGGo34m8alVcHYmFLseDiboYikIv9p69s=
=N/K+
-----END PGP SIGNATURE-----
>From b0ebfcd37a9dd64d7cd7209d8630dfad9e52bcb6 Mon Sep 17 00:00:00 2001
From: Uli Schlachter <psyc...@znc.in>
Date: Wed, 5 Aug 2009 16:24:13 +0200
Subject: [PATCH 1/3] Add the concept of data sources

obvious.data.* are supposed to be a collection of general data sources. Via
obvious.widget.progressbar() one can turn one of them into a widget and add that
to the statusbar.

For example: obvious.widget.progressbar(obvious.data.wlan("wlan0"))

Signed-off-by: Uli Schlachter <psyc...@znc.in>
---
 data/init.lua                   |   10 ++++++++
 data/wlan.lua                   |   44 +++++++++++++++++++++++++++++++++++++++
 init.lua                        |    3 +-
 lib/init.lua                    |    3 +-
 lib/{widgets.lua => widget.lua} |    2 +-
 lib/wlan.lua                    |   30 --------------------------
 widget/init.lua                 |   28 ++++++++++++++++++++++++
 wlan/init.lua                   |    3 +-
 8 files changed, 88 insertions(+), 35 deletions(-)
 create mode 100644 data/init.lua
 create mode 100644 data/wlan.lua
 rename lib/{widgets.lua => widget.lua} (97%)
 delete mode 100644 lib/wlan.lua
 create mode 100644 widget/init.lua

diff --git a/data/init.lua b/data/init.lua
new file mode 100644
index 0000000..4379583
--- /dev/null
+++ b/data/init.lua
@@ -0,0 +1,10 @@
+-----------------------------------
+-- Author: Uli Schlachter        --
+-- Copyright 2009 Uli Schlachter --
+-----------------------------------
+
+require("obvious.data.wlan")
+
+module("obvious.data")
+
+-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80
diff --git a/data/wlan.lua b/data/wlan.lua
new file mode 100644
index 0000000..d366efa
--- /dev/null
+++ b/data/wlan.lua
@@ -0,0 +1,44 @@
+--------------------------------
+-- Author: Gregor Best        --
+-- Copyright 2009 Gregor Best --
+--------------------------------
+
+local tonumber = tonumber
+local setmetatable = setmetatable
+local io = {
+    open = io.open
+}
+
+module("obvious.data.wlan")
+
+local function get_data(device)
+    local link
+    local fd = io.open("/proc/net/wireless")
+    if not fd then return end
+
+    for line in fd:lines() do
+        if line:match("^ "..device) then
+            link = tonumber(line:match("   (%d?%d?%d)"))
+            break
+        end
+    end
+    fd:close()
+
+    return link
+end
+
+local function get_data_source(device)
+    local device = device or "wlan0"
+    local ret = {}
+
+    ret.device = device
+    ret.max = 100
+    ret.get = function (obj)
+        return get_data(obj.device)
+    end
+
+    return ret
+end
+
+setmetatable(_M, { __call = function (_, ...) return get_data_source(...) end })
+-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80
diff --git a/init.lua b/init.lua
index 2474541..e157b63 100644
--- a/init.lua
+++ b/init.lua
@@ -4,12 +4,13 @@
 -------------------------------------------
 
 require("obvious.lib")
+require("obvious.data")
 require("obvious.clock")
 require("obvious.battery")
 require("obvious.volume_alsa")
 require("obvious.wlan")
-require("obvious.wlan.bar")
 require("obvious.popup_run_prompt")
 require("obvious.basic_mpd")
+require("obvious.widget")
 
 module("obvious")
diff --git a/lib/init.lua b/lib/init.lua
index 31fe374..c7377c6 100644
--- a/lib/init.lua
+++ b/lib/init.lua
@@ -5,9 +5,8 @@
 
 require("obvious.lib.hooks")
 require("obvious.lib.util")
-require("obvious.lib.widgets")
+require("obvious.lib.widget")
 require("obvious.lib.mpd")
-require("obvious.lib.wlan")
 
 module("obvious.lib")
 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80
diff --git a/lib/widgets.lua b/lib/widget.lua
similarity index 97%
rename from lib/widgets.lua
rename to lib/widget.lua
index 40f73c2..9a54404 100644
--- a/lib/widgets.lua
+++ b/lib/widget.lua
@@ -8,7 +8,7 @@ local awful = {
     widget = require("awful.widget")
 }
 
-module("obvious.lib.widgets")
+module("obvious.lib.widget")
 
 function progressbar(layout)
     local theme = beautiful.get()
diff --git a/lib/wlan.lua b/lib/wlan.lua
deleted file mode 100644
index 802c974..0000000
--- a/lib/wlan.lua
+++ /dev/null
@@ -1,30 +0,0 @@
---------------------------------
--- Author: Gregor Best        --
--- Copyright 2009 Gregor Best --
---------------------------------
-
-local tonumber = tonumber
-local setmetatable = setmetatable
-local io = {
-    open = io.open
-}
-
-module("obvious.lib.wlan")
-
-local function get_data(device)
-    local link
-    local fd = io.open("/proc/net/wireless")
-    if not fd then return end
-
-    for line in fd:lines() do
-        if line:match("^ "..device) then
-            link = tonumber(line:match("   (%d?%d?%d)"))
-            break
-        end
-    end
-    fd:close()
-
-    return link
-end
-
-setmetatable(_M, { __call = function (_, ...) return get_data(...) end })
diff --git a/widget/init.lua b/widget/init.lua
new file mode 100644
index 0000000..de8176f
--- /dev/null
+++ b/widget/init.lua
@@ -0,0 +1,28 @@
+-----------------------------------
+-- Author: Uli Schlachter        --
+-- Copyright 2009 Uli Schlachter --
+-----------------------------------
+
+local lib = require("obvious.lib")
+
+module("obvious.widget")
+
+local function update(widget)
+    local max = widget.data.max
+    local val = widget.data:get()
+    widget:set_value(val / max)
+end
+
+function progressbar(data, layout)
+    local widget = lib.widget.progressbar(layout)
+    widget.data = data
+    local up = function()
+        update(widget)
+    end
+    lib.hooks.timer.register(10, 60, up)
+    lib.hooks.timer.start(up)
+    up()
+    return widget
+end
+
+-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80
diff --git a/wlan/init.lua b/wlan/init.lua
index 8dd4420..84cff50 100644
--- a/wlan/init.lua
+++ b/wlan/init.lua
@@ -14,6 +14,7 @@ local capi = {
 
 local awful = require("awful")
 local lib = require("obvious.lib")
+local wlan = require("obvious.data.wlan")
 
 module("obvious.wlan")
 
@@ -25,7 +26,7 @@ widget = capi.widget({
 device = "wlan0"
 
 local function update()
-    local link = lib.wlan(device)
+    local link = wlan(device):get()
 
     local color = "#009000"
     if link < 50 and link > 10 then
-- 
1.6.3.3

>From 5c0a4638215969eeeea298825c80db66e354aa26 Mon Sep 17 00:00:00 2001
From: Uli Schlachter <psyc...@znc.in>
Date: Wed, 5 Aug 2009 16:33:07 +0200
Subject: [PATCH 2/3] Add a graphs

Now one can turn a datasource into a graph via obvious.widget.graph().
Usage is identical to obvious.widget.progressbar().

Signed-off-by: Uli Schlachter <psyc...@znc.in>
---
 lib/widget.lua  |   18 ++++++++++++++++++
 widget/init.lua |   27 +++++++++++++++++++++++++--
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/lib/widget.lua b/lib/widget.lua
index 9a54404..f506726 100644
--- a/lib/widget.lua
+++ b/lib/widget.lua
@@ -28,4 +28,22 @@ function progressbar(layout)
     return widget
 end
 
+function graph(layout, scale)
+    local theme = beautiful.get()
+    local color = theme.progressbar_fg_color or theme.widget_fg_color or theme.fg_normal
+    local back  = theme.progressbar_bg_color or theme.widget_bg_color or theme.bg_normal
+    local border= theme.progressbar_border or theme.widget_border or theme.border_normal
+
+    local widget = awful.widget.graph({ layout = layout })
+    widget:set_color(color)
+    widget:set_border_color(border)
+    widget:set_background_color(back)
+
+    if scale then
+        widget:set_scale(true)
+    end
+
+    return widget
+end
+
 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
diff --git a/widget/init.lua b/widget/init.lua
index de8176f..0917b3c 100644
--- a/widget/init.lua
+++ b/widget/init.lua
@@ -7,7 +7,7 @@ local lib = require("obvious.lib")
 
 module("obvious.widget")
 
-local function update(widget)
+local function update_pbar(widget)
     local max = widget.data.max
     local val = widget.data:get()
     widget:set_value(val / max)
@@ -17,7 +17,30 @@ function progressbar(data, layout)
     local widget = lib.widget.progressbar(layout)
     widget.data = data
     local up = function()
-        update(widget)
+        update_pbar(widget)
+    end
+    lib.hooks.timer.register(10, 60, up)
+    lib.hooks.timer.start(up)
+    up()
+    return widget
+end
+
+local function update_graph(widget)
+    local max = widget.data.max or 1
+    local val = widget.data:get()
+    widget:add_value(val / max)
+end
+
+function graph(data, layout)
+    local scale = true
+    if data.max then
+        scale = false
+    end
+
+    local widget = lib.widget.graph(layout, scale)
+    widget.data = data
+    local up = function()
+        update_graph(widget)
     end
     lib.hooks.timer.register(10, 60, up)
     lib.hooks.timer.start(up)
-- 
1.6.3.3

>From b1b54914e18bea4d866dc261dcddbee53fd32622 Mon Sep 17 00:00:00 2001
From: Uli Schlachter <psyc...@znc.in>
Date: Wed, 5 Aug 2009 16:34:25 +0200
Subject: [PATCH 3/3] Remove obvious.wlan.bar

It was replaced with the more flexible obvious.data.wlan data source.

Signed-off-by: Uli Schlachter <psyc...@znc.in>
---
 wlan/bar.lua |   35 -----------------------------------
 1 files changed, 0 insertions(+), 35 deletions(-)
 delete mode 100644 wlan/bar.lua

diff --git a/wlan/bar.lua b/wlan/bar.lua
deleted file mode 100644
index 71b265c..0000000
--- a/wlan/bar.lua
+++ /dev/null
@@ -1,35 +0,0 @@
------------------------------------
--- Author: Uli Schlachter        --
--- Copyright 2009 Uli Schlachter --
------------------------------------
-
-local setmetatable = setmetatable
-local lib = require("obvious.lib")
-
-module("obvious.wlan.bar")
-
-device = "wlan0"
-widget = false
-
-local function update()
-    local link = lib.wlan(device)
-    widget:set_value(link / 100)
-end
-
-function set_device(dev)
-    device = dev
-    if widget then update() end
-end
-
-local function get(layout)
-    if not widget then
-        -- We must wait until now or beautiful isn't initialized yet
-        widget = lib.widgets.progressbar(layout)
-        update()
-        lib.hooks.timer.register(10, 60, update)
-        lib.hooks.timer.start(update)
-    end
-    return widget
-end
-
-setmetatable(_M, { __call = function (_, ...) return get(...) end })
-- 
1.6.3.3

Reply via email to