Class: Colours::Colours

Inherits:
Base
  • Object
show all
Defined in:
lib/colours/class/colours.rb

Overview

Colours::Colours

Constant Summary collapse

LEADING_PART =
#

LEADING_PART

We don’t include the “[” here.

#
"\033"
TRAILING_PART =
#

TRAILING_PART

#
"\033[0m"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#commandline_arguments?, #e, #extend_module_256_colours, #first_argument?, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(i = '') ⇒ Colours

#

initialize

#


50
51
52
53
54
55
56
# File 'lib/colours/class/colours.rb', line 50

def initialize(i = '')
  reset
  if i.is_a? Array
    i = i.join(' ').strip
  end
  @internal_hash[:raw_content] = i # Keep a reference-copy here.
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

Colours::Colours[]

#


359
360
361
# File 'lib/colours/class/colours.rb', line 359

def self.[](i = ARGV)
  new(i)
end

Instance Method Details

#+(i = '') ⇒ Object

#

+

#


281
282
283
# File 'lib/colours/class/colours.rb', line 281

def +(i = '')
  append(i)
end

#append(i) ⇒ Object

#

append

#


288
289
290
# File 'lib/colours/class/colours.rb', line 288

def append(i)
  "#{to_str}#{i.to_str}"
end

#background(i = :yellow) ⇒ Object Also known as: bg

#

background

#


256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/colours/class/colours.rb', line 256

def background(i = :yellow)
  i = i.to_s
  unless i.start_with? 'background_'
    i = i.dup if i.frozen?
    i.prepend('background_')
  end
  i = i.to_sym
  unless HASH_ANSI_COLOURS.has_key? i
    e 'Warning: the key '+i.to_s+' is not registered in the main Hash.'
  end
  i = HASH_ANSI_COLOURS[i].to_s
  set_use_this_background_colour(i)
  self
end

#brightObject Also known as: bold

#

bright

#


248
249
250
251
# File 'lib/colours/class/colours.rb', line 248

def bright
  @internal_hash[:leading_colour_component] = '1'
  self
end

#build_the_main_stringObject Also known as: to_str, to_s, build_main_string

#

build_the_main_string (main tag)

This method must always rebuild the full, modified content.

#


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/colours/class/colours.rb', line 297

def build_the_main_string
  result = ''.dup
  result << leading_part?
  result << '['
  result << string_increased_intensity?
  result << ";#{use_this_colour?}" unless use_this_colour? == '0'
  if use_this_background_colour?
    result << ";#{use_this_background_colour?}"
  end
  result << ';3' if use_italic?
  result << ';4' if use_underline?
  result << ';5' if slow_blink?
  result << ';7' if reversed?
  result << 'm'
  result << raw_content?
  result << trailing_end?
  return result
end

#displayObject Also known as: report

#

display

#


185
186
187
# File 'lib/colours/class/colours.rb', line 185

def display
  print build_main_string(content?)
end

#increased_intensity?Boolean

#

increased_intensity?

#

Returns:

  • (Boolean)


229
230
231
232
# File 'lib/colours/class/colours.rb', line 229

def increased_intensity?
  _ = @internal_hash[:leading_colour_component]
  return (_ and !_.empty?)
end

#italicObject

#

italic

#


344
345
346
347
# File 'lib/colours/class/colours.rb', line 344

def italic
  @internal_hash[:use_italic] = true
  self
end

#leading_part?Boolean

#

leading_part?

#

Returns:

  • (Boolean)


206
207
208
# File 'lib/colours/class/colours.rb', line 206

def leading_part?
  "#{LEADING_PART}"
end

#raw_content?Boolean Also known as: content?, string?

#

raw_content?

#

Returns:

  • (Boolean)


156
157
158
# File 'lib/colours/class/colours.rb', line 156

def raw_content?
  @internal_hash[:raw_content]
end

#redObject Also known as: blue, black, green, brown, yellow, purple, magenta, cyan, light_blue, light_gray, light_green, light_magenta, white, light_red, grey

#

red

#


321
322
323
324
325
# File 'lib/colours/class/colours.rb', line 321

def red
  real_name_of_the_method = HASH_ANSI_COLOURS[__callee__]
  set_use_this_colour(real_name_of_the_method)
  self
end

#resetObject

#

reset (reset tag)

#


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/colours/class/colours.rb', line 61

def reset
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  @internal_hash = {}
  # ======================================================================= #
  # === :raw_content
  # ======================================================================= #
  @internal_hash[:raw_content] = nil
  # ======================================================================= #
  # === :leading_colour_component
  #
  # This can be '', '0' or '1'. The '1' means bright; the '0' is for
  # regular colours. The default is ''.
  # ======================================================================= #
  @internal_hash[:leading_colour_component] = ''.dup
  # ======================================================================= #
  # === :use_italic
  # ======================================================================= #
  @internal_hash[:use_italic] = false
  # ======================================================================= #
  # === :use_underline
  # ======================================================================= #
  @internal_hash[:use_underline] = false
  # ======================================================================= #
  # === :slow_blink
  # ======================================================================= #
  @internal_hash[:slow_blink] = false
  # ======================================================================= #
  # === :use_this_background_colour
  # ======================================================================= #
  @internal_hash[:use_this_background_colour] = nil
  # ======================================================================= #
  # === :use_this_colour
  #
  # We must use a default colour.
  # ======================================================================= #
  @internal_hash[:use_this_colour] = '0'
  # ======================================================================= #
  # === :reversed
  #
  # If this is true then "7" will be used, which will invert foreground
  # colour and background colour.
  # ======================================================================= #
  @internal_hash[:reversed] = false
end

#reversedObject

#

reversed

#


134
135
136
137
# File 'lib/colours/class/colours.rb', line 134

def reversed
  @internal_hash[:reversed] = true
  self
end

#reversed?Boolean

#

reversed?

#

Returns:

  • (Boolean)


111
112
113
# File 'lib/colours/class/colours.rb', line 111

def reversed?
  @internal_hash[:reversed]
end

#set_content(i) ⇒ Object

#

set_content

#


192
193
194
# File 'lib/colours/class/colours.rb', line 192

def set_content(i)
  @internal_hash[:raw_content] = i.dup
end
#
#


149
150
151
# File 'lib/colours/class/colours.rb', line 149

def set_slow_blink_to_true
  @internal_hash[:slow_blink] = true
end

#set_use_this_background_colour(i) ⇒ Object

#

set_use_this_background_colour

#


127
128
129
# File 'lib/colours/class/colours.rb', line 127

def set_use_this_background_colour(i)
  @internal_hash[:use_this_background_colour] = i
end

#set_use_this_colour(i) ⇒ Object

#

set_use_this_colour

#


274
275
276
# File 'lib/colours/class/colours.rb', line 274

def set_use_this_colour(i)
  @internal_hash[:use_this_colour] = i
end

#set_use_underline_to_trueObject

#

set_use_underline_to_true

#


142
143
144
# File 'lib/colours/class/colours.rb', line 142

def set_use_underline_to_true
  @internal_hash[:use_underline] = true
end
#
#


213
214
215
216
# File 'lib/colours/class/colours.rb', line 213

def slow_blink
  set_slow_blink_to_true
  self
end

#slow_blink?Boolean

#
#

Returns:

  • (Boolean)


178
179
180
# File 'lib/colours/class/colours.rb', line 178

def slow_blink?
  @internal_hash[:slow_blink]
end

#string_increased_intensity?Boolean

#

string_increased_intensity?

#

Returns:

  • (Boolean)


237
238
239
240
241
242
243
# File 'lib/colours/class/colours.rb', line 237

def string_increased_intensity?
  _ = @internal_hash[:leading_colour_component]
  if _ and !_.empty?
    return "#{_}" # ;"
  end
  return ''
end

#trailing_end?Boolean

#

trailing_end?

#

Returns:

  • (Boolean)


199
200
201
# File 'lib/colours/class/colours.rb', line 199

def trailing_end?
  TRAILING_PART
end

#underlineObject Also known as: underlined

#

underline

#


221
222
223
224
# File 'lib/colours/class/colours.rb', line 221

def underline
  set_use_underline_to_true
  self
end

#use_italic?Boolean

#

use_italic?

#

Returns:

  • (Boolean)


352
353
354
# File 'lib/colours/class/colours.rb', line 352

def use_italic?
  @internal_hash[:use_italic]
end

#use_this_background_colour?Boolean

#

use_this_background_colour?

Background colours go from e. g. 40 to 47, give or take.

#

Returns:

  • (Boolean)


120
121
122
# File 'lib/colours/class/colours.rb', line 120

def use_this_background_colour?
  @internal_hash[:use_this_background_colour]
end

#use_this_colour?Boolean

#

use_this_colour?

#

Returns:

  • (Boolean)


164
165
166
# File 'lib/colours/class/colours.rb', line 164

def use_this_colour?
  @internal_hash[:use_this_colour]
end

#use_underline?Boolean

#

use_underline?

#

Returns:

  • (Boolean)


171
172
173
# File 'lib/colours/class/colours.rb', line 171

def use_underline?
  @internal_hash[:use_underline]
end