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

ruby-changes:37339

From: usa <ko1@a...>
Date: Tue, 27 Jan 2015 16:01:06 +0900 (JST)
Subject: [ruby-changes:37339] usa:r49420 (trunk): * tool/redmine-backporter.rb: added history feature for platforms which

usa	2015-01-27 16:00:50 +0900 (Tue, 27 Jan 2015)

  New Revision: 49420

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49420

  Log:
    * tool/redmine-backporter.rb: added history feature for platforms which
      lack readline.

  Modified files:
    trunk/ChangeLog
    trunk/tool/redmine-backporter.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49419)
+++ ChangeLog	(revision 49420)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jan 27 15:58:23 2015  NAKAMURA Usaku  <usa@r...>
+
+	* tool/redmine-backporter.rb: added history feature for platforms which
+	  lack readline.
+
 Mon Jan 26 22:09:35 2015  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* .gitignore: ignored temporary file with git.
Index: tool/redmine-backporter.rb
===================================================================
--- tool/redmine-backporter.rb	(revision 49419)
+++ tool/redmine-backporter.rb	(revision 49420)
@@ -200,35 +200,74 @@ def more(sio) https://github.com/ruby/ruby/blob/trunk/tool/redmine-backporter.rb#L200
   end
 end
 
-def Readline.readline(prompt = '')
-  console = IO.console
-  console.binmode
-  ly, lx = console.winsize
-  if /mswin|mingw/ =~ RUBY_PLATFORM or /^(?:vt\d\d\d|xterm)/i =~ ENV["TERM"]
-    cls = "\r\e[2K"
-  else
-    cls = "\r" << (" " * lx)
-  end
-  cls << "\r" << prompt
-  console.print prompt
-  console.flush
-  line = ''
-  while 1
-    case c = console.getch
-    when "\r", "\n"
-      puts
-      return line
-    when "\C-?", "\b" # DEL/BS
-      print "\b \b" if line.chop!
-    when "\C-u"
-      print cls
-      line.clear
-    when "\C-d"
-      return nil if line.empty?
-      line << c
+class << Readline
+  def readline(prompt = '')
+    console = IO.console
+    console.binmode
+    ly, lx = console.winsize
+    if /mswin|mingw/ =~ RUBY_PLATFORM or /^(?:vt\d\d\d|xterm)/i =~ ENV["TERM"]
+      cls = "\r\e[2K"
     else
-      print c
-      line << c
+      cls = "\r" << (" " * lx)
+    end
+    cls << "\r" << prompt
+    console.print prompt
+    console.flush
+    line = ''
+    while 1
+      case c = console.getch
+      when "\r", "\n"
+        puts
+        HISTORY << line
+        return line
+      when "\C-?", "\b" # DEL/BS
+        print "\b \b" if line.chop!
+      when "\C-u"
+        print cls
+        line.clear
+      when "\C-d"
+        return nil if line.empty?
+        line << c
+      when "\C-p"
+        HISTORY.pos -= 1
+        line = HISTORY.current
+        print cls
+        print line
+      when "\C-n"
+        HISTORY.pos += 1
+        line = HISTORY.current
+        print cls
+        print line
+      else
+        print c
+        line << c
+      end
+    end
+  end
+
+  HISTORY = []
+  def HISTORY.<<(val)
+    HISTORY.push(val)
+    @pos = self.size
+    self
+  end
+  def HISTORY.pos
+    @pos ||= 0
+  end
+  def HISTORY.pos=(val)
+    @pos = val
+    if @pos < 0
+      @pos = -1
+    elsif @pos >= self.size
+      @pos = self.size
+    end
+  end
+  def HISTORY.current
+    @pos ||= 0
+    if @pos < 0 || @pos >= self.size
+      ''
+    else
+      self[@pos]
     end
   end
 end unless defined?(Readline.readline)

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

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