brakeman 4.5.0 → 4.5.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of brakeman might be problematic. Click here for more details.

Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +15 -0
  3. data/README.md +6 -6
  4. data/bundle/load.rb +3 -3
  5. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/AUTHORS +0 -0
  6. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/COPYING +0 -0
  7. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/Changelog.md +211 -15
  8. data/bundle/ruby/2.5.0/gems/highline-2.0.2/Gemfile +22 -0
  9. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/LICENSE +0 -0
  10. data/bundle/ruby/2.5.0/gems/highline-2.0.2/README.md +202 -0
  11. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/TODO +0 -0
  12. data/bundle/ruby/2.5.0/gems/highline-2.0.2/appveyor.yml +37 -0
  13. data/bundle/ruby/2.5.0/gems/highline-2.0.2/highline.gemspec +35 -0
  14. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline.rb +650 -0
  15. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/builtin_styles.rb +129 -0
  16. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/lib/highline/color_scheme.rb +49 -32
  17. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/compatibility.rb +23 -0
  18. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/custom_errors.rb +57 -0
  19. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/import.rb +48 -0
  20. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/io_console_compatible.rb +37 -0
  21. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/list.rb +177 -0
  22. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/list_renderer.rb +261 -0
  23. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/menu.rb +576 -0
  24. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/menu/item.rb +32 -0
  25. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/paginator.rb +52 -0
  26. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/lib/highline/question.rb +281 -131
  27. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/question/answer_converter.rb +103 -0
  28. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/question_asker.rb +150 -0
  29. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/simulate.rb +59 -0
  30. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/statement.rb +88 -0
  31. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/string.rb +36 -0
  32. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/string_extensions.rb +130 -0
  33. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/style.rb +325 -0
  34. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/template_renderer.rb +62 -0
  35. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/terminal.rb +190 -0
  36. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/terminal/io_console.rb +36 -0
  37. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/terminal/ncurses.rb +38 -0
  38. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/terminal/unix_stty.rb +51 -0
  39. data/bundle/ruby/2.5.0/gems/{highline-1.7.10 → highline-2.0.2}/lib/highline/version.rb +3 -1
  40. data/bundle/ruby/2.5.0/gems/highline-2.0.2/lib/highline/wrapper.rb +53 -0
  41. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/History.rdoc +32 -0
  42. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/Manifest.txt +0 -0
  43. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/README.rdoc +0 -0
  44. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/compare/normalize.rb +0 -0
  45. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/debugging.md +0 -0
  46. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/rp_extensions.rb +1 -1
  47. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/rp_stringscanner.rb +0 -0
  48. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby20_parser.rb +2427 -2432
  49. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby20_parser.y +32 -29
  50. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby21_parser.rb +2101 -2109
  51. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby21_parser.y +32 -29
  52. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby22_parser.rb +2080 -2095
  53. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby22_parser.y +32 -29
  54. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0/lib/ruby25_parser.rb → ruby_parser-3.13.1/lib/ruby23_parser.rb} +2339 -2333
  55. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby23_parser.y +32 -29
  56. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby24_parser.rb +2347 -2335
  57. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby24_parser.y +32 -23
  58. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0/lib/ruby23_parser.rb → ruby_parser-3.13.1/lib/ruby25_parser.rb} +2349 -2337
  59. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby25_parser.y +32 -23
  60. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby26_parser.rb +2351 -2338
  61. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby26_parser.y +32 -23
  62. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby_lexer.rb +253 -161
  63. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby_lexer.rex +25 -25
  64. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby_lexer.rex.rb +68 -26
  65. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby_parser.rb +3 -1
  66. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby_parser.yy +34 -23
  67. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/lib/ruby_parser_extras.rb +64 -43
  68. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/tools/munge.rb +2 -1
  69. data/bundle/ruby/2.5.0/gems/{ruby_parser-3.13.0 → ruby_parser-3.13.1}/tools/ripper.rb +6 -1
  70. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/CHANGELOG.md +4 -0
  71. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/MIT-LICENSE.txt +0 -0
  72. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/README.md +1 -1
  73. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/data/display_width.marshal.gz +0 -0
  74. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/lib/unicode/display_width.rb +0 -0
  75. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/lib/unicode/display_width/constants.rb +2 -2
  76. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/lib/unicode/display_width/index.rb +0 -0
  77. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/lib/unicode/display_width/no_string_ext.rb +0 -0
  78. data/bundle/ruby/2.5.0/gems/{unicode-display_width-1.5.0 → unicode-display_width-1.6.0}/lib/unicode/display_width/string_ext.rb +0 -0
  79. data/lib/brakeman.rb +7 -0
  80. data/lib/brakeman/app_tree.rb +34 -22
  81. data/lib/brakeman/checks.rb +7 -7
  82. data/lib/brakeman/checks/base_check.rb +9 -9
  83. data/lib/brakeman/checks/check_cross_site_scripting.rb +5 -0
  84. data/lib/brakeman/checks/check_default_routes.rb +5 -0
  85. data/lib/brakeman/checks/check_deserialize.rb +52 -0
  86. data/lib/brakeman/checks/check_dynamic_finders.rb +1 -1
  87. data/lib/brakeman/checks/check_force_ssl.rb +27 -0
  88. data/lib/brakeman/checks/check_json_parsing.rb +5 -0
  89. data/lib/brakeman/checks/check_link_to_href.rb +6 -1
  90. data/lib/brakeman/checks/check_mail_to.rb +1 -1
  91. data/lib/brakeman/checks/check_model_attr_accessible.rb +1 -1
  92. data/lib/brakeman/checks/check_model_attributes.rb +12 -50
  93. data/lib/brakeman/checks/check_model_serialize.rb +1 -1
  94. data/lib/brakeman/checks/check_nested_attributes_bypass.rb +3 -3
  95. data/lib/brakeman/checks/check_secrets.rb +1 -1
  96. data/lib/brakeman/checks/check_session_settings.rb +10 -10
  97. data/lib/brakeman/checks/check_simple_format.rb +5 -0
  98. data/lib/brakeman/checks/check_skip_before_filter.rb +1 -1
  99. data/lib/brakeman/checks/check_sql.rb +15 -17
  100. data/lib/brakeman/checks/check_validation_regex.rb +1 -1
  101. data/lib/brakeman/file_parser.rb +6 -8
  102. data/lib/brakeman/file_path.rb +71 -0
  103. data/lib/brakeman/options.rb +7 -0
  104. data/lib/brakeman/parsers/template_parser.rb +3 -3
  105. data/lib/brakeman/processor.rb +3 -4
  106. data/lib/brakeman/processors/alias_processor.rb +12 -6
  107. data/lib/brakeman/processors/base_processor.rb +8 -7
  108. data/lib/brakeman/processors/controller_alias_processor.rb +10 -7
  109. data/lib/brakeman/processors/controller_processor.rb +5 -9
  110. data/lib/brakeman/processors/haml_template_processor.rb +5 -0
  111. data/lib/brakeman/processors/lib/module_helper.rb +8 -8
  112. data/lib/brakeman/processors/lib/processor_helper.rb +3 -3
  113. data/lib/brakeman/processors/lib/rails2_config_processor.rb +3 -3
  114. data/lib/brakeman/processors/lib/rails2_route_processor.rb +2 -2
  115. data/lib/brakeman/processors/lib/rails3_config_processor.rb +3 -3
  116. data/lib/brakeman/processors/lib/rails3_route_processor.rb +2 -2
  117. data/lib/brakeman/processors/lib/render_helper.rb +2 -2
  118. data/lib/brakeman/processors/lib/render_path.rb +18 -1
  119. data/lib/brakeman/processors/library_processor.rb +5 -5
  120. data/lib/brakeman/processors/model_processor.rb +4 -5
  121. data/lib/brakeman/processors/output_processor.rb +5 -0
  122. data/lib/brakeman/processors/template_alias_processor.rb +4 -5
  123. data/lib/brakeman/processors/template_processor.rb +4 -4
  124. data/lib/brakeman/report.rb +3 -3
  125. data/lib/brakeman/report/ignore/config.rb +2 -3
  126. data/lib/brakeman/report/ignore/interactive.rb +2 -2
  127. data/lib/brakeman/report/pager.rb +1 -0
  128. data/lib/brakeman/report/report_base.rb +51 -6
  129. data/lib/brakeman/report/report_codeclimate.rb +3 -3
  130. data/lib/brakeman/report/report_hash.rb +1 -1
  131. data/lib/brakeman/report/report_html.rb +2 -2
  132. data/lib/brakeman/report/report_json.rb +1 -24
  133. data/lib/brakeman/report/report_table.rb +20 -4
  134. data/lib/brakeman/report/report_tabs.rb +1 -1
  135. data/lib/brakeman/report/report_text.rb +2 -2
  136. data/lib/brakeman/rescanner.rb +9 -12
  137. data/lib/brakeman/scanner.rb +19 -14
  138. data/lib/brakeman/tracker.rb +4 -4
  139. data/lib/brakeman/tracker/collection.rb +4 -3
  140. data/lib/brakeman/tracker/config.rb +6 -0
  141. data/lib/brakeman/util.rb +1 -147
  142. data/lib/brakeman/version.rb +1 -1
  143. data/lib/brakeman/warning.rb +23 -13
  144. data/lib/brakeman/warning_codes.rb +1 -0
  145. data/lib/ruby_parser/bm_sexp_processor.rb +1 -0
  146. metadata +78 -61
  147. data/bundle/ruby/2.5.0/gems/highline-1.7.10/Gemfile +0 -11
  148. data/bundle/ruby/2.5.0/gems/highline-1.7.10/INSTALL +0 -59
  149. data/bundle/ruby/2.5.0/gems/highline-1.7.10/README.rdoc +0 -74
  150. data/bundle/ruby/2.5.0/gems/highline-1.7.10/highline.gemspec +0 -37
  151. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline.rb +0 -1048
  152. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/compatibility.rb +0 -16
  153. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/import.rb +0 -41
  154. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/menu.rb +0 -381
  155. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/simulate.rb +0 -48
  156. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/string_extensions.rb +0 -111
  157. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/style.rb +0 -192
  158. data/bundle/ruby/2.5.0/gems/highline-1.7.10/lib/highline/system_extensions.rb +0 -254
  159. data/bundle/ruby/2.5.0/gems/highline-1.7.10/setup.rb +0 -1360
