lit-cli 0.2.0 → 0.6.1

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 +42 -17
  3. data/lib/lit_cli.rb +111 -38
  4. data/lib/lit_pry.rb +96 -0
  5. metadata +18 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 642edf5650f6fbfd2b92ed421ee1b7a205c6fbe7fa14a4e9c24fe06d07d443c8
4
- data.tar.gz: 9b090b0c6a37d47d2257eb5e1723df8ea5793e18d9eddb224a06817e111501ad
3
+ metadata.gz: 39a417c815a30ecdf0e99aaa9aa47d22b4def3f2d891f6da1911414a7187a86b
4
+ data.tar.gz: 5bb7af898274544027202aedb33d9428401044867ad0693ae6314b1c361c9418
5
5
  SHA512:
6
- metadata.gz: 122442d065db2aaf73e01d4629940a57e12b5ae88bdd2bf57bcdaddaab2548950b9ee522ca1080ad5577bb9ab7e3db5aea61db0d7ca319f1c3a1415225bf76af
7
- data.tar.gz: 3c438491edf8cd0b0c7854321486f37c0a443ccac2ed6d1de32776b1aa05822152030d73fed0a315e832ebea4d688a82484fa1ce6c3aaef5d26ddc7782e258d7
6
+ metadata.gz: 1b45c6ae31e1db4cf3ac3fd338e8a11df273618cd8aa8b7d870d3764c9ae86b472b5823504fec11956c33fcc7fe0ca044db544e6c872f167a9db7bbfeaf881b5
7
+ data.tar.gz: 47a8560f08cbd269877dcd1a6bd14b718a684b190991b80b439602fe766b15f84bc1c44826aafed8416277e2fb425f66d35d447f32676cacdc8d9ef4fa31a8a0
data/lib/config.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'set'
2
+
1
3
  module LitCLI
2
4
 
3
5
  ##############################################################################
@@ -16,23 +18,31 @@ module LitCLI
16
18
 
17
19
  class Config
18
20
 
21
+ # Track errors and only show them once.
22
+ @@errors = Set.new
23
+
19
24
  attr_accessor :enabled
25
+ attr_accessor :statuses
20
26
  attr_accessor :types
21
- attr_accessor :type
27
+ # Flags.
22
28
  attr_accessor :step
29
+ attr_accessor :status
30
+ attr_accessor :type
23
31
  attr_accessor :delay
24
32
 
25
33
  def initialize()
26
34
 
27
- @types = {
28
- :info => { icon: "ℹ", color: :blue },
29
- :pass => { icon: "✔", color: :green },
30
- :warn => { icon: "⚠", color: :yellow },
31
- :fail => { icon: "⨯", color: :red },
32
- :error => { icon: "!", color: :red },
33
- :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] },
34
42
  }
35
43
 
44
+ @types = nil
45
+
36
46
  # Lit is disabled by default, then enabled via the `lit` command.
37
47
  # Or it can be permanently enabled, without the use of the `lit` command.
38
48
  @enabled = false
@@ -43,12 +53,15 @@ module LitCLI
43
53
  # Flag defaults when not supplied via command line.
44
54
  ##
45
55
 
46
- # Array of types to filter by, for example... [:warn, :error, :fail]
47
- @type = nil
48
-
49
56
  # Boolean on whether or not to step through each lit() breakpoint.
50
57
  @step = false
51
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
+
52
65
  # Integer or float representing amount of seconds to delay each lit() by.
53
66
  @delay = 0
54
67
 
@@ -83,16 +96,28 @@ module LitCLI
83
96
  end
84
97
  end
85
98
 
86
- @type = flags[:type] if flags.has_key? :type
87
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
88
104
 
89
- if flags.has_key? :delay
90
- unless flags[:delay].nil?
91
- @delay = flags[:delay].to_f
92
- else
93
- puts "🔥 ERROR: Invalid argument."
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
94
116
  end
117
+ return false
95
118
  end
119
+
120
+ true
96
121
  end
97
122
 
98
123
  end
data/lib/lit_cli.rb CHANGED
@@ -1,67 +1,144 @@
1
+ require 'pry'
1
2
  require 'pastel'
2
3
  require 'config'
4
+ require 'lit_pry'
3
5
 
4
6
  module LitCLI
5
7
  @@pastel = Pastel.new
6
8
  @@config = Config.new
9
+ @@is_prying = false
7
10
 
8
- def lit(message, type = :info)
11
+ def lit(message, status = :info, type = nil, context = nil)
9
12
  if @@config.enabled
10
- filter(type)
11
- render(type)
12
- step()
13
- delay()
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()
14
18
  end
