Class: Potty::Widgets::Label

Inherits:
Base
  • Object
show all
Defined in:
lib/potty/widgets/label.rb

Overview

Static, non-focusable text — form field labels, headings, captions. Single line, truncated to the rect width. Color is a theme name; ‘bold:` ORs in A_BOLD via the theme.

Instance Attribute Summary collapse

Attributes inherited from Base

#app, #focused, #parent, #rect

Instance Method Summary collapse

Methods inherited from Base

#activate, #blur, #deactivate, #focus, #handle_escape, #handle_key, #hide, #layout, #on_blur, #on_focus, #on_layout, #show, #theme, #tick, #visible=, #visible?

Methods included from Events

#emit, #listeners?, #off, #on

Constructor Details

#initialize(app, text: '', color: :normal, bold: false) ⇒ Label

Returns a new instance of Label.



13
14
15
16
17
18
# File 'lib/potty/widgets/label.rb', line 13

def initialize(app, text: '', color: :normal, bold: false)
  super(app)
  @text = text
  @color = color
  @bold = bold
end

Instance Attribute Details

#boldObject

Returns the value of attribute bold.



11
12
13
# File 'lib/potty/widgets/label.rb', line 11

def bold
  @bold
end

#colorObject

Returns the value of attribute color.



11
12
13
# File 'lib/potty/widgets/label.rb', line 11

def color
  @color
end

#textObject

Returns the value of attribute text.



11
12
13
# File 'lib/potty/widgets/label.rb', line 11

def text
  @text
end

Instance Method Details

#can_focus?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/potty/widgets/label.rb', line 20

def can_focus?
  false
end

#preferred_height(_width) ⇒ Object



24
25
26
# File 'lib/potty/widgets/label.rb', line 24

def preferred_height(_width)
  1
end

#render(window) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/potty/widgets/label.rb', line 28

def render(window)
  return unless @visible && @rect

  attr = @bold ? theme.attr(@color, bold: true) : theme[@color]
  window.setpos(@rect.y, @rect.x)
  window.attron(attr) { window.addstr(@text.to_s[0, @rect.width] || '') }
end