Class: RubyTUI::Widgets::InputField
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::InputField
- Includes:
- StatefulWidget
- Defined in:
- lib/rubytui/widgets/input_field.rb
Overview
InputField renders a single-line text input with cursor.
Instance Method Summary collapse
-
#initialize(style: Style::DEFAULT, cursor_style: nil, placeholder: "", placeholder_style: nil, block: nil) ⇒ InputField
constructor
A new instance of InputField.
-
#render(area, buf, state) ⇒ void
Render the visible part of the text, or the placeholder, on the first row of the area and apply
cursor_styleto the cell at the cursor.
Constructor Details
#initialize(style: Style::DEFAULT, cursor_style: nil, placeholder: "", placeholder_style: nil, block: nil) ⇒ InputField
Returns a new instance of InputField.
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/rubytui/widgets/input_field.rb', line 179 def initialize( style: Style::DEFAULT, cursor_style: nil, placeholder: "", placeholder_style: nil, block: nil ) @style = style @cursor_style = cursor_style || Style.new.reversed @placeholder = placeholder @placeholder_style = placeholder_style || Style.new.fg(Color::BRIGHT_BLACK) @block = block end |
Instance Method Details
#render(area, buf, state) ⇒ void
This method returns an undefined value.
Render the visible part of the text, or the placeholder, on the first row of
the area and apply cursor_style to the cell at the cursor.
Updates state.scroll_offset in place so the cursor stays visible. Scrolling and cursor placement count characters, not display columns.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rubytui/widgets/input_field.rb', line 203 def render(area, buf, state) return if area.empty? render_area = area if @block @block.render(area, buf) render_area = @block.inner(area) end return if render_area.empty? width = render_area.width # Adjust scroll offset to keep cursor visible if state.cursor < state.scroll_offset state.scroll_offset = state.cursor elsif state.cursor >= state.scroll_offset + width state.scroll_offset = state.cursor - width + 1 end if state.text.empty? # Show placeholder visible = @placeholder[0, width] buf.set_string(render_area.x, render_area.y, visible, @placeholder_style) else # Show text with scrolling visible = state.text[state.scroll_offset, width] || "" buf.set_string(render_area.x, render_area.y, visible, @style) end # Draw cursor cursor_screen_x = render_area.x + state.cursor - state.scroll_offset if cursor_screen_x >= render_area.x && cursor_screen_x < render_area.right cell = buf[cursor_screen_x, render_area.y] if cell char = cell.symbol == " " && state.cursor >= state.text.length ? " " : cell.symbol cell.set(char, @cursor_style) end end end |