xXm0dzXx

Untitled

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