]> git.madduck.net Git - etc/awesome.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

#122 merged
authorcopycat-killer <dada@archlinux.info>
Wed, 5 Aug 2015 10:28:54 +0000 (12:28 +0200)
committercopycat-killer <dada@archlinux.info>
Wed, 5 Aug 2015 11:31:16 +0000 (13:31 +0200)
asyncshell.lua
widgets/abase.lua
widgets/contrib/moc.lua
widgets/imap.lua
widgets/mpd.lua
widgets/weather.lua

index 0aafa17e7df9ffb2194c4ee76baa25575af65774..827cf4be4b0f500174ba2279429039d5f39516aa 100644 (file)
@@ -2,78 +2,71 @@
 --[[
                                                   
      Licensed under GNU General Public License v2 
+      * (c) 2015, worron                          
       * (c) 2013, Alexander Yakushev              
                                                   
 --]]
 
 -- Asynchronous io.popen for Awesome WM.
--- How to use...
--- ...asynchronously:
--- asyncshell.request('wscript -Kiev', function(f) wwidget.text = f:read("*l") end)
--- ...synchronously:
--- widget:set_text(asyncshell.demand('wscript -Kiev', 5):read("*l") or "Error")
+-- How to use:
+-- asyncshell.request('wscript -Kiev', function(output) wwidget.text = output end)
 
-local spawn = require('awful.util').spawn
+-- Grab environment
+local awful = require('awful')
 
-asyncshell               = {}
-asyncshell.request_table = {}
-asyncshell.id_counter    = 0
-asyncshell.folder        = "/tmp/asyncshell"
-asyncshell.file_template = asyncshell.folder .. '/req'
+-- Initialize tables for module
+asyncshell = { request_table = {}, id_counter = 0 }
 
--- Create a directory for asynchell response files
-os.execute("mkdir -p " .. asyncshell.folder)
-
--- Returns next tag - unique identifier of the request
+-- Request counter
 local function next_id()
-   asyncshell.id_counter = (asyncshell.id_counter + 1) % 100000
-   return asyncshell.id_counter
+    asyncshell.id_counter = (asyncshell.id_counter + 1) % 10000
+    return asyncshell.id_counter
+end
+
+-- Remove given request
+function asyncshell.clear(id)
+    if asyncshell.request_table[id] then
+        if asyncshell.request_table[id].timer then
+            asyncshell.request_table[id].timer:stop()
+            asyncshell.request_table[id].timer = nil
+        end
+        asyncshell.request_table[id] = nil
+    end
 end
 
--- Sends an asynchronous request for an output of the shell command.
+-- Sends an asynchronous request for an output of the shell command
 -- @param command Command to be executed and taken output from
 -- @param callback Function to be called when the command finishes
--- @return Request ID
-function asyncshell.request(command, callback)
-   local id = next_id()
-   local tmpfname = asyncshell.file_template .. id
-   asyncshell.request_table[id] = { callback = callback }
-   local req =
-      string.format("sh -c '%s > %s; " ..
-                    'echo "asyncshell.deliver(%s)" | ' ..
-                    "awesome-client' 2> /dev/null",
-                    string.gsub(command, "'", "'\\''"), tmpfname, id)
-   spawn(req, false)
-   return id
-end
+-- @param timeout Maximum amount of time to wait for the result (optional)
+function asyncshell.request(command, callback, timeout)
+    local id = next_id()
+    asyncshell.request_table[id] = { callback = callback }
 
--- Calls the remembered callback function on the output of the shell
--- command.
--- @param id Request ID
--- @param output The output file of the shell command to be delievered
-function asyncshell.deliver(id)
-   if asyncshell.request_table[id] and
-      asyncshell.request_table[id].callback then
-      local output = io.open(asyncshell.file_template .. id, 'r')
-      asyncshell.request_table[id].callback(output)
-   end
+    local formatted_command = string.gsub(command, '"','\"')
+
+    local req = string.format(
+        "echo \"asyncshell.deliver(%s, [[\\\"$(%s)\\\"]])\" | awesome-client &",
+        id, formatted_command
+    )
+
+    awful.util.spawn_with_shell(req)
+
+    if timeout then
+        asyncshell.request_table[id].timer = timer({ timeout = timeout })
+        asyncshell.request_table[id].timer:connect_signal("timeout", function() asyncshell.clear(id) end)
+        asyncshell.request_table[id].timer:start()
+    end
 end
 
--- Sends a synchronous request for an output of the command. Waits for
--- the output, but if the given timeout expires returns nil.
--- @param command Command to be executed and taken output from
--- @param timeout Maximum amount of time to wait for the result
--- @return File handler on success, nil otherwise
-function asyncshell.demand(command, timeout)
-   local id = next_id()
-   local tmpfname = asyncshell.file_template .. id
-   local f = io.popen(string.format("(%s > %s; echo asyncshell_done) & " ..
-                                    "(sleep %s; echo asynchell_timeout)",
-                                    command, tmpfname, timeout))
-   local result = f:read("*line")
-   if result == "asyncshell_done" then
-      return io.open(tmpfname)
-   end
+-- Calls the remembered callback function on the output of the shell command
+-- @param id Request ID
+-- @param output Shell command output to be delievered
+function asyncshell.deliver(id, output)
+    local output = string.sub(output, 2, -2)
+    if asyncshell.request_table[id] then
+        asyncshell.request_table[id].callback(output)
+        asyncshell.clear(id)
+    end
 end
 
 return asyncshell
index 1c132cd9decfc211c5a255ea7fb41812fc4ffe7e..98f7818d844fe2b2c7c004b2ed39a9abacd91eb9 100644 (file)
@@ -27,8 +27,7 @@ local function worker(args)
 
     function abase.update()
         async.request(cmd, function(f)
-            output = f:read("*all")
-            f:close()
+            output = f
             widget = abase.widget
             settings()
         end)
index bd25ffbe22335bf07df81dbee720a5f71d6cac03..b818bb6d3bcdeda76fe51acd1e00ca70f98bdf46 100644 (file)
@@ -59,7 +59,7 @@ local function worker(args)
                 total   = "N/A"
             }
 
-            for line in f:lines() do
+            for line in string.gmatch(f, "[^\n]+") do
                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
                     if k == "State" then moc_now.state = v
                     elseif k == "File" then moc_now.file = v
index ec7bd1f40421a7436f0b730ba4db3091d65ec7f5..62b33d711c5f1061d1ed944e37954b13045ac287 100644 (file)
@@ -64,10 +64,7 @@ local function worker(args)
                head_command, server, port, mail, password, request)
 
         async.request(curl, function(f)
-            ws = f:read("*all")
-            f:close()
-
-            _, mailcount = string.gsub(ws, "%d+", "")
+            _, mailcount = string.gsub(f, "%d+", "")
             _ = nil
 
             widget = imap.widget
index d5f07a800c6dd2dd787f51bb794d05019ccfe3d6..5af898b482ff5dd1f97635fb5ab6e50bf004eced 100644 (file)
@@ -67,7 +67,7 @@ local function worker(args)
                 elapsed = "N/A"
             }
 
-            for line in f:lines() do
+            for line in string.gmatch(f, "[^\n]+") do
                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
                     if     k == "state"   then mpd_now.state   = v
                     elseif k == "file"    then mpd_now.file    = v
index 741c9dc17566f004cfaa8d28c914ea676f97a47c..fb37a522e9d2bcc0d868ab723e314416cb647571 100644 (file)
@@ -78,9 +78,7 @@ local function worker(args)
     function weather.forecast_update()
         local cmd = string.format(forecast_call, city_id, units, lang, cnt)
         async.request(cmd, function(f)
-            j = f:read("*all")
-            f:close()
-            weather_now, pos, err = json.decode(j, 1, nil)
+            weather_now, pos, err = json.decode(f, 1, nil)
 
             if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
                 weather.notification_text = ''
@@ -110,9 +108,7 @@ local function worker(args)
     function weather.update()
         local cmd = string.format(current_call, city_id, units, lang)
         async.request(cmd, function(f)
-            j = f:read("*all")
-            f:close()
-            weather_now, pos, err = json.decode(j, 1, nil)
+            weather_now, pos, err = json.decode(f, 1, nil)
 
             if not err and weather_now ~= nil and tonumber(weather_now["cod"]) == 200 then
                 weather.icon_path = icons_path .. weather_now["weather"][1]["icon"] .. ".png"