]> gitweb.fluxo.info Git - awesompd.git/commitdiff
Improve UTF symbol transformation from Jamendo responses
authorAlexander Yakushev <yakushev.alex@gmail.com>
Sat, 22 Oct 2011 23:03:05 +0000 (02:03 +0300)
committerAlexander Yakushev <yakushev.alex@gmail.com>
Sat, 22 Oct 2011 23:03:05 +0000 (02:03 +0300)
Awesompd was working with 1 or 2-byte symbols (up to \u7ff). This commit fixes that, allowing UTF symbols up to 4 bytes.
Also a change was made to be able to replace on search box with other. It is useful when search crashes (it still happens sometimes) and the searchbox is just hanging there.

awesompd.lua
jamendo.lua

index 1a1e0aa2a8998fecccb67affb1a651d9740df11d..f59d77d14dcd137512ce7e08813c2fb9b447b3b9 100644 (file)
@@ -30,6 +30,7 @@ local beautiful = require('beautiful')
 local naughty = naughty
 local awful = awful
 local format = string.format
+local keygrabber = keygrabber
 
 -- Debug stuff
 
@@ -921,8 +922,10 @@ end
 -- Use it like this:
 -- self:display_inputbox("Search music on Jamendo", "Artist", print)
 function awesompd:display_inputbox(title_text, prompt_text, hook)
-   if self.inputbox then -- Inputbox already exists, do nothing
-      return
+   if self.inputbox then -- Inputbox already exists, replace it
+      keygrabber.stop()
+      self.inputbox.screen = nil
+      self.inputbox = nil
    end
    local width = 200
    local height = 30
index 5af6121c17e5d693851d89953056f0debbe5f350..57eccb53da45fb707619c323a482a63cd90450a9 100644 (file)
@@ -307,20 +307,44 @@ end
 -- Jamendo returns Unicode symbols as \uXXXX. Lua does not transform
 -- them into symbols so we need to do it ourselves.
 function utf8_codes_to_symbols (s)
+--   print(utf8_codes_to_symbols, s)
    local hexnums = "[%dabcdefABCDEF]"
-   local pattern = string.format("\\u(%s%s%s%s?)", 
-                                 hexnums, hexnums, hexnums, hexnums)
+   local pattern = string.format("\\u(%s%s%s%s?%s?)", 
+                                 hexnums, hexnums, hexnums, hexnums, hexnums)
    local decode = function(code)
+                     print("Look at me! I parse " .. code)
                      code = tonumber(code, 16)
-                     -- Grab high and low byte
-                     local hi = math.floor(code / 256) * 4 + 192
-                     local lo = math.mod(code, 256)
-                     -- Reduce low byte to 64, add overflow to high
-                     local oflow = math.floor(lo / 64)
-                     hi = hi + oflow
-                     lo = math.mod(code, 64) + 128
-                     -- Return symbol as \hi\lo
-                     return string.char(hi, lo)
+                     if code < 128 then -- one-byte symbol
+                        return string.char(code)
+                     elseif code < 2048 then -- two-byte symbol
+                        -- Grab high and low bytes
+                        local hi = math.floor(code / 64)
+                        local lo = math.mod(code, 64)
+                        -- Return symbol as \hi\lo
+                        return string.char(hi + 192, lo + 128)
+                     elseif code < 65536 then
+                        -- Grab high, middle and low bytes
+                        local hi = math.floor(code / 4096)
+                        local leftover = code - hi * 4096
+                        local mi = math.floor(leftover / 64)
+                        leftover = leftover - mi * 64
+                        local lo = math.mod(leftover, 64)
+                        -- Return symbol as \hi\mi\lo
+                        return string.char(hi + 224, mi + 160, lo + 128)
+                     elseif code < 1114112 then
+                        print("I am actually here")
+                        -- Grab high, highmiddle, lowmiddle and low bytes
+                        local hi = math.floor(code / 262144)
+                        local leftover = code - hi * 262144
+                        local hm = math.floor(leftover / 4096)
+                        leftover = leftover - hm * 4096
+                        local lm = math.floor(leftover / 64)
+                        local lo = math.mod(leftover, 64)
+                        -- Return symbol as \hi\hm\lm\lo
+                        return string.char(hi + 240, hm + 128, lm + 128, lo + 128)
+                     else -- It is not Unicode symbol at all
+                        return tostring(code)
+                     end
                   end
    return string.gsub(s, pattern, decode)
 end