[前][次][番号順一覧][スレッド一覧]

ruby-changes:60940

From: aycabta <ko1@a...>
Date: Wed, 29 Apr 2020 19:14:31 +0900 (JST)
Subject: [ruby-changes:60940] 98a346d065 (master): [ruby/irb] Add irb_info command

https://git.ruby-lang.org/ruby.git/commit/?id=98a346d065

From 98a346d065cc981d60ec0e45f7f15ba7328b6ad6 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Mon, 27 Apr 2020 18:27:19 +0900
Subject: [ruby/irb] Add irb_info command

https://github.com/ruby/irb/commit/a6fe58e916

diff --git a/lib/irb/cmd/info.rb b/lib/irb/cmd/info.rb
new file mode 100644
index 0000000..ddf57b0
--- /dev/null
+++ b/lib/irb/cmd/info.rb
@@ -0,0 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/cmd/info.rb#L1
+# frozen_string_literal: false
+
+require_relative "nop"
+
+# :stopdoc:
+module IRB
+  module ExtendCommand
+    class Info < Nop
+      def execute
+        Class.new {
+          def inspect
+            <<~EOM.chomp
+              Ruby version: #{RUBY_VERSION}
+              IRB version: #{IRB.version}
+              InputMethod: #{IRB.CurrentContext.io.inspect}
+              .irbrc path: #{IRB.rc_file}
+            EOM
+          end
+          alias_method :to_s, :inspect
+        }.new
+      end
+    end
+  end
+end
+# :startdoc:
diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb
index de145e9..828103c 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -121,6 +121,10 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/extend-command.rb#L121
         [:help, NO_OVERRIDE],
       ],
 
+      [
+        :irb_info, :Info, "irb/cmd/info"
+      ],
+
     ]
 
     # Installs the default irb commands:
diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index 9fbbaeb..47fc5db 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -43,6 +43,11 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L43
     def readable_after_eof?
       false
     end
+
+    # For debug message
+    def inspect
+      'Abstract InputMethod'
+    end
   end
 
   class StdioInputMethod < InputMethod
@@ -93,6 +98,11 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L98
     def encoding
       @stdin.external_encoding
     end
+
+    # For debug message
+    def inspect
+      'StdioInputMethod'
+    end
   end
 
   # Use a File for IO with irb, see InputMethod
@@ -125,6 +135,11 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L135
     def encoding
       @io.external_encoding
     end
+
+    # For debug message
+    def inspect
+      'FileInputMethod'
+    end
   end
 
   begin
@@ -202,6 +217,13 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L217
       end
       Readline.completion_append_character = nil
       Readline.completion_proc = IRB::InputCompletor::CompletionProc
+
+      # For debug message
+      def inspect
+        inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
+        readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
+        "ReadlineInputMethod with #{readline_impl} #{Readline::VERSION} and #{inputrc_path}"
+      end
     end
   rescue LoadError
   end
@@ -297,5 +319,16 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/input-method.rb#L319
     def encoding
       @stdin.external_encoding
     end
+
+    # For debug message
+    def inspect
+      config = Reline::Config.new
+      if config.respond_to?(:inputrc_path)
+        inputrc_path = config.inputrc_path
+      else
+        inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
+      end
+      "ReidlineInputMethod with Reline #{Reline::VERSION} and #{inputrc_path}"
+    end
   end
 end
diff --git a/test/irb/test_cmd.rb b/test/irb/test_cmd.rb
new file mode 100644
index 0000000..1f38551
--- /dev/null
+++ b/test/irb/test_cmd.rb
@@ -0,0 +1,64 @@ https://github.com/ruby/ruby/blob/trunk/test/irb/test_cmd.rb#L1
+# frozen_string_literal: false
+require "test/unit"
+require "irb"
+require "irb/extend-command"
+
+module TestIRB
+  class ExtendCommand < Test::Unit::TestCase
+    def setup
+      @pwd = Dir.pwd
+      @tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}")
+      begin
+        Dir.mkdir(@tmpdir)
+      rescue Errno::EEXIST
+        FileUtils.rm_rf(@tmpdir)
+        Dir.mkdir(@tmpdir)
+      end
+      Dir.chdir(@tmpdir)
+      @home_backup = ENV["HOME"]
+      ENV["HOME"] = @tmpdir
+    end
+
+    def teardown
+      ENV["HOME"] = @home_backup
+      Dir.chdir(@pwd)
+      FileUtils.rm_rf(@tmpdir)
+    end
+
+    def test_irb_info_multiline
+      FileUtils.touch("#{@tmpdir}/.inputrc")
+      FileUtils.touch("#{@tmpdir}/.irbrc")
+      IRB.setup(__FILE__, argv: [])
+      IRB.conf[:USE_MULTILINE] = true
+      IRB.conf[:USE_SINGLELINE] = false
+      workspace = IRB::WorkSpace.new(self)
+      irb = IRB::Irb.new(workspace)
+      IRB.conf[:MAIN_CONTEXT] = irb.context
+      expected = %r{
+        Ruby\sversion: .+\n
+        IRB\sversion:\sirb .+\n
+        InputMethod:\sReidlineInputMethod\swith\sReline .+ and .+\n
+        \.irbrc\spath: .+
+      }x
+      assert_match expected, irb.context.main.irb_info.to_s
+    end
+
+    def test_irb_info_singleline
+      FileUtils.touch("#{@tmpdir}/.inputrc")
+      FileUtils.touch("#{@tmpdir}/.irbrc")
+      IRB.setup(__FILE__, argv: [])
+      IRB.conf[:USE_MULTILINE] = false
+      IRB.conf[:USE_SINGLELINE] = true
+      workspace = IRB::WorkSpace.new(self)
+      irb = IRB::Irb.new(workspace)
+      IRB.conf[:MAIN_CONTEXT] = irb.context
+      expected = %r{
+        Ruby\sversion: .+\n
+        IRB\sversion:\sirb .+\n
+        InputMethod:\sReadlineInputMethod\swith .+ and .+\n
+        \.irbrc\spath: .+
+      }x
+      assert_match expected, irb.context.main.irb_info.to_s
+    end
+  end
+end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]