Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- basic excel like program
- by Big SHiny Toys
- ]]--
- local ver = 0.2
- local function load(fileLocation)
- local file = fs.open(fileLocation,"r")
- local sData
- local outData = {}
- local tWidth = {}
- if file then
- sData = file.readLine()
- while sData do
- local temp = {}
- local cols = 0
- for w in string.gmatch(sData, "(.-)\t") do
- table.insert(temp,w)
- cols = cols + 1
- end
- table.insert(outData,temp)
- table.insert(tWidth,cols)
- sData = file.readLine()
- end
- file.close()
- end
- local tSpacing = {}
- for i = 1,math.max(unpack(tWidth)) do
- table.insert(tSpacing,8)
- end
- return outData,tSpacing
- end
- local function drawGrid(grid)
- local sBlank = ""
- for k,v in pairs(grid.colWidth) do
- sBlank = sBlank..string.rep(" ",v-1).."|"
- end
- term.setCursorPos(grid.posX,grid.posY)
- term.setBackgroundColor(colors.gray)
- for k,v in pairs(grid.data[1]) do
- term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
- end
- term.setBackgroundColor(colors.black)
- for i = grid.posY + 1,grid.hight+grid.posY - 1 do
- term.setCursorPos(grid.posX,i)
- local sel = i - grid.posY + 1 + grid.selOffY
- if sel ~= 1 and grid.data[sel] then
- for k,v in pairs(grid.data[sel]) do
- term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
- end
- else
- term.write(sBlank)
- end
- end
- end
- local function manipulateGrid(grid)
- local lastEvent = nil
- local lastBlock = nil
- local list = {}
- while true do
- term.clear()
- drawGrid(grid)
- local event = {os.pullEvent()}
- if event[1] == "mouse_click" then
- if event[2] == 1 then -- left button
- local row = event[4] - grid.posY + grid.selOffY + 1
- --if event[4] == grid.posY - grid.selOffY then -- X line
- local total = grid.posX - 1
- for i = 1,#grid.colWidth do
- total = total + grid.colWidth[i]
- if event[3] == total then
- lastBlock = i
- end
- end
- --elseif grid.data[row] then
- --end
- end
- elseif event[1] == "mouse_drag" then
- if event[2] == 1 then -- left button
- if lastBlock then -- X line
- --if event[4] == grid.posY - grid.selOffY and lastBlock then -- X line
- local nTest = event[3] - lastEvent[3]
- if nTest == 1 or nTest == -1 then
- grid.colWidth[lastBlock] = grid.colWidth[lastBlock] + nTest
- else
- lastBlock = nil
- end
- if lastBlock and grid.colWidth[lastBlock] < 1 then
- grid.colWidth[lastBlock] = 1
- lastBlock = nil
- end
- else
- lastBlock = nil
- end
- end
- elseif event[1] == "mouse_scroll" then
- grid.selOffY = grid.selOffY + event[2]
- elseif event[1] == "key" then
- if event[2] == 14 then
- return
- elseif event[2] == 200 then -- up
- grid.selOffY = grid.selOffY - 1
- elseif event[2] == 208 then -- down
- grid.selOffY = grid.selOffY + 1
- elseif event[2] == 203 then -- left
- grid.posX = grid.posX + 1
- elseif event[2] == 205 then -- right
- grid.posX = grid.posX - 1
- elseif event[2] == 28 then -- enter
- return
- end
- elseif event[1] == "peripheral" or event[1] == "peripheral_detach" then
- end
- if #grid.data > grid.hight then
- if grid.selOffY < 0 then
- grid.selOffY = 0
- elseif grid.selOffY > #grid.data - grid.hight + 1 then
- grid.selOffY = #grid.data - grid.hight + 1
- end
- else
- grid.selOffY = 0
- end
- lastEvent = event
- end
- end
- local testGrid = {
- posX = 1,
- posY = 1,
- width = 51,
- hight = 19,
- selOffX = 0,
- selOffY = 0,
- colWidth = {},
- data = {}
- }
- print("Basic grid manipulation Ver : "..tostring(ver))
- print("File to Open ?")
- write("location : ")
- local fileLocation = read()
- if fs.exists(fileLocation) then
- testGrid.data,testGrid.colWidth = load(fileLocation)
- manipulateGrid(testGrid)
- else
- error("File not found!")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement