Module: Cliner

Defined in:
lib/cliner/time.rb,
lib/cliner/token.rb,
lib/cliner/cliner.rb,
lib/cliner/module.rb,
lib/cliner/colours.rb,
lib/cliner/version/version.rb

Overview

#

Constant Summary collapse

ASSIGNMENT_TOKEN =
#

ASSIGNMENT_TOKEN

#
'='
VERSION =
#

VERSION

#
'1.0.33'
LAST_UPDATE =
#

LAST_UPDATE

#
'08.08.2022'

Class Method Summary collapse

Class Method Details

.[](n_times = 80) ⇒ Object

#

Cliner[]

This module-method accepts one argument, which is how many ‘=’ we will use. By default we will show 80 ‘=’ tokens.

#


29
30
31
32
33
34
35
36
# File 'lib/cliner/module.rb', line 29

def self.[](n_times = 80)
  cliner(
    ASSIGNMENT_TOKEN,
    n_times,
    nil,
    false # false for "return the result".
  )
end

.cliner(which_token_to_use = Cliner.token?, how_often_to_use_that_token = :default, use_colour = ( (Object.const_defined? :Cliner) ? Cliner.use_colours? : nil ), output_the_result = true) ⇒ Object

#

Cliner.cliner

This method will output a line.

The third argument is optional - we can use a specific colour with it.

Usage examples:

Cliner.cliner '','',BLUE
Cliner.cliner token: 'X'
Cliner.cliner :show_time
Cliner.cliner :show_time, :default, :default, :return_as_string
Cliner.cliner colour: :green
Cliner.cliner use_colour: :dodgerblue
Cliner.cliner use_colour: :konsole_dodgerblue
Cliner.cliner(:show_time) { use_n_tokens: 30 }
#


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cliner/cliner.rb', line 30

def self.cliner(
    which_token_to_use          = Cliner.token?,
    how_often_to_use_that_token = :default,
    # ===================================================================== #
    # The following is a bit hard to understand, so let's break it down.
    # We need to first query whether the constant Cliner is defined.
    # If it is, we can tap into a module method, but if it is not,
    # we have to default to nil.
    # ===================================================================== #
    use_colour                  = (
      (Object.const_defined? :Cliner) ? Cliner.use_colours? : nil
    ),
    output_the_result           = true # <- Whether to output the result.
  )
  show_time = false # Default.
  case use_colour
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default
    use_colour = (Object.const_defined? :Cliner) ? Cliner.use_colours? : nil
  end
  case output_the_result
  when :return_as_string
    output_the_result = false
  end
  case which_token_to_use
  # ======================================================================= #
  # === :show_time
  # ======================================================================= #
  when :show_time
    which_token_to_use = Cliner.token?
    show_time = true
  # ======================================================================= #
  # === :return_time
  # ======================================================================= #
  when :return_time
    which_token_to_use = Cliner.token?
    show_time = true
    output_the_result = false
  when :default
    which_token_to_use = Cliner.token?
  end
  default_token = Cliner.token?
  append = ''.dup
  case how_often_to_use_that_token
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default
    how_often_to_use_that_token = 80
  end
  if how_often_to_use_that_token.to_s.empty?
    how_often_to_use_that_token = 80
  end
  # ======================================================================= #
  # === Hash input for which_token_to_use
  #
  # Check if we use a hash for the variable which_token_to_use next.
  # ======================================================================= #
  if which_token_to_use.is_a? Hash
    # ===================================================================== #
    # === :colour
    # ===================================================================== #
    if which_token_to_use.has_key? :colour
      use_colour = which_token_to_use.delete :colour
    elsif which_token_to_use.has_key? :colours
      use_colour = which_token_to_use.delete :colours
    elsif which_token_to_use.has_key? :use_colour
      use_colour = which_token_to_use.delete :use_colour
    end
    # ===================================================================== #
    # === :length
    # ===================================================================== #
    if which_token_to_use.has_key?    :length
      how_often_to_use_that_token = which_token_to_use.delete(:length)
      which_token_to_use = default_token
    elsif which_token_to_use.has_key? :token
      which_token_to_use = which_token_to_use[:token]
    # ===================================================================== #
    # === :use_this_token
    # ===================================================================== #
    elsif which_token_to_use.has_key? :use_this_token
      which_token_to_use = which_token_to_use[:use_this_token]
    else
      which_token_to_use = Cliner.token? # Default value. Reassign it here.
    end
  end
  # ======================================================================= #
  # === Handle colours given as the first argument
  # ======================================================================= #
  if which_token_to_use.is_a? Symbol
    use_colour = which_token_to_use
    which_token_to_use = Cliner.token?
  end
  if which_token_to_use.to_s.empty?
    which_token_to_use = Cliner.token?
  end
  how_often_to_use_that_token = how_often_to_use_that_token.to_i
  result = ''.dup
  case use_colour # case tag
  when :do_not_use_colours
    use_colour = false
  end
  # ======================================================================= #
  # === Handle colours next
  # ======================================================================= #
  if use_colour and !use_colour.is_a?(TrueClass) # Must not be the boolean "true".
    # ===================================================================== #
    # === Handle symbols
    # ===================================================================== #
    if use_colour.is_a? Symbol # If it is a symbol, convert it.
      if use_colour.to_s.start_with? 'konsole_' # For instance, :konsole_slateblue
        # ================================================================= #
        # === Handle konsole colours next
        # ================================================================= #
        _ = use_colour.to_s.sub(/^konsole_/,'')
        use_colour = ::Colours.send(_.to_sym) if Object.const_defined? :Colours
      else
        use_colour = Colours.send(use_colour.to_sym) { :omit_end }
      end
    # ===================================================================== #
    # === Is it a valid colour?
    # ===================================================================== #
    elsif Colours.is_a_valid_colour?(use_colour)
      use_colour = Colours.beautiful(use_colour)
    end
    result = result.dup if result.frozen? # Unfreeze the String.
    result << use_colour.to_s # Append the colour.
    append << Colours.rev
  end
  if result.frozen?
    result = result.dup
  end
  # ======================================================================= #
  # Handle show_time next:
  # ======================================================================= #
  if show_time
    _ = return_hhmmss
    how_often_to_use_that_token -= (_.size + 3)
  end
  result << which_token_to_use * how_often_to_use_that_token # The latter must be an Integer at all times.
  if show_time
    # ===================================================================== #
    # We have to pad the resulting string a little bit:
    # ===================================================================== #
    result[3,0] = " #{_} " 
  end
  result << append
  if output_the_result
    if respond_to? :e
      e result
    else
      puts result
    end
  else # Else return it.
    return result
  end
  if block_given?
    yield
    cliner(
      which_token_to_use,
      how_often_to_use_that_token,
      use_colour
    )
  end
end

.resetObject

#

Cliner.reset

#


19
20
21
# File 'lib/cliner/module.rb', line 19

def self.reset
  Cliner.set_colour nil
end

.return_hhmmssObject

#

Cliner.return_hhmmss

This method will return hh:mm:ss, such as in “10:58:32”.

#


15
16
17
# File 'lib/cliner/time.rb', line 15

def self.return_hhmmss
  Time.now.strftime('%H:%M:%S')
end

.set_colour(i = 'lightblue') ⇒ Object

#

Cliner.set_colour

Use this method to designate a special colour to use.

#


19
20
21
22
23
# File 'lib/cliner/colours.rb', line 19

def self.set_colour(
    i = 'lightblue'
  )
  @use_this_colour = i
end

.token?Boolean

#

Cliner.token?

#

Returns:

  • (Boolean)


17
18
19
# File 'lib/cliner/token.rb', line 17

def self.token?
  ASSIGNMENT_TOKEN
end

.use_colours?Boolean

#

Cliner.use_colours?

#

Returns:

  • (Boolean)


28
29
30
# File 'lib/cliner/colours.rb', line 28

def self.use_colours?
  @use_this_colour
end