15
19
  end
16
20
  alias 🔥 lit
17
21
 
18
- def render(type)
19
- type_config = @@config.types[type]
22
+ def self.render(message, status, type, context)
23
+ text = "🔥"
24
+
25
+ # Time.
26
+ time = LitCLI.format(Time.now().strftime("%k:%M"), color: :cyan)
27
+ text << " #{time}"
28
+
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
41
+ end
20
42
 
21
- time_text = LitCLI.colorize(Time.now().strftime("%k:%M"), :cyan)
22
- type_text = type_config[:icon] + " " + type.to_s
23
- type_text_color = LitCLI.colorize(type_text, type_config[:color])
43
+ # Context.
44
+ text << LitCLI.format(" #{context}", styles: [:bold, :dim])
24
45
 
25
- message = "🔥 #{time_text} #{type_text_color} #{message}"
46
+ # Message.
47
+ text << " #{message}"
26
48
 
27
- puts message
49
+ puts text
28
50
  end
29
51
 
30
- def filter(type)
31
- if @@config.type
32
-
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"
33
58
  end
34
59
  end
35
60
 
36
- def step()
37
- if @@config.step
61
+ def self.filter_status? status
62
+ unless @@config.status.nil? || @@config.status.include?(status)
63
+ return true
64
+ end
65
+
66
+ false
67
+ end
38
68
 
69
+ def self.filter_type? type
70
+ unless @@config.type.nil? || @@config.type.include?(type)
71
+ return true
39
72
  end
73
+
74
+ false
40
75
  end
41
76
 
42
- def delay()
77
+ def self.delay()
43
78
  if @@config.delay > 0
44
79
  sleep(@@config.delay)
45
80
  end
46
81
  end
47
82
 
48
- def self.colorize(text, color)
49
- case color
50
- when :blue
51
- return @@pastel.bright_blue(text)
52
- when :green
53
- return @@pastel.green(text)
54
- when :yellow
55
- return @@pastel.yellow(text)
56
- when :red
57
- return @@pastel.red(text)
58
- when :purple, :magenta
59
- return @@pastel.magenta(text)
60
- when :cyan
61
- return @@pastel.cyan(text)
62
- else
63
- return text
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
64
139
  end
140
+
141
+ text
65
142
  end
66
143
 
67
144
  # Override config from application.
@@ -72,8 +149,4 @@ module LitCLI
72
149
  @@config.cli_configure()
73
150
  end
74
151
 
75
- def self.error(message)
76
- puts "🔥 ERROR: #{message}"
77
- end
78
-
79
152
  end
data/lib/lit_pry.rb ADDED
@@ -0,0 +1,96 @@
1
+ # Only override kernel methods when Lit's @step flag is true.
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
+
8
+ module Kernel
9
+
10
+ @@lit_processed_paths = Set.new
11
+
12
+ def require_relative relative_path, current_directory = nil
13
+ return false if relative_path.nil?
14
+
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
49
+
50
+ file_name = relative_path
51
+ end
52
+
53
+ # Append default extension.
54
+ unless file_name.end_with? '.rb'
55
+ file_name << '.rb'
56
+ end
57
+
58
+ file_path = File.join(absolute_path.join('/'), file_name)
59
+
60
+ unless @@lit_processed_paths.include? file_path
61
+ @@lit_processed_paths.add file_path
62
+
63
+ new_lines = ''
64
+ File.foreach(file_path) do |line|
65
+
66
+ # Pass current directory into the next file's requires.
67
+ if line.strip.start_with? 'require_relative '
68
+ line = line.strip + ", '#{absolute_path.join('/')}'\n"
69
+ new_lines << line
70
+ else
71
+ new_lines << line
72
+ end
73
+
74
+ # Add pry binding beneath each lit message.
75
+ if line.strip.start_with? 'lit "'
76
+ new_lines << "binding.pry if LitCLI.is_prying?\n"
77
+ new_lines << "@@is_prying = false\n"
78
+ end
79
+ end
80
+
81
+ eval(new_lines, get_binding(__dir__ = absolute_path), file_path)
82
+ return true
83
+ # Path has already been required.
84
+ else
85
+ return false
86
+ end
87
+ end
88
+
89
+ def get_binding(__dir__)
90
+ # Don't go to this binding when Pry session activated.
91
+ # The variables have already been evaluated by eval(). NEEDS CONFIRMATION.
92
+ # Setting file_path in eval() negates this fix but will keep just in case.
93
+ return binding unless LitCLI.is_prying?
94
+ end
95
+ end
96
+ 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.2.0
4
+ version: 0.6.1
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-06 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