User Tools

Site Tools


useful_programs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
useful_programs [2020/01/10 09:33] – [Universal Timer (TON / TOFF)] emozolyakuseful_programs [2020/01/13 10:17] – [Debug printing] emozolyak
Line 140: Line 140:
  
 <code lua> <code lua>
-function DBG( ...) -- ... accepts multiple arguments in a table arg +function DBG(...) 
 +local tc = tabToStr
  
-local table_sign = '|' -- if there is divider sign, the values will be grouped in 2 rows +local function align2s(s1, s2) -- appends space to smaller string  
-local table_sign_found false         +    local d, s (#s1 - #s2), ' '  
-local tab_s_index = 0 +    if (d ~= 0) then return (((d > 0) and {s1, s2 .. s:rep(d)}) or ({s1 .. s:rep(math.abs(d)), s2})) 
 +    else  
 +        return {s1, s2} 
 +    end  
 +end 
  
-if ENABLE_DEBUG then -- should be global in the calling script  +    if ENABLE_DEBUG then  
- +        local arg_str tc(arg) 
-    -- find divider +        local t_s_pos string.find(arg_str, "|"
-    for i 1, #arg do  +
-        if arg[i] == table_sign then  +
-            table_sign_found = true  +
-            tab_s_index = i  +
-            break  +
-        end  +
-    end +
          
-    if not table_sign_found then   +        if not t_s_pos then  
-        INFO(tabToStr(arg)) -- outputs table values in a single row  +            INFO(arg_str -- Just printing single line 
-    else  +
-        local header_t, value_t = {}, {} -- prepare header and value rows +
-          +
-        for k = 1, tab_s_index - 1 do  +
-            header_t[k] = arg[k] +
-        end  +
-        for j = tab_s_index + 1, #arg do  +
-            value_t[j - tab_s_index] = tostring(arg[j]) +
-        end  +
-         +
-        if (#header_t ~= #value_t) then  +
-             ERROR("column count differs!")+
         else          else 
-            for g = 1, #header_t do  +            local h_row, v_row {}, {}  
-                local delta = #header_t[g] - #value_t[g] -- makes aligment by adding spaces to shorter strings... +            -- header  
-                if delta ~= then  +            for w in string.gmatch(arg[1]"%S+"do 
-                    if (delta > 0then  +                h_row[#h_row + 1= w       
-                        value_t[gvalue_t[g] .. string.rep(' 'delta)  +            end  
-                    else  +            -- values  
-                        header_t[g] = header_t[g] .. string.rep(' 'math.abs(delta))  +            for i = 3, #arg do  
-                    end +                v_row[#v_row + 1] = tostring(arg[i]) 
 +            end  
 +             
 +            if (#h_row ~= #v_row) then  
 +                ERROR("Inconsistent header and value rows in DBGnew!"
 +                return  
 +            else  
 +                for g = 1, #h_row do  
 +                    h_row[g], v_row[g] = unpack(align2s(h_row[g], v_row[g]))
                 end                  end 
-            end -- for  +            end  
-            INFO(tabToStr(header_t)) ; INFO(tabToStr(value_t)) +            INFO(tc(h_row)) ; INFO(tc(v_row)) 
-        end +        end    
     end      end 
-end -- if ENABLE_DEBUG +end  
- +
-return true  +
-end -- DBG+
  
 function tabToStr(t)  -- glue all and add spaces, use tostring to protect from nil and bool argument  function tabToStr(t)  -- glue all and add spaces, use tostring to protect from nil and bool argument 
     local s = ""                 local s = ""            
     for i = 1, #t do      for i = 1, #t do 
-        s = s .. tostring(t[i]) .. ' ' +        local e = t[i] ; if (e == nil) then e = 'nil' end  
 +        s = s .. tostring(e) .. ' ' 
     end -- for      end -- for 
     return s      return s 
-end +end
  
 </code> </code>
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki