Class: RubyText::Window::GetString
- Inherits:
-
Object
- Object
- RubyText::Window::GetString
- Defined in:
- lib/output.rb
Instance Method Summary collapse
- #add(ch) ⇒ Object
- #backspace ⇒ Object
- #enter ⇒ Object
- #history_next ⇒ Object
- #history_prev ⇒ Object
-
#initialize(win = STDSCR, str = "", i = 0, history: [], limit: nil) ⇒ GetString
constructor
A new instance of GetString.
- #left_arrow ⇒ Object
- #right_arrow ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(win = STDSCR, str = "", i = 0, history: [], limit: nil) ⇒ GetString
Returns a new instance of GetString.
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/output.rb', line 142 def initialize(win = STDSCR, str = "", i = 0, history: [], limit: nil) @win = win @r0, @c0 = @win.rc @limit = limit || (@win.cols - @r0 - 1) raise ArgumentError unless @limit.is_a?(Numeric) @str, @i = str[0..(@limit-1)], i @win.print @str @win.left @str.length @history = history @h = @history.length - 1 @maxlen = 0 # longest string in history end |
Instance Method Details
#add(ch) ⇒ Object
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/output.rb', line 208 def add(ch) if @str.length >= @limit Curses.beep return end @str.insert(@i, ch) @win.right @win.go(@r0, @c0) { @win.print @str } @i += 1 end |
#backspace ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/output.rb', line 175 def backspace # remember: may be in middle of string return if @i == 0 @i -= 1 @str[@i] = "" @win.left @win.rcprint @r0, @c0, @str + " " end |
#enter ⇒ Object
155 156 157 158 159 |
# File 'lib/output.rb', line 155 def enter @win.crlf @history << @str @h = @history.length - 1 end |
#history_next ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/output.rb', line 196 def history_next return if @history.empty? @h = (@h + 1) % @history.length @win.go @r0, @c0 @maxlen = @history.map(&:length).max @win.print(" "*@maxlen) @str = @history[@h] @i = @str.length @win.go @r0, @c0 @win.print @str end |
#history_prev ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/output.rb', line 184 def history_prev return if @history.empty? @win.go @r0, @c0 @maxlen = @history.map(&:length).max @win.print(" "*@maxlen) @h = (@h - 1) % @history.length @str = @history[@h] @i = @str.length @win.go @r0, @c0 @win.print @str end |
#left_arrow ⇒ Object
161 162 163 164 165 166 |
# File 'lib/output.rb', line 161 def left_arrow if @i > 0 @i -= 1 @win.left end end |
#right_arrow ⇒ Object
168 169 170 171 172 173 |
# File 'lib/output.rb', line 168 def right_arrow if @i < @str.length @i += 1 @win.right end end |
#value ⇒ Object
219 220 221 |
# File 'lib/output.rb', line 219 def value @str end |