xXm0dzXx

Untitled

Jul 20th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs == 0 then
  4. print( "Usage: edit <path>" )
  5. return
  6. end
  7.  
  8. local sPath = shell.resolve( tArgs[1] )
  9. local bReadOnly = fs.isReadOnly( sPath )
  10. if fs.exists( sPath ) and fs.isDir( sPath ) then
  11. print( "Cannot edit a directory" )
  12. return
  13. end
  14.  
  15. local w,h = term.getSize()
  16. local x,y = 1,1
  17. local scrollX, scrollY = 0,0
  18.  
  19. local bMenu = false
  20. local nMenuItem = 1
  21.  
  22. local sStatus = ""
  23. local tLines = {}
  24.  
  25. local function load()
  26. tLines = {}
  27. if fs.exists( sPath ) then
  28. local file = io.open( sPath, "r" )
  29. local sLine = file:read()
  30. while sLine do
  31. table.insert( tLines, sLine )
  32. sLine = file:read()
  33. end
  34. file:close()
  35. else
  36. table.insert( tLines, "" )
  37. end
  38. end
  39.  
  40. local function save()
  41. local file = io.open( sPath, "w" )
  42. if file then
  43. for n, sLine in ipairs( tLines ) do
  44. file:write( sLine .. "\n" )
  45. end
  46. file:close()
  47. return true
  48. end
  49. return false
  50. end
  51.  
  52. local function redrawText()
  53. for y=1,h-1 do
  54. term.setCursorPos( 1 - scrollX, y )
  55. term.clearLine()
  56.  
  57. local sLine = tLines[ y + scrollY ]
  58. if sLine ~= nil then
  59. term.write( sLine )
  60. end
  61. end
  62. term.setCursorPos( x - scrollX, y - scrollY )
  63. end
  64.  
  65. local function redrawLine()
  66. local sLine = tLines[y]
  67. term.setCursorPos( 1 - scrollX, y - scrollY )
  68. term.clearLine()
  69. term.write( sLine )
  70. term.setCursorPos( x - scrollX, y - scrollY )
  71. end
  72.  
  73. local tMenuItems = { "Save", "Exit" }
  74.  
  75. local function redrawMenu()
  76. term.setCursorPos( 1, h )
  77. term.clearLine()
  78.  
  79. if not bMenu then
  80. term.write( sStatus )
  81. else
  82. for n,sItem in ipairs( tMenuItems ) do
  83. if n == nMenuItem then
  84. term.write( "["..sItem.."]" )
  85. else
  86. term.write( " "..sItem.." " )
  87. end
  88. end
  89. end
  90.  
  91. term.setCursorPos( x - scrollX, y - scrollY )
  92. end
  93.  
  94. local function setStatus( _sText )
  95. sStatus = _sText
  96. redrawMenu()
  97. end
  98.  
  99. local function setCursor( x, y )
  100. local screenX = x - scrollX
  101. local screenY = y - scrollY
  102.  
  103. local bRedraw = false
  104. if screenX < 1 then
  105. scrollX = x - 1
  106. screenX = 1
  107. bRedraw = true
  108. elseif screenX > w then
  109. scrollX = x - w
  110. screenX = w
  111. bRedraw = true
  112. end
  113.  
  114. if screenY < 1 then
  115. scrollY = y - 1
  116. screenY = 1
  117. bRedraw = true
  118. elseif screenY > h-1 then
  119. scrollY = y - (h-1)
  120. screenY = h-1
  121. bRedraw = true
  122. end
  123.  
  124. if bRedraw then
  125. redrawText()
  126. end
  127. term.setCursorPos( screenX, screenY )
  128. end
  129.  
  130. load()
  131.  
  132. term.clear()
  133. term.setCursorPos(x,y)
  134. term.setCursorBlink( true )
  135.  
  136. redrawText()
  137. setStatus( "Press Ctrl to access menu" )
  138.  
  139. local bRunning = true
  140. local tMenuFunctions = {
  141. ["Exit"] = function()
  142. bRunning = false
  143. end,
  144. ["Save"] = function()
  145. if save() then
  146. setStatus( "Saved to "..sPath )
  147. else
  148. setStatus( "Access denied" )
  149. end
  150. end,
  151. }
  152.  
  153. local function doMenuItem( _n )
  154. local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
  155. if fnMenu then
  156. fnMenu()
  157. end
  158.  
  159. bMenu = false
  160. term.setCursorBlink( true )
  161. redrawMenu()
  162. end
  163.  
  164. while bRunning do
  165. local sEvent, param = os.pullEvent()
  166. if sEvent == "key" then
  167. if param == 200 then
  168. -- Up
  169. if not bMenu then
  170. -- Move cursor up
  171. if y > 1 then
  172. y = y - 1
  173. x = math.min( x, string.len( tLines[y] ) + 1 )
  174. setCursor( x, y )
  175. end
  176. end
  177.  
  178. elseif param == 208 then
  179. -- Down
  180. if not bMenu then
  181. -- Move cursor down
  182. if y < #tLines then
  183. y = y + 1
  184. x = math.min( x, string.len( tLines[y] ) + 1 )
  185. setCursor( x, y )
  186. end
  187. end
  188.  
  189. elseif param == 203 then
  190. -- Left
  191. if not bMenu then
  192. -- Move cursor left
  193. if x > 1 then
  194. x = x - 1
  195. setCursor( x, y )
  196. end
  197. else
  198. -- Move menu left
  199. nMenuItem = nMenuItem - 1
  200. if nMenuItem < 1 then
  201. nMenuItem = #tMenuItems
  202. end
  203. redrawMenu()
  204. end
  205.  
  206.  
  207. elseif param == 205 then
  208. -- Right
  209. if not bMenu then
  210. -- Move cursor right
  211. if x < string.len( tLines[y] ) + 1 then
  212. x = x + 1
  213. setCursor( x, y )
  214. end
  215. else
  216. -- Move menu right
  217. nMenuItem = nMenuItem + 1
  218. if nMenuItem > #tMenuItems then
  219. nMenuItem = 1
  220. end
  221. redrawMenu()
  222. end
  223.  
  224. elseif param == 14 then
  225. -- Backspace
  226. if not bMenu then
  227. if x > 1 then
  228. -- Remove character
  229. local sLine = tLines[y]
  230. tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  231. redrawLine()
  232.  
  233. x = x - 1
  234. setCursor( x, y )
  235.  
  236. elseif y > 1 then
  237. -- Remove newline
  238. local sPrevLen = string.len( tLines[y-1] )
  239. tLines[y-1] = tLines[y-1] .. tLines[y]
  240. table.remove( tLines, y )
  241. redrawText()
  242.  
  243. x = sPrevLen + 1
  244. y = y - 1
  245. setCursor( x, y )
  246. end
  247. end
  248.  
  249. elseif param == 28 then
  250. -- Enter
  251. if not bMenu then
  252. -- Newline
  253. local sLine = tLines[y]
  254. tLines[y] = string.sub(sLine,1,x-1)
  255. table.insert( tLines, y+1, string.sub(sLine,x) )
  256. redrawText()
  257.  
  258. x = 1
  259. y = y + 1
  260. setCursor( x, y )
  261.  
  262. else
  263. -- Menu selection
  264. doMenuItem( nMenuItem )
  265. end
  266.  
  267. elseif param == 29 then
  268. -- Menu toggle
  269. bMenu = not bMenu
  270. if bMenu then
  271. term.setCursorBlink( false )
  272. nMenuItem = 1
  273. else
  274. term.setCursorBlink( true )
  275. end
  276. redrawMenu()
  277.  
  278. elseif param == 15 then
  279. --Tab
  280. if not bMenu then
  281. -- Input text
  282. local sLine = tLines[y]
  283. tLines[y] = string.sub(sLine,1,x-1) .. " " .. string.sub(sLine,x)
  284. redrawLine()
  285.  
  286. x = x + string.len( param )
  287. setCursor( x, y )
  288. end
  289.  
  290. end
  291.  
  292. elseif sEvent == "char" then
  293. if not bMenu then
  294. -- Input text
  295. local sLine = tLines[y]
  296. tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  297. redrawLine()
  298.  
  299. x = x + string.len( param )
  300. setCursor( x, y )
  301.  
  302. else
  303. -- Select menu items
  304. for n,sMenuItem in ipairs( tMenuItems ) do
  305. if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  306. doMenuItem( n )
  307. break
  308. end
  309. end
  310. end
  311. end
  312. end
  313.  
  314. term.clear()
  315. term.setCursorBlink( false )
  316. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment