DrBlocks

TACO

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