Advertisement
Guest User

taco

a guest
Dec 15th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 47.07 KB | None | 0 0
  1.  
  2. -- usage:
  3. --  taco
  4. --  taco filename
  5.  
  6. local tArgs = {...}
  7. local betaKey = ""
  8. local currentFile = ""
  9. local isSaved = true
  10. local isChanged = false
  11. local fileLinesArr = {""}
  12. local curLine,curCol,lastCol=1,0,0
  13. local menus = {}
  14. local inMenu=false
  15. local selectedMenu = 1
  16. local selectedSubMenu = 1
  17. local running = true
  18. local config = {isFullScreen = false, clipboard=""}
  19. local showFirstTimeMessage = false
  20. local showAboutBox=false
  21. local collectedInputs = {file="My Input Data"}
  22. local collectedInputsCursor = {file=1}
  23. local collectedInputsOffsets = {file=0}
  24. local collectedInputsSize = {file=0}
  25. local w,h = term.getSize()
  26. local scrollOffsetX,scrollOffsetY = 0,0
  27. local isBlinking = false
  28. local state = "edit"
  29. local message = {}
  30. local showingMessage=false
  31. local showingFileDialog = false
  32. local selectedMessageOption=1
  33. local lastBlinkTime = 0
  34. sleep(0.1)
  35. local timer = os.startTimer(0)
  36.  
  37. local function setColors(monitor, bg, fg, bg2, fg2)
  38.      if monitor then
  39.         if monitor.isColor() then
  40.             monitor.setBackgroundColor(bg)
  41.             monitor.setTextColor(fg)
  42.         else
  43.             monitor.setBackgroundColor(bg2)
  44.             monitor.setTextColor(fg2)
  45.         end
  46.     end
  47. end
  48.  
  49. local function checkLinePosition()
  50.         local maxLines = #fileLinesArr
  51.         if curLine < 1 then
  52.                 curLine = 1
  53.         elseif curLine > #fileLinesArr then
  54.                 curLine = maxLines
  55.         end
  56.         local yOffset = 2
  57.         if config.isFullScreen then
  58.             yOffset = 0
  59.         end
  60.         if scrollOffsetY > h-yOffset-curLine then
  61.                 scrollOffsetY=h-yOffset-curLine
  62.         elseif scrollOffsetY < 1-curLine then
  63.                 scrollOffsetY=1-curLine
  64.         end
  65. end
  66. local function checkColPosition()
  67.         if curCol < 0 then
  68.             curCol = 0
  69.         elseif curCol > string.len(fileLinesArr[curLine]) then
  70.             curCol = string.len(fileLinesArr[curLine])
  71.         end
  72.         local total = h-3 --todo: possible bug
  73.         if #fileLinesArr < total then
  74.                 total = #fileLinesArr
  75.         end
  76.         local padWidth = string.len(tostring(total))
  77.         local workingWidth = w - padWidth - 1
  78.        
  79.         if scrollOffsetX >= workingWidth - curCol-1 then
  80.                 scrollOffsetX=workingWidth-curCol-1
  81.         elseif scrollOffsetX < 0-curCol then
  82.                 scrollOffsetX=0-curCol
  83.         end
  84. end
  85. local function checkMenuPositions()
  86.     if selectedMenu < 1 then
  87.         selectedMenu = #menus
  88.         elseif selectedMenu > #menus then
  89.             selectedMenu = 1
  90.     end
  91.     if selectedSubMenu < 1 then
  92.         selectedSubMenu = #menus[selectedMenu].items
  93.     elseif selectedSubMenu > #menus[selectedMenu].items then
  94.         selectedSubMenu = 1
  95.     end
  96. end
  97. local function checkMessageOptionPositions()
  98.     if selectedMessageOption < 1 then
  99.         selectedMessageOption = #message.footer
  100.     elseif selectedMessageOption > #message.footer then
  101.         selectedMessageOption = 1
  102.     end
  103. end
  104. local function loadFile(file)
  105.         if fs.exists(file) and not fs.isDir(file) then
  106.                 local tmpArr = {}
  107.                 local h = fs.open(file, "r")
  108.                 while true do
  109.                         local data = h.readLine()
  110.                         if data == nil then break end
  111.                         table.insert(tmpArr, data)
  112.                 end
  113.                 h.close()
  114.                 return tmpArr
  115.         end
  116.         return null
  117. end
  118. local function openFile(file)
  119.     local data = loadFile(file)
  120.     if data then
  121.         fileLinesArr = {""}
  122.         currentFile = file
  123.         isSaved = false
  124.         isChanged = false
  125.         scrollOffsetX,scrollOffsetY=0,0
  126.         if data then
  127.             fileLinesArr = data
  128.             isSaved = true
  129.         end
  130.         curLine,curCol,lastCol=1,0,0
  131.         checkLinePosition()
  132.         checkColPosition()
  133.         return true
  134.     end
  135.     return false
  136. end
  137. local function saveFile(file, dataArr)
  138.         local tmpArr = {}
  139.         local h = fs.open(file, "w")
  140.         if h then
  141.             for i=1, #dataArr do
  142.                     h.writeLine(dataArr[i])
  143.             end
  144.             h.close()
  145.             isSaved = true
  146.             isChanged = false
  147.             return true
  148.         end
  149.         return false
  150. end
  151. local function loadSettings()
  152.         if fs.exists("taco.db") then
  153.                 local cfg
  154.                 local h = fs.open("taco.db", "r")
  155.                 while true do
  156.                         data = h.readLine()
  157.                         if data == nil then break end
  158.                         if data == "--[[CONFIG:" then
  159.                                 local tmpCfg = h.readAll()
  160.                                 cfg=textutils.unserialize(string.sub(tmpCfg,1,string.len(tmpCfg)-2))
  161.                         end
  162.                 end
  163.                 h.close()
  164.                 if cfg then
  165.                         if cfg.isFullScreen then
  166.                                 config.isFullScreen = cfg.isFullScreen
  167.                         end
  168.                         if cfg.clipboard then
  169.                                 config.clipboard = cfg.clipboard
  170.                         end
  171.                 end
  172.         else
  173.                 showFirstTimeMessage = true
  174.         end
  175. end
  176. local function saveSettings()
  177.         settingsStr=textutils.serialize(config)
  178.         local h = fs.open("taco.db", "w")
  179.         h.writeLine('print("This is a settings file for TACO :)")')
  180.         h.writeLine('--[[CONFIG:')
  181.         h.writeLine(settingsStr)
  182.         h.writeLine(']]')
  183.         h.close()
  184. end
  185. local function newFile()
  186.         currentFile = ""
  187.         fileLinesArr={""}
  188.         curLine,curCol,lastCol=1,0,0
  189.         checkLinePosition()
  190.         checkColPosition()
  191.         isSaved = true
  192.         isChanged = false
  193. end
  194.        
  195. table.insert(menus, {
  196.                                                 name="File",
  197.                                                 hotkey=keys.f,
  198.                                                 items={
  199.                                                                 {"New", "N", "new_file", true},
  200.                                                                 {"Open..", "O", "open_file", true},
  201.                                                                 {"Save", "S", "save_file", false},
  202.                                                                 {"Save As..", "A", "saveas_file", false},
  203.                                                                 {"--"},
  204.                                                                 {"Revert", "R", "revert_file", false},
  205.                                                                 --{"Print..", "P", "print_file", false},
  206.                                                                 --{"Run Script", "E", "run_file", false},
  207.                                                                 {"--"},
  208.                                                                 {"Exit", "X", "exit_app", true}
  209.                                                         }
  210.                                         })
  211. table.insert(menus, {
  212.                                                 name="Edit",
  213.                                                 hotkey=keys.e,
  214.                                                 items={
  215.                                                                 --{"Cut", "X", "cut_selection", false},
  216.                                                                 --{"Copy", "C", "copy_selection", false},
  217.                                                                 --{"Paste", "V", "paste_selection", false},
  218.                                                                 --{"--"},
  219.                                                                 --{"Delete", "D", "clear_selection", false},
  220.                                                                 --{"--"},
  221.                                                                 {"Cut Line", "K", "cut_line", false},
  222.                                                                 {"Copy Line", "J", "copy_line", false},
  223.                                                                 {"Paste Line", "U", "paste_line", false},
  224.                                                                 {"--"},
  225.                                                                 {"Delete Line", "R", "delete_line", false},
  226.                                                                 {"--"},
  227.                                                                 {"Clone Line", "R", "clone_line", false},
  228.                                                         }
  229.                                         })
  230.                                         --[[
  231. table.insert(menus, {
  232.                                                 name="Search",
  233.                                                 hotkey=keys.s,
  234.                                                 items={
  235.                                                                 {"Find..", "F", "find_text", false},
  236.                                                                 {"Replace..", "R", "replace_text", false}
  237.                                                         }
  238.                                         })]]
  239. table.insert(menus, {
  240.                                                 name="Options",
  241.                                                 hotkey=keys.o,
  242.                                                 items={
  243.                                                                 {"Full Screen Mode", "F", "fullscreen_toggle", false},
  244.                                                                 --{"Settings..", "S", "show_settings", false}
  245.                                                         }
  246.                                         })
  247. table.insert(menus, {
  248.                                                 name="Help",
  249.                                                 hotkey=keys.h,
  250.                                                 items={
  251.                                                                 --{"Help", "H", "show_help", true},
  252.                                                                 {"Grab Updates", "U", "perform_update", true},
  253.                                                                 {"--"},
  254.                                                                 {"About TACO", "A", "show_about", true},
  255.                                                         }
  256.                                         })
  257. local function lpad(str, len, char)
  258.         if char == nil then char = ' ' end
  259.         local i = len - #str
  260.         if i >= 0 then
  261.             return string.rep(char, i) .. str
  262.         else
  263.             return str
  264.         end
  265. end
  266. local function rpad(str, len, char)
  267.         if char == nil then char = ' ' end
  268.         local i = len - #str
  269.         if i >= 0 then
  270.             return str .. string.rep(char, i)
  271.         else
  272.             return str
  273.         end
  274. end
  275. local function getMenuOffset(menu)
  276.     local menuOffset = 3
  277.  
  278.     for i = 1, selectedMenu - 1 do
  279.         menuOffset = menuOffset + string.len(menus[i].name) + 2
  280.     end
  281.  
  282.     return menuOffset
  283. end
  284. local function getMenuWidth(menu)
  285.     menuWidth = 1
  286.  
  287.     for i = 1, #menus[selectedMenu].items do
  288.         local len = string.len(menus[selectedMenu].items[i][1]) + 2
  289.  
  290.         if len > menuWidth then
  291.             menuWidth = len
  292.         end
  293.     end
  294.  
  295.     return menuWidth
  296. end
  297. local function getMenuHeight(menu)
  298.     return #menus[selectedMenu].items
  299. end
  300. local function drawScreen(monitor)
  301.         w,h = monitor.getSize()
  302.         monitor.setBackgroundColor(colors.black)
  303.         monitor.clear()
  304.         --header bar
  305.         local guiOffset = 1
  306.         local lineTotalOffset = 2
  307.         if config.isFullScreen then
  308.                 guiOffset = 0
  309.                 lineTotalOffset = 0
  310.         end
  311.         --editor area
  312.         local ii=1
  313.         local total = h-lineTotalOffset-scrollOffsetY+curLine
  314.         if #fileLinesArr < total then
  315.                 total = #fileLinesArr
  316.         end
  317.         local padWidth = string.len(tostring(total))
  318.         for i=1+guiOffset, h-(guiOffset/2) do
  319.                 local currentLineIndex = ii-scrollOffsetY
  320.                 local workingWidth = w - padWidth - 1
  321.                 if 1==2 then
  322.                     workingWidth=workingWidth-1 --scrollbar
  323.                 end
  324.                 monitor.setCursorPos(1,i)
  325.                 setColors(monitor, colors.blue, colors.lightGray, colors.black, colors.white)
  326.                 monitor.write(string.rep(" ", w)) --subtract the two "+"'s
  327.                 if currentLineIndex <= #fileLinesArr then
  328.                         local currentLine = fileLinesArr[currentLineIndex]
  329.                         setColors(monitor, colors.white, colors.red, colors.white, colors.black)
  330.                         monitor.setCursorPos(1,i)
  331.                         monitor.write(lpad(tostring(currentLineIndex), padWidth, " "))
  332.                         if curLine == currentLineIndex then
  333.                                 setColors(monitor, colors.lightBlue, colors.white, colors.black, colors.white)
  334.                         else
  335.                                 setColors(monitor, colors.blue, colors.lightGray, colors.black, colors.white)
  336.                         end
  337.                         monitor.setCursorPos(padWidth+1,ii+guiOffset)
  338.                         currentLineFit = currentLine
  339.                         if currentLineFit then
  340.                                 currentLineFit = string.sub(currentLineFit, -scrollOffsetX+1, string.len(currentLineFit))
  341.                                 if string.len(currentLineFit) > workingWidth then
  342.                                         currentLineFit = string.sub(currentLineFit, 1, workingWidth)
  343.                                 end
  344.                                 monitor.write(" "..rpad(currentLineFit, workingWidth, " ").." ")
  345.                         end
  346.                         if curLine == currentLineIndex and (isBlinking or inMenu) then
  347.                                 monitor.setCursorPos(padWidth+2+curCol+scrollOffsetX,i)
  348.                                 setColors(monitor, colors.white, colors.lightBlue, colors.white, colors.black)
  349.                                 local msg = string.sub(currentLineFit,curCol+scrollOffsetX+1,curCol+scrollOffsetX+1)
  350.                                 if msg == "" then msg = " " end
  351.                                 monitor.write(msg)
  352.                         end
  353.                 end
  354.                 ii=ii+1
  355.         end
  356.        
  357.         if not config.isFullScreen or inMenu then
  358.                 --footer bar
  359.                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  360.                 monitor.setCursorPos(1,h)
  361.                 monitor.write(string.rep(" ", w))
  362.                
  363.                 monitor.setCursorPos(1,h)
  364.                 if not isSaved or isChanged then
  365.                     setColors(monitor, colors.lightGray, colors.red, colors.white, colors.black)
  366.                     monitor.write("*")
  367.                 end
  368.                 if currentFile == "" then
  369.                     setColors(monitor, colors.lightGray, colors.green, colors.white, colors.black)
  370.                     monitor.write("new_file")
  371.                 else
  372.                     monitor.write(currentFile)
  373.                 end
  374.                
  375.                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  376.                 local footerMsg = "Ln "..curLine..":"..#fileLinesArr.." Col "..(curCol+1) --.." XOff="..scrollOffsetX
  377.                 monitor.setCursorPos(w-string.len(footerMsg)+1,h)
  378.                 monitor.write(footerMsg)
  379.        
  380.                 --header bar
  381.                 monitor.setCursorPos(1,1)
  382.                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  383.                 monitor.write(string.rep(" ", w))
  384.                 monitor.setCursorPos(2,1)
  385.                 for i=1, #menus do
  386.                         if inMenu and i == selectedMenu then
  387.                                 setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  388.                         else
  389.                                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  390.                         end
  391.                         monitor.write(" "..menus[i].name.." ")
  392.                 end
  393.                
  394.                 -- Time
  395.                 local time = os.time()
  396.                 local timeFmt = textutils.formatTime(time, false)
  397.                 local timeLen = string.len(timeFmt)
  398.                 monitor.setCursorPos(w-timeLen,1)
  399.                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  400.                 monitor.write(timeFmt)
  401.         end
  402.  
  403.         if inMenu then
  404.                 monitor.setCursorPos(1,1)
  405.                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  406.                 local menuWidth = getMenuWidth(selectedMenu)
  407.                 local menuOffset = getMenuOffset(selectedMenu)
  408.                 for i=1, #menus[selectedMenu].items do
  409.                         if i == selectedSubMenu then
  410.                                 setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  411.                         else
  412.                                 setColors(monitor, colors.lightGray, colors.black, colors.white, colors.black)
  413.                         end
  414.                         posX = menuOffset
  415.                         posY = i + 1
  416.                         monitor.setCursorPos(posX,posY)
  417.                         monitor.write(string.rep(" ", menuWidth))
  418.                         monitor.setCursorPos(posX,posY)
  419.                         local str = menus[selectedMenu].items[i][1]
  420.                         if str == "--" then
  421.                                 if monitor.isColor() then
  422.                                     monitor.setTextColor(colors.gray)
  423.                                 else
  424.                                     monitor.setTextColor(colors.black)
  425.                                 end
  426.                                 monitor.write(string.rep("-", menuWidth))
  427.                         else
  428.                                 monitor.write(" "..str.." ")
  429.                         end
  430.                 end
  431.         end
  432.        
  433. end
  434.  
  435. function updateAllTheThings()
  436.         shell.run("market get gjdh1m taco "..betaKey.." y")
  437. end
  438. local function resetBlink()
  439.         lastBlinkTime = os.clock()+.5
  440.         isBlinking = true
  441. end
  442. local function centerText(monitor, tY, tText)
  443.         local offX = (w+1)/2 - (string.len(tText)-1)/2
  444.         monitor.setCursorPos(offX, tY)
  445.         monitor.write(tText)
  446. end
  447. local function centerTextWidth(monitor, tX, tY, width, tText)
  448.         local offX = (width+1)/2 - (string.len(tText)-1)/2
  449.         monitor.setCursorPos(offX+tX, tY)
  450.         monitor.write(tText)
  451. end
  452. local logo = {
  453. "        1111111111 ";
  454. "   11115e454e5e11 ";
  455. " 11545e5111111111";
  456. "1154e411111111111";
  457. "1e455111111111111";
  458. "15ec5111111111111";
  459. "1cccc1111111111    ";
  460. "1cccc111111            ";
  461. " 111111                    ";
  462. }
  463. --[[Stolen Shamelessly from nPaintPro - http://pastebin.com/4QmTuJGU]]
  464. local function getColourOf(hex)
  465.     local value = tonumber(hex, 16)
  466.     if not value then return nil end
  467.     value = math.pow(2,value)
  468.     return value
  469. end
  470. local function drawPictureTable(mon, image, xinit, yinit, alpha)
  471.     if not alpha then alpha = 1 end
  472.     for y=1,#image do
  473.         for x=1,#image[y] do
  474.             mon.setCursorPos(xinit + x-1, yinit + y-1)
  475.             local col = getColourOf(string.sub(image[y], x, x))
  476.             if not col then col = alpha end
  477.             if term.isColor() then
  478.                 mon.setBackgroundColour(col)
  479.             else
  480.                 local pixel = string.sub(image[y], x, x)
  481.                 if pixel == "c" or pixel == "5" or pixel == " " then
  482.                     mon.setBackgroundColour(colors.white)
  483.                 else
  484.                     mon.setBackgroundColour(colors.black)
  485.                 end
  486.             end
  487.             mon.write(" ")
  488.         end
  489.     end
  490. end
  491. --[[End Theft]]
  492. local function trySaveFileFromDialog()
  493.     currentFile = collectedInputs.file
  494.     if saveFile(currentFile, fileLinesArr) then
  495.         showingFileDialog=false
  496.     else
  497.         messageBox("Error!", {"Couldn't save file!", "Try another name.."}, {{"OK",null}})
  498.     end
  499. end
  500. local function tryOpenFileFromDialog()
  501.     if openFile(collectedInputs.file) then
  502.         showingFileDialog=false
  503.     else
  504.         messageBox("Error!", {"Couldn't open file!"}, {{"OK",null}})
  505.     end
  506. end
  507. local function hideFileDialog()
  508.     showingFileDialog=false
  509. end
  510. local fileDialog={}
  511. fileDialog.title="Open File"
  512. fileDialog.basePath="/"
  513. fileDialog.currentTab=1
  514. fileDialog.currentTabMax=4
  515. fileDialog.dirScrollOffset=0
  516. fileDialog.fileScrollOffset=0
  517. fileDialog.dirSelected=1
  518. fileDialog.fileSelected=1
  519. fileDialog.currentPathFiles={}
  520. fileDialog.currentPathFolders={}
  521. fileDialog.selectedFooterOption=1
  522. fileDialog.footer={}
  523. local function addSlashIfNeeded(path)
  524.     local len = string.len(path)
  525.     if string.sub(path, len, len) ~= "/" then
  526.         return path.."/"
  527.     else
  528.         return path
  529.     end
  530. end
  531. local function addSlashIfNeededLeft(path)
  532.     if string.sub(path, 1,1) ~= "/" then
  533.         return "/"..path
  534.     else
  535.         return path
  536.     end
  537. end
  538. local function getFilePath(file)
  539.         local fName = fs.getName(file)
  540.         local lenBpath = string.len(file)
  541.         local lenFname = string.len(fName)
  542.         return string.sub(file, 1, lenBpath - lenFname-1)
  543. end
  544. local function prepareDialog()
  545.     local tmpList = fs.list(fileDialog.basePath)
  546.     local tmpFileArr,tmpFolderArr={},{}
  547.     fileDialog.basePath = addSlashIfNeeded(fileDialog.basePath)
  548.     if fileDialog.basePath ~= "/" then
  549.             table.insert(tmpFolderArr, {"..", getFilePath(fileDialog.basePath)})
  550.     end
  551.     for key, file in ipairs(tmpList) do
  552.         local len = string.len(fileDialog.basePath)
  553.         file = addSlashIfNeeded(fileDialog.basePath)..file
  554.         if fs.isDir(file) then
  555.             table.insert(tmpFolderArr, {fs.getName(file), file.."/"})
  556.         else
  557.             table.insert(tmpFileArr, {fs.getName(file), file})
  558.         end
  559.     end
  560.     fileDialog.currentPathFiles = tmpFileArr
  561.     fileDialog.currentPathFolders = tmpFolderArr
  562.     fileDialog.dirScrollOffset=0
  563.     fileDialog.fileScrollOffset=0
  564.     checkDialogLimits()
  565. end
  566. function dialogReset()
  567.     fileDialog.currentTab=1
  568.     fileDialog.dirScrollOffset=0
  569.     fileDialog.fileScrollOffset=0
  570.     fileDialog.dirSelected=1
  571.     fileDialog.fileSelected=1
  572. end
  573. local dirFileListHeight = 1
  574. function checkDialogLimits()
  575.     if fileDialog.selectedFooterOption < 1 then
  576.         fileDialog.selectedFooterOption = #fileDialog.footer
  577.     end
  578.     if fileDialog.selectedFooterOption > #fileDialog.footer then
  579.         fileDialog.selectedFooterOption = 1
  580.     end
  581.  
  582.     if fileDialog.dirSelected < 1 then
  583.         fileDialog.dirSelected = 1
  584.     end
  585.     if fileDialog.fileSelected < 1 then
  586.         fileDialog.fileSelected = 1
  587.     end
  588.     if fileDialog.fileSelected > #fileDialog.currentPathFiles then
  589.         fileDialog.fileSelected = #fileDialog.currentPathFiles
  590.     end
  591.     if fileDialog.dirSelected > #fileDialog.currentPathFolders then
  592.         fileDialog.dirSelected = #fileDialog.currentPathFolders
  593.     end
  594.    
  595.     if fileDialog.dirScrollOffset > dirFileListHeight-fileDialog.dirSelected+1 then
  596.             fileDialog.dirScrollOffset=dirFileListHeight-fileDialog.dirSelected+1
  597.     elseif fileDialog.dirScrollOffset < 1-fileDialog.dirSelected then
  598.             fileDialog.dirScrollOffset=1-fileDialog.dirSelected
  599.     end
  600.    
  601.     if fileDialog.fileScrollOffset > dirFileListHeight-fileDialog.fileSelected+1 then
  602.             fileDialog.fileScrollOffset=dirFileListHeight-fileDialog.fileSelected+1
  603.     elseif fileDialog.fileScrollOffset < 1-fileDialog.fileSelected then
  604.             fileDialog.fileScrollOffset=1-fileDialog.fileSelected
  605.     end
  606.    
  607.    
  608.    
  609.     if collectedInputsCursor.file < 0 then
  610.             collectedInputsCursor.file = 0
  611.     elseif collectedInputsCursor.file > string.len(collectedInputs.file) then
  612.             collectedInputsCursor.file = string.len(collectedInputs.file)
  613.     end
  614.     if collectedInputsOffsets.file >= collectedInputsSize.file - collectedInputsCursor.file-1 then
  615.             collectedInputsOffsets.file=collectedInputsSize.file-collectedInputsCursor.file-1
  616.     elseif collectedInputsOffsets.file < 0-collectedInputsCursor.file then
  617.             collectedInputsOffsets.file=0-collectedInputsCursor.file
  618.     end
  619. end
  620. function showFileSaveAsDialog()
  621.     if fs.exists(currentFile) and not fs.isDir(currentFile) then
  622.         fileDialog.basePath=addSlashIfNeededLeft(shell.resolve(getFilePath(currentFile)))
  623.     else
  624.         --fileDialog.basePath=addSlashIfNeededLeft(shell.resolve(""))
  625.     end
  626.     prepareDialog()
  627.     dialogReset()
  628.     fileDialog.title="Save File As.."
  629.     collectedInputs.file = addSlashIfNeededLeft(shell.resolve(currentFile))
  630.     collectedInputsCursor.file = string.len(collectedInputs.file)
  631.     collectedInputsOffsets.file = 0
  632.     collectedInputsSize.file = 0
  633.     fileDialog.footer={{"Save",trySaveFileFromDialog},{"Cancel",hideFileDialog}}
  634.     showingFileDialog = true
  635. end
  636. function showFileOpenDialog()
  637.     if fs.exists(currentFile) and not fs.isDir(currentFile) then
  638.         fileDialog.basePath=addSlashIfNeededLeft(shell.resolve(getFilePath(currentFile)))
  639.     else
  640.         --fileDialog.basePath=addSlashIfNeededLeft(shell.resolve(""))
  641.     end
  642.     prepareDialog()
  643.     dialogReset()
  644.     fileDialog.title="Open File"
  645.     collectedInputs.file = addSlashIfNeededLeft(shell.resolve(currentFile))
  646.     collectedInputsCursor.file = string.len(collectedInputs.file)
  647.     collectedInputsOffsets.file = 0
  648.     collectedInputsSize.file = 0
  649.     fileDialog.footer={{"Open",tryOpenFileFromDialog},{"Cancel",hideFileDialog}}
  650.     showingFileDialog = true
  651. end
  652. function drawFileDialog(monitor)
  653.         w,h = monitor.getSize()
  654.         local height = h-2
  655.         local width = w-4
  656.         local offX = (w+1)/2 - (width-1)/2
  657.         local offY = (h+1)/2 - (height-1)/2
  658.         local footerMsg = ""
  659.         for o=1, #fileDialog.footer do
  660.             local item = fileDialog.footer[o][1]
  661.             if fileDialog.selectedFooterOption == o then
  662.                 item = "["..item.."]"
  663.             else
  664.                 item = " "..item.." "
  665.             end
  666.             if o > 1 then
  667.                 item = " "..item
  668.             end
  669.             footerMsg=footerMsg..item
  670.         end
  671.         if monitor.isColor() then
  672.             monitor.setBackgroundColor(colors.cyan)
  673.         else
  674.             monitor.setBackgroundColor(colors.black)
  675.         end
  676.         monitor.clear()
  677.         for i=1, height do
  678.                 setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  679.                 monitor.setCursorPos(offX, offY+i-1)
  680.                 if i == 1 or i == height then
  681.                         monitor.write("+")
  682.                         monitor.write(string.rep("-", width-2))
  683.                         monitor.write("+")
  684.                         if i == 1 then
  685.                                 setColors(monitor, colors.black, colors.yellow, colors.white, colors.black)
  686.                                 centerText(monitor, offY+i-1, " "..fileDialog.title.." ")
  687.                                 setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  688.                         elseif i == height then
  689.                                 if fileDialog.currentTab == 4 then
  690.                                     setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  691.                                 else
  692.                                     setColors(monitor, colors.lightGray, colors.white, colors.white, colors.black)
  693.                                 end
  694.                                 centerText(monitor, offY+i-1, " "..footerMsg.." ")
  695.                                 setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  696.                         end
  697.                 else
  698.                         setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  699.                         monitor.write("|")
  700.                         setColors(monitor, colors.white, colors.black, colors.white, colors.black)
  701.                         monitor.write(string.rep(" ", width-2))
  702.                         setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  703.                         monitor.write("|")
  704.                 end
  705.         end
  706.         setColors(monitor, colors.white, colors.gray, colors.white, colors.black)
  707.         monitor.setCursorPos(offX + 2, offY + 2)
  708.         monitor.write("File: ")
  709.         if fileDialog.currentTab == 1 then
  710.             setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  711.         else
  712.             setColors(monitor, colors.lightGray, colors.white, colors.black, colors.white)
  713.         end
  714.         local workingWidth = width - 10
  715.         local textWidth = workingWidth
  716.         collectedInputsSize.file = textWidth
  717.         monitor.write(string.rep(" ", workingWidth))
  718.         monitor.setCursorPos(offX + 8, offY + 2)       
  719.  
  720.         currentLineFit = collectedInputs.file
  721.         inputCursor = collectedInputsCursor.file
  722.         inputOffsetX = collectedInputsOffsets.file
  723.         if currentLineFit then
  724.                 currentLineFit = string.sub(currentLineFit, -inputOffsetX+1, string.len(currentLineFit))
  725.                 if string.len(currentLineFit) > textWidth then
  726.                         currentLineFit = string.sub(currentLineFit, 1, textWidth)
  727.                 end
  728.                 monitor.write(rpad(currentLineFit, textWidth, " "))
  729.         end
  730.         if fileDialog.currentTab == 1 and isBlinking then
  731.                 monitor.setCursorPos(offX+8+inputCursor+inputOffsetX,offY + 2)
  732.                 setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  733.                 local msg = string.sub(currentLineFit,inputCursor+inputOffsetX+1,inputCursor+inputOffsetX+1)
  734.                 if msg == "" then msg = " " end
  735.                 monitor.write(msg)
  736.         end
  737.                        
  738.        
  739.         local fileStart = 15
  740.         setColors(monitor, colors.white, colors.gray, colors.white, colors.black)
  741.         monitor.setCursorPos(offX + 2, offY + 4)
  742.         monitor.write("Dir: ")
  743.         monitor.setCursorPos(offX + fileStart, offY + 4)
  744.         monitor.write("Files: "..fileDialog.basePath)
  745.        
  746.         dirFileListHeight = height - offY - 6
  747.         for i=0, dirFileListHeight do
  748.             local dirIndex = i+1-fileDialog.dirScrollOffset
  749.             local fileIndex = i+1-fileDialog.fileScrollOffset
  750.                
  751.             if fileDialog.currentTab == 2 then
  752.                 setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  753.             else
  754.                 setColors(monitor, colors.lightGray, colors.white, colors.black, colors.white)
  755.             end
  756.             monitor.setCursorPos(offX + 2, offY + 5+i)
  757.             monitor.write(string.rep(" ", fileStart - offX))
  758.             if fileDialog.currentPathFolders[dirIndex] then
  759.                 if fileDialog.currentTab == 2 and dirIndex == fileDialog.dirSelected then
  760.                     setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  761.                 end
  762.                 monitor.setCursorPos(offX + 2, offY + 5+i)
  763.                 monitor.write(fileDialog.currentPathFolders[dirIndex][1])
  764.             end
  765.  
  766.             if fileDialog.currentTab == 3 then
  767.                 setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  768.             else
  769.                 setColors(monitor, colors.lightGray, colors.white, colors.black, colors.white)
  770.             end
  771.             monitor.setCursorPos(offX + fileStart, offY + 5+i)
  772.             monitor.write(string.rep(" ", width - fileStart - 2))
  773.             if fileDialog.currentPathFiles[fileIndex] then
  774.                 if fileDialog.currentTab == 3 and fileIndex == fileDialog.fileSelected then
  775.                     setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  776.                 end
  777.                 monitor.setCursorPos(offX + fileStart, offY + 5+i)
  778.                 monitor.write(fileDialog.currentPathFiles[fileIndex][1])
  779.             end
  780.         end
  781.        
  782.         --gui debug
  783.         --monitor.setCursorPos(1,1)
  784.         --monitor.write("ICur="..collectedInputsCursor.file.." IOff="..collectedInputsOffsets.file.." ISize="..collectedInputsSize.file.." IValSize="..string.len(collectedInputs.file))
  785. end
  786. function drawAbout(monitor)
  787.         w,h = monitor.getSize()
  788.         local height = 13
  789.         local width = 39
  790.         local offX = (w+1)/2 - (width-1)/2
  791.         local offY = (h+1)/2 - (height-1)/2
  792.         if monitor.isColor() then
  793.             monitor.setBackgroundColor(colors.cyan)
  794.         else
  795.             monitor.setBackgroundColor(colors.black)
  796.         end
  797.         monitor.clear()
  798.         for i=1, height do
  799.                 setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  800.                 monitor.setCursorPos(offX, offY+i-1)
  801.                 if i == 1 or i == height then
  802.                         monitor.write("+")
  803.                         monitor.write(string.rep("-", width-2))
  804.                         monitor.write("+")
  805.                         if i == 1 then
  806.                                 setColors(monitor, colors.black, colors.yellow, colors.black, colors.white)
  807.                                 centerText(monitor, offY+i-1, " About TACO BETA ")
  808.                                 setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  809.                         end
  810.                 else
  811.                         setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  812.                         monitor.write("|")
  813.                         setColors(monitor, colors.white, colors.black, colors.white, colors.black)
  814.                         monitor.write(string.rep(" ", width-2))
  815.                         setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  816.                         monitor.write("|")
  817.                 end
  818.         end
  819.         drawPictureTable(monitor, logo, offX + 2, offY + 2, colours.white)
  820.         setColors(monitor, colors.white, colors.orange, colors.white, colors.black)
  821.         centerTextWidth(monitor, offX + 14, offY+3, width - ((offX*2) + 10), "=TACO BETA=")
  822.         setColors(monitor, colors.white, colors.gray, colors.white, colors.black)
  823.         centerTextWidth(monitor, offX + 14, offY+4, width - ((offX*2) + 10), "Edit with flavor!")
  824.         setColors(monitor, colors.white, colors.lightGray, colors.white, colors.black)
  825.         centerTextWidth(monitor, offX + 14, offY+6, width - ((offX*2) + 10), "By: da404lewzer")
  826.         centerTextWidth(monitor, offX + 14, offY+8, width - ((offX*2) + 10), "TurtleScripts.com")
  827.         centerTextWidth(monitor, offX + 14, offY+9, width - ((offX*2) + 10), "Project #gjdh01")
  828. end
  829. function drawMessage(monitor)
  830.     w,h = monitor.getSize()
  831.     local height = #message.body + 2 + 2 --2 for header/footer, 2 for border
  832.     local width = 1
  833.     for i=1, #message.body do
  834.         if string.len(message.body[i])+4 > width then
  835.                 width = string.len(message.body[i])+4
  836.         end
  837.     end
  838.     local footerMsg = ""
  839.     for o=1, #message.footer do
  840.         local item = message.footer[o][1]
  841.         if selectedMessageOption == o then
  842.             item = "["..item.."]"
  843.         else
  844.             item = " "..item.." "
  845.         end
  846.         if o > 1 then
  847.             item = " "..item
  848.         end
  849.         footerMsg=footerMsg..item
  850.     end
  851.     if string.len(message.title)+4 > width then
  852.             width = string.len(message.title)+4
  853.     end
  854.     if string.len(footerMsg)+4 > width then
  855.             width = string.len(footerMsg)+4
  856.     end
  857.     local offX = (w+1)/2 - (width-1)/2
  858.     local offY = (h+1)/2 - (height-1)/2
  859.     for i=1, height do
  860.         setColors(monitor, colors.orange, colors.black, colors.black, colors.white)
  861.         monitor.setCursorPos(offX, offY+i-1)
  862.         if i == 1 or i == height then
  863.                 monitor.write("+")
  864.                 monitor.write(string.rep("-", width-2))
  865.                 monitor.write("+")
  866.                 if i == 1 then
  867.                         setColors(monitor, colors.black, colors.yellow, colors.black, colors.white)
  868.                         centerText(monitor, offY+i-1, " "..message.title.." ")
  869.                         setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  870.                 elseif i == height then
  871.                         setColors(monitor, colors.black, colors.white, colors.black, colors.white)
  872.                         centerText(monitor, offY+i-1, " "..footerMsg.." ")
  873.                         setColors(monitor, colors.orange, colors.black, colors.white, colors.black)
  874.                 end
  875.         else
  876.                 setColors(monitor, colors.orange, colors.black, colors.black, colors.white)
  877.                 monitor.write("|")
  878.                 setColors(monitor, colors.white, colors.black, colors.white, colors.black)
  879.                 monitor.write(string.rep(" ", width-2))
  880.                 setColors(monitor, colors.orange, colors.black, colors.black, colors.white)
  881.                 monitor.write("|")
  882.                 if i-2 > 0 and i-2 <= #message.body then
  883.                         setColors(monitor, colors.white, colors.black, colors.white, colors.black)
  884.                         centerText(monitor, offY+i-1, message.body[i-2])
  885.                 end
  886.                
  887.         end
  888.     end
  889. end
  890. function messageBox(title, bodyArr, footer)
  891.     message.title = title
  892.     message.body = bodyArr
  893.     message.footer = footer
  894.     showingMessage = true
  895. end
  896. local function trySaveFileAs()
  897.     showFileSaveAsDialog()
  898. end
  899. local function stopEditor()
  900.     running = false
  901. end
  902. local function trySaveFile()
  903.     if currentFile == ""then
  904.         trySaveFileAs()
  905.     else
  906.         saveFile(currentFile, fileLinesArr)
  907.     end
  908. end
  909. local function trySaveFileThenNew()
  910.     if currentFile == ""then
  911.         trySaveFileAs()
  912.     else
  913.         saveFile(currentFile, fileLinesArr)
  914.         if isSaved and not isChanged then
  915.             newFile()
  916.         end
  917.     end
  918. end
  919. local function trySaveFileThenOpen()
  920.     if currentFile == ""then
  921.         trySaveFileAs()
  922.     else
  923.         saveFile(currentFile, fileLinesArr)
  924.         if isSaved and not isChanged then
  925.             showFileOpenDialog()
  926.         end
  927.     end
  928. end
  929. local function trySaveFileThenExit()
  930.     if currentFile == ""then
  931.         trySaveFileAs()
  932.     else
  933.         saveFile(currentFile, fileLinesArr)
  934.         if isSaved and not isChanged then
  935.             stopEditor()
  936.         end
  937.     end
  938. end
  939. local function tryExitEditor()
  940.     if isSaved and not isChanged then
  941.         stopEditor()
  942.     else
  943.         messageBox( "Exit Editor?",
  944.                                 {
  945.                                     "The current file isn't saved, save it first?"
  946.                                 },
  947.                                 {{"YES", trySaveFileThenExit}, {"NO", stopEditor}, {"CANCEL", null}}
  948.                             )
  949.     end
  950. end
  951. local function tryOpenFile()
  952.     if isSaved and not isChanged then
  953.         showFileOpenDialog()
  954.     else
  955.         messageBox( "Create New File..",
  956.                                 {
  957.                                     "The current file isn't saved, save it first?"
  958.                                 },
  959.                                 {{"YES", trySaveFileThenOpen}, {"NO", showFileOpenDialog}, {"CANCEL", null}}
  960.                             )
  961.     end
  962. end
  963. local function tryNewFile()
  964.     if isSaved and not isChanged then
  965.         newFile()
  966.     else
  967.         messageBox( "Create New File..",
  968.                                 {
  969.                                     "The current file isn't saved, save it first?"
  970.                                 },
  971.                                 {{"YES", trySaveFileThenNew}, {"NO", newFile}, {"CANCEL", null}}
  972.                             )
  973.     end
  974. end
  975. local function revertFile()
  976.     if currentFile == "" then
  977.         fileLinesArr = {""}
  978.         isChanged = false
  979.     else
  980.         if not openFile(currentFile) then
  981.             messageBox("Error!", {"Couldn't revert file!"}, {{"OK",null}})
  982.         end
  983.     end
  984. end
  985. local function tryRevertFile()
  986.     messageBox( "Revert file?",
  987.                             {
  988.                                 "This cannot be done, sure?"
  989.                             },
  990.                             {{"YES", revertFile}, {"NO", null}}
  991.                         )
  992. end
  993. local function doFileDialogDefaultOption()
  994.     local item = fileDialog.footer[1][2]
  995.     if item then
  996.         item()
  997.     end
  998.     fileDialog.selectedFooterOption = 1
  999. end
  1000. local function doFileDialogMessageOption()
  1001.     local item = fileDialog.footer[fileDialog.selectedFooterOption][2]
  1002.     if item then
  1003.         item()
  1004.     end
  1005.     fileDialog.selectedFooterOption = 1
  1006. end
  1007. local function doMessageOption()
  1008.     local item = message.footer[selectedMessageOption][2]
  1009.     if item then
  1010.         item()
  1011.     end
  1012.     selectedMessageOption = 1
  1013. end
  1014. local function doMenuAction(action)
  1015.         if action == "exit_app" then
  1016.                 tryExitEditor()
  1017.         elseif action == "new_file" then
  1018.                 tryNewFile()
  1019.         elseif action == "open_file" then
  1020.                 tryOpenFile()
  1021.         elseif action == "revert_file" then
  1022.                 tryRevertFile()
  1023.         elseif action == "save_file" then
  1024.                 trySaveFile()
  1025.         elseif action == "saveas_file" then
  1026.                 trySaveFileAs()
  1027.         elseif action == "paste_line" then
  1028.                 table.insert(fileLinesArr, curLine, config.clipboard)
  1029.                 isChanged=true
  1030.         elseif action == "clone_line" then
  1031.                 table.insert(fileLinesArr, curLine, fileLinesArr[curLine])
  1032.                 isChanged=true
  1033.         elseif action == "copy_line" then
  1034.                 config.clipboard = fileLinesArr[curLine]
  1035.         elseif action == "cut_line" then
  1036.                 config.clipboard = fileLinesArr[curLine]
  1037.                 table.remove(fileLinesArr, curLine)
  1038.                 if #fileLinesArr == 0 then
  1039.                     fileLinesArr = {""}
  1040.                 end
  1041.                 isChanged=true
  1042.         elseif action == "delete_line" then
  1043.                 table.remove(fileLinesArr, curLine)
  1044.                 if #fileLinesArr == 0 then
  1045.                     fileLinesArr = {""}
  1046.                 end
  1047.                 isChanged=true
  1048.         elseif action == "perform_update" then
  1049.                 setColors(temp, colors.black, colors.white, colors.black, colors.white)
  1050.                 term.clear()
  1051.                 term.setCursorPos(1,1)
  1052.                 updateAllTheThings()
  1053.                 messageBox("Downloaded Latest",
  1054.                                         {
  1055.                                                 "Restart TACO to complete :)"
  1056.                                         },
  1057.                                         {{"OK", null}})
  1058.             timer = os.startTimer(0.01)
  1059.         elseif action == "show_about" then
  1060.                 showAboutBox = true
  1061.         elseif action == "fullscreen_toggle" then
  1062.                 config.isFullScreen = not config.isFullScreen
  1063.                 checkLinePosition()
  1064.                 checkColPosition()
  1065.         else
  1066.                 --show_help
  1067.                 --show_settings
  1068.                 --replace_text
  1069.                 --find_text
  1070.                 --delete_line
  1071.                 --paste_line
  1072.                 --copy_line
  1073.                 --cut_line
  1074.                 --clear_selection
  1075.                 --paste_selection
  1076.                 --cut_selection
  1077.                 --copy_selection
  1078.                 --print_file
  1079.                 --revert_file
  1080.                 --open_file
  1081.                
  1082.         end
  1083.         selectedMenu = 1
  1084.         selectedSubMenu = 1
  1085. end
  1086.  
  1087.  
  1088. loadSettings()
  1089.  
  1090. if #tArgs > 0 then
  1091.     --openFile(tArgs[1])
  1092.     if not openFile(addSlashIfNeededLeft(shell.resolve(getFilePath(tArgs[1])))) then
  1093.         messageBox("Error!", {"Couldn't open file!"}, {{"OK",null}})
  1094.     end
  1095. end
  1096. prepareDialog()
  1097. if showFirstTimeMessage then
  1098.         messageBox("Welcome to TACO BETA",
  1099.                                 {
  1100.                                         "Welcome to the editor with flavor!",
  1101.                                         "This is BETA sorry if you find bugs :(",
  1102.                                         "",
  1103.                                         "Presse ENTER to continue,",
  1104.                                         "Press CONTROL to access menus :)"
  1105.                                 },
  1106.                                 {{"OK", null}})
  1107. end
  1108. while running do
  1109.     local event, p1,p2,p3 = os.pullEvent()
  1110.     if event == "mouse_scroll" then
  1111.         if not showingMessage and not showAboutBox and not inMenu then
  1112.             curLine=curLine+p1
  1113.             checkLinePosition()
  1114.             checkColPosition()
  1115.             resetBlink()
  1116.         end
  1117.     elseif event == "mouse_click" then
  1118.         if not showingMessage and not showAboutBox and not showingFileDialog then-- and not inMenu
  1119.             local padWidth = string.len(tostring(#fileLinesArr))
  1120.             local offsetY = 1
  1121.             if config.isFullScreen then offsetY = 0 end
  1122.             if p1 == 1 then
  1123.                 if p3 <= 1 then
  1124.                     pos = 1
  1125.                     prevPos = pos
  1126.  
  1127.                     for i = 1, #menus do
  1128.                         pos = pos + string.len(menus[i].name) + 2
  1129.  
  1130.                         if p2 > prevPos and p2 <= pos then
  1131.                             if i == selectedMenu and inMenu then
  1132.                                 inMenu = false
  1133.                             else
  1134.                                 inMenu = true
  1135.                                 selectedMenu = i
  1136.                                 selectedSubMenu = 1
  1137.                             end
  1138.  
  1139.                             break
  1140.                         end
  1141.  
  1142.                         prevpos = pos
  1143.                     end
  1144.                 end
  1145.  
  1146.                 if inMenu and p3 > 1 then
  1147.                     menuOffset = getMenuOffset(selectedMenu)
  1148.  
  1149.                     if p2 > menuOffset - 1 and p2 < menuOffset + getMenuWidth(selectedMenu) and p3 <= getMenuHeight(selectedMenu) + 1 then
  1150.                         items = menus[selectedMenu].items
  1151.                         itemCount = #items
  1152.  
  1153.                         for i = 1, itemCount do
  1154.                             if p3 - 1 == i then
  1155.                                 if items[i][1] == "--" then
  1156.                                     if i >= itemCount then
  1157.                                         if i == 1 then
  1158.                                             selectedSubMenu = 1
  1159.                                         else
  1160.                                             selectedSubMenu = i - 1
  1161.                                         end
  1162.                                     else
  1163.                                         selectedSubMenu = i + 1
  1164.                                     end
  1165.                                 else
  1166.                                     selectedSubMenu = i
  1167.                                     doMenuAction(menus[selectedMenu].items[selectedSubMenu][3])
  1168.                                     inMenu = false
  1169.                                 end
  1170.                             end
  1171.                         end
  1172.                     else
  1173.                         inMenu = false
  1174.                     end
  1175.                 elseif not inMenu and p3 > 1 then
  1176.                     curCol=p2-padWidth-scrollOffsetX-2
  1177.                     curLine=p3-offsetY-scrollOffsetY
  1178.                     checkLinePosition()
  1179.                     checkColPosition()
  1180.                     resetBlink()
  1181.                 end
  1182.             end
  1183.         elseif showingMessage then
  1184.             height = #message.body + 4
  1185.             posY = (((h + 1) / 2) - ((height - 1) / 2)) + height - 1
  1186.  
  1187.             footerWidth = 1
  1188.  
  1189.             for i = 1, #message.footer do
  1190.                 item = message.footer[i][1]
  1191.                 footerWidth = footerWidth + string.len(item) + 2
  1192.  
  1193.                 if i > 1 then
  1194.                     footerWidth = footerWidth + 1
  1195.                 end
  1196.             end
  1197.  
  1198.             posX = ((w + 1) / 2) - ((footerWidth - 1) / 2)  -- + 1 for space before buttons
  1199.  
  1200.             if p3 == posY then
  1201.                 pos = posX
  1202.                 prevPos = pos
  1203.  
  1204.                 for i = 1, #message.footer do
  1205.                     pos = pos + string.len(message.footer[i][1]) + 2
  1206.  
  1207.                     if p2 >= prevPos and p2 < pos then
  1208.                         selectedMessageOption = i
  1209.                         showingMessage = false
  1210.                         doMessageOption()
  1211.                         break
  1212.                     end
  1213.  
  1214.                     pos = pos + 1
  1215.                     prevPos = pos
  1216.                 end
  1217.             end
  1218.         elseif showingFileDialog then
  1219.             if p3 == 4 and p2 > 10 and p2 <= w - 4 then -- File textbox
  1220.                 fileDialog.currentTab = 1
  1221.                 collectedInputsCursor.file = p2 - collectedInputsOffsets.file - 11
  1222.                 checkDialogLimits()
  1223.             elseif p3 > 6 and p3 <= h - 3 then  -- Dir and File horizontal area
  1224.                 if p2 > 4 and p2 <= 16 then
  1225.                     fileDialog.currentTab = 2
  1226.                     fileDialog.dirSelected = fileDialog.dirScrollOffset + (p3 - 6)
  1227.                     checkDialogLimits()
  1228.                     fileDialog.basePath = fileDialog.currentPathFolders[fileDialog.dirSelected][2]
  1229.                     fileDialog.dirScrollOffset = 1
  1230.                     fileDialog.dirSelected = 1
  1231.                     prepareDialog()
  1232.                 elseif p2 > 17 and p2 <= w - 4 then
  1233.                     fileDialog.currentTab = 3
  1234.                     fileDialog.fileSelected = fileDialog.fileScrollOffset + (p3 - 6)
  1235.                     checkDialogLimits()
  1236.                     collectedInputs.file = fileDialog.currentPathFiles[fileDialog.fileSelected][2]
  1237.                 end
  1238.             elseif p3 == h - 1 then
  1239.                 footerWidth = 1
  1240.  
  1241.                 for i = 1, #fileDialog.footer do
  1242.                     item = fileDialog.footer[i][1]
  1243.                     footerWidth = footerWidth + string.len(item) + 2
  1244.  
  1245.                     if i > 1 then
  1246.                         footerWidth = footerWidth + 1
  1247.                     end
  1248.                 end
  1249.  
  1250.                 footerOffset = ((w + 1) / 2) - (footerWidth / 2)
  1251.  
  1252.                 if p2 >= footerOffset and p2 <= footerOffset + footerWidth then
  1253.                     fileDialog.currentTab = 4
  1254.                 end
  1255.  
  1256.                 pos = footerOffset + 1
  1257.                 prevPos = pos
  1258.  
  1259.                 for i = 1, #fileDialog.footer do
  1260.                     pos = pos + string.len(fileDialog.footer[i][1]) + 2
  1261.  
  1262.                     if p2 >= prevPos and p2 < pos then
  1263.                         fileDialog.selectedFooterOption = i
  1264.                         doFileDialogMessageOption()
  1265.                         break
  1266.                     end
  1267.  
  1268.                     pos = pos + 1
  1269.                     prevPos = pos
  1270.                 end
  1271.             end
  1272.         elseif showAboutBox then
  1273.             showAboutBox = false
  1274.         end
  1275.     elseif event == "key" then
  1276.         if showingMessage then
  1277.             if p1 == keys.enter then
  1278.                 showingMessage = false
  1279.                 doMessageOption()
  1280.             elseif p1 == keys.left or p1 == keys.up then
  1281.                 selectedMessageOption=selectedMessageOption-1
  1282.                 checkMessageOptionPositions()
  1283.             elseif p1 == keys.right or p1 == keys.down then
  1284.                 selectedMessageOption=selectedMessageOption+1
  1285.                 checkMessageOptionPositions()
  1286.             end
  1287.         elseif showAboutBox then
  1288.             if p1 == keys.enter then
  1289.                 showAboutBox = false
  1290.             end
  1291.         elseif showingFileDialog then
  1292.             if p1 == keys.enter then
  1293.                 if fileDialog.currentTab == 1 then
  1294.                     doFileDialogDefaultOption()
  1295.                 elseif fileDialog.currentTab == 2 then
  1296.                     fileDialog.basePath = fileDialog.currentPathFolders[fileDialog.dirSelected][2]                 
  1297.                     prepareDialog()
  1298.                 elseif fileDialog.currentTab == 3 then
  1299.                     collectedInputs.file = fileDialog.currentPathFiles[fileDialog.fileSelected][2]
  1300.                     fileDialog.currentTab = 4
  1301.                     prepareDialog()
  1302.                 elseif fileDialog.currentTab == 4 then
  1303.                     doFileDialogMessageOption()
  1304.                 end
  1305.             elseif p1 == keys.tab then
  1306.                 fileDialog.currentTab=fileDialog.currentTab+1
  1307.                 if fileDialog.currentTab > fileDialog.currentTabMax then
  1308.                     fileDialog.currentTab = 1
  1309.                 end
  1310.             elseif p1 == keys.home then
  1311.                 if fileDialog.currentTab == 1 then
  1312.                     collectedInputsCursor.file=0
  1313.                 end
  1314.                 checkDialogLimits()
  1315.             elseif p1 == keys["end"] then
  1316.                 if fileDialog.currentTab == 1 then
  1317.                     collectedInputsCursor.file=string.len(collectedInputs.file)+1
  1318.                 end
  1319.                 checkDialogLimits()
  1320.  
  1321.             elseif p1 == keys.backspace then
  1322.                 if fileDialog.currentTab == 1 then
  1323.                     if collectedInputsCursor.file > 0 then
  1324.                         local leftSide = string.sub(collectedInputs.file, 1, collectedInputsCursor.file-1)
  1325.                         local rightSide = string.sub(collectedInputs.file, collectedInputsCursor.file+1)
  1326.                         collectedInputs.file = leftSide..rightSide
  1327.                         collectedInputsCursor.file=collectedInputsCursor.file-1
  1328.                     end
  1329.                 end
  1330.                 checkDialogLimits()
  1331.             elseif p1 == keys.delete then
  1332.                 if fileDialog.currentTab == 1 then
  1333.                     if collectedInputsCursor.file <= string.len(collectedInputs.file) then
  1334.                         local leftSide = string.sub(collectedInputs.file, 1, collectedInputsCursor.file)
  1335.                         local rightSide = string.sub(collectedInputs.file, collectedInputsCursor.file+2)
  1336.                         collectedInputs.file = leftSide..rightSide
  1337.                     end
  1338.                 end
  1339.                 checkDialogLimits()
  1340.             elseif p1 == keys.up or p1 == keys.left then
  1341.                 if fileDialog.currentTab == 1 then
  1342.                     collectedInputsCursor.file=collectedInputsCursor.file-1
  1343.                 elseif fileDialog.currentTab == 2 then
  1344.                     fileDialog.dirSelected=fileDialog.dirSelected-1
  1345.                 elseif fileDialog.currentTab == 3 then
  1346.                     fileDialog.fileSelected=fileDialog.fileSelected-1
  1347.                 elseif fileDialog.currentTab == 4 then
  1348.                     fileDialog.selectedFooterOption=fileDialog.selectedFooterOption-1
  1349.                 end
  1350.                 checkDialogLimits()
  1351.             elseif p1 == keys.down or p1 == keys.right then
  1352.                 if fileDialog.currentTab == 1 then
  1353.                     collectedInputsCursor.file=collectedInputsCursor.file+1
  1354.                 elseif fileDialog.currentTab == 2 then
  1355.                     fileDialog.dirSelected=fileDialog.dirSelected+1
  1356.                 elseif fileDialog.currentTab == 3 then
  1357.                     fileDialog.fileSelected=fileDialog.fileSelected+1
  1358.                 elseif fileDialog.currentTab == 4 then
  1359.                     fileDialog.selectedFooterOption=fileDialog.selectedFooterOption+1
  1360.                 end
  1361.                 checkDialogLimits()
  1362.             end
  1363.         else
  1364.             if state == "edit" then
  1365.                 if p1 == 29 or p1 == 157 then
  1366.                     inMenu = not inMenu
  1367.                 end
  1368.                 if inMenu then
  1369.                     if p1 == keys.up then
  1370.                         selectedSubMenu=selectedSubMenu-1
  1371.                         checkMenuPositions()
  1372.                         if menus[selectedMenu].items[selectedSubMenu][1] == "--" then
  1373.                             selectedSubMenu=selectedSubMenu-1
  1374.                             checkMenuPositions()
  1375.                         end
  1376.                     elseif p1 == keys.down then
  1377.                         selectedSubMenu=selectedSubMenu+1
  1378.                         checkMenuPositions()
  1379.                         if menus[selectedMenu].items[selectedSubMenu][1] == "--" then
  1380.                             selectedSubMenu=selectedSubMenu+1
  1381.                             checkMenuPositions()
  1382.                         end
  1383.                     elseif p1 == keys.left then
  1384.                         selectedSubMenu = 1
  1385.                         selectedMenu=selectedMenu-1
  1386.                         checkMenuPositions()
  1387.                     elseif p1 == keys.right then
  1388.                         selectedSubMenu = 1
  1389.                         selectedMenu=selectedMenu+1
  1390.                         checkMenuPositions()
  1391.                     elseif p1 == keys.enter then
  1392.                         doMenuAction(menus[selectedMenu].items[selectedSubMenu][3])
  1393.                         inMenu = false
  1394.                     end
  1395.                 else
  1396.                     if p1 == keys.up then
  1397.                         curLine=curLine-1
  1398.                     elseif p1 == keys.down then
  1399.                         curLine=curLine+1
  1400.                     elseif p1 == 201 then -- page up
  1401.                         curLine=curLine-(h-2)
  1402.                     elseif p1 == 209 then --page down
  1403.                         curLine=curLine+(h-2)
  1404.                     elseif p1 == keys.left then
  1405.                         curCol=curCol-1
  1406.                     elseif p1 == keys.right then
  1407.                         curCol=curCol+1
  1408.                     elseif p1 == keys.home then
  1409.                         curCol=0
  1410.                     elseif p1 == keys["end"] then
  1411.                         curCol=string.len(fileLinesArr[curLine])+1
  1412.                     elseif p1 == keys.tab then
  1413.                         isChanged=true
  1414.                         local leftSide = string.sub(fileLinesArr[curLine], 1, curCol)
  1415.                         local rightSide = string.sub(fileLinesArr[curLine], curCol+1)
  1416.                         fileLinesArr[curLine] = leftSide.." "..rightSide
  1417.                         curCol=curCol+2
  1418.                         resetBlink()
  1419.                         checkLinePosition()
  1420.                         checkColPosition()
  1421.                     elseif p1 == keys.enter then
  1422.                         isChanged=true
  1423.                         if curCol == string.len(fileLinesArr[curLine]) then
  1424.                             curLine=curLine+1
  1425.                             curCol=0
  1426.                             table.insert(fileLinesArr, curLine, "")
  1427.                         elseif curCol == 0 then
  1428.                             table.insert(fileLinesArr, curLine, "")
  1429.                             curLine=curLine+1
  1430.                         else                                       
  1431.                             local leftSide = string.sub(fileLinesArr[curLine], 1, curCol)
  1432.                             local rightSide = string.sub(fileLinesArr[curLine], curCol+1)
  1433.                             fileLinesArr[curLine] = leftSide
  1434.                             table.insert(fileLinesArr, curLine+1, rightSide)     
  1435.                             curLine=curLine+1
  1436.                             curCol=0
  1437.                         end
  1438.                     elseif p1 == keys.backspace then
  1439.                         isChanged=true
  1440.                         if curCol > 0 then
  1441.                             local leftSide = string.sub(fileLinesArr[curLine], 1, curCol-1)
  1442.                             local rightSide = string.sub(fileLinesArr[curLine], curCol+1)
  1443.                             fileLinesArr[curLine] = leftSide..rightSide
  1444.                             curCol=curCol-1
  1445.                         elseif curCol == 0 and curLine > 1 then
  1446.                             local newCurCol = string.len(fileLinesArr[curLine-1])
  1447.                             fileLinesArr[curLine-1] = fileLinesArr[curLine-1] .. fileLinesArr[curLine]
  1448.                             table.remove(fileLinesArr, curLine)
  1449.                             curLine = curLine -1
  1450.                             curCol = newCurCol
  1451.                         end
  1452.                     elseif p1 == keys.delete then
  1453.                         isChanged=true
  1454.                         if curCol < string.len(fileLinesArr[curLine]) then
  1455.                             local leftSide = string.sub(fileLinesArr[curLine], 1, curCol)
  1456.                             local rightSide = string.sub(fileLinesArr[curLine], curCol+2)
  1457.                             fileLinesArr[curLine] = leftSide..rightSide
  1458.                         elseif curCol == string.len(fileLinesArr[curLine]) and curLine < #fileLinesArr then
  1459.                             fileLinesArr[curLine] = fileLinesArr[curLine] .. fileLinesArr[curLine+1]
  1460.                             table.remove(fileLinesArr, curLine+1)
  1461.                         end
  1462.                     end
  1463.                     checkLinePosition()
  1464.                     checkColPosition()
  1465.                 end                
  1466.                 resetBlink()
  1467.             elseif state == "msg" then
  1468.                 if p1 == keys.enter then
  1469.                     state = "edit"
  1470.                 end
  1471.             end
  1472.         end
  1473.     elseif event == "char" then
  1474.         if showingMessage then
  1475.         elseif showAboutBox then
  1476.         elseif showingFileDialog then
  1477.             if fileDialog.currentTab == 1 then
  1478.                 local leftSide = string.sub(collectedInputs.file, 1, collectedInputsCursor.file)
  1479.                 local rightSide = string.sub(collectedInputs.file, collectedInputsCursor.file+1)               
  1480.                 collectedInputs.file=leftSide..p1..rightSide
  1481.                 collectedInputsCursor.file=collectedInputsCursor.file+1
  1482.                 checkDialogLimits()
  1483.             end
  1484.         else
  1485.             if state == "edit" then
  1486.                 isChanged=true
  1487.                 local leftSide = string.sub(fileLinesArr[curLine], 1, curCol)
  1488.                 local rightSide = string.sub(fileLinesArr[curLine], curCol+1)
  1489.                 fileLinesArr[curLine] = leftSide..p1..rightSide
  1490.                 curCol=curCol+1
  1491.                 resetBlink()
  1492.                 checkLinePosition()
  1493.                 checkColPosition()
  1494.             end
  1495.         end
  1496.     elseif event == "timer" and p1 == timer then
  1497.         if state == "edit" then
  1498.             drawScreen(term)
  1499.         elseif state == "additem" then
  1500.             --displayAddItem()
  1501.         elseif state == "msg" then
  1502.             --displayMsg(currentMessage)
  1503.         end
  1504.         if showingFileDialog then
  1505.             drawFileDialog(term)
  1506.         end
  1507.         if showingMessage then
  1508.             drawMessage(term)
  1509.         end
  1510.         if showAboutBox then
  1511.             drawAbout(term)
  1512.         end
  1513.         timer = os.startTimer(0.01)
  1514.     end
  1515.     if os.clock() - lastBlinkTime > 0.5 then
  1516.         lastBlinkTime = os.clock()
  1517.         isBlinking = not isBlinking
  1518.     end
  1519. end
  1520.  
  1521. saveSettings()
  1522. setColors(term, colors.black, colors.white, colors.black, colors.white)
  1523. term.clear()
  1524. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement