lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, im writing an lua-app that will have nesting abilities on its output. I was
wondering if anyone could look over this and see if i am doing this correctly or
efficiently. Or give me some helpful pointers?

local g_Nested = 0;

local function IncVar(vGlobal)
   vGlobal = vGlobal or 0
   local d = vGlobal + 1
   return d
end

local function DecVar(vGlobal)
   vGlobal = vGlobal or 0
   local d = vGlobal - 1
   if d < 0 then
      d = 0
   end
   return d
end

local function Nested(x)
   if(x == true) then
      g_Nested = IncVar(g_Nested)
   else
      g_Nested = DecVar(g_Nested)
   end
end

local function Write(...)
   local i, newLevel, indent;
   if(g_Nested < 0) then
      g_Nested = 0
   elseif(g_Nested > 100) then
      g_Nested = 100
   end
   if(g_Nested) then
      indent = g_Nested*3;
      if(indent<0) then
         indent = 0
      end
      
      if(indent > 0) then
         for i=0,indent do
            io.write(" ")
         end
      end
   end
   io.write(string.format(unpack(arg)).."\n")
end

--Testing is below
Write("Test")
Nested(true)
Write("Testing nested")
Write("Second Line")
Nested(true)
Write("Testing sencond nest indentation")
Write("Testing sencond nest indentation")
Nested(false)
Write("Out of Second nested indentation")
Nested(false)
Write("Nolonger nested")