kreezxil

taco.lua

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