claide 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/claide/helper.rb DELETED
@@ -1,115 +0,0 @@
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
- if paragraph.start_with?(' ' * 4)
37
- paragraph.gsub!(/\n/, "\n#{' ' * indent}")
38
- else
39
- paragraph = wrap_with_indent(paragraph, indent, max_width)
40
- end
41
- paragraph.insert(0, ' ' * indent).rstrip
42
- end
43
- paragraphs.join("\n\n")
44
- end
45
-
46
- # @return [String] Wraps a string to the terminal width taking into
47
- # account the given indentation.
48
- #
49
- # @param [String] string
50
- # The string to indent.
51
- #
52
- # @param [Fixnum] indent
53
- # The number of spaces to insert before the string.
54
- #
55
- # @param [Fixnum] max_width
56
- # The maximum width to use to format the string if the terminal is
57
- # too wide.
58
- #
59
- def self.wrap_with_indent(string, indent = 0, max_width = 80)
60
- if terminal_width == 0
61
- width = max_width
62
- else
63
- width = [terminal_width, max_width].min
64
- end
65
-
66
- full_line = string.gsub("\n", ' ')
67
- available_width = width - indent
68
- space = ' ' * indent
69
- word_wrap(full_line, available_width).split("\n").join("\n#{space}")
70
- end
71
-
72
- # @return [String] Lifted straight from ActionView. Thanks guys!
73
- #
74
- def self.word_wrap(line, line_width)
75
- line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
76
- end
77
-
78
- # @return [String] Lifted straight from ActiveSupport. Thanks guys!
79
- #
80
- def self.strip_heredoc(string)
81
- if min = string.scan(/^[ \t]*(?=\S)/).min
82
- string.gsub(/^[ \t]{#{min.size}}/, '')
83
- else
84
- string
85
- end
86
- end
87
-
88
- # Returns the Levenshtein distance between the given strings.
89
- # From: http://rosettacode.org/wiki/Levenshtein_distance#Ruby
90
- #
91
- # @param [String] a
92
- # The first string to compare.
93
- #
94
- # @param [String] b
95
- # The second string to compare.
96
- #
97
- # @return [Fixnum] The distance between the strings.
98
- #
99
- # rubocop:disable all
100
- def self.levenshtein_distance(a, b)
101
- a, b = a.downcase, b.downcase
102
- costs = Array(0..b.length)
103
- (1..a.length).each do |i|
104
- costs[0], nw = i, i - 1
105
- (1..b.length).each do |j|
106
- costs[j], nw = [
107
- costs[j] + 1, costs[j - 1] + 1, a[i - 1] == b[j - 1] ? nw : nw + 1
108
- ].min, costs[j]
109
- end
110
- end
111
- costs[b.length]
112
- end
113
- # rubocop:enable all
114
- end
115
- end
data/lib/claide/mixins.rb DELETED
@@ -1,25 +0,0 @@
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