mrnosuch

Lua Minimodule Demo

Jan 4th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. -- minimodule.lua
  2.  
  3. -- set up table to hold "module" bits
  4. local dsign = {}
  5.  
  6.  
  7. dsign.init = function()
  8.  
  9.   -- set up default data structures and values
  10.   -- this could actually just be created on the fly
  11.   -- but having an init function helps to document
  12.  
  13.   dsign.monitors = {}
  14.   dsign.signCount = 0
  15.  
  16.   dsign.signs = {}
  17.  
  18. end
  19.  
  20.  
  21. dsign.addMonitors = function(monitorTable)
  22.    
  23.   -- Add monitors, we can take an "array" of monitor names,
  24.   -- or a single string
  25.   -- these will be added to our "array" of monitors in the order
  26.   -- they are added
  27.  
  28.   -- this exceprt only adds the name, but it's here where you could
  29.   -- generate all info for a monitor (reference, bounds, etc)
  30.  
  31.  
  32.   if type(monitorTable)=="string" then
  33.     local singleMonitor = monitorTable
  34.     monitorTable = { singleMonitor }
  35.   end
  36.  
  37.   for m, monitorName in ipairs(monitorTable) do
  38.    
  39.     local monitorDetails = {}
  40.    
  41.     monitorDetails.name = monitorName
  42.     -- you can add other monitor attributes to the table above
  43.  
  44.     -- store all the attributes in the monitors array
  45.     table.insert(dsign.monitors,monitorDetails)
  46.  
  47.   end
  48.  
  49.  
  50. end
  51.  
  52. dsign.showMonitors = function()
  53.  
  54.   -- this example function shows how to "walk" the monitors
  55.   -- and access their attributes
  56.  
  57.   for i = 1, table.getn(dsign.monitors) do
  58.    
  59.     print(i,dsign.monitors[i].name)
  60.    
  61.   end
  62.  
  63. end
  64.  
  65.  
  66. -- "array" of monitor names
  67. local monitors = { "top", "left", "monitor_0" }
  68.  
  69. -- Here we set up our "module"
  70. dsign.init()
  71.  
  72. -- add the monitors from the array
  73. dsign.addMonitors(monitors)
  74.  
  75. -- show we can also add a string
  76. dsign.addMonitors("oneMore")
  77.  
  78. -- function to demonstrate how to use this info
  79. dsign.showMonitors()
Advertisement
Add Comment
Please, Sign In to add comment