Class: RatatuiRuby::Layout::Position
- Inherits:
-
Object
- Object
- RatatuiRuby::Layout::Position
- Defined in:
- lib/ratatui_ruby/layout/position.rb
Overview
A position in terminal coordinates.
Layout code passes x/y pairs between functions. Bundling them into separate variables is verbose and prone to ordering mistakes.
This class wraps column and row into a single immutable object. Pass it around, destructure it, or convert from a Rect.
Use it for cursor positioning, mouse coordinates, or anywhere you need to represent a single point on the terminal grid.
Example
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++
pos = Layout::Position.new(x: 10, y: 5)
puts "Cursor at column #{pos.x}, row #{pos.y}"
# Extract from a Rect
rect = Layout::Rect.new(x: 10, y: 5, width: 80, height: 24)
pos = rect.as_position # => Position(x: 10, y: 5)
– SPDX-SnippetEnd ++
Instance Method Summary collapse
-
#deconstruct ⇒ Object
(also: #to_ary)
Enables array destructuring for convenient coordinate extraction.
-
#initialize(x: 0, y: 0) ⇒ Position
constructor
Creates a new Position.
Constructor Details
#initialize(x: 0, y: 0) ⇒ Position
Creates a new Position.
- x
-
Column index (Integer, coerced via Integer()).
- y
-
Row index (Integer, coerced via Integer()).
50 51 52 |
# File 'lib/ratatui_ruby/layout/position.rb', line 50 def initialize(x: 0, y: 0) super(x: Integer(x), y: Integer(y)) end |
Instance Method Details
#deconstruct ⇒ Object Also known as: to_ary
Enables array destructuring for convenient coordinate extraction.
- Returns
-
Array of [x, y] coordinates.
Example
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2026 Kerrick Long SPDX-License-Identifier: MIT-0 ++
pos = Position.new(x: 10, y: 5)
x, y = pos # Uses deconstruct
puts "Column: #{x}, Row: #{y}"
– SPDX-SnippetEnd ++
71 72 73 |
# File 'lib/ratatui_ruby/layout/position.rb', line 71 def deconstruct [x, y] end |