Class: TuiTui::Rect

Inherits:
Data
  • Object
show all
Defined in:
lib/tui_tui/rect.rb

Overview

A 1-origin screen rectangle with pure layout helpers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col

Returns:

  • (Object)

    the current value of col



5
6
7
# File 'lib/tui_tui/rect.rb', line 5

def col
  @col
end

#colsObject (readonly)

Returns the value of attribute cols

Returns:

  • (Object)

    the current value of cols



5
6
7
# File 'lib/tui_tui/rect.rb', line 5

def cols
  @cols
end

#rowObject (readonly)

Returns the value of attribute row

Returns:

  • (Object)

    the current value of row



5
6
7
# File 'lib/tui_tui/rect.rb', line 5

def row
  @row
end

#rowsObject (readonly)

Returns the value of attribute rows

Returns:

  • (Object)

    the current value of 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

#split_v(left_cols) ⇒ Object



21
22
23
24
25
# File 'lib/tui_tui/rect.rb', line 21

def split_v(left_cols)
  left = Rect.new(row: row, col: col, rows: rows, cols: left_cols)
  right = Rect.new(row: row, col: col + left_cols, rows: rows, cols: cols - left_cols)
  [left, right]
end