Class: TuiTui::Rect
- Inherits:
-
Data
- Object
- Data
- TuiTui::Rect
- Defined in:
- lib/tui_tui/rect.rb
Overview
A 1-origin screen rectangle with pure layout helpers.
Instance Attribute Summary collapse
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Class Method Summary collapse
Instance Method Summary collapse
- #shift_right(by) ⇒ Object
-
#split_gutter(width = 1) ⇒ Object
Carve ‘width` columns off the right edge for a scrollbar gutter.
- #split_h(top_rows) ⇒ Object
-
#split_ratio(ratio, min: 0, gutter: 0) ⇒ Object
Split into [left, right] by ‘ratio` of the width.
- #split_v(left_cols) ⇒ Object
Instance Attribute Details
#col ⇒ Object (readonly)
Returns the value of attribute col
5 6 7 |
# File 'lib/tui_tui/rect.rb', line 5 def col @col end |
#cols ⇒ Object (readonly)
Returns the value of attribute cols
5 6 7 |
# File 'lib/tui_tui/rect.rb', line 5 def cols @cols end |
#row ⇒ Object (readonly)
Returns the value of attribute row
5 6 7 |
# File 'lib/tui_tui/rect.rb', line 5 def row @row end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows
5 6 7 |
# File 'lib/tui_tui/rect.rb', line 5 def rows @rows end |
Class Method Details
.centered(within, cols:, rows:) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/tui_tui/rect.rb', line 6 def self.centered(within, cols:, rows:) new( row: [((within.rows - rows) / 2) + 1, 1].max, col: [((within.cols - cols) / 2) + 1, 1].max, rows: rows, cols: cols ) end |
Instance Method Details
#shift_right(by) ⇒ Object
36 37 38 |
# File 'lib/tui_tui/rect.rb', line 36 def shift_right(by) Rect.new(row: row, col: col + by, rows: rows, cols: cols - by) end |
#split_gutter(width = 1) ⇒ Object
Carve ‘width` columns off the right edge for a scrollbar gutter. Returns [body, gutter]; gutter is nil when the rect is too narrow to spare them.
42 43 44 45 46 |
# File 'lib/tui_tui/rect.rb', line 42 def split_gutter(width = 1) return [self, nil] if cols <= width [with(cols: cols - width), Rect.new(row: row, col: col + cols - width, rows: rows, cols: width)] end |
#split_h(top_rows) ⇒ Object
15 16 17 18 19 |
# File 'lib/tui_tui/rect.rb', line 15 def split_h(top_rows) top = Rect.new(row: row, col: col, rows: top_rows, cols: cols) bottom = Rect.new(row: row + top_rows, col: col, rows: rows - top_rows, cols: cols) [top, bottom] end |
#split_ratio(ratio, min: 0, gutter: 0) ⇒ Object
Split into [left, right] by ‘ratio` of the width
28 29 30 31 32 33 34 |
# File 'lib/tui_tui/rect.rb', line 28 def split_ratio(ratio, min: 0, gutter: 0) lo = min hi = cols - min - gutter left_cols = hi < lo ? cols / 2 : (cols * ratio).round.clamp(lo, hi) left, right = split_v(left_cols) [left, right.shift_right(gutter)] end |