simple-cli 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cca558cbc71212156372b50cbbb96f1852a98f52
4
- data.tar.gz: b7015c7ca8d0b8305f57a7927b85920a8940d27c
3
+ metadata.gz: 8806833d77aa2c4e1c4ba7ed10ac0945e11e9e5f
4
+ data.tar.gz: 5e444bf139b52082aa7308bdc432eba4037dc2aa
5
5
  SHA512:
6
- metadata.gz: 7ac446ec3a52cf37763809cca7a8c1cdcf701023527b6871603568679937a92f8e2e830c1169c29243be0157db30631069f9d2c17c3facb7bcd558b694fd77ec
7
- data.tar.gz: 4c236b552b914b003e9e40562e3035c21bf727209a0295492cbaf8c560c681e5f7ba84ae1519914bd8652e58fcf113294335717bbf7b823134cbbc6e0f6b2f7b
6
+ metadata.gz: de98dea9484600fa3cbda2f98c3c9baa6bb6ab7eb48a9c77b886f46146d04c0409157b95cda1404e580ab8a4a0aaef7b80e04c9dfdf783ec1d86271dee7a5519
7
+ data.tar.gz: 122ac23744b706423991bc0a94f5aa620804ebf06b27ebe374e6814357a6d9177e45ae2191ea962111b18f6bd06dc8e43defb73ccae06823f50841604ec013d1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-cli (0.1.4)
4
+ simple-cli (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -4,4 +4,12 @@ module Simple::CLI::Adapter
4
4
 
5
5
  Simple::CLI::Runner.run(self, *args)
6
6
  end
7
+
8
+ def logger=(logger)
9
+ Simple::CLI.logger = logger
10
+ end
11
+
12
+ def logger
13
+ Simple::CLI.logger
14
+ end
7
15
  end
@@ -14,6 +14,10 @@ module Simple::CLI::Helpers
14
14
  exit 1
15
15
  end
16
16
 
17
+ def logger
18
+ ::Simple::CLI.logger
19
+ end
20
+
17
21
  def sh!(cmd, *args)
18
22
  command = Command.new(cmd, *args)
19
23
  result = command.sh
@@ -0,0 +1,36 @@
1
+ class Simple::CLI::Logger::Adapter
2
+ def initialize(logger)
3
+ @logger = logger
4
+ end
5
+
6
+ def method_missing(*args, &block)
7
+ @logger.send *args, &block
8
+ end
9
+
10
+ def success(msg, *args, &block)
11
+ if @logger.respond_to?(:success)
12
+ @logger.send :success, msg, *args, &block
13
+ else
14
+ info "success: #{msg}", *args, &block
15
+ end
16
+ end
17
+
18
+ def benchmark(msg, *args, &block)
19
+ severity = :warn
20
+ if msg.is_a?(Symbol)
21
+ severity, msg = msg, args.shift
22
+ end
23
+
24
+ start = Time.now
25
+ r = yield
26
+
27
+ msg += ": #{(1000 * (Time.now - start)).to_i} msecs."
28
+ send severity, msg, *args
29
+
30
+ r
31
+ rescue StandardError
32
+ msg += "raises #{$!.class.name} after #{(1000 * (Time.now - start)).to_i} msecs."
33
+ send severity, msg, *args
34
+ raise $!
35
+ end
36
+ end
@@ -0,0 +1,97 @@
1
+ module Simple::CLI::Logger::ColoredLogger
2
+ extend self
3
+
4
+ attr :level, true
5
+
6
+ COLORS = {
7
+ clear: "\e[0m", # Embed in a String to clear all previous ANSI sequences.
8
+ bold: "\e[1m", # The start of an ANSI bold sequence.
9
+ black: "\e[30m", # Set the terminal's foreground ANSI color to black.
10
+ red: "\e[31m", # Set the terminal's foreground ANSI color to red.
11
+ green: "\e[32m", # Set the terminal's foreground ANSI color to green.
12
+ yellow: "\e[33m", # Set the terminal's foreground ANSI color to yellow.
13
+ blue: "\e[34m", # Set the terminal's foreground ANSI color to blue.
14
+ magenta: "\e[35m", # Set the terminal's foreground ANSI color to magenta.
15
+ cyan: "\e[36m", # Set the terminal's foreground ANSI color to cyan.
16
+ white: "\e[37m", # Set the terminal's foreground ANSI color to white.
17
+
18
+ on_black: "\e[40m", # Set the terminal's background ANSI color to black.
19
+ on_red: "\e[41m", # Set the terminal's background ANSI color to red.
20
+ on_green: "\e[42m", # Set the terminal's background ANSI color to green.
21
+ on_yellow: "\e[43m", # Set the terminal's background ANSI color to yellow.
22
+ on_blue: "\e[44m", # Set the terminal's background ANSI color to blue.
23
+ on_magenta: "\e[45m", # Set the terminal's background ANSI color to magenta.
24
+ on_cyan: "\e[46m", # Set the terminal's background ANSI color to cyan.
25
+ on_white: "\e[47m" # Set the terminal's background ANSI color to white.
26
+ }
27
+
28
+ @@started_at = Time.now
29
+
30
+ MESSAGE_COLOR = {
31
+ :info => :cyan,
32
+ :warn => :yellow,
33
+ :error => :red,
34
+ :success => :green,
35
+ }
36
+
37
+ def debug(msg, *args)
38
+ log :debug, msg, *args
39
+ end
40
+
41
+ def info(msg, *args)
42
+ log :info, msg, *args
43
+ end
44
+
45
+ def warn(msg, *args)
46
+ log :warn, msg, *args
47
+ end
48
+
49
+ def error(msg, *args)
50
+ log :error, msg, *args
51
+ end
52
+
53
+ def success(msg, *args)
54
+ log :success, msg, *args
55
+ end
56
+
57
+ private
58
+
59
+ REQUIRED_LOG_LEVELS = {
60
+ debug: ::Logger::DEBUG,
61
+ info: ::Logger::INFO,
62
+ warn: ::Logger::WARN,
63
+ error: ::Logger::ERROR,
64
+ success: ::Logger::INFO
65
+ }
66
+
67
+ def log(sym, msg, *args)
68
+ log_level = level
69
+ required_log_level = REQUIRED_LOG_LEVELS.fetch(sym)
70
+ return if required_log_level < log_level
71
+
72
+ formatted_runtime = "%.3f secs" % (Time.now - @@started_at)
73
+ msg = "[#{formatted_runtime}] #{msg}"
74
+ unless args.empty?
75
+ msg += ": " + args.map(&:inspect).join(", ")
76
+ end
77
+
78
+ msg_length = msg.length
79
+
80
+ if color = COLORS[MESSAGE_COLOR[sym]]
81
+ msg = "#{color}#{msg}#{COLORS[:clear]}"
82
+ end
83
+
84
+ if log_level < Logger::INFO
85
+ padding = " " * (90 - msg_length) if msg_length < 90
86
+ msg = "#{msg}#{padding}"
87
+
88
+ source = caller[2]
89
+ source.gsub!( Dir.getwd, "." )
90
+ source.gsub!( File.expand_path("~"), "~" )
91
+
92
+ msg = "#{msg}from #{source}"
93
+ end
94
+
95
+ STDERR.puts msg
96
+ end
97
+ end
@@ -1,22 +1,25 @@
1
+ module Simple::CLI::Logger
2
+ end
3
+
1
4
  require "logger"
2
5
 
6
+ require_relative "logger/adapter"
7
+ require_relative "logger/colored_logger"
8
+
3
9
  module Simple::CLI::Logger
4
- def logger=(logger)
5
- @logger = logger
10
+ def logger
11
+ @logger ||= Adapter.new(default_logger)
6
12
  end
7
13
 
8
- def logger
9
- @logger ||= build_default_logger
14
+ def logger=(logger)
15
+ @logger = Adapter.new(logger)
10
16
  end
11
17
 
12
18
  private
13
19
 
14
- def build_default_logger
15
- logger = Logger.new(STDOUT)
16
- logger.formatter = proc do |severity, _datetime, _progname, msg|
17
- "#{severity}: #{msg}\n"
18
- end
19
- logger.level = Logger::INFO
20
+ def default_logger
21
+ logger = STDERR.isatty ? ColoredLogger : ::Logger.new(STDERR)
22
+ logger.level = ::Logger::INFO
20
23
  logger
21
24
  end
22
25
  end
@@ -0,0 +1,10 @@
1
+ require "pp"
2
+
3
+ begin
4
+ require "awesome_print"
5
+
6
+ def pp(*args)
7
+ ap(*args)
8
+ end
9
+ rescue LoadError
10
+ end
@@ -26,10 +26,8 @@ class Simple::CLI::Runner
26
26
  def extract_default_flags!(args)
27
27
  args.reject! do |arg|
28
28
  case arg
29
- when "-v", "--verbose" then
30
- logger.level = Logger::DEBUG
31
- when "-q", "--quiet" then
32
- logger.level = Logger::WARN
29
+ when "--verbose", "-v" then logger.level = Logger::DEBUG
30
+ when "--quiet", "-q" then logger.level = Logger::WARN
33
31
  end
34
32
  end
35
33
  end
@@ -79,7 +77,7 @@ class Simple::CLI::Runner
79
77
  end
80
78
 
81
79
  def on_exception(e)
82
- raise(e) if Simple::CLI::DEBUG
80
+ raise(e) if Simple::CLI.logger.level == Logger::DEBUG
83
81
 
84
82
  case e
85
83
  when ArgumentError
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module CLI
3
- VERSION = "0.1.4"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/simple/cli.rb CHANGED
@@ -1,26 +1,21 @@
1
1
  module Simple; end
2
2
  module Simple::CLI; end
3
3
 
4
+ require_relative "cli/pp"
5
+
4
6
  require_relative "cli/helpers"
5
7
  require_relative "cli/runner"
6
8
  require_relative "cli/adapter"
7
9
  require_relative "cli/logger"
8
10
 
9
- require "pp"
10
-
11
11
  module Simple::CLI
12
- DEBUG = true
12
+ extend ::Simple::CLI::Logger
13
13
 
14
14
  def self.included(base)
15
15
  base.extend(::Simple::CLI::Adapter)
16
16
  base.include(::Simple::CLI::Helpers)
17
17
  end
18
18
 
19
- extend ::Simple::CLI::Logger
20
- def logger
21
- Simple::CLI::Logger.logger
22
- end
23
-
24
19
  # Simple::CLI.run! is called from Runner.run. It is called with a method
25
20
  # name, which is derived from the command passed in via the command line,
26
21
  # and parsed arguments.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-02-16 00:00:00.000000000 Z
12
+ date: 2018-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -84,6 +84,9 @@ files:
84
84
  - lib/simple/cli/adapter.rb
85
85
  - lib/simple/cli/helpers.rb
86
86
  - lib/simple/cli/logger.rb
87
+ - lib/simple/cli/logger/adapter.rb
88
+ - lib/simple/cli/logger/colored_logger.rb
89
+ - lib/simple/cli/pp.rb
87
90
  - lib/simple/cli/runner.rb
88
91
  - lib/simple/cli/runner/autocompletion.rb
89
92
  - lib/simple/cli/runner/command_help.rb