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
es_cli-0.1.0 vendor/bundle/ruby/3.1.0/gems/thor-1.3.2/lib/thor/line_editor/readline.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/thor-1.3.1/lib/thor/line_editor/readline.rb
thor-1.3.2 lib/thor/line_editor/readline.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/thor-1.3.1/lib/thor/line_editor/readline.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/lib/thor/line_editor/readline.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/lib/thor/line_editor/readline.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/lib/thor/line_editor/readline.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/lib/thor/line_editor/readline.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
thor-1.3.1 lib/thor/line_editor/readline.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/line_editor/readline.rb