Marlingaming

CC Tweaked CCSPS Iron - Text Editor

Jan 28th, 2022 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. local AllowedPaths = {"os/System/Files","os/System/Client/Files","exit"}
  2. local OpenedPath = "n"
  3.  
  4. local Line = 1
  5. local Lines = {}
  6. local tArg = {...}
  7. local options = {}
  8.  
  9. local FilePath = ""
  10. local w, h = term.getSize()
  11.  
  12. local function Clear()
  13. term.clear()
  14. term.setCursorPos(1,1)
  15. end
  16.  
  17. function OpenFile()
  18. Lines = {}
  19.  
  20. Line = 1
  21.  
  22. local file = fs.open(FilePath,"r")
  23. repeat
  24. local Contents = file.readLine()
  25. if Contents ~= nil then Lines[#Lines + 1] = Contents end
  26. until Contents == nil
  27. file.close()
  28.  
  29. Lines[#Lines + 1] = "=NewLine="
  30.  
  31. Lines[#Lines + 1] = "=exit="
  32. end
  33.  
  34. function SaveFile()
  35. local file = fs.open(FilePath,"w")
  36. for i = 1, #Lines do
  37. file.writeLine(Lines[i])
  38. end
  39. file.close()
  40. Lines = {}
  41. Line = 1
  42. end
  43.  
  44. function FindFile(name)
  45. FilePath = fs.combine(OpenedPath,name)
  46. end
  47.  
  48. function CUI(m,y) --declare function
  49. n=1
  50. local l = #m
  51. while true do
  52. term.setCursorPos(1,y)
  53. for i=1, #m, 1 do --traverse the table of options
  54. if i==n then term.clearLine() print(i, " >",m[i]) else term.clearLine() print(i, " ", m[i]) end --print them
  55. end
  56. a, b= os.pullEvent("key") --wait for keypress
  57. if b==keys.w and n>1 then n=n-1 end
  58. if b==keys.s and n<l then n=n+1 end
  59. if b==keys.enter then break end
  60. end
  61. return n --return the value
  62. end
  63.  
  64. function TextMenu_Base()
  65. Clear()
  66. options = Lines
  67. local n = CUI(options,1)
  68. if options[n] == "=exit=" then
  69. Lines[#Lines] = nil
  70. Lines[#Lines] = nil
  71. SaveFile()
  72. if tArg[1] == "direct" then
  73.  
  74. else
  75. FileMenu()
  76. end
  77. elseif options[n] == "=NewLine=" then
  78. Lines[n] = "empty"
  79. Lines[#Lines] = "=NewLine="
  80. Lines[#Lines + 1] = "=exit="
  81. TextMenu_Base()
  82. else
  83. Line = n
  84. TextMenu_Line()
  85. end
  86. end
  87.  
  88. function TextMenu_Line()
  89. Clear()
  90. print("line ",Line," :",Lines[Line])
  91. term.setCursorPos(1,3)
  92. local input = Lines[Line]
  93. while true do
  94. local event = {os.pullEvent("key")}
  95. if event[2] == keys.enter then break end
  96. input = read()
  97. end
  98. Lines[Line] = input
  99. TextMenu_Base()
  100. end
  101.  
  102. function FileMenu()
  103. Clear()
  104. options = nil
  105. if OpenedPath == "n" then
  106. options = AllowedPaths
  107. else
  108. options = fs.list(OpenedPath)
  109. options[#fs.list(OpenedPath) + 1] = "return"
  110. end
  111. local n = CUI(options,2)
  112. if options[n] == "exit" then
  113.  
  114. elseif options[n] == "return" then
  115. OpenedPath = "n"
  116. FileMenu()
  117. elseif OpenedPath == "n" then
  118. OpenedPath = options[n]
  119. FileMenu()
  120. else
  121. FindFile(options[n])
  122. OpenFile()
  123. TextMenu_Base()
  124. end
  125. end
  126.  
  127. if tArg[1] == "direct" then
  128. FilePath = tArg[2]
  129. OpenFile()
  130. TextMenu_Base()
  131. else
  132. FileMenu()
  133. end
Add Comment
Please, Sign In to add comment