Guest User

Untitled

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