Class: NattyUI::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/natty-ui/theme.rb

Overview

TODO:

This chapter needs more documentation.

A theme defines the style of elements.

Defined Under Namespace

Classes: Builder

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme) ⇒ Theme

Returns a new instance of Theme.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/natty-ui/theme.rb', line 221

def initialize(theme)
  @heading = create_heading(theme.heading, theme.heading_sytle).freeze
  @border = create_border(theme.border).freeze
  @mark = create_mark(theme.mark).freeze
  @task_style = as_style(theme.task_style)
  @choice_current_style = as_style(theme.choice_current_style)
  @choice_style = as_style(theme.choice_style)
  @sh_out_style = as_style(theme.sh_out_style)
  @sh_err_style = as_style(theme.sh_err_style)
  @sections =
    create_sections(
      SectionBorder.create(border(theme.section_border)),
      theme.section_styles.dup.compare_by_identity
    )
  @option_states = create_option_states
end

Class Attribute Details

.currentTheme (readonly)

Currently used theme

Returns:



15
16
17
# File 'lib/natty-ui/theme.rb', line 15

def current
  @current
end

.current_nameSymbol (readonly)

Name of currently used theme

Returns:

  • (Symbol)


20
21
22
# File 'lib/natty-ui/theme.rb', line 20

def current_name
  @current_name
end

.namesArray<Symbol> (readonly)

Names of all registered themes

Returns:

  • (Array<Symbol>)


26
# File 'lib/natty-ui/theme.rb', line 26

def names = @ll.keys.sort!

Instance Attribute Details

#choice_current_styleObject (readonly)

Returns the value of attribute choice_current_style.



187
188
189
# File 'lib/natty-ui/theme.rb', line 187

def choice_current_style
  @choice_current_style
end

#choice_styleObject (readonly)

Returns the value of attribute choice_style.



187
188
189
# File 'lib/natty-ui/theme.rb', line 187

def choice_style
  @choice_style
end

#option_statesObject (readonly)

Returns the value of attribute option_states.



187
188
189
# File 'lib/natty-ui/theme.rb', line 187

def option_states
  @option_states
end

#sh_err_styleObject (readonly)

Returns the value of attribute sh_err_style.



187
188
189
# File 'lib/natty-ui/theme.rb', line 187

def sh_err_style
  @sh_err_style
end

#sh_out_styleObject (readonly)

Returns the value of attribute sh_out_style.



187
188
189
# File 'lib/natty-ui/theme.rb', line 187

def sh_out_style
  @sh_out_style
end

#task_styleObject (readonly)

Returns the value of attribute task_style.



187
188
189
# File 'lib/natty-ui/theme.rb', line 187

def task_style
  @task_style
end

Class Method Details

.description(name) ⇒ String

Get the descrition of a theme.

Parameters:

  • name (Symbol)

Returns:

  • (String)

    description of the theme



46
# File 'lib/natty-ui/theme.rb', line 46

def description(name) = find(name).first

.register(name, description = nil) {|builder| ... } ⇒ Theme

Register a theme

Parameters:

  • name (Symbol)

    theme name

  • description (#to_s) (defaults to: nil)

    theme description

Yield Parameters:

  • builder (Builder)

    theme build helper

Returns:

Raises:

  • (ArgumentError)


54
55
56
57
58
# File 'lib/natty-ui/theme.rb', line 54

def register(name, description = nil, &block)
  raise(ArgumentError, 'block missing') unless block
  @ll[name.to_sym] = [description&.to_s || name.to_s.capitalize, block]
  self
end

.use(name) ⇒ Symbol

Use a theme

Parameters:

  • name (Symbol)

Returns:

  • (Symbol)

    name of used theme



32
33
34
35
36
37
38
39
40
# File 'lib/natty-ui/theme.rb', line 32

def use(name)
  sel = find(name).last
  if sel.is_a?(Proc)
    sel[builder = Builder.new]
    sel = @ll[name][-1] = builder.build.freeze
  end
  @current = sel
  @current_name = name
end

Instance Method Details

#border(value) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/natty-ui/theme.rb', line 203

def border(value)
  return @border[value] if value.is_a?(Symbol)
  case Text.width(value = value.to_s, bbcode: false)
  when 1
    "#{value * 11}  "
  when 11
    "#{value}  "
  when 13
    value
  else
    @border[:default]
  end
end

#defined_bordersObject



195
# File 'lib/natty-ui/theme.rb', line 195

def defined_borders = @border.keys.sort!

#defined_marksObject



194
# File 'lib/natty-ui/theme.rb', line 194

def defined_marks = @mark.keys.sort!

#heading(index) ⇒ Object



196
# File 'lib/natty-ui/theme.rb', line 196

def heading(index) = @heading[index.to_i.clamp(1, 6) - 1]

#mark(value) ⇒ Object



198
199
200
201
# File 'lib/natty-ui/theme.rb', line 198

def mark(value)
  return @mark[value] if value.is_a?(Symbol)
  (element = Str.new(value, true)).empty? ? @mark[:default] : element
end

#section_border(kind) ⇒ Object



217
218
219
# File 'lib/natty-ui/theme.rb', line 217

def section_border(kind)
  kind.is_a?(Symbol) ? @sections[kind] : @sections[:default]
end