Module: LitCLI

Defined in:
lib/config.rb,
lib/lit_cli.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

@@pastel =
Pastel.new
@@config =
Config.new
@@is_prying =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure {|@@config| ... } ⇒ Object

Override config from application.

Yields:

  • (@@config)


162
163
164
165
166
167
# File 'lib/lit_cli.rb', line 162

def self.configure()
  yield(@@config)

  # Override config from flags on command line.
  @@config.cli_configure()
end

.delayObject



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

def self.delay()
  if @@config.delay > 0
    sleep(@@config.delay)
  end
end

.filter_status?(status) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/lit_cli.rb', line 78

def self.filter_status? status
  unless @@config.status.nil? || @@config.status.include?(status)
    return true
  end

  false
end

.filter_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/lit_cli.rb', line 86

def self.filter_type? type
  unless @@config.type.nil? || @@config.type.include?(type)
    return true
  end

  false
end

.format(text, config) ⇒ Object



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

def self.format(text, config)

  if config.has_key? :styles
    # Change characters first.
    config[:styles].each do |style|
      case style
      when :upcase
        text = text.upcase
      when :downcase
        text = text.downcase
      when :capitalize
        text = text.capitalize
      end
    end
    # Then apply styling.
    config[:styles].each do |style|
      case style
      when :clear
        text = @@pastel.clear(text)
      when :bold
        text = @@pastel.bold(text)
      when :dim
        text = @@pastel.dim(text)
      when :italic
        text = @@pastel.italic(text)
      when :underline
        text = @@pastel.underline(text)
      when :inverse
        text = @@pastel.inverse(text)
      when :hidden
        text = @@pastel.hidden(text)
      when :strike, :strikethrough
        text = @@pastel.strikethrough(text)
      end
    end
  end

  if config.has_key? :color
    case config[:color]
    when :blue
      text = @@pastel.bright_blue(text)
    when :green
      text = @@pastel.green(text)
    when :yellow
      text = @@pastel.yellow(text)
    when :red
      text = @@pastel.red(text)
    when :purple, :magenta
      text = @@pastel.magenta(text)
    when :cyan
      text = @@pastel.cyan(text)
    end
  end

  text
end

.is_prying?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/lit_cli.rb', line 100

def self.is_prying?
  @@config.step && @@is_prying
end

.render(message, status, type, context) ⇒ Object



26
27
28
29
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
# File 'lib/lit_cli.rb', line 26

def self.render(message, status, type, context)
  output = "🔥"

  # Time.
  time = LitCLI.format(Time.now().strftime("%k:%M"), color: :cyan)
  output << " #{time}"

  # Status.
  config = @@config.statuses[status]
  output << LitCLI.format(" #{config[:icon]} #{status.to_s}", config)

  # Type.
  if !@@config.types.nil? && @@config.types.has_key?(type)
    config = @@config.types[type]
    if config.has_key? :icon
      output << LitCLI.format(" #{config[:icon]} #{type.to_s}", config)
    else
      output << LitCLI.format(" #{type.to_s}", config)
    end
  end

  # Context.
  output << LitCLI.format(" #{context}", styles: [:bold, :dim])

  # Message.
  while message.start_with?('^')
    message.delete_prefix!('^').strip!
    unless @@config.status || @@config.type
      output = "\n" + output
    end
  end

  while message.start_with?('>')
    message.delete_prefix!('>').strip!
    unless @@config.status || @@config.type
      output = '  ' + output
    end
  end

  output << " #{message}"
  puts output
end

.stepObject



69
70
71
72
73
74
75
76
# File 'lib/lit_cli.rb', line 69

def self.step()
  if @@config.step
    puts "🔥 Press ENTER to step or P to Pry:"
    input = gets.chomp
    binding while input == nil
    @@is_prying = true if input.downcase == "p"
  end
end

Instance Method Details

#lit(message, status = :info, type = nil, context = nil) ⇒ Object Also known as: 🔥



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lit_cli.rb', line 11

def lit(message, status = :info, type = nil, context = nil)
  if @@config.enabled
    return if LitCLI.filter_status? status
    return if LitCLI.filter_type? type

    LitCLI.render(message, status, type, context)

    LitCLI.step()
    yield if block_given?

    LitCLI.delay()
  end
end