lit-cli 0.1.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/bin/lit +1 -3
  3. data/lib/config.rb +107 -1
  4. data/lib/lit_cli.rb +137 -31
  5. data/lib/lit_pry.rb +70 -0
  6. metadata +18 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a459079c92763142e2a67ca0edfadecfc1ed5e0755ad5c89bba8f6806c923541
4
- data.tar.gz: 6cbaca173d279eb5d60b361cf03a075f760045913d2c8f756913db37a1b2b9fc
3
+ metadata.gz: 839f03037b67dd56c7b7d4c38ba26138082594823cc32dbc7217c78363d34088
4
+ data.tar.gz: 3ab13437fcb2e6884ccd9465d85c72c0f3f7623b2e4076cd21ea22ba369a160b
5
5
  SHA512:
6
- metadata.gz: eef119aa4d3711abddea64974001c946135eba964d06886c8b2a49344b90868aef87079b1c4b293a6af2df8b1f83997ab80596655272c7e581a5d5a59b88d3ac
7
- data.tar.gz: 2a8a974813a2c013b16f8830a3bd3670d5bbbf1a03d881bd69587b7ae8f4692681f248e2971aeec45581c35439a1f9b6d95f96472a7989ed19cbaa313e2a103c
6
+ metadata.gz: 97d737397a07cfd123166f9c233757e0db4606072c75f22235c85d421474bc05bf94ef2ec8e216420b3c1cca9aecd1615c9ad20ade4b76ee6dde3770d36ba89f
7
+ data.tar.gz: 7e832c959dd947b99609f2e0ff8e30bb45a8f117b13a617a4671bb4236ee6a1a455f595b8eb35d9d682bc06abe9411e336b2f82553e328cd574215481b565770
data/bin/lit CHANGED
@@ -8,13 +8,11 @@
8
8
  # lit rails server
9
9
  ################################################################################
10
10
 
11
- # Enable Lit API.
12
11
  ENV['LIT_ENABLED'] = 'true'
13
12
 
13
+ # Get flags from arguments.
14
14
  args = []
15
15
  flags = []
16
-
17
- # Get flags from arguments.
18
16
  ARGV.each do |arg|
19
17
  if arg.start_with? '@'
20
18
  flags << arg.delete_prefix('@')
data/lib/config.rb CHANGED
@@ -1,10 +1,38 @@
1
+ require 'set'
2
+
1
3
  module LitCLI
4
+
5
+ ##############################################################################
6
+ # Default config that can be overridden from the command line or application.
7
+ #
8
+ # @usage:
9
+ # LitCLI.configure do |config|
10
+ # config.<property> = <value>
11
+ # end
12
+ #
13
+ # @precedence:
14
+ # 3. Defaults - initialize()
15
+ # 2. Application - LitCLI.configure()
16
+ # 1. Command line flags - cli_configure()
17
+ ##############################################################################
18
+
2
19
  class Config
3
20
 
21
+ # Track errors and only show them once.
22
+ @@errors = Set.new
23
+
24
+ attr_accessor :enabled
25
+ attr_accessor :statuses
4
26
  attr_accessor :types
27
+ # Flags.
28
+ attr_accessor :step
29
+ attr_accessor :status
30
+ attr_accessor :type
31
+ attr_accessor :delay
5
32
 
6
33
  def initialize()
7
- @types = {
34
+
35
+ @statuses = {
8
36
  :info => { icon: "ℹ", color: :blue },
9
37
  :pass => { icon: "✔", color: :green },
10
38
  :warn => { icon: "⚠", color: :yellow },
@@ -12,6 +40,84 @@ module LitCLI
12
40
  :error => { icon: "!", color: :red },
13
41
  :debug => { icon: "?", color: :purple },
14
42
  }
43
+
44
+ @types = nil
45
+
46
+ # Lit is disabled by default, then enabled via the `lit` command.
47
+ # Or it can be permanently enabled, without the use of the `lit` command.
48
+ @enabled = false
49
+
50
+ ##
51
+ # FLAGS.
52
+ #
53
+ # Flag defaults when not supplied via command line.
54
+ ##
55
+
56
+ # Boolean on whether or not to step through each lit() breakpoint.
57
+ @step = false
58
+
59
+ # Array of statuses to filter by, for example: [:warn, :error, :fail]
60
+ @status = nil
61
+
62
+ # Array of types to filter by, for example: [:cat, :dog, :tree]
63
+ @type = nil
64
+
65
+ # Integer or float representing amount of seconds to delay each lit() by.
66
+ @delay = 0
67
+
68
+ cli_configure()
69
+ end
70
+
71
+ # Override config from command line.
72
+ def cli_configure()
73
+ @enabled = true if ENV['LIT_ENABLED'] == 'true'
74
+
75
+ # Convert flag string to hash.
76
+ flags = {}
77
+ if ENV['LIT_FLAGS'] && !ENV['LIT_FLAGS'].empty?
78
+ ENV['LIT_FLAGS'].split().each do |flag|
79
+ values = flag.split('=')
80
+
81
+ key = values.shift.to_sym
82
+
83
+ # No arguments.
84
+ if values.empty?
85
+ flags[key] = nil
86
+ else
87
+ args = values.pop.split(',')
88
+ # Single argument.
89
+ if args.count == 1
90
+ flags[key] = args.first
91
+ # Multiple arguments.
92
+ else
93
+ flags[key] = args
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ @step = true if flags.has_key? :step
100
+ @status = Array(flags[:status]).map(&:to_sym) if valid? flags, :status
101
+ @type = Array(flags[:type]).map(&:to_sym) if valid? flags, :type
102
+ @delay = flags[:delay].to_f if valid? flags, :delay
103
+ end
104
+
105
+ def valid? flags, flag
106
+ # Has flag even been entered on the command line?
107
+ unless flags.has_key? flag
108
+ return false
109
+ end
110
+
111
+ if flags[flag].nil?
112
+ error = "🔥 ERROR: Invalid argument for @#{flag}."
113
+ unless @@errors.include? error
114
+ @@errors.add error
115
+ puts error
116
+ end
117
+ return false
118
+ end
119
+
120
+ true
15
121
  end
16
122
 
17
123
  end
data/lib/lit_cli.rb CHANGED
@@ -1,46 +1,152 @@
1
+ require 'pry'
1
2
  require 'pastel'
2
3
  require 'config'
4
+ require 'lit_pry'
3
5
 
4
6
  module LitCLI
5
-
6
7
  @@pastel = Pastel.new
7
- @@config = Config.new()
8
+ @@config = Config.new
9
+ @@is_prying = false
10
+
11
+ def lit(message, status = :info, type = nil, context = nil)
12
+ if @@config.enabled
13
+ return if LitCLI.filter_status? status
14
+ return if LitCLI.filter_type? type
15
+ LitCLI.render(message, status, type, context)
16
+ LitCLI.step()
17
+ LitCLI.delay()
18
+ end
19
+ end
20
+ alias 🔥 lit
8
21
 
9
- def lit(message, type = :info)
10
- if ENV['LIT_ENABLED'] == 'true'
11
- type_config = @@config.types[type]
22
+ def self.render(message, status, type, context)
23
+ text = "🔥"
12
24
 
13
- time_text = LitCLI.colorize(Time.now().strftime("%k:%M"), :cyan)
14
- type_text = type_config[:icon] + " " + type.to_s
15
- type_text_color = LitCLI.colorize(type_text, type_config[:color])
25
+ # Time.
26
+ time = LitCLI.format(Time.now().strftime("%k:%M"), color: :cyan)
27
+ text << " #{time}"
16
28
 
17
- message = "🔥 #{time_text} #{type_text_color} #{message}"
18
- puts message
29
+ # Status.
30
+ config = @@config.statuses[status]
31
+ text << LitCLI.format(" #{config[:icon]} #{status.to_s}", config)
32
+
33
+ # Type.
34
+ if !@@config.types.nil? && @@config.types.has_key?(type)
35
+ config = @@config.types[type]
36
+ if config.has_key? :icon
37
+ text << LitCLI.format(" #{config[:icon]} #{type.to_s}", config)
38
+ else
39
+ text << LitCLI.format(" #{type.to_s}", config)
40
+ end
19
41
  end
42
+
43
+ # Context.
44
+ text << LitCLI.format(" #{context}", styles: [:bold, :dim])
45
+
46
+ # Message.
47
+ text << " #{message}"
48
+
49
+ puts text
50
+ end
51
+
52
+ def self.step()
53
+ if @@config.step
54
+ puts "🔥 Press ENTER to step or P to Pry:"
55
+ input = gets.chomp
56
+ binding while input == nil
57
+ @@is_prying = true if input.downcase == "p"
58
+ end
59
+ end
60
+
61
+ def self.filter_status? status
62
+ unless @@config.status.nil? || @@config.status.include?(status)
63
+ return true
64
+ end
65
+
66
+ false
20
67
  end
21
- alias 🔥 lit
22
68
 
23
- def self.colorize(text, color)
24
- case color
25
- when :blue
26
- return @@pastel.bright_blue(text)
27
- when :green
28
- return @@pastel.green(text)
29
- when :yellow
30
- return @@pastel.yellow(text)
31
- when :red
32
- return @@pastel.red(text)
33
- when :purple, :magenta
34
- return @@pastel.magenta(text)
35
- when :cyan
36
- return @@pastel.cyan(text)
37
- else
38
- return text
39
- end
40
- end
41
-
42
- def self.configure
69
+ def self.filter_type? type
70
+ unless @@config.type.nil? || @@config.type.include?(type)
71
+ return true
72
+ end
73
+
74
+ false
75
+ end
76
+
77
+ def self.delay()
78
+ if @@config.delay > 0
79
+ sleep(@@config.delay)
80
+ end
81
+ end
82
+
83
+ def self.is_prying?
84
+ @@config.step && @@is_prying
85
+ end
86
+
87
+ def self.format(text, config)
88
+
89
+ if config.has_key? :styles
90
+ # Change characters first.
91
+ config[:styles].each do |style|
92
+ case style
93
+ when :upcase
94
+ text = text.upcase
95
+ when :downcase
96
+ text = text.downcase
97
+ when :capitalize
98
+ text = text.capitalize
99
+ end
100
+ end
101
+ # Then apply styling.
102
+ config[:styles].each do |style|
103
+ case style
104
+ when :clear
105
+ text = @@pastel.clear(text)
106
+ when :bold
107
+ text = @@pastel.bold(text)
108
+ when :dim
109
+ text = @@pastel.dim(text)
110
+ when :italic
111
+ text = @@pastel.italic(text)
112
+ when :underline
113
+ text = @@pastel.underline(text)
114
+ when :inverse
115
+ text = @@pastel.inverse(text)
116
+ when :hidden
117
+ text = @@pastel.hidden(text)
118
+ when :strike, :strikethrough
119
+ text = @@pastel.strikethrough(text)
120
+ end
121
+ end
122
+ end
123
+
124
+ if config.has_key? :color
125
+ case config[:color]
126
+ when :blue
127
+ text = @@pastel.bright_blue(text)
128
+ when :green
129
+ text = @@pastel.green(text)
130
+ when :yellow
131
+ text = @@pastel.yellow(text)
132
+ when :red
133
+ text = @@pastel.red(text)
134
+ when :purple, :magenta
135
+ text = @@pastel.magenta(text)
136
+ when :cyan
137
+ text = @@pastel.cyan(text)
138
+ end
139
+ end
140
+
141
+ text
142
+ end
143
+
144
+ # Override config from application.
145
+ def self.configure()
43
146
  yield(@@config)
147
+
148
+ # Override config from command line.
149
+ @@config.cli_configure()
44
150
  end
45
151
 
46
152
  end
data/lib/lit_pry.rb ADDED
@@ -0,0 +1,70 @@
1
+ # Only override kernel methods when Lit's @step flag is true.
2
+ if ENV['LIT_FLAGS'] && ENV['LIT_FLAGS'].include?('step')
3
+ module Kernel
4
+
5
+ def require_relative relative_path
6
+ filename = relative_path
7
+
8
+ # Get directory of the file that called this file.
9
+ absolute_path = caller_locations.first.absolute_path.split('/')
10
+ absolute_path.pop
11
+
12
+ # When relative path is current directory.
13
+ if relative_path.start_with?('./')
14
+ filename = relative_path.delete_prefix! './'
15
+ end
16
+
17
+ # When relative path is parent directory.
18
+ while relative_path.start_with?('../')
19
+ relative_path.delete_prefix! '../'
20
+ absolute_path.pop
21
+ end
22
+
23
+ # Append default extension.
24
+ unless filename.end_with? '.rb'
25
+ filename << '.rb'
26
+ end
27
+
28
+ filepath = File.join(absolute_path.join('/'), relative_path)
29
+
30
+ # Add pry binding beneath each lit message.
31
+ new_lines = ''
32
+ File.foreach(filepath) do |line|
33
+ new_lines << line
34
+ if line.strip.start_with? 'lit "'
35
+ new_lines << "binding.pry if LitCLI.is_prying?\n"
36
+ new_lines << "@@is_prying = false\n"
37
+ end
38
+ end
39
+
40
+ eval(new_lines)
41
+ end
42
+
43
+ # TODO: Investigate RubyGems `require` before overriding.
44
+ # SEE: https://github.com/ruby/ruby/blob/v2_6_3/lib/rubygems/core_ext/kernel_require.rb
45
+ #
46
+ # def require path
47
+ # absolute_path = File.join(Dir.pwd, path)
48
+ # #p "requiring #{absolute_path}"
49
+ # #p __dir__
50
+ # #p File.dirname(File.realpath(__FILE__))
51
+ # #p File.realpath(__FILE__)
52
+ #
53
+ # # Split client-side and server-side code.
54
+ # new_lines = []
55
+ # binding_line = "binding.pry if LitCLI.is_prying?"
56
+ #
57
+ # # Add pry binding beneath each lit message.
58
+ # p path
59
+ # File.foreach(path) do |line|
60
+ # new_lines << line
61
+ # if line.strip.start_with? 'lit "'
62
+ # new_lines << binding_line
63
+ # end
64
+ # end
65
+ #
66
+ # eval(new_lines.join)
67
+ # end
68
+
69
+ end
70
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lit-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maedi Prichard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-03 00:00:00.000000000 Z
11
+ date: 2021-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -24,7 +24,21 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Show more detailed logs by starting commands with 'lit'.
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Console logs that are only visible after prefixing commands with 'lit'.
28
42
  email: maediprichard@gmail.com
29
43
  executables:
30
44
  - lit
@@ -34,6 +48,7 @@ files:
34
48
  - bin/lit
35
49
  - lib/config.rb
36
50
  - lib/lit_cli.rb
51
+ - lib/lit_pry.rb
37
52
  homepage: https://reflekt.dev/lit
38
53
  licenses:
39
54
  - MPL-2.0