Sha256: a17d251ad02ef097204f1c9eb2d8f91b0d672f74ff5cf0e2d4c7a9dd5623a7da

Contents?: true

Size: 1.79 KB

Versions: 202

Compression:

Stored size: 1.79 KB

Contents

class Thor
  module LineEditor
    class Readline < Basic
      def self.available?
        begin
          require "readline"
        rescue LoadError
        end

        Object.const_defined?(:Readline)
      end

      def readline
        if echo?
          ::Readline.completion_append_character = nil
          # rb-readline does not allow Readline.completion_proc= to receive nil.
          if complete = completion_proc
            ::Readline.completion_proc = complete
          end
          ::Readline.readline(prompt, add_to_history?)
        else
          super
        end
      end

    private

      def add_to_history?
        options.fetch(:add_to_history, true)
      end

      def completion_proc
        if use_path_completion?
          proc { |text| PathCompletion.new(text).matches }
        elsif completion_options.any?
          proc do |text|
            completion_options.select { |option| option.start_with?(text) }
          end
        end
      end

      def completion_options
        options.fetch(:limited_to, [])
      end

      def use_path_completion?
        options.fetch(:path, false)
      end

      class PathCompletion
        attr_reader :text
        private :text

        def initialize(text)
          @text = text
        end

        def matches
          relative_matches
        end

      private

        def relative_matches
          absolute_matches.map { |path| path.sub(base_path, "") }
        end

        def absolute_matches
          Dir[glob_pattern].map do |path|
            if File.directory?(path)
              "#{path}/"
            else
              path
            end
          end
        end

        def glob_pattern
          "#{base_path}#{text}*"
        end

        def base_path
          "#{Dir.pwd}/"
        end
      end
    end
  end
end

Version data entries

202 entries across 155 versions & 22 rubygems

Version Path
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb