smigger22

Untitled

Feb 20th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. function read( _sReplaceChar, _tHistory )
  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 w = term.getSize()
  12. local sx = term.getCursorPos()
  13.  
  14. local function redraw( _sCustomReplaceChar )
  15. local nScroll = 0
  16. if sx + nPos >= w then
  17. nScroll = (sx + nPos) - w
  18. end
  19.  
  20. local cx,cy = term.getCursorPos()
  21. term.setCursorPos( sx, cy )
  22. local sReplace = _sCustomReplaceChar or _sReplaceChar
  23. if sReplace then
  24. term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  25. else
  26. term.write( string.sub( sLine, nScroll + 1 ) )
  27. end
  28. term.setCursorPos( sx + nPos - nScroll, cy )
  29. end
  30.  
  31. while true do
  32. local sEvent, param = os.pullEvent()
  33. if sEvent == "char" then
  34. -- Typed key
  35. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  36. nPos = nPos + 1
  37. redraw()
  38.  
  39. elseif sEvent == "paste" then
  40. -- Pasted text
  41. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  42. nPos = nPos + string.len( param )
  43. redraw()
  44.  
  45. elseif sEvent == "key" then
  46. if param == keys.enter then
  47. -- Enter
  48. break
  49.  
  50. elseif param == keys.left then
  51. -- Left
  52. if nPos > 0 then
  53. nPos = nPos - 1
  54. redraw()
  55. end
  56.  
  57. elseif param == keys.right then
  58. -- Right
  59. if nPos < string.len(sLine) then
  60. redraw(" ")
  61. nPos = nPos + 1
  62. redraw()
  63. end
  64.  
  65. elseif param == keys.up or param == keys.down then
  66. -- Up or down
  67. if _tHistory then
  68. redraw(" ")
  69. if param == keys.up then
  70. -- Up
  71. if nHistoryPos == nil then
  72. if #_tHistory > 0 then
  73. nHistoryPos = #_tHistory
  74. end
  75. elseif nHistoryPos > 1 then
  76. nHistoryPos = nHistoryPos - 1
  77. end
  78. else
  79. -- Down
  80. if nHistoryPos == #_tHistory then
  81. nHistoryPos = nil
  82. elseif nHistoryPos ~= nil then
  83. nHistoryPos = nHistoryPos + 1
  84. end
  85. end
  86. if nHistoryPos then
  87. sLine = _tHistory[nHistoryPos]
  88. nPos = string.len( sLine )
  89. else
  90. sLine = ""
  91. nPos = 0
  92. end
  93. redraw()
  94. end
  95. elseif param == keys.backspace then
  96. -- Backspace
  97. if nPos > 0 then
  98. redraw(" ")
  99. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  100. nPos = nPos - 1
  101. redraw()
  102. end
  103. elseif param == keys.home then
  104. -- Home
  105. redraw(" ")
  106. nPos = 0
  107. redraw()
  108. elseif param == keys.delete then
  109. -- Delete
  110. if nPos < string.len(sLine) then
  111. redraw(" ")
  112. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  113. redraw()
  114. end
  115. elseif param == keys["end"] then
  116. -- End
  117. redraw(" ")
  118. nPos = string.len(sLine)
  119. redraw()
  120. end
  121.  
  122. elseif sEvent == "term_resize" then
  123. -- Terminal resized
  124. w = term.getSize()
  125. redraw()
  126.  
  127. end
  128. end
  129.  
  130. local cx, cy = term.getCursorPos()
  131. term.setCursorBlink( false )
  132. term.setCursorPos( w + 1, cy )
  133. print()
  134.  
  135. return sLine
  136. end
Advertisement
Add Comment
Please, Sign In to add comment