claide 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +4 -3
- data/lib/claide.rb +4 -3
- data/lib/claide/ansi.rb +126 -0
- data/lib/claide/ansi/cursor.rb +69 -0
- data/lib/claide/ansi/graphics.rb +72 -0
- data/lib/claide/ansi/string_escaper.rb +81 -0
- data/lib/claide/argv.rb +63 -108
- data/lib/claide/argv/parser.rb +83 -0
- data/lib/claide/command.rb +245 -300
- data/lib/claide/command/banner.rb +133 -117
- data/lib/claide/command/banner/prettifier.rb +59 -0
- data/lib/claide/command/options.rb +87 -0
- data/lib/claide/command/parser.rb +47 -0
- data/lib/claide/command/plugins_helper.rb +112 -0
- data/lib/claide/command/shell_completion_helper.rb +39 -0
- data/lib/claide/command/shell_completion_helper/zsh_completion_generator.rb +191 -0
- data/lib/claide/command/validation_helper.rb +82 -0
- data/lib/claide/help.rb +2 -18
- data/lib/claide/helper.rb +113 -0
- data/lib/claide/informative_error.rb +0 -2
- data/lib/claide/mixins.rb +25 -0
- metadata +16 -2
data/lib/claide/help.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module CLAide
|
4
|
-
|
5
4
|
require 'claide/informative_error'
|
6
5
|
|
7
6
|
# The exception class that is raised to indicate a help banner should be
|
@@ -20,20 +19,6 @@ module CLAide
|
|
20
19
|
#
|
21
20
|
attr_reader :error_message
|
22
21
|
|
23
|
-
# @return [Bool] Whether the error message should use ANSI codes to
|
24
|
-
# prettify output.
|
25
|
-
#
|
26
|
-
attr_reader :ansi_output
|
27
|
-
alias_method :ansi_output?, :ansi_output
|
28
|
-
|
29
|
-
def colorize
|
30
|
-
warn "[!] The use of `CLAide::Help#colorize` has been " \
|
31
|
-
"deprecated. Use `CLAide::Help#ansi_output` instead. " \
|
32
|
-
"(Called from: #{caller.first})"
|
33
|
-
ansi_output
|
34
|
-
end
|
35
|
-
alias_method :colorize?, :colorize
|
36
|
-
|
37
22
|
# @param [String] banner @see banner
|
38
23
|
# @param [String] error_message @see error_message
|
39
24
|
#
|
@@ -41,10 +26,9 @@ module CLAide
|
|
41
26
|
# terminate the program with, will be set to `1`, otherwise a {Help}
|
42
27
|
# exception is treated as not being a real error and exits with `0`.
|
43
28
|
#
|
44
|
-
def initialize(banner, error_message = nil
|
29
|
+
def initialize(banner, error_message = nil)
|
45
30
|
@banner = banner
|
46
31
|
@error_message = error_message
|
47
|
-
@ansi_output = ansi_output
|
48
32
|
@exit_status = @error_message.nil? ? 0 : 1
|
49
33
|
end
|
50
34
|
|
@@ -61,7 +45,7 @@ module CLAide
|
|
61
45
|
# @return [String]
|
62
46
|
#
|
63
47
|
def prettify_error_message(message)
|
64
|
-
|
48
|
+
message.ansi.red
|
65
49
|
end
|
66
50
|
|
67
51
|
# @return [String] The optional error message, combined with the help
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CLAide
|
4
|
+
module Helper
|
5
|
+
# @return [Fixnum] The width of the current terminal, unless being piped.
|
6
|
+
#
|
7
|
+
def self.terminal_width
|
8
|
+
unless @terminal_width
|
9
|
+
if STDOUT.tty? && system('which tput > /dev/null 2>&1')
|
10
|
+
@terminal_width = `tput cols`.to_i
|
11
|
+
else
|
12
|
+
@terminal_width = 0
|
13
|
+
end
|
14
|
+
end
|
15
|
+
@terminal_width
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [String] Formats a markdown string by stripping heredoc
|
19
|
+
# indentation and wrapping by word to the terminal width taking
|
20
|
+
# into account a maximum one, and indenting the string. Code lines
|
21
|
+
# (i.e. indented by four spaces) are not word wrapped.
|
22
|
+
#
|
23
|
+
# @param [String] string
|
24
|
+
# The string to format.
|
25
|
+
#
|
26
|
+
# @param [Fixnum] indent
|
27
|
+
# The number of spaces to insert before the string.
|
28
|
+
#
|
29
|
+
# @param [Fixnum] max_width
|
30
|
+
# The maximum width to use to format the string if the terminal is
|
31
|
+
# too wide.
|
32
|
+
#
|
33
|
+
def self.format_markdown(string, indent = 0, max_width = 80)
|
34
|
+
paragraphs = Helper.strip_heredoc(string).split("\n\n")
|
35
|
+
paragraphs = paragraphs.map do |paragraph|
|
36
|
+
unless paragraph.start_with?(' ' * 4)
|
37
|
+
full_line = paragraph.gsub("\n", ' ')
|
38
|
+
paragraph = wrap_with_indent(full_line, indent, max_width)
|
39
|
+
end
|
40
|
+
paragraph.insert(0, ' ' * indent).rstrip
|
41
|
+
end
|
42
|
+
paragraphs.join("\n\n")
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [String] Wraps a string to the terminal width taking into
|
46
|
+
# account the given indentation.
|
47
|
+
#
|
48
|
+
# @param [String] string
|
49
|
+
# The string to indent.
|
50
|
+
#
|
51
|
+
# @param [Fixnum] indent
|
52
|
+
# The number of spaces to insert before the string.
|
53
|
+
#
|
54
|
+
# @param [Fixnum] max_width
|
55
|
+
# The maximum width to use to format the string if the terminal is
|
56
|
+
# too wide.
|
57
|
+
#
|
58
|
+
def self.wrap_with_indent(string, indent = 0, max_width = 80)
|
59
|
+
if terminal_width == 0
|
60
|
+
width = max_width
|
61
|
+
else
|
62
|
+
width = [terminal_width, max_width].min
|
63
|
+
end
|
64
|
+
|
65
|
+
available_width = width - indent
|
66
|
+
space = ' ' * indent
|
67
|
+
word_wrap(string, available_width).split("\n").join("\n#{space}")
|
68
|
+
end
|
69
|
+
|
70
|
+
# @return [String] Lifted straight from ActionView. Thanks guys!
|
71
|
+
#
|
72
|
+
def self.word_wrap(line, line_width)
|
73
|
+
line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
|
74
|
+
end
|
75
|
+
|
76
|
+
# @return [String] Lifted straight from ActiveSupport. Thanks guys!
|
77
|
+
#
|
78
|
+
def self.strip_heredoc(string)
|
79
|
+
if min = string.scan(/^[ \t]*(?=\S)/).min
|
80
|
+
string.gsub(/^[ \t]{#{min.size}}/, '')
|
81
|
+
else
|
82
|
+
string
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns the Levenshtein distance between the given strings.
|
87
|
+
# From: http://rosettacode.org/wiki/Levenshtein_distance#Ruby
|
88
|
+
#
|
89
|
+
# @param [String] a
|
90
|
+
# The first string to compare.
|
91
|
+
#
|
92
|
+
# @param [String] b
|
93
|
+
# The second string to compare.
|
94
|
+
#
|
95
|
+
# @return [Fixnum] The distance between the strings.
|
96
|
+
#
|
97
|
+
# rubocop:disable all
|
98
|
+
def self.levenshtein_distance(a, b)
|
99
|
+
a, b = a.downcase, b.downcase
|
100
|
+
costs = Array(0..b.length)
|
101
|
+
(1..a.length).each do |i|
|
102
|
+
costs[0], nw = i, i - 1
|
103
|
+
(1..b.length).each do |j|
|
104
|
+
costs[j], nw = [
|
105
|
+
costs[j] + 1, costs[j - 1] + 1, a[i - 1] == b[j - 1] ? nw : nw + 1
|
106
|
+
].min, costs[j]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
costs[b.length]
|
110
|
+
end
|
111
|
+
# rubocop:enable all
|
112
|
+
end
|
113
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module CLAide
|
4
|
-
|
5
4
|
# Including this module into an exception class will ensure that when raised,
|
6
5
|
# while running {Command.run}, only the message of the exception will be
|
7
6
|
# shown to the user. Unless disabled with the `--verbose` flag.
|
@@ -10,7 +9,6 @@ module CLAide
|
|
10
9
|
# is set to `true`.
|
11
10
|
#
|
12
11
|
module InformativeError
|
13
|
-
|
14
12
|
# @return [Numeric] The exist status code that should be used to terminate
|
15
13
|
# the program with. Defaults to `1`.
|
16
14
|
#
|
@@ -0,0 +1,25 @@
|
|
1
|
+
unless String.method_defined? :strip_margin
|
2
|
+
class String
|
3
|
+
# @return [String] The method strips the characters preceding a special
|
4
|
+
# margin character. Useful for HEREDOCs and other multi-line strings.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
#
|
8
|
+
# code = <<-END.strip_margin('|')
|
9
|
+
# |def test
|
10
|
+
# | some_method
|
11
|
+
# | other_method
|
12
|
+
# |end
|
13
|
+
# END
|
14
|
+
#
|
15
|
+
# #=> "def\n some_method\n \nother_method\nend"
|
16
|
+
#
|
17
|
+
# @note Extracted from the powerpack gem available under the MIT license
|
18
|
+
# and copyright (c) 2013 Bozhidar Batsov.
|
19
|
+
#
|
20
|
+
def strip_margin(margin_characters)
|
21
|
+
margin = Regexp.quote(margin_characters)
|
22
|
+
gsub(/^\s+#{margin}/, '')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: claide
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eloy Duran
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -22,11 +22,25 @@ files:
|
|
22
22
|
- LICENSE
|
23
23
|
- README.markdown
|
24
24
|
- lib/claide.rb
|
25
|
+
- lib/claide/ansi.rb
|
26
|
+
- lib/claide/ansi/cursor.rb
|
27
|
+
- lib/claide/ansi/graphics.rb
|
28
|
+
- lib/claide/ansi/string_escaper.rb
|
25
29
|
- lib/claide/argv.rb
|
30
|
+
- lib/claide/argv/parser.rb
|
26
31
|
- lib/claide/command.rb
|
27
32
|
- lib/claide/command/banner.rb
|
33
|
+
- lib/claide/command/banner/prettifier.rb
|
34
|
+
- lib/claide/command/options.rb
|
35
|
+
- lib/claide/command/parser.rb
|
36
|
+
- lib/claide/command/plugins_helper.rb
|
37
|
+
- lib/claide/command/shell_completion_helper.rb
|
38
|
+
- lib/claide/command/shell_completion_helper/zsh_completion_generator.rb
|
39
|
+
- lib/claide/command/validation_helper.rb
|
28
40
|
- lib/claide/help.rb
|
41
|
+
- lib/claide/helper.rb
|
29
42
|
- lib/claide/informative_error.rb
|
43
|
+
- lib/claide/mixins.rb
|
30
44
|
homepage: https://github.com/CocoaPods/CLAide
|
31
45
|
licenses:
|
32
46
|
- MIT
|