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.2.0 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb