lit-cli 0.4.0 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config.rb +20 -11
  3. data/lib/lit_cli.rb +96 -27
  4. data/lib/lit_pry.rb +90 -47
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ed5d7713a6943b0043bf26568c069dc4c0a647a8581dc48d7a88c05abb3b5de
4
- data.tar.gz: 53c911d1424038fa5bb836dfc68f739514d53a62b91ed813dc506078d4c5742d
3
+ metadata.gz: c0bb5bf94135acf7647df72b3431dc89663ee6bca855cfb3c75f298d8a79099d
4
+ data.tar.gz: a1c226eec222a72c0be6f38ee4940f5a3f70809557805bcc0992ebff50e11e89
5
5
  SHA512:
6
- metadata.gz: e75aab70ca6f9aac9258b68f289ca7efdaf24d8328030a7195c91e24ecd7828eaf32d42d7cee87acc6b1d188d2ef692969cc402055b1a98ed71bc5a0edf4632e
7
- data.tar.gz: 0f88b27bcb0308d93b8e7ba223a91a11125faf4c29fdb600e3ff77a18a1526c1be51f5dc4c60e276f5b2a0aca2e91577a92e8f4ef85b6e6c233d80cab8c0a2c3
6
+ metadata.gz: 335100d58b9ee3c5f6fa599725c91c0911237b1e6ba64fadd4d62b59569478e897eee752f4f9abf70365ae102fa99f9a4d849099c8b99521e6207ec9ad00ae2a
7
+ data.tar.gz: 97b8638b199affa210e563aa529f8178a45fe0f5023c844460a78e6f742a3b20dbcb502cf64922001b40fa974a009118bce2d7040fba7d3cf88cc0190dc7281d
data/lib/config.rb CHANGED
@@ -22,22 +22,27 @@ module LitCLI
22
22
  @@errors = Set.new
23
23
 
24
24
  attr_accessor :enabled
25
+ attr_accessor :statuses
25
26
  attr_accessor :types
26
- attr_accessor :type
27
+ # Flags.
27
28
  attr_accessor :step
29
+ attr_accessor :status
30
+ attr_accessor :type
28
31
  attr_accessor :delay
29
32
 
30
33
  def initialize()
31
34
 
32
- @types = {
33
- :info => { icon: "ℹ", color: :blue },
34
- :pass => { icon: "✔", color: :green },
35
- :warn => { icon: "⚠", color: :yellow },
36
- :fail => { icon: "⨯", color: :red },
37
- :error => { icon: "!", color: :red },
38
- :debug => { icon: "?", color: :purple },
35
+ @statuses = {
36
+ :info => { icon: "ℹ", color: :blue, styles: [:upcase] },
37
+ :pass => { icon: "✔", color: :green, styles: [:upcase] },
38
+ :warn => { icon: "⚠", color: :yellow, styles: [:upcase] },
39
+ :fail => { icon: "⨯", color: :red, styles: [:upcase] },
40
+ :error => { icon: "!", color: :red, styles: [:upcase] },
41
+ :debug => { icon: "?", color: :purple, styles: [:upcase] },
39
42
  }
40
43
 
44
+ @types = nil
45
+
41
46
  # Lit is disabled by default, then enabled via the `lit` command.
42
47
  # Or it can be permanently enabled, without the use of the `lit` command.
43
48
  @enabled = false
@@ -48,12 +53,15 @@ module LitCLI
48
53
  # Flag defaults when not supplied via command line.
49
54
  ##
50
55
 
51
- # Array of types to filter by, for example... [:warn, :error, :fail]
52
- @type = nil
53
-
54
56
  # Boolean on whether or not to step through each lit() breakpoint.
55
57
  @step = false
56
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
+
57
65
  # Integer or float representing amount of seconds to delay each lit() by.
58
66
  @delay = 0
59
67
 
@@ -89,6 +97,7 @@ module LitCLI
89
97
  end
90
98
 
91
99
  @step = true if flags.has_key? :step
100
+ @status = Array(flags[:status]).map(&:to_sym) if valid? flags, :status
92
101
  @type = Array(flags[:type]).map(&:to_sym) if valid? flags, :type
93
102
  @delay = flags[:delay].to_f if valid? flags, :delay
94
103
  end
data/lib/lit_cli.rb CHANGED
@@ -8,26 +8,49 @@ module LitCLI
8
8
  @@config = Config.new
9
9
  @@is_prying = false
10
10
 
11
- def lit(message, type = :info)
11
+ def lit(message, status = :info, type = nil, context = nil)
12
12
  if @@config.enabled
13
- return if LitCLI.filter? type
14
- LitCLI.render(type)
13
+ return if LitCLI.filter_status? status
14
+ return if LitCLI.filter_type? type
15
+
16
+ LitCLI.render(message, status, type, context)
17
+
15
18
  LitCLI.step()
19
+ yield if block_given?
20
+
16
21
  LitCLI.delay()
17
22
  end
18
23
  end
19
24
  alias 🔥 lit
20
25
 
21
- def self.render(type)
22
- type_config = @@config.types[type]
26
+ def self.render(message, status, type, context)
27
+ text = "🔥"
23
28
 
24
- time_text = LitCLI.colorize(Time.now().strftime("%k:%M"), :cyan)
25
- type_text = type_config[:icon] + " " + type.to_s
26
- type_text_color = LitCLI.colorize(type_text, type_config[:color])
29
+ # Time.
30
+ time = LitCLI.format(Time.now().strftime("%k:%M"), color: :cyan)
31
+ text << " #{time}"
27
32
 
28
- message = "🔥 #{time_text} #{type_text_color} #{message}"
33
+ # Status.
34
+ config = @@config.statuses[status]
35
+ text << LitCLI.format(" #{config[:icon]} #{status.to_s}", config)
36
+
37
+ # Type.
38
+ if !@@config.types.nil? && @@config.types.has_key?(type)
39
+ config = @@config.types[type]
40
+ if config.has_key? :icon
41
+ text << LitCLI.format(" #{config[:icon]} #{type.to_s}", config)
42
+ else
43
+ text << LitCLI.format(" #{type.to_s}", config)
44
+ end
45
+ end
29
46
 
30
- puts message
47
+ # Context.
48
+ text << LitCLI.format(" #{context}", styles: [:bold, :dim])
49
+
50
+ # Message.
51
+ text << " #{message}"
52
+
53
+ puts text
31
54
  end
32
55
 
33
56
  def self.step()
@@ -39,7 +62,15 @@ module LitCLI
39
62
  end
40
63
  end
41
64
 
42
- def self.filter? type
65
+ def self.filter_status? status
66
+ unless @@config.status.nil? || @@config.status.include?(status)
67
+ return true
68
+ end
69
+
70
+ false
71
+ end
72
+
73
+ def self.filter_type? type
43
74
  unless @@config.type.nil? || @@config.type.include?(type)
44
75
  return true
45
76
  end
@@ -57,23 +88,61 @@ module LitCLI
57
88
  @@config.step && @@is_prying
58
89
  end
59
90
 
60
- def self.colorize(text, color)
61
- case color
62
- when :blue
63
- return @@pastel.bright_blue(text)
64
- when :green
65
- return @@pastel.green(text)
66
- when :yellow
67
- return @@pastel.yellow(text)
68
- when :red
69
- return @@pastel.red(text)
70
- when :purple, :magenta
71
- return @@pastel.magenta(text)
72
- when :cyan
73
- return @@pastel.cyan(text)
74
- else
75
- return text
91
+ def self.format(text, config)
92
+
93
+ if config.has_key? :styles
94
+ # Change characters first.
95
+ config[:styles].each do |style|
96
+ case style
97
+ when :upcase
98
+ text = text.upcase
99
+ when :downcase
100
+ text = text.downcase
101
+ when :capitalize
102
+ text = text.capitalize
103
+ end
104
+ end
105
+ # Then apply styling.
106
+ config[:styles].each do |style|
107
+ case style
108
+ when :clear
109
+ text = @@pastel.clear(text)
110
+ when :bold
111
+ text = @@pastel.bold(text)
112
+ when :dim
113
+ text = @@pastel.dim(text)
114
+ when :italic
115
+ text = @@pastel.italic(text)
116
+ when :underline
117
+ text = @@pastel.underline(text)
118
+ when :inverse
119
+ text = @@pastel.inverse(text)
120
+ when :hidden
121
+ text = @@pastel.hidden(text)
122
+ when :strike, :strikethrough
123
+ text = @@pastel.strikethrough(text)
124
+ end
125
+ end
126
+ end
127
+
128
+ if config.has_key? :color
129
+ case config[:color]
130
+ when :blue
131
+ text = @@pastel.bright_blue(text)
132
+ when :green
133
+ text = @@pastel.green(text)
134
+ when :yellow
135
+ text = @@pastel.yellow(text)
136
+ when :red
137
+ text = @@pastel.red(text)
138
+ when :purple, :magenta
139
+ text = @@pastel.magenta(text)
140
+ when :cyan
141
+ text = @@pastel.cyan(text)
142
+ end
76
143
  end
144
+
145
+ text
77
146
  end
78
147
 
79
148
  # Override config from application.
data/lib/lit_pry.rb CHANGED
@@ -1,68 +1,111 @@
1
1
  # Only override kernel methods when Lit's @step flag is true.
2
2
  if ENV['LIT_FLAGS'] && ENV['LIT_FLAGS'].include?('step')
3
+ require 'set'
4
+
5
+ # TODO: Investigate RubyGems `require` before overriding.
6
+ # SEE: https://github.com/ruby/ruby/blob/v2_6_3/lib/rubygems/core_ext/kernel_require.rb
7
+
3
8
  module Kernel
4
9
 
5
- def require_relative relative_path
6
- filename = relative_path
10
+ @@lit_processed_paths = Set.new
7
11
 
8
- # Get directory of the file that called this file.
9
- absolute_path = caller_locations.first.absolute_path.split('/')
10
- absolute_path.pop
12
+ def require_relative relative_path, current_directory = nil
13
+ return false if relative_path.nil?
11
14
 
12
- # When relative path is current directory.
13
- if relative_path.start_with?('./')
14
- filename = relative_path.delete_prefix! './'
15
- end
15
+ # Handle absolute path.
16
+ if relative_path.start_with?('/') && File.exist?(relative_path)
17
+ absolute_path = relative_path.split('/')
18
+ file_name = absolute_path.pop
19
+ # Handle relative path.
20
+ else
21
+ # Get directory of the file that called this file.
22
+ if current_directory.nil?
23
+ absolute_path = caller_locations.first.absolute_path.split('/')
24
+ absolute_path.pop
25
+ else
26
+ absolute_path = current_directory.split('/')
27
+ end
28
+
29
+ # When path is current directory.
30
+ if relative_path.start_with?('./')
31
+ relative_path.delete_prefix! './'
32
+ end
33
+
34
+ # When path is parent directory.
35
+ while relative_path.start_with?('../')
36
+ relative_path.delete_prefix! '../'
37
+ absolute_path.pop
38
+ end
39
+
40
+ # When path contains child directories.
41
+ if relative_path.split('/').count > 1
42
+ # Add child directories to absolute path and remove from relative path.
43
+ child_path = relative_path.split('/')
44
+ relative_path = child_path.pop
45
+ child_path.each do |dir|
46
+ absolute_path << dir
47
+ end
48
+ end
16
49
 
17
- # When relative path is parent directory.
18
- while relative_path.start_with?('../')
19
- relative_path.delete_prefix! '../'
20
- absolute_path.pop
50
+ file_name = relative_path
21
51
  end
22
52
 
23
53
  # Append default extension.
24
- unless filename.end_with? '.rb'
25
- filename << '.rb'
54
+ unless file_name.end_with? '.rb'
55
+ file_name << '.rb'
26
56
  end
27
57
 
28
- filepath = File.join(absolute_path.join('/'), relative_path)
58
+ file_path = File.join(absolute_path.join('/'), file_name)
29
59
 
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"
60
+ unless @@lit_processed_paths.include? file_path
61
+ @@lit_processed_paths.add file_path
62
+
63
+ new_lines = ''
64
+ line_count = 0
65
+ new_lines_count = 0
66
+
67
+ File.foreach(file_path) do |line|
68
+ line_count = line_count + 1
69
+
70
+ # Pass current directory into the next file's requires.
71
+ if line.strip.start_with? 'require_relative '
72
+ line = line.strip + ", '#{absolute_path.join('/')}'\n"
73
+ new_lines << line
74
+ # Add pry binding on each lit method.
75
+ elsif lit_line = Kernel.add_lit_binding(line)
76
+ new_lines << lit_line
77
+ else
78
+ new_lines << line
79
+ end
37
80
  end
81
+
82
+ eval(new_lines, get_binding(__dir__ = absolute_path), file_path)
83
+
84
+ return true
85
+ # Path has already been required.
86
+ else
87
+ return false
38
88
  end
89
+ end
39
90
 
40
- eval(new_lines)
91
+ def get_binding(__dir__)
92
+ # Don't go to this binding when Pry session activated.
93
+ # The variables have already been evaluated by eval(). NEEDS CONFIRMATION.
94
+ # Setting file_path in eval() negates this fix but will keep just in case.
95
+ return binding unless LitCLI.is_prying?
41
96
  end
42
97
 
43
- # TODO: Investigate RubyGems `require` before overriding.
44
- # def require path
45
- # absolute_path = File.join(Dir.pwd, path)
46
- # #p "requiring #{absolute_path}"
47
- # #p __dir__
48
- # #p File.dirname(File.realpath(__FILE__))
49
- # #p File.realpath(__FILE__)
50
- #
51
- # # Split client-side and server-side code.
52
- # new_lines = []
53
- # binding_line = "binding.pry if LitCLI.is_prying?"
54
- #
55
- # # Add pry binding beneath each lit message.
56
- # p path
57
- # File.foreach(path) do |line|
58
- # new_lines << line
59
- # if line.strip.start_with? 'lit "'
60
- # new_lines << binding_line
61
- # end
62
- # end
63
- #
64
- # eval(new_lines.join)
65
- # end
98
+ def self.add_lit_binding(line)
99
+ block = "{ binding.pry if LitCLI.is_prying?; @@is_prying = false }"
66
100
 
101
+ ['lit ', 'lit(', '🔥 ', '🔥(', '🔥'].each do |needle|
102
+ if line.strip.start_with? needle
103
+ args = line.strip.delete_prefix(needle).delete_suffix(")").chomp
104
+ return "lit(#{args}) #{block} \n"
105
+ end
106
+ end
107
+
108
+ false
109
+ end
67
110
  end
68
111
  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.4.0
4
+ version: 0.6.3
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-07 00:00:00.000000000 Z
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel