minif

file

Jan 25th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. --Minif OS File Core
  2.  
  3. local start = 0
  4. osFileRoot = ""
  5.  
  6. fileAction = ""
  7. fileName = ""
  8.  
  9. function edit(file)
  10. fileAction = "edit"
  11. fileName = file
  12. end
  13.  
  14. function paint(file)
  15. fileAction = "paint"
  16. fileName = file
  17. end
  18.  
  19. function drawFile()
  20. paintutils.drawFilledBox(3,6,mos.osScreenSizeX-2,mos.osScreenSizeY-2,colors.white)
  21. term.setCursorPos(mos.osScreenSizeX-6,mos.osScreenSizeY-3)
  22. term.setTextColor(colors.black)
  23. term.setBackgroundColor(colors.green)
  24. term.write("New")
  25. term.setBackgroundColor(colors.white)
  26. end
  27.  
  28.  
  29. function runThing(file)
  30. if fs.isDir(file) then
  31. mos.osDrawWindow("Open File")
  32. osListFiles(file.."/")
  33. else
  34. paintutils.drawFilledBox(3,6,mos.osScreenSizeX-2,mos.osScreenSizeY-2,colors.white)
  35. term.setBackgroundColor(colors.white)
  36. term.setTextColor(colors.black)
  37. term.setCursorPos(4,7)
  38. term.write(file)
  39. term.setCursorPos(4,8)
  40. term.write("Open this file using...")
  41. term.setCursorPos(4,10)
  42. term.setBackgroundColor(colors.lightBlue)
  43. term.write("Text")
  44. term.setCursorPos(9,10)
  45. term.write("Draw")
  46. term.setCursorPos(4,12)
  47. term.write("Copy")
  48. term.setCursorPos(9,12)
  49. term.write("Rename")
  50. term.setCursorPos(16,12)
  51. term.write("Delete")
  52. while true do
  53. osPEvent, osPB, osPX, osPY = os.pullEvent()
  54. if osPEvent == "mouse_click" then
  55. if osPY == 10 and osPX >= 4 and osPX <= 7 then
  56. edit(file)
  57. return
  58. else if osPY == 10 and osPX >= 9 and osPY <= 12 then
  59. paint(file)
  60. return
  61. else if osPY == 12 and osPX >= 4 and osPX <= 7 then
  62. term.setCursorPos(4,mos.osScreenSizeY-4)
  63. term.setBackgroundColor(colors.white)
  64. term.setTextColor(colors.black)
  65. local fname = read()
  66. if fs.exists(osFileRoot..fname) then
  67. term.setCursorPos(4,mos.osScreenSizeY-3)
  68. term.setTextColor(colors.red)
  69. term.write("File Exists")
  70. sleep(2)
  71. else
  72. fs.copy(file, osFileRoot..fname)
  73. term.setCursorPos(4,mos.osScreenSizeY-3)
  74. term.setTextColor(colors.black)
  75. term.write("Copied to "..osFileRoot..fname)
  76. sleep(2)
  77. end
  78. return
  79. else if osPY == 12 and osPX >= 9 and osPX <= 14 then
  80. term.setCursorPos(4,mos.osScreenSizeY-4)
  81. term.setBackgroundColor(colors.white)
  82. term.setTextColor(colors.black)
  83. local fname = read()
  84. if fs.exists(osFileRoot..fname) then
  85. term.setCursorPos(4,mos.osScreenSizeY-3)
  86. term.setTextColor(colors.red)
  87. term.write("File Exists")
  88. sleep(2)
  89. else
  90. fs.move(file, osFileRoot..fname)
  91. term.setCursorPos(4,mos.osScreenSizeY-3)
  92. term.setTextColor(colors.black)
  93. term.write("Renamed to "..osFileRoot..fname)
  94. sleep(2)
  95. end
  96. return
  97. else if osPY == 12 and osPX >= 16 and osPX <= 21 then
  98. fs.delete(file)
  99. term.setBackgroundColor(colors.white)
  100. term.setCursorPos(4,mos.osScreenSizeY-3)
  101. term.setTextColor(colors.black)
  102. term.write("Deleted "..file)
  103. sleep(2)
  104. return
  105. else
  106. return
  107. end
  108. end
  109. end
  110. end
  111. end
  112. else
  113. if osPEvent == "key" then
  114. if osPB == keys.t then
  115. shell.run('edit "'..file..'"')
  116. end
  117. if osPB == keys.d then
  118. shell.run('paint "'..file..'"')
  119. end
  120. return
  121. end
  122. end
  123. end
  124. return
  125. end
  126. end
  127.  
  128. function newFile(loc)
  129. term.setCursorPos(4,mos.osScreenSizeY-3)
  130. term.setBackgroundColor(colors.white)
  131. term.setTextColor(colors.green)
  132. term.write("New: ")
  133. local newFile = read()
  134. if fs.exists(loc..newFile) then
  135. term.setCursorPos(4,mos.osScreenSizeY-2)
  136. term.setTextColor(colors.red)
  137. print("File Exists!")
  138. sleep(2)
  139. else
  140. run = false
  141. runThing(loc..newFile)
  142. end
  143. term.setTextColor(colors.black)
  144. end
  145.  
  146. function osListFiles(location)
  147. local run = true
  148. local start = 0
  149. local list = fs.list(location)
  150. drawFile()
  151. while run do
  152. for i, file in ipairs(list) do
  153. if i>mos.osScreenSizeY-4-7+start then
  154. term.setCursorPos(4,mos.osScreenSizeY-3)
  155. term.write("Next>")
  156. osPEvent, osPB, osPX, osPY = os.pullEvent("mouse_click")
  157. if osPX>=3 and osPX<=(mos.osScreenSizeX-2) and osPY>=7 and osPY<=(mos.osScreenSizeY-5) then
  158. for e, file in ipairs(list) do
  159. if e == osPY + start-6 then
  160. if run then
  161. runThing(location..file)
  162. run = false
  163. end
  164. end
  165. end
  166. return
  167. else
  168. if osPY == mos.osScreenSizeY-3 and osPX >= mos.osScreenSizeX-6 and osPX <= mos.osScreenSizeX-3 then
  169. newFile(location)
  170. return
  171. else
  172. drawFile()
  173. start = i-1
  174. end
  175. end
  176. end
  177. term.setCursorPos(4,i+6-start)
  178. if fs.isDir(location..file) then
  179. term.setTextColor(colors.green)
  180. else
  181. term.setTextColor(colors.black)
  182. end
  183. print(i.." "..file)
  184. end
  185. if run then
  186. osPEvent, osPB, osPX, osPY = os.pullEvent("mouse_click")
  187. if osPX>=3 and osPX<=(mos.osScreenSizeX-2) and osPY>=7 and osPY<=(mos.osScreenSizeY-4) then
  188. for e, file in ipairs(list) do
  189. if e == osPY + start-6 then
  190. if run then
  191. runThing(location..file)
  192. run = false
  193. end
  194. end
  195. end
  196. return
  197. else
  198. if osPY == mos.osScreenSizeY-3 and osPX >= mos.osScreenSizeX-6 and osPX <= mos.osScreenSizeX-3 then
  199. newFile(location)
  200. return
  201. else
  202. return
  203. end
  204. end
  205. end
  206. end
  207. end
  208.  
  209. function osListFilesAtRoot(root)
  210. osFileRoot = root
  211. osListFiles(root)
  212. return fileAction, fileName
  213. end
Advertisement
Add Comment
Please, Sign In to add comment