Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. local m = component.modem -- get primary modem component
  2. m.open(321)
  3. print(m.isOpen(321)) -- true
  4. m.setStrength(500)
  5. -- Send some message.
  6.  
  7. if not term.isAvailable() then
  8. return
  9. end
  10.  
  11. local args, options = shell.parse(...)
  12.  
  13. args[1] = "Menu.txt"
  14.  
  15. local filename = shell.resolve(args[1])
  16.  
  17. local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly()
  18.  
  19. if fs.isDirectory(filename) or readonly and not fs.exists(filename) then
  20. print("file not found")
  21. return
  22. end
  23.  
  24. term.clear()
  25. term.setCursorBlink(true)
  26.  
  27. local running = true
  28. local buffer = {}
  29. local scrollX, scrollY = 0, 0
  30. -------------------------------------------------------------------------------
  31.  
  32. local function setStatus(value)
  33. local w, h = component.gpu.getResolution()
  34. component.gpu.set(1, h, text.padRight(unicode.sub(value, 1, w - 10), w - 10))
  35. end
  36.  
  37. local function getSize()
  38. local w, h = component.gpu.getResolution()
  39. return w, h - 1
  40. end
  41.  
  42. local function getCursor()
  43. local cx, cy = term.getCursor()
  44. return cx + scrollX, cy + scrollY
  45. end
  46.  
  47. local function line()
  48. local cbx, cby = getCursor()
  49. return buffer[cby]
  50. end
  51.  
  52. local function setCursor(nbx, nby)
  53. local w, h = getSize()
  54. nby = math.max(1, math.min(#buffer, nby))
  55.  
  56. local ncy = nby - scrollY
  57. if ncy > h then
  58. term.setCursorBlink(false)
  59. local sy = nby - h
  60. local dy = math.abs(scrollY - sy)
  61. scrollY = sy
  62. component.gpu.copy(1, 1 + dy, w, h - dy, 0, -dy)
  63. for by = nby - (dy - 1), nby do
  64. local str = text.padRight(unicode.sub(buffer[by], 1 + scrollX), w)
  65. component.gpu.set(1, by - scrollY, str)
  66. end
  67. elseif ncy < 1 then
  68. term.setCursorBlink(false)
  69. local sy = nby - 1
  70. local dy = math.abs(scrollY - sy)
  71. scrollY = sy
  72. component.gpu.copy(1, 1, w, h - dy, 0, dy)
  73. for by = nby, nby + (dy - 1) do
  74. local str = text.padRight(unicode.sub(buffer[by], 1 + scrollX), w)
  75. component.gpu.set(1, by - scrollY, str)
  76. end
  77. end
  78. term.setCursor(term.getCursor(), nby - scrollY)
  79.  
  80. nbx = math.max(1, math.min(unicode.len(line()) + 1, nbx))
  81. local ncx = nbx - scrollX
  82. if ncx > w then
  83. term.setCursorBlink(false)
  84. local sx = nbx - w
  85. local dx = math.abs(scrollX - sx)
  86. scrollX = sx
  87. component.gpu.copy(1 + dx, 1, w - dx, h, -dx, 0)
  88. for by = 1 + scrollY, math.min(h + scrollY, #buffer) do
  89. local str = unicode.sub(buffer[by], nbx - (dx - 1), nbx)
  90. str = text.padRight(str, dx)
  91. component.gpu.set(1 + (w - dx), by - scrollY, str)
  92. end
  93. elseif ncx < 1 then
  94. term.setCursorBlink(false)
  95. local sx = nbx - 1
  96. local dx = math.abs(scrollX - sx)
  97. scrollX = sx
  98. component.gpu.copy(1, 1, w - dx, h, dx, 0)
  99. for by = 1 + scrollY, math.min(h + scrollY, #buffer) do
  100. local str = unicode.sub(buffer[by], nbx, nbx + dx)
  101. --str = text.padRight(str, dx)
  102. component.gpu.set(1, by - scrollY, str)
  103. end
  104. end
  105. term.setCursor(nbx - scrollX, nby - scrollY)
  106.  
  107. component.gpu.set(w - 9, h + 1, text.padLeft(string.format("%d,%d", nby, nbx), 10))
  108. end
  109.  
  110. local function up(n)
  111. n = n or 1
  112. local cbx, cby = getCursor()
  113. if cby > 1 then
  114. setCursor(cbx, cby - n)
  115. else
  116. cby = #buffer + 1
  117. setCursor(cbx, cby - n)
  118. end
  119. end
  120.  
  121. local function down(n)
  122. n = n or 1
  123. local cbx, cby = getCursor()
  124. if cby < #buffer then
  125. setCursor(cbx, cby + n)
  126. else
  127. cby = 0
  128. setCursor(cbx, cby + n)
  129. end
  130. end
  131.  
  132. function trim(s)
  133. return (s:gsub("^%s*(.-)%s*$", "%1"))
  134. end
  135.  
  136. local function enter()
  137. term.setCursorBlink(false)
  138. local cx, cy = term.getCursor()
  139. local cbx, cby = getCursor()
  140. local w, h = getSize()
  141. if cby == #buffer then
  142. running = false
  143. end
  144. m.broadcast(321, trim(buffer[cby]))
  145. setCursor(1, cby)
  146. end
  147.  
  148. local controlKeyCombos = {[keyboard.keys.s]=true,[keyboard.keys.w]=true,
  149. [keyboard.keys.c]=true,[keyboard.keys.x]=true}
  150. local function onKeyDown(char, code)
  151. if code == keyboard.keys.up then
  152. up()
  153. elseif code == keyboard.keys.down then
  154. down()
  155. elseif code == keyboard.keys.enter and not readonly then
  156. enter()
  157. end
  158. end
  159.  
  160. -------------------------------------------------------------------------------
  161.  
  162. do
  163. local f = io.open(filename)
  164. if f then
  165. local w, h = getSize()
  166. local chars = 0
  167. for line in f:lines() do
  168. table.insert(buffer, line)
  169. chars = chars + unicode.len(line)
  170. if #buffer <= h then
  171. component.gpu.set(1, #buffer, line)
  172. end
  173. end
  174. f:close()
  175. if #buffer == 0 then
  176. table.insert(buffer, "")
  177. end
  178. end
  179. setCursor(1, 1)
  180. end
  181.  
  182. while running do
  183. local event, address, arg1, arg2, arg3 = event.pull()
  184. if type(address) == "string" and component.isPrimary(address) then
  185. local blink = true
  186. if event == "key_down" then
  187. onKeyDown(arg1, arg2)
  188. else
  189. blink = false
  190. end
  191. if blink then
  192. term.setCursorBlink(true)
  193. term.setCursorBlink(true) -- force toggle to caret
  194. end
  195. end
  196. end
  197.  
  198. term.clear()
  199. term.setCursorBlink(false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement