highline 2.0.0.pre.develop.9 → 2.0.0.pre.develop.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +59 -5
- data/.travis.yml +9 -4
- data/Changelog.md +11 -0
- data/Gemfile +12 -19
- data/Rakefile +5 -11
- data/examples/ansi_colors.rb +6 -11
- data/examples/asking_for_arrays.rb +4 -3
- data/examples/basic_usage.rb +29 -22
- data/examples/color_scheme.rb +11 -10
- data/examples/get_character.rb +6 -5
- data/examples/limit.rb +2 -1
- data/examples/menus.rb +11 -11
- data/examples/overwrite.rb +7 -6
- data/examples/page_and_wrap.rb +5 -4
- data/examples/password.rb +2 -1
- data/examples/repeat_entry.rb +7 -5
- data/examples/trapping_eof.rb +2 -1
- data/examples/using_readline.rb +2 -1
- data/highline.gemspec +25 -25
- data/lib/highline.rb +103 -111
- data/lib/highline/builtin_styles.rb +45 -41
- data/lib/highline/color_scheme.rb +32 -28
- data/lib/highline/compatibility.rb +3 -3
- data/lib/highline/custom_errors.rb +2 -1
- data/lib/highline/import.rb +8 -11
- data/lib/highline/list.rb +4 -8
- data/lib/highline/list_renderer.rb +207 -201
- data/lib/highline/menu.rb +75 -63
- data/lib/highline/menu/item.rb +2 -0
- data/lib/highline/paginator.rb +5 -6
- data/lib/highline/question.rb +38 -36
- data/lib/highline/question/answer_converter.rb +2 -2
- data/lib/highline/question_asker.rb +15 -17
- data/lib/highline/simulate.rb +11 -13
- data/lib/highline/statement.rb +12 -10
- data/lib/highline/string.rb +9 -8
- data/lib/highline/string_extensions.rb +30 -14
- data/lib/highline/style.rb +68 -45
- data/lib/highline/template_renderer.rb +5 -5
- data/lib/highline/terminal.rb +24 -31
- data/lib/highline/terminal/io_console.rb +2 -2
- data/lib/highline/terminal/ncurses.rb +4 -3
- data/lib/highline/terminal/unix_stty.rb +12 -9
- data/lib/highline/version.rb +1 -1
- data/lib/highline/wrapper.rb +12 -11
- metadata +34 -43
- data/test/acceptance/acceptance.rb +0 -62
- data/test/acceptance/acceptance_test.rb +0 -69
- data/test/acceptance/at_color_output_using_erb_templates.rb +0 -17
- data/test/acceptance/at_echo_false.rb +0 -23
- data/test/acceptance/at_readline.rb +0 -37
- data/test/io_console_compatible.rb +0 -37
- data/test/string_methods.rb +0 -35
- data/test/test_answer_converter.rb +0 -26
- data/test/test_color_scheme.rb +0 -94
- data/test/test_helper.rb +0 -22
- data/test/test_highline.rb +0 -1627
- data/test/test_import.rb +0 -55
- data/test/test_list.rb +0 -60
- data/test/test_menu.rb +0 -749
- data/test/test_paginator.rb +0 -73
- data/test/test_question_asker.rb +0 -20
- data/test/test_simulator.rb +0 -24
- data/test/test_string_extension.rb +0 -72
- data/test/test_string_highline.rb +0 -42
- data/test/test_style.rb +0 -613
- data/test/test_wrapper.rb +0 -188
    
        data/lib/highline/menu.rb
    CHANGED
    
    | @@ -13,7 +13,8 @@ require "highline/menu/item" | |
| 13 13 |  | 
| 14 14 | 
             
            class HighLine
         | 
| 15 15 | 
             
              #
         | 
| 16 | 
            -
              # Menu objects encapsulate all the details of a call to | 
| 16 | 
            +
              # Menu objects encapsulate all the details of a call to
         | 
| 17 | 
            +
              # {HighLine#choose HighLine#choose}.
         | 
| 17 18 | 
             
              # Using the accessors and {Menu#choice} and {Menu#choices}, the block passed
         | 
| 18 19 | 
             
              # to {HighLine#choose} can detail all aspects of menu display and control.
         | 
| 19 20 | 
             
              #
         | 
| @@ -21,16 +22,16 @@ class HighLine | |
| 21 22 | 
             
                # Pass +false+ to _color_ to turn off HighLine::Menu's
         | 
| 22 23 | 
             
                # index coloring.
         | 
| 23 24 | 
             
                # Pass a color and the Menu's indices will be colored.
         | 
| 24 | 
            -
                 | 
| 25 | 
            -
                   | 
| 25 | 
            +
                class << self
         | 
| 26 | 
            +
                  attr_writer :index_color
         | 
| 26 27 | 
             
                end
         | 
| 27 28 |  | 
| 28 29 | 
             
                # Initialize it
         | 
| 29 30 | 
             
                self.index_color = false
         | 
| 30 31 |  | 
| 31 32 | 
             
                # Returns color used for coloring Menu's indices
         | 
| 32 | 
            -
                 | 
| 33 | 
            -
                   | 
| 33 | 
            +
                class << self
         | 
| 34 | 
            +
                  attr_reader :index_color
         | 
| 34 35 | 
             
                end
         | 
| 35 36 |  | 
| 36 37 | 
             
                #
         | 
| @@ -53,10 +54,10 @@ class HighLine | |
| 53 54 | 
             
                  # Initialize Question objects with ignored values, we'll
         | 
| 54 55 | 
             
                  # adjust ours as needed.
         | 
| 55 56 | 
             
                  #
         | 
| 56 | 
            -
                  super("Ignored", [ | 
| 57 | 
            +
                  super("Ignored", [], &nil) # avoiding passing the block along
         | 
| 57 58 |  | 
| 58 | 
            -
                  @items           = [ | 
| 59 | 
            -
                  @hidden_items    = [ | 
| 59 | 
            +
                  @items           = []
         | 
| 60 | 
            +
                  @hidden_items    = []
         | 
| 60 61 | 
             
                  @help            = Hash.new("There's no help for that topic.")
         | 
| 61 62 |  | 
| 62 63 | 
             
                  @index           = :number
         | 
| @@ -75,13 +76,13 @@ class HighLine | |
| 75 76 | 
             
                  @index_color     = self.class.index_color
         | 
| 76 77 |  | 
| 77 78 | 
             
                  # Override Questions responses, we'll set our own.
         | 
| 78 | 
            -
                  @responses       = { | 
| 79 | 
            +
                  @responses       = {}
         | 
| 79 80 | 
             
                  # Context for action code.
         | 
| 80 81 | 
             
                  @highline        = nil
         | 
| 81 82 |  | 
| 82 83 | 
             
                  yield self if block_given?
         | 
| 83 84 |  | 
| 84 | 
            -
                  init_help if @shell  | 
| 85 | 
            +
                  init_help if @shell && !@help.empty?
         | 
| 85 86 | 
             
                end
         | 
| 86 87 |  | 
| 87 88 | 
             
                #
         | 
| @@ -179,26 +180,27 @@ class HighLine | |
| 179 180 | 
             
                #   cli.choose do |menu|
         | 
| 180 181 | 
             
                #     menu.shell = true
         | 
| 181 182 | 
             
                #
         | 
| 182 | 
            -
                #     menu.choice(:load, text: 'Load a file', | 
| 183 | 
            +
                #     menu.choice(:load, text: 'Load a file',
         | 
| 184 | 
            +
                #                 help: "Load a file using your favourite editor.")
         | 
| 183 185 | 
             
                #     menu.choice(:save, help: "Save data in file.")
         | 
| 184 186 | 
             
                #     menu.choice(:quit, help: "Exit program.")
         | 
| 185 187 | 
             
                #
         | 
| 186 188 | 
             
                #     menu.help("rules", "The rules of this system are as follows...")
         | 
| 187 189 | 
             
                #   end
         | 
| 188 190 |  | 
| 189 | 
            -
                def choice( | 
| 191 | 
            +
                def choice(name, help = nil, text = nil, &action)
         | 
| 190 192 | 
             
                  item = Menu::Item.new(name, text: text, help: help, action: action)
         | 
| 191 193 | 
             
                  @items << item
         | 
| 192 194 | 
             
                  @help.merge!(item.item_help)
         | 
| 193 | 
            -
                  update_responses | 
| 195 | 
            +
                  update_responses # rebuild responses based on our settings
         | 
| 194 196 | 
             
                end
         | 
| 195 197 |  | 
| 196 198 | 
             
                #
         | 
| 197 | 
            -
                # This method helps reduce the namespaces in the original call, | 
| 198 | 
            -
                # like this: HighLine::Menu::Item.new(...)
         | 
| 199 | 
            +
                # This method helps reduce the namespaces in the original call,
         | 
| 200 | 
            +
                # which would look like this: HighLine::Menu::Item.new(...)
         | 
| 199 201 | 
             
                # With #build_item, it looks like this: menu.build_item(...)
         | 
| 200 | 
            -
                # @param *args splat args, the same args you would pass to an | 
| 201 | 
            -
                # | 
| 202 | 
            +
                # @param *args splat args, the same args you would pass to an
         | 
| 203 | 
            +
                #   initialization of HighLine::Menu::Item
         | 
| 202 204 | 
             
                # @return [HighLine::Menu::Item] the menu item
         | 
| 203 205 |  | 
| 204 206 | 
             
                def build_item(*args)
         | 
| @@ -206,8 +208,8 @@ class HighLine | |
| 206 208 | 
             
                end
         | 
| 207 209 |  | 
| 208 210 | 
             
                #
         | 
| 209 | 
            -
                # Adds an item directly to the menu. If you want more  | 
| 210 | 
            -
                # use this method
         | 
| 211 | 
            +
                # Adds an item directly to the menu. If you want more configuration
         | 
| 212 | 
            +
                # or options, use this method
         | 
| 211 213 | 
             
                #
         | 
| 212 214 | 
             
                # @param item [Menu::Item] item containing choice fields and more
         | 
| 213 215 | 
             
                # @return [void]
         | 
| @@ -223,7 +225,8 @@ class HighLine | |
| 223 225 | 
             
                # warned:</b>  An _action_ set here will apply to *all* provided
         | 
| 224 226 | 
             
                # _names_.  This is considered to be a feature, so you can easily
         | 
| 225 227 | 
             
                # hand-off interface processing to a different chunk of code.
         | 
| 226 | 
            -
                # @param names [Array<#to_s>] menu item titles/headers/names to be | 
| 228 | 
            +
                # @param names [Array<#to_s>] menu item titles/headers/names to be
         | 
| 229 | 
            +
                #   displayed.
         | 
| 227 230 | 
             
                # @param action (see #choice)
         | 
| 228 231 | 
             
                # @return [void]
         | 
| 229 232 | 
             
                # @example (see HighLine::Menu#initialize)
         | 
| @@ -231,7 +234,7 @@ class HighLine | |
| 231 234 | 
             
                # choice has more options available to you, like longer text or help (and
         | 
| 232 235 | 
             
                # of course, individual actions)
         | 
| 233 236 | 
             
                #
         | 
| 234 | 
            -
                def choices( | 
| 237 | 
            +
                def choices(*names, &action)
         | 
| 235 238 | 
             
                  names.each { |n| choice(n, &action) }
         | 
| 236 239 | 
             
                end
         | 
| 237 240 |  | 
| @@ -242,7 +245,7 @@ class HighLine | |
| 242 245 | 
             
                # @param action (see #choice)
         | 
| 243 246 | 
             
                # @return (see #choice)
         | 
| 244 247 |  | 
| 245 | 
            -
                def hidden( | 
| 248 | 
            +
                def hidden(name, help = nil, &action)
         | 
| 246 249 | 
             
                  item = Menu::Item.new(name, text: name, help: help, action: action)
         | 
| 247 250 | 
             
                  @hidden_items << item
         | 
| 248 251 | 
             
                  @help.merge!(item.item_help)
         | 
| @@ -263,31 +266,36 @@ class HighLine | |
| 263 266 | 
             
                # _index_suffix_ to a single space and _select_by_ to <tt>:name</tt>.
         | 
| 264 267 | 
             
                # Because of this, you should make a habit of setting the _index_ first.
         | 
| 265 268 | 
             
                #
         | 
| 266 | 
            -
                def index=( | 
| 269 | 
            +
                def index=(style)
         | 
| 267 270 | 
             
                  @index = style
         | 
| 268 271 |  | 
| 272 | 
            +
                  return unless @index == :none || @index.is_a?(::String)
         | 
| 273 | 
            +
             | 
| 269 274 | 
             
                  # Default settings.
         | 
| 270 | 
            -
                   | 
| 271 | 
            -
             | 
| 272 | 
            -
                    @select_by    = :name
         | 
| 273 | 
            -
                  end
         | 
| 275 | 
            +
                  @index_suffix = " "
         | 
| 276 | 
            +
                  @select_by    = :name
         | 
| 274 277 | 
             
                end
         | 
| 275 278 |  | 
| 276 279 | 
             
                #
         | 
| 277 280 | 
             
                # Initializes the help system by adding a <tt>:help</tt> choice, some
         | 
| 278 281 | 
             
                # action code, and the default help listing.
         | 
| 279 282 | 
             
                #
         | 
| 280 | 
            -
                def init_help | 
| 283 | 
            +
                def init_help
         | 
| 281 284 | 
             
                  return if @items.include?(:help)
         | 
| 282 285 |  | 
| 283 286 | 
             
                  topics    = @help.keys.sort
         | 
| 284 | 
            -
                  help_help = | 
| 285 | 
            -
             | 
| 286 | 
            -
             | 
| 287 | 
            -
             | 
| 288 | 
            -
             | 
| 289 | 
            -
             | 
| 290 | 
            -
             | 
| 287 | 
            +
                  help_help =
         | 
| 288 | 
            +
                    if @help.include?("help")
         | 
| 289 | 
            +
                      @help["help"]
         | 
| 290 | 
            +
                    else
         | 
| 291 | 
            +
                      "This command will display helpful messages about " \
         | 
| 292 | 
            +
                        "functionality, like this one.  To see the help for " \
         | 
| 293 | 
            +
                        "a specific topic enter:\n\thelp [TOPIC]\nTry asking " \
         | 
| 294 | 
            +
                        "for help on any of the following:\n\n" \
         | 
| 295 | 
            +
                        "<%= list(#{topics.inspect}, :columns_across) %>"
         | 
| 296 | 
            +
                    end
         | 
| 297 | 
            +
             | 
| 298 | 
            +
                  choice(:help, help_help) do |_command, topic|
         | 
| 291 299 | 
             
                    topic.strip!
         | 
| 292 300 | 
             
                    topic.downcase!
         | 
| 293 301 | 
             
                    if topic.empty?
         | 
| @@ -302,11 +310,11 @@ class HighLine | |
| 302 310 | 
             
                # Used to set help for arbitrary topics.  Use the topic <tt>"help"</tt>
         | 
| 303 311 | 
             
                # to override the default message. Mainly for internal use.
         | 
| 304 312 | 
             
                #
         | 
| 305 | 
            -
                # @param topic [String] the menu item header/title/name to be associated | 
| 306 | 
            -
                #   a help message.
         | 
| 313 | 
            +
                # @param topic [String] the menu item header/title/name to be associated
         | 
| 314 | 
            +
                #   with a help message.
         | 
| 307 315 | 
             
                # @param help [String] the help message to be associated with the menu
         | 
| 308 316 | 
             
                #   item/title/name.
         | 
| 309 | 
            -
                def help( | 
| 317 | 
            +
                def help(topic, help)
         | 
| 310 318 | 
             
                  @help[topic] = help
         | 
| 311 319 | 
             
                end
         | 
| 312 320 |  | 
| @@ -339,14 +347,14 @@ class HighLine | |
| 339 347 | 
             
                # will default to <tt>:none</tt> and _flow_ will default to
         | 
| 340 348 | 
             
                # <tt>:inline</tt>.
         | 
| 341 349 | 
             
                #
         | 
| 342 | 
            -
                def layout=( | 
| 350 | 
            +
                def layout=(new_layout)
         | 
| 343 351 | 
             
                  @layout = new_layout
         | 
| 344 352 |  | 
| 345 353 | 
             
                  # Default settings.
         | 
| 346 354 | 
             
                  case @layout
         | 
| 347 355 | 
             
                  when :one_line, :menu_only
         | 
| 348 356 | 
             
                    self.index = :none
         | 
| 349 | 
            -
                    @flow | 
| 357 | 
            +
                    @flow = :inline
         | 
| 350 358 | 
             
                  end
         | 
| 351 359 | 
             
                end
         | 
| 352 360 |  | 
| @@ -387,12 +395,14 @@ class HighLine | |
| 387 395 | 
             
                # rules for this Menu object.  If an action was provided for the
         | 
| 388 396 | 
             
                # selection, it will be executed as described in {#choice}.
         | 
| 389 397 | 
             
                #
         | 
| 390 | 
            -
                # @param highline_context [HighLine] a HighLine instance to be used | 
| 391 | 
            -
                #  | 
| 398 | 
            +
                # @param highline_context [HighLine] a HighLine instance to be used
         | 
| 399 | 
            +
                #   as context.
         | 
| 400 | 
            +
                # @param selection [String, Integer] index or title of the selected
         | 
| 401 | 
            +
                #   menu item.
         | 
| 392 402 | 
             
                # @param details additional parameter to be passed when in shell mode.
         | 
| 393 403 | 
             
                # @return [nil, Object] if @nil_on_handled is set it returns +nil+,
         | 
| 394 404 | 
             
                #   else it returns the action return value.
         | 
| 395 | 
            -
                def select( | 
| 405 | 
            +
                def select(highline_context, selection, details = nil)
         | 
| 396 406 | 
             
                  # add in any hidden menu commands
         | 
| 397 407 | 
             
                  items = all_items
         | 
| 398 408 |  | 
| @@ -423,18 +433,20 @@ class HighLine | |
| 423 433 | 
             
                def get_item_by_letter(items, selection)
         | 
| 424 434 | 
             
                  item = items.find { |i| i.name == selection }
         | 
| 425 435 | 
             
                  return item if item
         | 
| 426 | 
            -
             | 
| 427 | 
            -
                   | 
| 436 | 
            +
             | 
| 437 | 
            +
                  # 97 is the "a" letter at ascii table
         | 
| 438 | 
            +
                  # Ex: For "a" it will return 0, and for "c" it will return 2
         | 
| 439 | 
            +
                  index = selection.ord - 97
         | 
| 428 440 | 
             
                  items[index]
         | 
| 429 441 | 
             
                end
         | 
| 430 442 |  | 
| 431 443 | 
             
                def value_for_selected_item(item, details)
         | 
| 432 444 | 
             
                  if item.action
         | 
| 433 | 
            -
                    if @shell
         | 
| 434 | 
            -
             | 
| 435 | 
            -
             | 
| 436 | 
            -
             | 
| 437 | 
            -
             | 
| 445 | 
            +
                    result = if @shell
         | 
| 446 | 
            +
                               item.action.call(item.name, details)
         | 
| 447 | 
            +
                             else
         | 
| 448 | 
            +
                               item.action.call(item.name)
         | 
| 449 | 
            +
                             end
         | 
| 438 450 | 
             
                    @nil_on_handled ? nil : result
         | 
| 439 451 | 
             
                  else
         | 
| 440 452 | 
             
                    item.name
         | 
| @@ -451,7 +463,7 @@ class HighLine | |
| 451 463 | 
             
                  elsif selections.is_a?(Hash)
         | 
| 452 464 | 
             
                    value_for_hash_selections(items, selections, details)
         | 
| 453 465 | 
             
                  else
         | 
| 454 | 
            -
                     | 
| 466 | 
            +
                    raise ArgumentError, "selections must be either Array or Hash"
         | 
| 455 467 | 
             
                  end
         | 
| 456 468 | 
             
                end
         | 
| 457 469 |  | 
| @@ -512,23 +524,23 @@ class HighLine | |
| 512 524 | 
             
                # Allows Menu to behave as a String, just like Question.  Returns the
         | 
| 513 525 | 
             
                # _layout_ to be rendered, which is used by HighLine.say().
         | 
| 514 526 | 
             
                #
         | 
| 515 | 
            -
                def to_s | 
| 527 | 
            +
                def to_s
         | 
| 516 528 | 
             
                  case @layout
         | 
| 517 529 | 
             
                  when :list
         | 
| 518 530 | 
             
                    %(<%= header ? "#{header}:\n" : '' %>) +
         | 
| 519 | 
            -
             | 
| 520 | 
            -
             | 
| 521 | 
            -
             | 
| 531 | 
            +
                      parse_list +
         | 
| 532 | 
            +
                      show_default_if_any +
         | 
| 533 | 
            +
                      "<%= prompt %>"
         | 
| 522 534 | 
             
                  when :one_line
         | 
| 523 535 | 
             
                    %(<%= header ? "#{header}:  " : '' %>) +
         | 
| 524 | 
            -
             | 
| 525 | 
            -
             | 
| 526 | 
            -
             | 
| 527 | 
            -
             | 
| 536 | 
            +
                      "<%= prompt %>" \
         | 
| 537 | 
            +
                      "(" + parse_list + ")" +
         | 
| 538 | 
            +
                      show_default_if_any +
         | 
| 539 | 
            +
                      "<%= prompt[/\s*$/] %>"
         | 
| 528 540 | 
             
                  when :menu_only
         | 
| 529 541 | 
             
                    parse_list +
         | 
| 530 | 
            -
             | 
| 531 | 
            -
             | 
| 542 | 
            +
                      show_default_if_any +
         | 
| 543 | 
            +
                      "<%= prompt %>"
         | 
| 532 544 | 
             
                  else
         | 
| 533 545 | 
             
                    @layout
         | 
| 534 546 | 
             
                  end
         | 
| @@ -536,11 +548,11 @@ class HighLine | |
| 536 548 |  | 
| 537 549 | 
             
                def parse_list
         | 
| 538 550 | 
             
                  "<%= list( menu, #{@flow.inspect},
         | 
| 539 | 
            -
                       #{@list_option.inspect} ) %>" | 
| 551 | 
            +
                       #{@list_option.inspect} ) %>"
         | 
| 540 552 | 
             
                end
         | 
| 541 553 |  | 
| 542 554 | 
             
                def show_default_if_any
         | 
| 543 | 
            -
                   | 
| 555 | 
            +
                  default.to_s.empty? ? "" : "(#{default}) "
         | 
| 544 556 | 
             
                end
         | 
| 545 557 |  | 
| 546 558 | 
             
                #
         | 
    
        data/lib/highline/menu/item.rb
    CHANGED
    
    
    
        data/lib/highline/paginator.rb
    CHANGED
    
    | @@ -3,7 +3,6 @@ | |
| 3 3 | 
             
            class HighLine
         | 
| 4 4 | 
             
              # Take the task of paginating some piece of text given a HighLine context
         | 
| 5 5 | 
             
              class Paginator
         | 
| 6 | 
            -
             | 
| 7 6 | 
             
                # @return [HighLine] HighLine context
         | 
| 8 7 | 
             
                attr_reader :highline
         | 
| 9 8 |  | 
| @@ -36,18 +35,18 @@ class HighLine | |
| 36 35 | 
             
                    # Return last line if user wants to abort paging
         | 
| 37 36 | 
             
                    return "...\n#{lines.last}" unless continue_paging?
         | 
| 38 37 | 
             
                  end
         | 
| 39 | 
            -
                   | 
| 38 | 
            +
                  lines.join
         | 
| 40 39 | 
             
                end
         | 
| 41 40 |  | 
| 42 41 | 
             
                #
         | 
| 43 | 
            -
                # Ask user if they wish to continue paging output. Allows them to | 
| 44 | 
            -
                # cancel the paging process.
         | 
| 42 | 
            +
                # Ask user if they wish to continue paging output. Allows them to
         | 
| 43 | 
            +
                # type "q" to cancel the paging process.
         | 
| 45 44 | 
             
                #
         | 
| 46 45 | 
             
                def continue_paging?
         | 
| 47 46 | 
             
                  command = highline.new_scope.ask(
         | 
| 48 47 | 
             
                    "-- press enter/return to continue or q to stop -- "
         | 
| 49 48 | 
             
                  ) { |q| q.character = true }
         | 
| 50 | 
            -
                  command !~ /\A[qQ]\Z/ | 
| 49 | 
            +
                  command !~ /\A[qQ]\Z/ # Only continue paging if Q was not hit.
         | 
| 51 50 | 
             
                end
         | 
| 52 51 | 
             
              end
         | 
| 53 | 
            -
            end
         | 
| 52 | 
            +
            end
         | 
    
        data/lib/highline/question.rb
    CHANGED
    
    | @@ -8,6 +8,7 @@ | |
| 8 8 | 
             
            #
         | 
| 9 9 | 
             
            #  This is Free Software.  See LICENSE and COPYING for details.
         | 
| 10 10 |  | 
| 11 | 
            +
            require "English"
         | 
| 11 12 | 
             
            require "optparse"
         | 
| 12 13 | 
             
            require "date"
         | 
| 13 14 | 
             
            require "pathname"
         | 
| @@ -59,11 +60,11 @@ class HighLine | |
| 59 60 | 
             
                  @case         = nil
         | 
| 60 61 | 
             
                  @in           = nil
         | 
| 61 62 | 
             
                  @first_answer = nil
         | 
| 62 | 
            -
                  @directory    = Pathname.new(File.expand_path(File.dirname($0)))
         | 
| 63 63 | 
             
                  @glob         = "*"
         | 
| 64 | 
            -
                  @user_responses = Hash.new
         | 
| 65 | 
            -
                  @internal_responses = default_responses_hash
         | 
| 66 64 | 
             
                  @overwrite    = false
         | 
| 65 | 
            +
                  @user_responses = {}
         | 
| 66 | 
            +
                  @internal_responses = default_responses_hash
         | 
| 67 | 
            +
                  @directory = Pathname.new(File.expand_path(File.dirname($PROGRAM_NAME)))
         | 
| 67 68 |  | 
| 68 69 | 
             
                  # allow block to override settings
         | 
| 69 70 | 
             
                  yield self if block_given?
         | 
| @@ -148,11 +149,11 @@ class HighLine | |
| 148 149 | 
             
                #
         | 
| 149 150 | 
             
                # Asks a yes or no confirmation question, to ensure a user knows what
         | 
| 150 151 | 
             
                # they have just agreed to.  The confirm attribute can be set to :
         | 
| 151 | 
            -
                # +true+  :     In this case the question will be, "Are you sure?". | 
| 152 | 
            -
                # Proc    :     The Proc is yielded the answer given. The Proc must | 
| 153 | 
            -
                #               output a string which is then used as the confirm | 
| 154 | 
            -
                #               question. | 
| 155 | 
            -
                # String  :     The String must use ERB syntax. The String is | 
| 152 | 
            +
                # +true+  :     In this case the question will be, "Are you sure?".
         | 
| 153 | 
            +
                # Proc    :     The Proc is yielded the answer given. The Proc must
         | 
| 154 | 
            +
                #               output a string which is then used as the confirm
         | 
| 155 | 
            +
                #               question.
         | 
| 156 | 
            +
                # String  :     The String must use ERB syntax. The String is
         | 
| 156 157 | 
             
                #               evaluated with access to question and answer and
         | 
| 157 158 | 
             
                #               is then used as the confirm question.
         | 
| 158 159 | 
             
                # When set to +false+ or +nil+ (the default), answers are not confirmed.
         | 
| @@ -261,8 +262,8 @@ class HighLine | |
| 261 262 |  | 
| 262 263 | 
             
                def default_responses_hash
         | 
| 263 264 | 
             
                  {
         | 
| 264 | 
            -
                    : | 
| 265 | 
            -
                    : | 
| 265 | 
            +
                    ask_on_error: "?  ",
         | 
| 266 | 
            +
                    mismatch: "Your entries didn't match."
         | 
| 266 267 | 
             
                  }
         | 
| 267 268 | 
             
                end
         | 
| 268 269 |  | 
| @@ -270,15 +271,15 @@ class HighLine | |
| 270 271 | 
             
                # @param message_source (see #build_responses)
         | 
| 271 272 | 
             
                # @return [Hash] responses hash
         | 
| 272 273 | 
             
                def build_responses_new_hash(message_source)
         | 
| 273 | 
            -
                  { : | 
| 274 | 
            -
             | 
| 275 | 
            -
                    : | 
| 276 | 
            -
                    : | 
| 277 | 
            -
             | 
| 278 | 
            -
                    : | 
| 279 | 
            -
             | 
| 280 | 
            -
                    : | 
| 281 | 
            -
             | 
| 274 | 
            +
                  { ambiguous_completion: "Ambiguous choice.  Please choose one of " +
         | 
| 275 | 
            +
                    choice_error_str(message_source) + ".",
         | 
| 276 | 
            +
                    invalid_type: "You must enter a valid #{message_source}.",
         | 
| 277 | 
            +
                    no_completion: "You must choose one of " +
         | 
| 278 | 
            +
                      choice_error_str(message_source) + ".",
         | 
| 279 | 
            +
                    not_in_range: "Your answer isn't within the expected range " \
         | 
| 280 | 
            +
                      "(#{expected_range}).",
         | 
| 281 | 
            +
                    not_valid: "Your answer isn't valid (must match " \
         | 
| 282 | 
            +
                      "#{validate.inspect})." }
         | 
| 282 283 | 
             
                end
         | 
| 283 284 |  | 
| 284 285 | 
             
                # This is the actual responses hash that gets used in determining output
         | 
| @@ -369,7 +370,7 @@ class HighLine | |
| 369 370 |  | 
| 370 371 | 
             
                # Returns an English explanation of the current range settings.
         | 
| 371 372 | 
             
                def expected_range
         | 
| 372 | 
            -
                  expected = [ | 
| 373 | 
            +
                  expected = []
         | 
| 373 374 |  | 
| 374 375 | 
             
                  expected << "above #{above}" if above
         | 
| 375 376 | 
             
                  expected << "below #{below}" if below
         | 
| @@ -392,7 +393,7 @@ class HighLine | |
| 392 393 |  | 
| 393 394 | 
             
                # Returns true if _first_answer_ is set.
         | 
| 394 395 | 
             
                def first_answer?
         | 
| 395 | 
            -
                   | 
| 396 | 
            +
                  true if @first_answer
         | 
| 396 397 | 
             
                end
         | 
| 397 398 |  | 
| 398 399 | 
             
                #
         | 
| @@ -402,9 +403,9 @@ class HighLine | |
| 402 403 | 
             
                # are not checked.
         | 
| 403 404 | 
             
                #
         | 
| 404 405 | 
             
                def in_range?
         | 
| 405 | 
            -
                  (!above  | 
| 406 | 
            -
             | 
| 407 | 
            -
             | 
| 406 | 
            +
                  (!above || answer > above) &&
         | 
| 407 | 
            +
                    (!below || answer < below) &&
         | 
| 408 | 
            +
                    (!@in || @in.include?(answer))
         | 
| 408 409 | 
             
                end
         | 
| 409 410 |  | 
| 410 411 | 
             
                #
         | 
| @@ -468,7 +469,7 @@ class HighLine | |
| 468 469 | 
             
                      File.basename(file)
         | 
| 469 470 | 
             
                    end
         | 
| 470 471 | 
             
                  else
         | 
| 471 | 
            -
                    [ | 
| 472 | 
            +
                    []
         | 
| 472 473 | 
             
                  end
         | 
| 473 474 | 
             
                end
         | 
| 474 475 |  | 
| @@ -485,9 +486,9 @@ class HighLine | |
| 485 486 | 
             
                # and case handling.
         | 
| 486 487 | 
             
                #
         | 
| 487 488 | 
             
                def valid_answer?
         | 
| 488 | 
            -
                  !validate  | 
| 489 | 
            -
             | 
| 490 | 
            -
             | 
| 489 | 
            +
                  !validate ||
         | 
| 490 | 
            +
                    (validate.is_a?(Regexp) && answer =~ validate) ||
         | 
| 491 | 
            +
                    (validate.is_a?(Proc)   && validate[answer])
         | 
| 491 492 | 
             
                end
         | 
| 492 493 |  | 
| 493 494 | 
             
                #
         | 
| @@ -534,11 +535,11 @@ class HighLine | |
| 534 535 | 
             
                  if confirm == true
         | 
| 535 536 | 
             
                    "Are you sure?  "
         | 
| 536 537 | 
             
                  elsif confirm.is_a?(Proc)
         | 
| 537 | 
            -
                    confirm.call( | 
| 538 | 
            +
                    confirm.call(answer)
         | 
| 538 539 | 
             
                  else
         | 
| 539 540 | 
             
                    # evaluate ERb under initial scope, so it will have
         | 
| 540 541 | 
             
                    # access to question and answer
         | 
| 541 | 
            -
                    template | 
| 542 | 
            +
                    template = ERB.new(confirm, nil, "%")
         | 
| 542 543 | 
             
                    template_renderer = TemplateRenderer.new(template, self, highline)
         | 
| 543 544 | 
             
                    template_renderer.render
         | 
| 544 545 | 
             
                  end
         | 
| @@ -547,7 +548,8 @@ class HighLine | |
| 547 548 | 
             
                # Provides the String to be asked when at an error situation.
         | 
| 548 549 | 
             
                # It may be just the question itself (repeat on error).
         | 
| 549 550 | 
             
                # @return [self] if :ask_on_error on responses Hash is set to :question
         | 
| 550 | 
            -
                # @return [String] if :ask_on_error on responses Hash is set to | 
| 551 | 
            +
                # @return [String] if :ask_on_error on responses Hash is set to
         | 
| 552 | 
            +
                #   something else
         | 
| 551 553 | 
             
                def ask_on_error_msg
         | 
| 552 554 | 
             
                  if final_responses[:ask_on_error] == :question
         | 
| 553 555 | 
             
                    self
         | 
| @@ -564,7 +566,7 @@ class HighLine | |
| 564 566 | 
             
                # @param highline [HighLine] context
         | 
| 565 567 | 
             
                # @return [void]
         | 
| 566 568 | 
             
                def show_question(highline)
         | 
| 567 | 
            -
                  highline.say(self) unless  | 
| 569 | 
            +
                  highline.say(self) unless readline && (echo == true && !limit)
         | 
| 568 570 | 
             
                end
         | 
| 569 571 |  | 
| 570 572 | 
             
                # Returns an echo string that is adequate for this Question settings.
         | 
| @@ -577,7 +579,7 @@ class HighLine | |
| 577 579 | 
             
                  if echo == true
         | 
| 578 580 | 
             
                    response
         | 
| 579 581 | 
             
                  # any truethy value, probably a String
         | 
| 580 | 
            -
                  elsif  | 
| 582 | 
            +
                  elsif echo
         | 
| 581 583 | 
             
                    echo
         | 
| 582 584 | 
             
                  # any falsy value, false or nil
         | 
| 583 585 | 
             
                  else
         | 
| @@ -594,11 +596,11 @@ class HighLine | |
| 594 596 | 
             
                #
         | 
| 595 597 | 
             
                def append_default
         | 
| 596 598 | 
             
                  if template =~ /([\t ]+)\Z/
         | 
| 597 | 
            -
                    template << "|#{default}|#{ | 
| 599 | 
            +
                    template << "|#{default}|#{Regexp.last_match(1)}"
         | 
| 598 600 | 
             
                  elsif template == ""
         | 
| 599 601 | 
             
                    template << "|#{default}|  "
         | 
| 600 602 | 
             
                  elsif template[-1, 1] == "\n"
         | 
| 601 | 
            -
                    template[-2, 0] = | 
| 603 | 
            +
                    template[-2, 0] = "  |#{default}|"
         | 
| 602 604 | 
             
                  else
         | 
| 603 605 | 
             
                    template << "  |#{default}|"
         | 
| 604 606 | 
             
                  end
         | 
| @@ -606,7 +608,7 @@ class HighLine | |
| 606 608 |  | 
| 607 609 | 
             
                def choice_error_str(message_source)
         | 
| 608 610 | 
             
                  if message_source.is_a? Array
         | 
| 609 | 
            -
                     | 
| 611 | 
            +
                    "[" + message_source.join(", ") + "]"
         | 
| 610 612 | 
             
                  else
         | 
| 611 613 | 
             
                    message_source.inspect
         | 
| 612 614 | 
             
                  end
         |