@@ -0,0 +1,325 @@
1
+ # coding: utf-8
2
+
3
+ #--
4
+ # originally color_scheme.rb
5
+ #
6
+ # Created by Richard LeBer on 2011-06-27.
7
+ # Copyright 2011. All rights reserved
8
+ #
9
+ # This is Free Software. See LICENSE and COPYING for details
10
+
11
+ class HighLine #:nodoc:
12
+ # Creates a style using {.find_or_create_style} or
13
+ # {.find_or_create_style_list}
14
+ # @param args [Array<Style, Hash, String>] style properties
15
+ # @return [Style]
16
+ def self.Style(*args)
17
+ args = args.compact.flatten
18
+ if args.size == 1
19
+ find_or_create_style(args.first)
20
+ else
21
+ find_or_create_style_list(*args)
22
+ end
23
+ end
24
+
25
+ # Search for a Style with the given properties and return it.
26
+ # If there's no matched Style, it creates one.
27
+ # You can pass a Style, String or a Hash.
28
+ # @param arg [Style, String, Hash] style properties
29
+ # @return [Style] found or creted Style
30
+ def self.find_or_create_style(arg)
31
+ if arg.is_a?(Style)
32
+ Style.list[arg.name] || Style.index(arg)
33
+ elsif arg.is_a?(::String) && arg =~ /^\e\[/ # arg is a code
34
+ styles = Style.code_index[arg]
35
+ if styles
36
+ styles.first
37
+ else
38
+ Style.new(code: arg)
39
+ end
40
+ elsif Style.list[arg]
41
+ Style.list[arg]
42
+ elsif HighLine.color_scheme && HighLine.color_scheme[arg]
43
+ HighLine.color_scheme[arg]
44
+ elsif arg.is_a?(Hash)
45
+ Style.new(arg)
46
+ elsif arg.to_s.downcase =~ /^rgb_([a-f0-9]{6})$/
47
+ Style.rgb(Regexp.last_match(1))
48
+ elsif arg.to_s.downcase =~ /^on_rgb_([a-f0-9]{6})$/
49
+ Style.rgb(Regexp.last_match(1)).on
50
+ else
51
+ raise NameError, "#{arg.inspect} is not a defined Style"
52
+ end
53
+ end
54
+
55
+ # Find a Style list or create a new one.
56
+ # @param args [Array<Symbol>] an Array of Symbols of each style
57
+ # that will be on the style list.
58
+ # @return [Style] Style list
59
+ # @example Creating a Style list of the combined RED and BOLD styles.
60
+ # style_list = HighLine.find_or_create_style_list(:red, :bold)
61
+
62
+ def self.find_or_create_style_list(*args)
63
+ name = args
64
+ Style.list[name] || Style.new(list: args)
65
+ end
66
+
67
+ # ANSI styles to be used by HighLine.
68
+ class Style
69
+ # Index the given style.
70
+ # Uses @code_index (Hash) as repository.
71
+ # @param style [Style]
72
+ # @return [Style] the given style
73
+ def self.index(style)
74
+ if style.name
75
+ @styles ||= {}
76
+ @styles[style.name] = style
77
+ end
78
+ unless style.list
79
+ @code_index ||= {}
80
+ @code_index[style.code] ||= []
81
+ @code_index[style.code].reject! do |indexed_style|
82
+ indexed_style.name == style.name
83
+ end
84
+ @code_index[style.code] << style
85
+ end
86
+ style
87
+ end
88
+
89
+ # Clear all custom Styles, restoring the Style index to
90
+ # builtin styles only.
91
+ # @return [void]
92
+ def self.clear_index
93
+ # reset to builtin only styles
94
+ @styles = list.select { |_name, style| style.builtin }
95
+ @code_index = {}
96
+ @styles.each_value { |style| index(style) }
97
+ end
98
+
99
+ # Converts all given color codes to hexadecimal and
100
+ # join them in a single string. If any given color code
101
+ # is already a String, doesn't perform any convertion.
102
+ #
103
+ # @param colors [Array<Numeric, String>] color codes
104
+ # @return [String] all color codes joined
105
+ # @example
106
+ # HighLine::Style.rgb_hex(9, 10, "11") # => "090a11"
107
+ def self.rgb_hex(*colors)
108
+ colors.map do |color|
109
+ color.is_a?(Numeric) ? format("%02x", color) : color.to_s
110
+ end.join
111
+ end
112
+
113
+ # Split an rgb code string into its 3 numerical compounds.
114
+ # @param hex [String] rgb code string like "010F0F"
115
+ # @return [Array<Numeric>] numerical compounds like [1, 15, 15]
116
+ # @example
117
+ # HighLine::Style.rgb_parts("090A0B") # => [9, 10, 11]
118
+
119
+ def self.rgb_parts(hex)
120
+ hex.scan(/../).map { |part| part.to_i(16) }
121
+ end
122
+
123
+ # Search for or create a new Style from the colors provided.
124
+ # @param colors (see .rgb_hex)
125
+ # @return [Style] a Style with the rgb colors provided.
126
+ # @example Creating a new Style based on rgb code
127
+ # rgb_style = HighLine::Style.rgb(9, 10, 11)
128
+ #
129
+ # rgb_style.name # => :rgb_090a0b
130
+ # rgb_style.code # => "\e[38;5;16m"
131
+ # rgb_style.rgb # => [9, 10, 11]
132
+ #
133
+ def self.rgb(*colors)
134
+ hex = rgb_hex(*colors)
135
+ name = ("rgb_" + hex).to_sym
136
+ style = list[name]
137
+ return style if style
138
+
139
+ parts = rgb_parts(hex)
140
+ new(name: name, code: "\e[38;5;#{rgb_number(parts)}m", rgb: parts)
141
+ end
142
+
143
+ # Returns the rgb number to be used as escape code on ANSI terminals.
144
+ # @param parts [Array<Numeric>] three numerical codes for red, green
145
+ # and blue
146
+ # @return [Numeric] to be used as escape code on ANSI terminals
147
+ def self.rgb_number(*parts)
148
+ parts = parts.flatten
149
+ 16 + parts.reduce(0) do |kode, part|
150
+ kode * 6 + (part / 256.0 * 6.0).floor
151
+ end
152
+ end
153
+
154
+ # From an ANSI number (color escape code), craft an 'rgb_hex' code of it
155
+ # @param ansi_number [Integer] ANSI escape code
156
+ # @return [String] all color codes joined as {.rgb_hex}
157
+ def self.ansi_rgb_to_hex(ansi_number)
158
+ raise "Invalid ANSI rgb code #{ansi_number}" unless
159
+ (16..231).cover?(ansi_number)
160
+ parts = (ansi_number - 16).
161
+ to_s(6).
162
+ rjust(3, "0").
163
+ scan(/./).
164
+ map { |d| (d.to_i * 255.0 / 6.0).ceil }
165
+
166
+ rgb_hex(*parts)
167
+ end
168
+
169
+ # @return [Hash] list of all cached Styles
170
+ def self.list
171
+ @styles ||= {}
172
+ end
173
+
174
+ # @return [Hash] list of all cached Style codes
175
+ def self.code_index
176
+ @code_index ||= {}
177
+ end
178
+
179
+ # Remove any ANSI color escape sequence of the given String.
180
+ # @param string [String]
181
+ # @return [String]
182
+ def self.uncolor(string)
183
+ string.gsub(/\e\[\d+(;\d+)*m/, "")
184
+ end
185
+
186
+ # Style name
187
+ # @return [Symbol] the name of the Style
188
+ attr_reader :name
189
+
190
+ # When a compound Style, returns a list of its components.
191
+ # @return [Array<Symbol>] components of a Style list
192
+ attr_reader :list
193
+
194
+ # @return [Array] the three numerical rgb codes. Ex: [10, 12, 127]
195
+ attr_accessor :rgb
196
+
197
+ # @return [Boolean] true if the Style is builtin or not.
198
+ attr_accessor :builtin
199
+
200
+ # Single color/styles have :name, :code, :rgb (possibly), :builtin
201
+ # Compound styles have :name, :list, :builtin
202
+ #
203
+ # @param defn [Hash] options for the Style to be created.
204
+ def initialize(defn = {})
205
+ @definition = defn
206
+ @name = defn[:name]
207
+ @code = defn[:code]
208
+ @rgb = defn[:rgb]
209
+ @list = defn[:list]
210
+ @builtin = defn[:builtin]
211
+ if @rgb
212
+ hex = self.class.rgb_hex(@rgb)
213
+ @name ||= "rgb_" + hex
214
+ elsif @list
215
+ @name ||= @list
216
+ end
217
+ self.class.index self unless defn[:no_index]
218
+ end
219
+
220
+ # Duplicate current Style using the same definition used to create it.
221
+ # @return [Style] duplicated Style
222
+ def dup
223
+ self.class.new(@definition)
224
+ end
225
+
226
+ # @return [Hash] the definition used to create this Style
227
+ def to_hash
228
+ @definition
229
+ end
230
+
231
+ # Uses the Style definition to add ANSI color escape codes
232
+ # to a a given String
233
+ # @param string [String] to be colored
234
+ # @return [String] an ANSI colored string
235
+ def color(string)
236
+ code + string + HighLine::CLEAR
237
+ end
238
+
239
+ # @return [String] all codes of the Style list joined together
240
+ # (if a Style list)
241
+ # @return [String] the Style code
242
+ def code
243
+ if @list
244
+ @list.map { |element| HighLine.Style(element).code }.join
245
+ else
246
+ @code
247
+ end
248
+ end
249
+
250
+ # @return [Integer] the RED component of the rgb code
251
+ def red
252
+ @rgb && @rgb[0]
253
+ end
254
+
255
+ # @return [Integer] the GREEN component of the rgb code
256
+ def green
257
+ @rgb && @rgb[1]
258
+ end
259
+
260
+ # @return [Integer] the BLUE component of the rgb code
261
+ def blue
262
+ @rgb && @rgb[2]
263
+ end
264
+
265
+ # Duplicate Style with some minor changes
266
+ # @param new_name [Symbol]
267
+ # @param options [Hash] Style attributes to be changed
268
+ # @return [Style] new Style with changed attributes
269
+ def variant(new_name, options = {})
270
+ raise "Cannot create a variant of a style list (#{inspect})" if @list
271
+ new_code = options[:code] || code
272
+ if options[:increment]
273
+ raise "Unexpected code in #{inspect}" unless
274
+ new_code =~ /^(.*?)(\d+)(.*)/
275
+
276
+ new_code =
277
+ Regexp.last_match(1) +
278
+ (Regexp.last_match(2).to_i +
279
+ options[:increment]).to_s +
280
+ Regexp.last_match(3)
281
+ end
282
+ new_rgb = options[:rgb] || @rgb
283
+ self.class.new(to_hash.merge(name: new_name,
284
+ code: new_code,
285
+ rgb: new_rgb))
286
+ end
287
+
288
+ # Uses the color as background and return a new style.
289
+ # @return [Style]
290
+ def on
291
+ new_name = ("on_" + @name.to_s).to_sym
292
+ self.class.list[new_name] ||= variant(new_name, increment: 10)
293
+ end
294
+
295
+ # @return [Style] a brighter version of this Style
296
+ def bright
297
+ create_bright_variant(:bright)
298
+ end
299
+
300
+ # @return [Style] a lighter version of this Style
301
+ def light
302
+ create_bright_variant(:light)
303
+ end
304
+
305
+ private
306
+
307
+ def create_bright_variant(variant_name)
308
+ raise "Cannot create a #{name} variant of a style list (#{inspect})" if
309
+ @list
310
+ new_name = ("#{variant_name}_" + @name.to_s).to_sym
311
+ new_rgb =
312
+ if @rgb == [0, 0, 0]
313
+ [128, 128, 128]
314
+ else
315
+ @rgb.map { |color| color.zero? ? 0 : [color + 128, 255].min }
316
+ end
317
+
318
+ find_style(new_name) || variant(new_name, increment: 60, rgb: new_rgb)
319
+ end
320
+
321
+ def find_style(name)
322
+ self.class.list[name]
323
+ end
324
+ end
325
+ end
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+
3
+ require "forwardable"
4
+
5
+ class HighLine
6
+ # Renders an erb template taking a {Question} and a {HighLine} instance
7
+ # as context.
8
+ class TemplateRenderer
9
+ extend Forwardable
10
+
11
+ def_delegators :@highline, :color, :list, :key
12
+ def_delegators :@source, :answer_type, :prompt, :header, :answer
13
+
14
+ # @return [ERB] ERB template being rendered
15
+ attr_reader :template
16
+
17
+ # @return [Question, Menu] Question instance used as context
18
+ attr_reader :source
19
+
20
+ # @return [HighLine] HighLine instance used as context
21
+ attr_reader :highline
22
+
23
+ # Initializes the TemplateRenderer object with its template and
24
+ # HighLine and Question contexts.
25
+ #
26
+ # @param template [ERB] ERB template.
27
+ # @param source [Question] question object.
28
+ # @param highline [HighLine] HighLine instance.
29
+
30
+ def initialize(template, source, highline)
31
+ @template = template
32
+ @source = source
33
+ @highline = highline
34
+ end
35
+
36
+ # @return [String] rendered template
37
+ def render
38
+ template.result(binding)
39
+ end
40
+
41
+ # Returns an error message when the called method
42
+ # is not available.
43
+ # @return [String] error message.
44
+ def method_missing(method, *args)
45
+ "Method #{method} with args #{args.inspect} " \
46
+ "is not available on #{inspect}. " \
47
+ "Try #{methods(false).sort.inspect}"
48
+ end
49
+
50
+ # @return [Question, Menu] {#source} attribute.
51
+ def menu
52
+ source
53
+ end
54
+
55
+ # If some constant is missing at this TemplateRenderer instance,
56
+ # get it from HighLine. Useful to get color and style contants.
57
+ # @param name [Symbol] automatically passed constant's name as Symbol
58
+ def self.const_missing(name)
59
+ HighLine.const_get(name)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,190 @@
1
+ # coding: utf-8
2
+
3
+ #--
4
+ # terminal.rb
5
+ #
6
+ # Originally created by James Edward Gray II on 2006-06-14 as
7
+ # system_extensions.rb.
8
+ # Copyright 2006 Gray Productions. All rights reserved.
9
+ #
10
+ # This is Free Software. See LICENSE and COPYING for details.
11
+
12
+ require "highline/compatibility"
13
+
14
+ class HighLine
15
+ # Basic Terminal class which HighLine will direct
16
+ # input and output to.
17
+ # The specialized Terminals all decend from this HighLine::Terminal class
18
+ class Terminal
19
+ # Probe for and return a suitable Terminal instance
20
+ # @param input [IO] input stream
21
+ # @param output [IO] output stream
22
+ def self.get_terminal(input, output)
23
+ # First of all, probe for io/console
24
+ begin
25
+ require "io/console"
26
+ require "highline/terminal/io_console"
27
+ terminal = HighLine::Terminal::IOConsole.new(input, output)
28
+ rescue LoadError
29
+ require "highline/terminal/unix_stty"
30
+ terminal = HighLine::Terminal::UnixStty.new(input, output)
31
+ end
32
+
33
+ terminal.initialize_system_extensions
34
+ terminal
35
+ end
36
+
37
+ # @return [IO] input stream
38
+ attr_reader :input
39
+
40
+ # @return [IO] output stream
41
+ attr_reader :output
42
+
43
+ # Creates a terminal instance based on given input and output streams.
44
+ # @param input [IO] input stream
45
+ # @param output [IO] output stream
46
+ def initialize(input, output)
47
+ @input = input
48
+ @output = output
49
+ end
50
+
51
+ # An initialization callback.
52
+ # It is called by {.get_terminal}.
53
+ def initialize_system_extensions; end
54
+
55
+ # @return [Array<Integer, Integer>] two value terminal
56
+ # size like [columns, lines]
57
+ def terminal_size
58
+ [80, 24]
59
+ end
60
+
61
+ # Enter Raw No Echo mode.
62
+ def raw_no_echo_mode; end
63
+
64
+ # Yieds a block to be executed in Raw No Echo mode and
65
+ # then restore the terminal state.
66
+ def raw_no_echo_mode_exec
67
+ raw_no_echo_mode
68
+ yield
69
+ ensure
70
+ restore_mode
71
+ end
72
+
73
+ # Restore terminal to its default mode
74
+ def restore_mode; end
75
+
76
+ # Get one character from the terminal
77
+ # @return [String] one character
78
+ def get_character; end # rubocop:disable Naming/AccessorMethodName
79
+
80
+ # Get one line from the terminal and format accordling.
81
+ # Use readline if question has readline mode set.
82
+ # @param question [HighLine::Question]
83
+ # @param highline [HighLine]
84
+ def get_line(question, highline)
85
+ raw_answer =
86
+ if question.readline
87
+ get_line_with_readline(question, highline)
88
+ else
89
+ get_line_default(highline)
90
+ end
91
+
92
+ question.format_answer(raw_answer)
93
+ end
94
+
95
+ # Get one line using #readline_read
96
+ # @param (see #get_line)
97
+ def get_line_with_readline(question, highline)
98
+ require "readline" # load only if needed
99
+
100
+ raw_answer = readline_read(question)
101
+
102
+ if !raw_answer && highline.track_eof?
103
+ raise EOFError, "The input stream is exhausted."
104
+ end
105
+
106
+ raw_answer || ""
107
+ end
108
+
109
+ # Use readline to read one line
110
+ # @param question [HighLine::Question] question from where to get
111
+ # autocomplete candidate strings
112
+ def readline_read(question)
113
+ # prep auto-completion
114
+ unless question.selection.empty?
115
+ Readline.completion_proc = lambda do |str|
116
+ question.selection.grep(/\A#{Regexp.escape(str)}/)
117
+ end
118
+ end
119
+
120
+ # work-around ugly readline() warnings
121
+ old_verbose = $VERBOSE
122
+ $VERBOSE = nil
123
+
124
+ raw_answer = run_preserving_stty do
125
+ Readline.readline("", true)
126
+ end
127
+
128
+ $VERBOSE = old_verbose
129
+
130
+ raw_answer
131
+ end
132
+
133
+ # Get one line from terminal using default #gets method.
134
+ # @param highline (see #get_line)
135
+ def get_line_default(highline)
136
+ raise EOFError, "The input stream is exhausted." if highline.track_eof? &&
137
+ highline.input.eof?
138
+ highline.input.gets
139
+ end
140
+
141
+ # @!group Enviroment queries
142
+
143
+ # Running on JRuby?
144
+ def jruby?
145
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
146
+ end
147
+
148
+ # Running on Rubinius?
149
+ def rubinius?
150
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
151
+ end
152
+
153
+ # Running on Windows?
154
+ def windows?
155
+ defined?(RUBY_PLATFORM) && (RUBY_PLATFORM =~ /mswin|mingw|cygwin/)
156
+ end
157
+
158
+ # @!endgroup
159
+
160
+ # Returns the class name as String. Useful for debuggin.
161
+ # @return [String] class name. Ex: "HighLine::Terminal::IOConsole"
162
+ def character_mode
163
+ self.class.name
164
+ end
165
+
166
+ private
167
+
168
+ # Yield a block using stty shell commands to preserve the terminal state.
169
+ def run_preserving_stty
170
+ save_stty
171
+ yield
172
+ ensure
173
+ restore_stty
174
+ end
175
+
176
+ # Saves terminal state using shell stty command.
177
+ def save_stty
178
+ @stty_save = begin
179
+ `stty -g`.chomp
180
+ rescue StandardError
181
+ nil
182
+ end
183
+ end
184
+
185
+ # Restores terminal state using shell stty command.
186
+ def restore_stty
187
+ system("stty", @stty_save) if @stty_save
188
+ end
189
+ end
190
+ end