Advertisement
Marlingaming

CC Tweaked Client App - AppStore - text editor

Jan 13th, 2022 (edited)
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.20 KB | None | 0 0
  1. --this program is a text Editor
  2. local w, h = term.getSize()
  3. local OpenedFile = "n"
  4. local FileData = {}
  5. local OpenedPath = "os"
  6. local PathParts = {"os"}
  7. local PathSection = 1
  8.  
  9. local function Clear()
  10. term.setBackgroundColor(colors.black)
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. end
  14.  
  15. local function CenterText(y,text)
  16.     local x = math.floor((w - string.len(text)) /2)
  17.     term.setCursorPos(x,y)
  18.     term.clearLine()
  19.     term.write(text)
  20. end
  21.  
  22. local function OpenFile(file)
  23. local Item = fs.open(file,"r")
  24. local line
  25. while true do
  26.     line = Item.readLine()
  27.     if line == nil then
  28.         break
  29.     else
  30.         FileData[#FileData + 1] = line
  31.     end
  32. end
  33. Item.close()
  34. EditorMenu()
  35. end
  36.  
  37. local function SaveFile(file)
  38. local Item = fs.open(file,"w")
  39. Item.clear()
  40. Item.setCursorPos(1,1)
  41. for i = 1, #FileData do
  42.     Item.writeLine(FileData[i])
  43. end
  44. Item.close()
  45. end
  46.  
  47. function EditorMenu()
  48. Clear()
  49. print(OpenedFile)
  50. local Options = {"=Exit=","=Save="}
  51. for i = 1, #FileData do
  52.     Options[#Options + 1] = FileData[i]
  53. end
  54. local n = CUI(Options,2)
  55. if Options[n] == "=Exit=" then
  56.     FileData = {}
  57.     OpenedFile = "n"
  58.     FileNavigator()
  59. elseif Options[n] == "=Save=" then
  60.     SaveFile(OpenedFile)
  61. else
  62.     local event
  63.     input = FileData[n]
  64.     while true do
  65.         event = {os.pullEvent("key")}
  66.         input = read
  67.         if event[2] == keys.enter then break end
  68.     end
  69.     FileData[n] = input
  70.     EditorMenu()
  71. end
  72. end
  73.  
  74. function CUI(m,y) --declare function
  75. n=1
  76. local l = #m
  77. while true do
  78. term.setCursorPos(1,y)
  79. term.clearLine()
  80. for i = 1, #m do
  81.  
  82. if i==n then print("["..m[i].."]") else print(m[i]) end
  83. end
  84. if b==keys.a and n>1 then n=n-1 end
  85. if b==keys.d and n<l then n=n+1 end
  86. if b==keys.enter then break end
  87. end
  88. return n --return the value
  89. end
  90.  
  91. function FileNavigator()
  92. Clear()
  93. print(OpenedPath)
  94. Files = {fs.list(OpenedPath)}
  95. local Options = {"exit","return"}
  96. for i = 1, #Files do
  97.     Options[#Options + 1] = Files[i]
  98. end
  99. local n = CUI(Options,2)
  100. if Options[n] == "exit" then
  101.     shell.run(settings.get("os_DesktopLoc"))
  102. elseif Options[n] == "return" then
  103.     OpenedPath = PathPart[PathSection-1]
  104. else
  105.     if fs.isDir(Options[n]) == true then
  106.         OpenedPath = Options[n]
  107.     else
  108.         OpenedFile = Options[n]
  109.         OpenFile(Options[n])
  110.     end
  111. end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement