lit-cli 0.0.1 → 0.3.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 +29 -0
  3. data/lib/config.rb +115 -0
  4. data/lib/lit_cli.rb +79 -0
  5. metadata +23 -6
  6. data/lib/cli.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9db5febc2b273d72a49fe3567a45eb9f6045fa1d4907677862eba0ce7157ba60
4
- data.tar.gz: 7eed4b5591f6c9d5f689ccda2912c93bdb7c02b66bfda3546cbf7c03945b6713
3
+ metadata.gz: 7da20adf8c48a70324edea2bbd6fd0ecce700d5963062517dbc3a966aab086be
4
+ data.tar.gz: f44d5c8433cfa2a640ea2d8785c742070d15ef5078fa131187c6a21c65bedec5
5
5
  SHA512:
6
- metadata.gz: 2db0ac7511c6adc7f5d9917c2f2c8aedf081b0d6f841c822abb14641e203ee671959719337ee0ced509d6c7175e5bd700ea10ee03f5265db710c89a8c9fdc17d
7
- data.tar.gz: c5be990e305ad1c4dbefe5e25976607109ce0f7ad62c860a021191da3e535b4be875994c734f77c435751dfbd5dab362606f61c9df40a590bf8265e10f5c4f7d
6
+ metadata.gz: b7cb6cf1c1095c166b91ad7b926b30fbb11635b31b2c54aeff776901f20c0ecfae54ebd2d03e88dc9b26a659c462692919cdd113989953e4f89367fa34cde7a0
7
+ data.tar.gz: 7b9938c6416e365a966e91bd659a7448a6ff31fd40ded6427e586ea5277c4d08f575e2f484ee888e4b008bd13f4b814d6f398b1d96bfd4240aaba296cf36b458
data/bin/lit ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ################################################################################
4
+ # Usage:
5
+ # lit <command>
6
+ #
7
+ # Example:
8
+ # lit rails server
9
+ ################################################################################
10
+
11
+ ENV['LIT_ENABLED'] = 'true'
12
+
13
+ # Get flags from arguments.
14
+ args = []
15
+ flags = []
16
+ ARGV.each do |arg|
17
+ if arg.start_with? '@'
18
+ flags << arg.delete_prefix('@')
19
+ else
20
+ args << arg
21
+ end
22
+ end
23
+ ENV['LIT_FLAGS'] = flags.join " "
24
+
25
+ # Get user input.
26
+ command = args.join " "
27
+
28
+ # Action original command.
29
+ system command
data/lib/config.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'set'
2
+
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
+
19
+ class Config
20
+
21
+ # Track errors and only show them once.
22
+ @@errors = Set.new
23
+
24
+ attr_accessor :enabled
25
+ attr_accessor :types
26
+ attr_accessor :type
27
+ attr_accessor :step
28
+ attr_accessor :delay
29
+
30
+ def initialize()
31
+
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 },
39
+ }
40
+
41
+ # Lit is disabled by default, then enabled via the `lit` command.
42
+ # Or it can be permanently enabled, without the use of the `lit` command.
43
+ @enabled = false
44
+
45
+ ##
46
+ # FLAGS.
47
+ #
48
+ # Flag defaults when not supplied via command line.
49
+ ##
50
+
51
+ # Array of types to filter by, for example... [:warn, :error, :fail]
52
+ @type = nil
53
+
54
+ # Boolean on whether or not to step through each lit() breakpoint.
55
+ @step = false
56
+
57
+ # Integer or float representing amount of seconds to delay each lit() by.
58
+ @delay = 0
59
+
60
+ cli_configure()
61
+ end
62
+
63
+ # Override config from command line.
64
+ def cli_configure()
65
+ @enabled = true if ENV['LIT_ENABLED'] == 'true'
66
+
67
+ # Convert flag string to hash.
68
+ flags = {}
69
+ if ENV['LIT_FLAGS'] && !ENV['LIT_FLAGS'].empty?
70
+ ENV['LIT_FLAGS'].split().each do |flag|
71
+ values = flag.split('=')
72
+
73
+ key = values.shift.to_sym
74
+
75
+ # No arguments.
76
+ if values.empty?
77
+ flags[key] = nil
78
+ else
79
+ args = values.pop.split(',')
80
+ # Single argument.
81
+ if args.count == 1
82
+ flags[key] = args.first
83
+ # Multiple arguments.
84
+ else
85
+ flags[key] = args
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ @step = true if flags.has_key? :step
92
+ @type = Array(flags[:type]).map(&:to_sym) if valid? flags, :type
93
+ @delay = flags[:delay].to_f if valid? flags, :delay
94
+ end
95
+
96
+ def valid? flags, flag
97
+ # Has flag even been entered on the command line?
98
+ unless flags.has_key? flag
99
+ return false
100
+ end
101
+
102
+ if flags[flag].nil?
103
+ error = "🔥 ERROR: Invalid argument for @#{flag}."
104
+ unless @@errors.include? error
105
+ @@errors.add error
106
+ puts error
107
+ end
108
+ return false
109
+ end
110
+
111
+ true
112
+ end
113
+
114
+ end
115
+ end
data/lib/lit_cli.rb ADDED
@@ -0,0 +1,79 @@
1
+ require 'pastel'
2
+ require 'config'
3
+
4
+ module LitCLI
5
+ @@pastel = Pastel.new
6
+ @@config = Config.new
7
+
8
+ def lit(message, type = :info)
9
+ if @@config.enabled
10
+ return if filter? type
11
+ render(type)
12
+ step()
13
+ delay()
14
+ end
15
+ end
16
+ alias 🔥 lit
17
+
18
+ def render(type)
19
+ type_config = @@config.types[type]
20
+
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])
24
+
25
+ message = "🔥 #{time_text} #{type_text_color} #{message}"
26
+
27
+ puts message
28
+ end
29
+
30
+ def step()
31
+ if @@config.step
32
+ puts "🔥 PRESS ENTER:"
33
+ input = gets.chomp
34
+ binding while input == nil
35
+ end
36
+ end
37
+
38
+ def filter? type
39
+ unless @@config.type.nil? || @@config.type.include?(type)
40
+ return true
41
+ end
42
+
43
+ false
44
+ end
45
+
46
+ def delay()
47
+ if @@config.delay > 0
48
+ sleep(@@config.delay)
49
+ end
50
+ end
51
+
52
+ def self.colorize(text, color)
53
+ case color
54
+ when :blue
55
+ return @@pastel.bright_blue(text)
56
+ when :green
57
+ return @@pastel.green(text)
58
+ when :yellow
59
+ return @@pastel.yellow(text)
60
+ when :red
61
+ return @@pastel.red(text)
62
+ when :purple, :magenta
63
+ return @@pastel.magenta(text)
64
+ when :cyan
65
+ return @@pastel.cyan(text)
66
+ else
67
+ return text
68
+ end
69
+ end
70
+
71
+ # Override config from application.
72
+ def self.configure()
73
+ yield(@@config)
74
+
75
+ # Override config from command line.
76
+ @@config.cli_configure()
77
+ end
78
+
79
+ end
metadata CHANGED
@@ -1,22 +1,39 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lit-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.3.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-01-30 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Show log messages in the terminal by starting commands with 'lit'.
11
+ date: 2021-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pastel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Console logs that are only visible after prefixing a process with `lit`
14
28
  email: maediprichard@gmail.com
15
- executables: []
29
+ executables:
30
+ - lit
16
31
  extensions: []
17
32
  extra_rdoc_files: []
18
33
  files:
19
- - lib/cli.rb
34
+ - bin/lit
35
+ - lib/config.rb
36
+ - lib/lit_cli.rb
20
37
  homepage: https://reflekt.dev/lit
21
38
  licenses:
22
39
  - MPL-2.0
data/lib/cli.rb DELETED
@@ -1,14 +0,0 @@
1
- ################################################################################
2
- # Usage:
3
- # lit <command>
4
- #
5
- # Example:
6
- # lit bundle exec rails server
7
- ################################################################################
8
-
9
- # Enable Lit API.
10
- ENV['LIT_ENABLED'] = 'true'
11
-
12
- # Action original command.
13
- command = ARGV.join " "
14
- system command