Advertisement
BigSHinyToys

basic Excel display

Apr 20th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. --[[
  2.         basic excel like program
  3.         by Big SHiny Toys
  4. ]]--
  5.  
  6. local ver = 0.2
  7.  
  8. local function load(fileLocation)
  9.    
  10.     local file = fs.open(fileLocation,"r")
  11.     local sData
  12.     local outData = {}
  13.     local tWidth = {}
  14.    
  15.     if file then
  16.         sData = file.readLine()
  17.        
  18.         while sData do
  19.             local temp = {}
  20.             local cols = 0
  21.             for w in string.gmatch(sData, "(.-)\t") do
  22.                 table.insert(temp,w)
  23.                 cols = cols + 1
  24.             end
  25.             table.insert(outData,temp)
  26.             table.insert(tWidth,cols)
  27.             sData = file.readLine()
  28.         end
  29.         file.close()
  30.        
  31.     end
  32.    
  33.     local tSpacing = {}
  34.     for i = 1,math.max(unpack(tWidth)) do
  35.         table.insert(tSpacing,8)
  36.     end
  37.     return outData,tSpacing
  38. end
  39.  
  40. local function drawGrid(grid)
  41.     local sBlank = ""
  42.     for k,v in pairs(grid.colWidth) do
  43.         sBlank = sBlank..string.rep(" ",v-1).."|"
  44.     end
  45.     term.setCursorPos(grid.posX,grid.posY)
  46.     term.setBackgroundColor(colors.gray)
  47.     for k,v in pairs(grid.data[1]) do
  48.         term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
  49.     end
  50.     term.setBackgroundColor(colors.black)
  51.     for i = grid.posY + 1,grid.hight+grid.posY - 1 do
  52.         term.setCursorPos(grid.posX,i)
  53.         local sel = i - grid.posY + 1 + grid.selOffY
  54.         if sel ~= 1 and grid.data[sel] then
  55.             for k,v in pairs(grid.data[sel]) do
  56.                 term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
  57.             end
  58.         else
  59.             term.write(sBlank)
  60.         end
  61.     end
  62. end
  63.  
  64. local function manipulateGrid(grid)
  65.     local lastEvent = nil
  66.     local lastBlock = nil
  67.     local list = {}
  68.     while true do
  69.         term.clear()
  70.         drawGrid(grid)
  71.         local event = {os.pullEvent()}
  72.         if event[1] == "mouse_click" then
  73.             if event[2] == 1 then -- left button
  74.                 local row = event[4] - grid.posY + grid.selOffY + 1
  75.                 --if event[4] == grid.posY - grid.selOffY then -- X line
  76.                     local total = grid.posX - 1
  77.                     for i = 1,#grid.colWidth do
  78.                         total = total + grid.colWidth[i]
  79.                         if event[3] == total then
  80.                             lastBlock = i
  81.                         end
  82.                     end
  83.                 --elseif grid.data[row] then
  84.                    
  85.                 --end
  86.             end
  87.         elseif event[1] == "mouse_drag" then
  88.             if event[2] == 1 then -- left button
  89.                 if lastBlock then -- X line
  90.                 --if event[4] == grid.posY - grid.selOffY and lastBlock then -- X line
  91.                     local nTest = event[3] - lastEvent[3]
  92.                     if nTest == 1 or nTest == -1 then
  93.                         grid.colWidth[lastBlock] = grid.colWidth[lastBlock] + nTest
  94.                     else
  95.                         lastBlock = nil
  96.                     end
  97.                     if lastBlock and grid.colWidth[lastBlock] < 1 then
  98.                         grid.colWidth[lastBlock] = 1
  99.                         lastBlock = nil
  100.                     end
  101.                 else
  102.                     lastBlock = nil
  103.                 end
  104.             end
  105.         elseif event[1] == "mouse_scroll" then
  106.             grid.selOffY = grid.selOffY + event[2]
  107.         elseif event[1] == "key" then
  108.             if event[2] == 14 then
  109.                 return
  110.             elseif event[2] == 200 then -- up
  111.                 grid.selOffY = grid.selOffY - 1
  112.             elseif event[2] == 208 then -- down
  113.                 grid.selOffY = grid.selOffY + 1
  114.             elseif event[2] == 203 then -- left
  115.                 grid.posX = grid.posX + 1
  116.             elseif event[2] == 205 then -- right
  117.                 grid.posX = grid.posX - 1
  118.             elseif event[2] == 28 then -- enter
  119.                 return
  120.             end
  121.         elseif event[1] == "peripheral" or event[1] == "peripheral_detach" then
  122.            
  123.         end
  124.         if #grid.data > grid.hight then
  125.             if grid.selOffY < 0 then
  126.                 grid.selOffY = 0
  127.             elseif grid.selOffY > #grid.data - grid.hight + 1 then
  128.                 grid.selOffY = #grid.data - grid.hight + 1
  129.             end
  130.         else
  131.             grid.selOffY = 0
  132.         end
  133.         lastEvent = event
  134.     end
  135. end
  136.  
  137. local testGrid = {
  138.     posX = 1,
  139.     posY = 1,
  140.     width = 51,
  141.     hight = 19,
  142.     selOffX = 0,
  143.     selOffY = 0,
  144.     colWidth = {},
  145.     data = {}
  146. }
  147.  
  148. print("Basic grid manipulation Ver : "..tostring(ver))
  149. print("File to Open ?")
  150. write("location : ")
  151. local fileLocation = read()
  152. if fs.exists(fileLocation) then
  153.     testGrid.data,testGrid.colWidth = load(fileLocation)
  154.     manipulateGrid(testGrid)
  155. else
  156.     error("File not found!")
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement