Module: L2

Defined in:
lib/L2.rb

Overview

·

Constant Summary collapse

COLORS =

standard color palette for texts

{ 
    default: '38', 
    yellow: '1;33',
    white: '1;37', 
    green: '32',  
    black: '30', 
    blue: '34', 
    red: '31'
}.freeze
BG_COLORS =

standard color palette for backgrounds

{ 
    default: '0', 
    yellow: '103', 
    white: '107', 
    green: '42', 
    black: '40', 
    blue: '44',
    red: '41'
}.freeze
WIDTH =

calculate the console width tputcols is not available on windows

`tput cols`.to_i rescue WIDTH = 1

Class Method Summary collapse

Class Method Details

.alert(*messages) ⇒ Object



117
118
119
# File 'lib/L2.rb', line 117

def self.alert *messages
    messages.each { |message| puts("\e[5m#{message}\e[0m") }
end

.br(count = 1) ⇒ Object



231
232
233
# File 'lib/L2.rb', line 231

def self.br count=1
    separator_blank(count)
end

.cow(message = "Process completed!") ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/L2.rb', line 184

def self.cow message="Process completed!"

    # ids of the prettiest cows in the library
    pretty_cows = [46,33,32,31,29,27,21,10,5]

    # get a random cow id
    random_cows = rand(0..(pretty_cows.size - 1))

    br()

    # show simple text message
    puts Cow.new({ :cow => Cow.cows[pretty_cows[random_cows]] }).say(message)
end

.danger(*messages) ⇒ Object



101
102
103
104
105
106
# File 'lib/L2.rb', line 101

def self.danger *messages
    puts(separator_pretty(:red))
    messages.each { |message| puts(pretty("ERROR: #{ message }", :yellow, :red)) }
    puts(pretty("PATH: #{ caller[0] }", :yellow, :red))
    puts(separator_pretty(:red))
end

.deprecation(message) ⇒ Object



122
123
124
125
126
# File 'lib/L2.rb', line 122

def self.deprecation message
    puts(separator_pretty(:red))
    puts(pretty("DEPRECATED METHOD: #{ caller[0] }, #{ message }", :white, :red))
    puts(separator_pretty(:red))
end

.error(*messages) ⇒ Object



227
228
229
# File 'lib/L2.rb', line 227

def self.error *messages
    danger(messages)
end

.fatal(*messages) ⇒ Object



109
110
111
112
113
114
# File 'lib/L2.rb', line 109

def self.fatal *messages
    puts(separator_pretty(:red))
    messages.each { |message| puts("\e[5m#{pretty(message, :white, :red)}\e[0m") }
    puts(pretty("PATH: #{ caller[0] }", :white, :red))
    puts(separator_pretty(:red))
end

.info(*messages) ⇒ Object



80
81
82
83
84
# File 'lib/L2.rb', line 80

def self.info *messages
    puts(separator_pretty(:blue))
    messages.each { |message| puts(pretty("INFO: #{ message }", :white, :blue)) }
    puts(separator_pretty(:blue))
end

.line(count = 8) ⇒ Object



235
236
237
# File 'lib/L2.rb', line 235

def self.line count=8
    separator_line(count)
end

.list(*messages) ⇒ Object



129
130
131
132
# File 'lib/L2.rb', line 129

def self.list *messages
    messages.each { |message| puts(" - #{ message }" )}
    separator_blank
end

.m(*messages) ⇒ Object



67
68
69
# File 'lib/L2.rb', line 67

def self.m *messages
    messages.each { |m| puts(m) }
end

.msg(*messages) ⇒ Object



72
73
74
75
76
77
# File 'lib/L2.rb', line 72

def self.msg *messages
    separator_blank
    self.m(messages)
    separator_line
    separator_blank
end

.spin_it(times) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/L2.rb', line 198

def self.spin_it(times)
    pinwheel =  ['', '', '', '', '', '', '', '', '', '']
    # times.times do
    #   print "\b" + pinwheel.rotate!.first
    #   sleep(0.1)
    # end

    check = "\u2713"
    heart = "\u2665"
    package = "\u{1F4E6}"
    fire_and_one_hundred = "\u{1F525 1F4AF}"
    puts heart
    puts package
    puts fire_and_one_hundred
    puts self.colorize(check, :green)
    pp check 
end

.success(*messages) ⇒ Object



87
88
89
90
91
# File 'lib/L2.rb', line 87

def self.success *messages
    puts(separator_pretty(:green))
    messages.each { |message| puts(pretty("SUCCESS: #{ message }", :black, :green)) }
    puts(separator_pretty(:green))
end

.table(data) ⇒ Object



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
# File 'lib/L2.rb', line 135

def self.table data
    
    return if Gem.win_platform?
    return unless data.size > 0

    if data.class.name == "ActiveRecord::Relation"
        data = data.to_a.map(&:serializable_hash) 
    end

    # get the available characters in terminal width
    terminal_width = `tput cols`.to_i

    # get the number of columns to print base on the data hash
    cols = data.first.keys.count + 1

    # get the available space for every column
    col_width = (terminal_width / cols) - 1

    # validate that we have minimum space to render the table
    return if col_width <= 0

    # separator for every column and row
    separator = ('| ' << ('- ' * (col_width / 2)))

    # add extra blank spaces to adjust the col_width only if col_width not a even number
    separator += (' ') if (col_width - separator.size).odd?

    # print data as table :)
    data.each_with_index do |row, index|

        # only for table header
        if index == 0 

            # print table titles
            puts '| ' << row.keys.map { |key| key.to_s.upcase.ljust(col_width) }.join('| ')

            # print header separators, only for visible columns
            puts separator * (cols - 1)

        end 

        # join hash values as a line and justify every value to print value
        # in its own column
        puts '| ' << row.values.map { |value| value.to_s.ljust(col_width) }.join('| ')

    end

end

.warn(*messages) ⇒ Object



222
223
224
# File 'lib/L2.rb', line 222

def self.warn *messages
    warning(messages)
end

.warning(*messages) ⇒ Object



94
95
96
97
98
# File 'lib/L2.rb', line 94

def self.warning *messages
    puts(separator_pretty(:yellow))
    messages.each { |message| puts(pretty("WARNING: #{ message }", :black, :yellow)) }
    puts(separator_pretty(:yellow))
end