From 7601b98f0e24050bcb70d74f510fe6384d4d8f39 Mon Sep 17 00:00:00 2001 From: kvokka Date: Tue, 19 Jan 2016 05:44:30 +0100 Subject: [PATCH 1/4] [FIX] Ctrl+arrow keys navigation fixed --- lib/coolline/coolline.rb | 42 +++++++++++++++++++++++++++++++++++++--- lib/coolline/version.rb | 2 +- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/coolline/coolline.rb b/lib/coolline/coolline.rb index 1a54750..7431676 100644 --- a/lib/coolline/coolline.rb +++ b/lib/coolline/coolline.rb @@ -51,8 +51,17 @@ class Coolline Handler.new(?\C-a..?\C-z) {}, Handler.new(/\A\e(?:\C-h|\x7F)\z/, &:kill_backward_word), + Handler.new("\e[1;5D", &:backward_word), + Handler.new("\e[1;5C", &:forward_word), Handler.new("\eb", &:backward_word), - Handler.new("\ef", &:forward_word), + Handler.new("\e[c", &:backward_word), + Handler.new("\e[5D", &:backward_word), + Handler.new("\eOD", &:backward_word), + Handler.new("\eOd", &:backward_word), + Handler.new("\e[d", &:forward_word), + Handler.new("\e[5C", &:forward_word), + Handler.new("\eOC", &:forward_word), + Handler.new("\eOc", &:forward_word), Handler.new("\e[A", &:previous_history_line), Handler.new("\e[B", &:next_history_line), Handler.new("\e[3~", &:kill_current_char), @@ -235,9 +244,8 @@ def readline_full(prompt = ">> ", default_line = "") @history << @line @input.raw do |raw_stdin| - until (char = raw_stdin.getc) == "\r" + until (char = read_char) == "\r" @menu.erase - handle(char) return if @should_exit @@ -493,4 +501,32 @@ def handle_escape(char) str end end + + def read_char + begin + # save previous state of stty + old_state = `stty -g` + # disable echoing and enable raw (not having to press enter) + system "stty raw -echo" + c = STDIN.getc.chr + # gather next two characters of special keys + if(c=="\e") + extra_thread = Thread.new{ + 7.times{c+= STDIN.getc.chr} + } + # wait just long enough for special keys to get swallowed + extra_thread.join(0.001) + # kill thread so not-so-long special keys don't wait on getc + extra_thread.kill + end + rescue => ex + puts "#{ex.class}: #{ex.message}" + puts ex.backtrace + ensure + # restore previous state of stty + system "stty #{old_state}" + end + return c +end + end diff --git a/lib/coolline/version.rb b/lib/coolline/version.rb index c95fc90..ea2400b 100644 --- a/lib/coolline/version.rb +++ b/lib/coolline/version.rb @@ -1,3 +1,3 @@ class Coolline - Version = "0.5.0" + Version = "0.5.1" end From 56540ec12f00744e90540b146003e7b85addfc6f Mon Sep 17 00:00:00 2001 From: kvokka Date: Tue, 19 Jan 2016 06:15:35 +0100 Subject: [PATCH 2/4] [BUG] Version name in gemspec --- coolline.gemspec | 6 ++---- lib/coolline/version.rb | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/coolline.gemspec b/coolline.gemspec index 8914ba8..675b8e7 100755 --- a/coolline.gemspec +++ b/coolline.gemspec @@ -1,14 +1,12 @@ #!/usr/bin/env ruby # -*- coding: utf-8 -*- -$LOAD_PATH.unshift File.expand_path(File.join("lib", File.dirname(__FILE__))) - -require 'coolline/version' +require File.expand_path('../lib/coolline/version', __FILE__) Gem::Specification.new do |s| s.name = "coolline" - s.version = Coolline::Version + s.version = Coolline::VERSION s.summary = "Sounds like readline, smells like readline, but isn't readline" s.description = <<-eof diff --git a/lib/coolline/version.rb b/lib/coolline/version.rb index ea2400b..2a8cda7 100644 --- a/lib/coolline/version.rb +++ b/lib/coolline/version.rb @@ -1,3 +1,3 @@ class Coolline - Version = "0.5.1" + VERSION = "0.5.1" end From 9d5a1394a09fa16f8fcddca5442839d74b08759c Mon Sep 17 00:00:00 2001 From: kvokka Date: Wed, 20 Jan 2016 01:36:44 +0100 Subject: [PATCH 3/4] Speedup and remove shell commands. RUBY 2.0+ only! --- lib/coolline/coolline.rb | 19 +++---------------- lib/coolline/version.rb | 2 +- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/lib/coolline/coolline.rb b/lib/coolline/coolline.rb index 7431676..414e675 100644 --- a/lib/coolline/coolline.rb +++ b/lib/coolline/coolline.rb @@ -503,30 +503,17 @@ def handle_escape(char) end def read_char - begin - # save previous state of stty - old_state = `stty -g` - # disable echoing and enable raw (not having to press enter) - system "stty raw -echo" - c = STDIN.getc.chr - # gather next two characters of special keys + c = STDIN.getch if(c=="\e") extra_thread = Thread.new{ 7.times{c+= STDIN.getc.chr} } # wait just long enough for special keys to get swallowed - extra_thread.join(0.001) + extra_thread.join(0.00001) # kill thread so not-so-long special keys don't wait on getc extra_thread.kill end - rescue => ex - puts "#{ex.class}: #{ex.message}" - puts ex.backtrace - ensure - # restore previous state of stty - system "stty #{old_state}" + c end - return c -end end diff --git a/lib/coolline/version.rb b/lib/coolline/version.rb index 2a8cda7..4dc3899 100644 --- a/lib/coolline/version.rb +++ b/lib/coolline/version.rb @@ -1,3 +1,3 @@ class Coolline - VERSION = "0.5.1" + VERSION = "0.5.2" end From 8395987726aa05085bf1ffb98a8416595545869f Mon Sep 17 00:00:00 2001 From: kvokka Date: Wed, 20 Jan 2016 23:53:44 +0100 Subject: [PATCH 4/4] changa back delay for ctrl+ keys --- lib/coolline/coolline.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/coolline/coolline.rb b/lib/coolline/coolline.rb index 414e675..0276083 100644 --- a/lib/coolline/coolline.rb +++ b/lib/coolline/coolline.rb @@ -509,7 +509,7 @@ def read_char 7.times{c+= STDIN.getc.chr} } # wait just long enough for special keys to get swallowed - extra_thread.join(0.00001) + extra_thread.join(0.0001) # kill thread so not-so-long special keys don't wait on getc extra_thread.kill end