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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/simple/cli/adapter.rb +8 -0
- data/lib/simple/cli/helpers.rb +4 -0
- data/lib/simple/cli/logger/adapter.rb +36 -0
- data/lib/simple/cli/logger/colored_logger.rb +97 -0
- data/lib/simple/cli/logger.rb +13 -10
- data/lib/simple/cli/pp.rb +10 -0
- data/lib/simple/cli/runner.rb +3 -5
- data/lib/simple/cli/version.rb +1 -1
- data/lib/simple/cli.rb +3 -8
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8806833d77aa2c4e1c4ba7ed10ac0945e11e9e5f
|
4
|
+
data.tar.gz: 5e444bf139b52082aa7308bdc432eba4037dc2aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de98dea9484600fa3cbda2f98c3c9baa6bb6ab7eb48a9c77b886f46146d04c0409157b95cda1404e580ab8a4a0aaef7b80e04c9dfdf783ec1d86271dee7a5519
|
7
|
+
data.tar.gz: 122ac23744b706423991bc0a94f5aa620804ebf06b27ebe374e6814357a6d9177e45ae2191ea962111b18f6bd06dc8e43defb73ccae06823f50841604ec013d1
|
data/Gemfile.lock
CHANGED
data/lib/simple/cli/adapter.rb
CHANGED
data/lib/simple/cli/helpers.rb
CHANGED
@@ -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
|
data/lib/simple/cli/logger.rb
CHANGED
@@ -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
|
5
|
-
@logger
|
10
|
+
def logger
|
11
|
+
@logger ||= Adapter.new(default_logger)
|
6
12
|
end
|
7
13
|
|
8
|
-
def logger
|
9
|
-
@logger
|
14
|
+
def logger=(logger)
|
15
|
+
@logger = Adapter.new(logger)
|
10
16
|
end
|
11
17
|
|
12
18
|
private
|
13
19
|
|
14
|
-
def
|
15
|
-
logger = Logger.new(
|
16
|
-
logger.
|
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
|
data/lib/simple/cli/runner.rb
CHANGED
@@ -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 "
|
30
|
-
|
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
|
data/lib/simple/cli/version.rb
CHANGED
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
|
-
|
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.
|
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-
|
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
|