fishermedders

hihihi

Jan 28th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. function read( _sReplaceChar, _tHistory, _fnComplete )
  2. term.setCursorBlink( true )
  3.  
  4. local sLine = ""
  5. local nHistoryPos
  6. local nPos = 0
  7. if _sReplaceChar then
  8. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  9. end
  10.  
  11. local tCompletions
  12. local nCompletion
  13. local function recomplete()
  14. if _fnComplete and nPos == string.len(sLine) then
  15. tCompletions = _fnComplete( sLine )
  16. if tCompletions and #tCompletions > 0 then
  17. nCompletion = 1
  18. else
  19. nCompletion = nil
  20. end
  21. else
  22. tCompletions = nil
  23. nCompletion = nil
  24. end
  25. end
  26.  
  27. local function uncomplete()
  28. tCompletions = nil
  29. nCompletion = nil
  30. end
  31.  
  32. local w = term.getSize()
  33. local sx = term.getCursorPos()
  34.  
  35. local function redraw( _bClear )
  36. local nScroll = 0
  37. if sx + nPos >= w then
  38. nScroll = (sx + nPos) - w
  39. end
  40.  
  41. local cx,cy = term.getCursorPos()
  42. term.setCursorPos( sx, cy )
  43. local sReplace = (_bClear and " ") or _sReplaceChar
  44. if sReplace then
  45. term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  46. else
  47. term.write( string.sub( sLine, nScroll + 1 ) )
  48. end
  49.  
  50. if nCompletion then
  51. local sCompletion = tCompletions[ nCompletion ]
  52. local oldText, oldBg
  53. if not _bClear then
  54. oldText = term.getTextColor()
  55. oldBg = term.getBackgroundColor()
  56. term.setTextColor( colors.white )
  57. term.setBackgroundColor( colors.gray )
  58. end
  59. if sReplace then
  60. term.write( string.rep( sReplace, string.len( sCompletion ) ) )
  61. else
  62. term.write( sCompletion )
  63. end
  64. if not _bClear then
  65. term.setTextColor( oldText )
  66. term.setBackgroundColor( oldBg )
  67. end
  68. end
  69.  
  70. term.setCursorPos( sx + nPos - nScroll, cy )
  71. end
  72.  
  73. local function clear()
  74. redraw( true )
  75. end
  76.  
  77. recomplete()
  78. redraw()
  79.  
  80. local function acceptCompletion()
  81. if nCompletion then
  82. -- Clear
  83. clear()
  84.  
  85. -- Find the common prefix of all the other suggestions which start with the same letter as the current one
  86. local sCompletion = tCompletions[ nCompletion ]
  87. sLine = sLine .. sCompletion
  88. nPos = string.len( sLine )
  89.  
  90. -- Redraw
  91. recomplete()
  92. redraw()
  93. end
  94. end
  95. while true do
  96. local sEvent, param = os.pullEvent()
  97. if sEvent == "char" then
  98. -- Typed key
  99. clear()
  100. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  101. nPos = nPos + 1
  102. recomplete()
  103. redraw()
  104.  
  105. elseif sEvent == "paste" then
  106. -- Pasted text
  107. clear()
  108. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  109. nPos = nPos + string.len( param )
  110. recomplete()
  111. redraw()
  112.  
  113. elseif sEvent == "key" then
  114. if param == keys.enter then
  115. -- Enter
  116. if nCompletion then
  117. clear()
  118. uncomplete()
  119. redraw()
  120. end
  121. break
  122.  
  123. elseif param == keys.left then
  124. -- Left
  125. if nPos > 0 then
  126. clear()
  127. nPos = nPos - 1
  128. recomplete()
  129. redraw()
  130. end
  131.  
  132. elseif param == keys.right then
  133. -- Right
  134. if nPos < string.len(sLine) then
  135. -- Move right
  136. clear()
  137. nPos = nPos + 1
  138. recomplete()
  139. redraw()
  140. else
  141. -- Accept autocomplete
  142. acceptCompletion()
  143. end
  144.  
  145. elseif param == keys.up or param == keys.down then
  146. -- Up or down
  147. if nCompletion then
  148. -- Cycle completions
  149. clear()
  150. if param == keys.up then
  151. nCompletion = nCompletion - 1
  152. if nCompletion < 1 then
  153. nCompletion = #tCompletions
  154. end
  155. elseif param == keys.down then
  156. nCompletion = nCompletion + 1
  157. if nCompletion > #tCompletions then
  158. nCompletion = 1
  159. end
  160. end
  161. redraw()
  162.  
  163. elseif _tHistory then
  164. -- Cycle history
  165. clear()
  166. if param == keys.up then
  167. -- Up
  168. if nHistoryPos == nil then
  169. if #_tHistory > 0 then
  170. nHistoryPos = #_tHistory
  171. end
  172. elseif nHistoryPos > 1 then
  173. nHistoryPos = nHistoryPos - 1
  174. end
  175. else
  176. -- Down
  177. if nHistoryPos == #_tHistory then
  178. nHistoryPos = nil
  179. elseif nHistoryPos ~= nil then
  180. nHistoryPos = nHistoryPos + 1
  181. end
  182. end
  183. if nHistoryPos then
  184. sLine = _tHistory[nHistoryPos]
  185. nPos = string.len( sLine )
  186. else
  187. sLine = ""
  188. nPos = 0
  189. end
  190. uncomplete()
  191. redraw()
  192.  
  193. end
  194.  
  195. elseif param == keys.backspace then
  196. -- Backspace
  197. if nPos > 0 then
  198. clear()
  199. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  200. nPos = nPos - 1
  201. recomplete()
  202. redraw()
  203. end
  204.  
  205. elseif param == keys.home then
  206. -- Home
  207. if nPos > 0 then
  208. clear()
  209. nPos = 0
  210. recomplete()
  211. redraw()
  212. end
  213.  
  214. elseif param == keys.delete then
  215. -- Delete
  216. if nPos < string.len(sLine) then
  217. clear()
  218. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  219. recomplete()
  220. redraw()
  221. end
  222.  
  223. elseif param == keys["end"] then
  224. -- End
  225. if nPos < string.len(sLine ) then
  226. clear()
  227. nPos = string.len(sLine)
  228. recomplete()
  229. redraw()
  230. end
  231.  
  232. elseif param == keys.tab then
  233. -- Tab (accept autocomplete)
  234. acceptCompletion()
  235.  
  236. end
  237.  
  238. elseif sEvent == "term_resize" then
  239. -- Terminal resized
  240. w = term.getSize()
  241. redraw()
  242.  
  243. end
  244. end
  245.  
  246. local cx, cy = term.getCursorPos()
  247. term.setCursorBlink( false )
  248. term.setCursorPos( w + 1, cy )
  249. print()
  250.  
  251. return sLine
  252. end
Advertisement
Add Comment
Please, Sign In to add comment