Class: OKF::TUI::Ui::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/okf/tui/ui.rb

Overview

A single output row assembled segment by segment, each optionally styled. The builder tracks how many columns it has spent, so a segment that would overflow is clipped before it is coloured and the finished row always measures exactly limit columns.

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ Line

Returns a new instance of Line.



212
213
214
215
216
# File 'lib/okf/tui/ui.rb', line 212

def initialize(limit)
  @limit = [ limit, 0 ].max
  @spent = 0
  @buffer = +""
end

Instance Method Details

#add(text, *styles) ⇒ Object

Append text, styled with zero or more Pastel colour names.



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/okf/tui/ui.rb', line 219

def add(text, *styles)
  room = @limit - @spent
  return self if room <= 0

  piece = Ui.clip(text, room)
  return self if piece.empty?

  @spent += Ui.width(piece)
  @buffer << (styles.empty? ? piece : Ui.pastel.decorate(piece, *styles))
  self
end

#empty?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/okf/tui/ui.rb', line 241

def empty?
  @spent.zero?
end

#space(count = 1) ⇒ Object

Append count spaces (no-op past the edge).



232
233
234
# File 'lib/okf/tui/ui.rb', line 232

def space(count = 1)
  add(" " * count)
end

#to_sObject

Pad the rest of the row out to limit columns.



237
238
239
# File 'lib/okf/tui/ui.rb', line 237

def to_s
  @buffer + (" " * (@limit - @spent))
end