Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mode = false --true = link, false = coords
- scroll = 0
- list = {}
- col1 = {}
- col2 = {}
- textbox1 = ""
- textbox2 = ""
- selectedbox = 0
- curScreen = "main"
- function saveListFile()
- file = fs.open("config", "w")
- file.write(textutils.serialize(list))
- file.close()
- end
- function loadListFile()
- if fs.exists("config") then
- file = fs.open("config", "r")
- text = textutils.unserialize(file.readAll())
- if text == nil then
- fs.move("config", "config_backup_"..tostring(os.day()).."_"..tostring(os.time()))
- saveListFile()
- error("Config file corrupted! Backing up and creating new (blank) one.")
- else
- list = text
- end
- file.close()
- else
- saveListFile()
- error("Config file missing! Creating new (blank) one.")
- end
- end
- function needSwap(text1, text2, char)
- byte1 = string.byte(text1, char)
- byte2 = string.byte(text2, char)
- if byte1 == nil then return false end
- if byte2 == nil then return true end
- if byte1 == byte2 then return needSwap(text1, text2, char + 1) end
- return byte1 > byte2
- end
- function sort(type)
- col1 = {}
- col2 = {}
- n = 1
- for key,value in pairs(list) do
- col1[n],col2[n] = value[1],value[2]
- n = n + 1
- end
- n = nil
- list = {}
- for i = 1, #col1 do
- list[i] = {col1[i], col2[i]}
- end
- if type then col1,col2 = col2,col1 end
- done = false
- while not done do
- done = true
- for i = 1, #col1 - 1 do
- if needSwap(string.lower(col1[i]), string.lower(col1[i + 1]), 1) then
- done = false
- col1[i], col1[i + 1] = col1[i + 1], col1[i]
- col2[i], col2[i + 1] = col2[i + 1], col2[i]
- list[i], list[i + 1] = list[i + 1], list[i]
- end
- end
- end
- end
- function drawMain()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorBlink(false)
- for i = 1, 18 do
- term.setCursorPos(1, i)
- if i % 2 == 0 then term.setBackgroundColor(colors.gray)
- else term.setBackgroundColor(colors.lightGray) end
- term.write(" ")
- term.setBackgroundColor(colors.red)
- term.write("Delete")
- end
- term.setCursorPos(1, 19)
- term.setBackgroundColor(colors.green)
- term.write("Toggle sort | Add entry | ")
- term.setCursorPos(27, 19)
- term.write("Line "..tostring(scroll))
- for i = 1, 18 do
- if col1[i + scroll] == nil then
- break
- else
- term.setCursorPos(1, i)
- if i % 2 == 0 then term.setBackgroundColor(colors.gray)
- else term.setBackgroundColor(colors.lightGray) end
- term.write(col1[i + scroll])
- term.setCursorPos(25, i)
- term.write(col2[i + scroll])
- end
- end
- end
- function drawAdd()
- term.setBackgroundColor(colors.lightBlue)
- term.setCursorPos(10,10)
- term.write("Coords: ")
- term.setCursorPos(10,11)
- term.write("Link: ")
- term.setCursorPos(10, 12)
- term.setBackgroundColor(colors.green)
- term.write("Save")
- term.setBackgroundColor(colors.lightBlue)
- term.write(" ")
- term.setBackgroundColor(colors.red)
- term.write("Cancel")
- term.setBackgroundColor(colors.lightBlue)
- term.setCursorPos(18, 10)
- term.write(textbox1)
- term.setCursorPos(18, 11)
- term.write(textbox2)
- if selectedbox == 1 then term.setCursorPos(18 + string.len(textbox1), 10)
- elseif selectedbox == 2 then term.setCursorPos(18 + string.len(textbox2), 11) end
- term.setCursorBlink(selectedbox ~= 0)
- end
- function getEvent()
- event,par,x,y = os.pullEvent()
- if event == "mouse_click" then
- if curScreen == "main" then
- if x > 45 then
- list[y + scroll] = nil
- saveListFile()
- sort(mode)
- drawMain()
- elseif y == 19 and x < 12 then
- mode = not mode
- sort(mode)
- drawMain()
- elseif y == 19 and x > 14 and x < 24 then
- curScreen = "add"
- drawAdd()
- end
- else --screen is add
- if y == 10 then
- selectedbox = 1
- drawAdd()
- elseif y == 11 then
- selectedbox = 2
- drawAdd()
- elseif y == 12 then
- if x > 9 and x < 14 then
- list[#list + 1] = {textbox1, textbox2}
- saveListFile()
- curScreen = "main"
- textbox1 = ""
- textbox2 = ""
- selectedbox = 0
- sort(mode)
- drawMain()
- elseif x > 27 and x < 34 then
- curScreen = "main"
- textbox1 = ""
- textbox2 = ""
- selectedbox = 0
- drawMain()
- end
- end
- end
- elseif event == "mouse_scroll" and curScreen == "main" then
- if par == -1 and scroll > 0 then
- scroll = scroll - 1
- elseif par == 1 and scroll < #col1 - 17 then
- scroll = scroll + 1
- end
- drawMain()
- elseif event == "char" and curScreen == "add" then
- term.setBackgroundColor(colors.lightBlue)
- term.write(par)
- if selectedbox == 1 then textbox1 = textbox1..par
- elseif selectedbox == 2 then textbox2 = textbox2..par end
- elseif event == "key" and par == 14 and curScreen == "add" then
- if selectedbox == 1 then textbox1 = string.sub(textbox1, 1, -2)
- elseif selectedbox == 2 then textbox2 = string.sub(textbox2, 1, -2) end
- drawAdd()
- end --event type
- end --function getEvent
- loadListFile()
- sort(mode)
- drawMain()
- while true do
- getEvent()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement