highline 1.6.13 → 1.6.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Below is a complete listing of changes for each revision of HighLine.
4
4
 
5
+ == 1.6.14
6
+
7
+ * Added JRuby 1.7 support (by Mina Nagy).
8
+ * Take into account color escape sequences when wrapping text (by Mark J.
9
+ Titorenko).
10
+
5
11
  == 1.6.13
6
12
 
7
13
  * Removed unneeded Shebang lines (by Scott Gonyea).
@@ -28,7 +28,7 @@ require "highline/style"
28
28
  #
29
29
  class HighLine
30
30
  # The version of the installed library.
31
- VERSION = "1.6.13".freeze
31
+ VERSION = "1.6.14".freeze
32
32
 
33
33
  # An internal HighLine error. User code does not need to trap this.
34
34
  class QuestionError < StandardError
@@ -175,23 +175,9 @@ class HighLine
175
175
  #
176
176
  def initialize( input = $stdin, output = $stdout,
177
177
  wrap_at = nil, page_at = nil )
178
+ super()
178
179
  @input = input
179
180
  @output = output
180
- if JRUBY
181
- require 'java'
182
- java_import 'java.io.OutputStreamWriter'
183
- java_import 'java.nio.channels.Channels'
184
- java_import 'jline.ConsoleReader'
185
- java_import 'jline.Terminal'
186
-
187
- @java_input = Channels.newInputStream($stdin.to_channel)
188
- @java_output = OutputStreamWriter.new(Channels.newOutputStream($stdout.to_channel))
189
- @java_terminal = Terminal.getTerminal
190
- @java_console = ConsoleReader.new(@java_input, @java_output)
191
- @java_console.setUseHistory(false)
192
- @java_console.setBellEnabled(true)
193
- @java_console.setUsePagination(false)
194
- end
195
181
 
196
182
  self.wrap_at = wrap_at
197
183
  self.page_at = page_at
@@ -255,7 +241,9 @@ class HighLine
255
241
  # readline() needs to handle it's own output, but readline only supports
256
242
  # full line reading. Therefore if @question.echo is anything but true,
257
243
  # the prompt will not be issued. And we have to account for that now.
258
- say(@question) unless (@question.readline and @question.echo == true)
244
+ # Also, JRuby-1.7's ConsoleReader.readLine() needs to be passed the prompt
245
+ # to handle line editing properly.
246
+ say(@question) unless ((JRUBY or @question.readline) and @question.echo == true)
259
247
  begin
260
248
  @answer = @question.answer_or_default(get_response)
261
249
  unless @question.valid_answer?(@answer)
@@ -796,13 +784,7 @@ class HighLine
796
784
  answer
797
785
  else
798
786
  if JRUBY
799
- enable_echo_afterwards = @java_terminal.isEchoEnabled
800
- @java_terminal.disableEcho
801
- begin
802
- raw_answer = @java_console.readLine(nil, nil)
803
- ensure
804
- @java_terminal.enableEcho if enable_echo_afterwards
805
- end
787
+ raw_answer = @java_console.readLine(@question.question, nil)
806
788
  else
807
789
  raise EOFError, "The input stream is exhausted." if @@track_eof and
808
790
  @input.eof?
@@ -813,16 +795,6 @@ class HighLine
813
795
  end
814
796
  end
815
797
 
816
- def get_single_character(is_stty)
817
- if JRUBY
818
- @java_console.readVirtualKey
819
- elsif is_stty
820
- @input.getbyte
821
- else
822
- get_character(@input)
823
- end
824
- end
825
-
826
798
  #
827
799
  # Return a line or character of input, as requested for this question.
828
800
  # Character input will be returned as a single character String,
@@ -841,18 +813,13 @@ class HighLine
841
813
  if @question.echo == true and @question.limit.nil?
842
814
  get_line
843
815
  else
844
- if JRUBY
845
- enable_echo_afterwards = @java_terminal.isEchoEnabled
846
- @java_terminal.disableEcho
847
- elsif stty
848
- raw_no_echo_mode
849
- end
816
+ raw_no_echo_mode
850
817
 
851
818
  line = ""
852
819
  backspace_limit = 0
853
820
  begin
854
821
 
855
- while character = get_single_character(stty)
822
+ while character = get_character(@input)
856
823
  # honor backspace and delete
857
824
  if character == 127 or character == 8
858
825
  line.slice!(-1, 1)
@@ -885,11 +852,7 @@ class HighLine
885
852
  break if @question.limit and line.size == @question.limit
886
853
  end
887
854
  ensure
888
- if JRUBY
889
- @java_terminal.enableEcho if enable_echo_afterwards
890
- elsif stty
891
- restore_mode
892
- end
855
+ restore_mode
893
856
  end
894
857
  if @question.overwrite
895
858
  @output.print("\r#{HighLine.Style(:erase_line).code}")
@@ -901,15 +864,12 @@ class HighLine
901
864
  @question.change_case(@question.remove_whitespace(line))
902
865
  end
903
866
  else
904
- if JRUBY
905
- enable_echo_afterwards = @java_terminal.isEchoEnabled
906
- @java_terminal.disableEcho
907
- end
867
+ raw_no_echo_mode
908
868
  begin
909
869
  if @question.character == :getc
910
- response = get_single_character(true).chr
870
+ response = @input.getbyte.chr
911
871
  else
912
- response = get_single_character(stty).chr
872
+ response = get_character(@input).chr
913
873
  if @question.overwrite
914
874
  @output.print("\r#{HighLine.Style(:erase_line).code}")
915
875
  @output.flush
@@ -925,9 +885,7 @@ class HighLine
925
885
  end
926
886
  end
927
887
  ensure
928
- if JRUBY
929
- @java_terminal.enableEcho if enable_echo_afterwards
930
- end
888
+ restore_mode
931
889
  end
932
890
  @question.change_case(response)
933
891
  end
@@ -971,15 +929,17 @@ class HighLine
971
929
  def wrap( text )
972
930
  wrapped = [ ]
973
931
  text.each_line do |line|
974
- while line =~ /([^\n]{#{@wrap_at + 1},})/
932
+ # take into account color escape sequences when wrapping
933
+ wrap_at = @wrap_at + (line.length - actual_length(line))
934
+ while line =~ /([^\n]{#{wrap_at + 1},})/
975
935
  search = $1.dup
976
936
  replace = $1.dup
977
- if index = replace.rindex(" ", @wrap_at)
937
+ if index = replace.rindex(" ", wrap_at)
978
938
  replace[index, 1] = "\n"
979
939
  replace.sub!(/\n[ \t]+/, "\n")
980
940
  line.sub!(search, replace)
981
941
  else
982
- line[$~.begin(1) + @wrap_at, 0] = "\n"
942
+ line[$~.begin(1) + wrap_at, 0] = "\n"
983
943
  end
984
944
  end
985
945
  wrapped << line
@@ -9,9 +9,41 @@ require "highline/compatibility"
9
9
 
10
10
  class HighLine
11
11
  module SystemExtensions
12
+ JRUBY = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
13
+
14
+ def initialize
15
+ if JRUBY
16
+ require 'java'
17
+ if JRUBY_VERSION =~ /^1.7/
18
+ java_import 'jline.console.ConsoleReader'
19
+
20
+ @java_console = ConsoleReader.new($stdin.to_inputstream, $stdout.to_outputstream)
21
+ @java_console.set_history_enabled(false)
22
+ @java_console.set_bell_enabled(true)
23
+ @java_console.set_pagination_enabled(false)
24
+ @java_terminal = @java_console.getTerminal
25
+ elsif JRUBY_VERSION =~ /^1.6/
26
+ java_import 'java.io.OutputStreamWriter'
27
+ java_import 'java.nio.channels.Channels'
28
+ java_import 'jline.ConsoleReader'
29
+ java_import 'jline.Terminal'
30
+
31
+ @java_input = Channels.newInputStream($stdin.to_channel)
32
+ @java_output = OutputStreamWriter.new(Channels.newOutputStream($stdout.to_channel))
33
+ @java_terminal = Terminal.getTerminal
34
+ @java_console = ConsoleReader.new(@java_input, @java_output)
35
+ @java_console.setUseHistory(false)
36
+ @java_console.setBellEnabled(true)
37
+ @java_console.setUsePagination(false)
38
+ end
39
+ end
40
+ end
41
+
12
42
  module_function
13
43
 
14
- JRUBY = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
44
+ def get_character( input = STDIN )
45
+ input.getbyte
46
+ end
15
47
 
16
48
  #
17
49
  # This section builds character reading and terminal size functions
@@ -38,6 +70,13 @@ class HighLine
38
70
  Win32API.new("crtdll", "_getch", [ ], "L").Call
39
71
  end
40
72
 
73
+ # We do not define a raw_no_echo_mode for Windows as _getch turns off echo
74
+ def raw_no_echo_mode
75
+ end
76
+
77
+ def restore_mode
78
+ end
79
+
41
80
  # A Windows savvy method to fetch the console columns, and rows.
42
81
  def terminal_size
43
82
  m_GetStdHandle = Win32API.new( 'kernel32',
@@ -63,114 +102,79 @@ class HighLine
63
102
 
64
103
  CHARACTER_MODE = "termios" # For Debugging purposes only.
65
104
 
66
- #
67
- # Unix savvy getc(). (First choice.)
68
- #
69
- # *WARNING*: This method requires the "termios" library!
70
- #
71
- def get_character( input = STDIN )
72
- return input.getbyte if input.is_a? StringIO
73
-
74
- old_settings = Termios.getattr(input)
75
-
76
- new_settings = old_settings.dup
105
+ def raw_no_echo_mode
106
+ @state = Termios.getattr(input)
107
+ new_settings = @state.dup
77
108
  new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
78
109
  new_settings.c_cc[Termios::VMIN] = 1
110
+ Termios.setattr(input, Termios::TCSANOW, new_settings)
111
+ end
79
112
 
80
- begin
81
- Termios.setattr(input, Termios::TCSANOW, new_settings)
82
- input.getbyte
83
- ensure
84
- Termios.setattr(input, Termios::TCSANOW, old_settings)
85
- end
113
+ def restore_mode
114
+ Termios.setattr(input, Termios::TCSANOW, @state)
86
115
  end
87
- rescue LoadError # If our first choice fails, try using ffi-ncurses.
88
- begin
89
- require 'ffi-ncurses' # The ffi gem is builtin to JRUBY and because stty does not
90
- # work correctly in JRuby manually installing the ffi-ncurses
91
- # gem is the only way to get highline to operate correctly in
92
- # JRuby. The ncurses library is only present on unix platforms
93
- # so this is not a solution for using highline in JRuby on
94
- # windows.
95
-
96
- CHARACTER_MODE = "ncurses" # For Debugging purposes only.
97
-
98
- #
99
- # ncurses savvy getc().
100
- #
101
- def get_character( input = STDIN )
102
- FFI::NCurses.initscr
103
- FFI::NCurses.cbreak
104
- begin
105
- FFI::NCurses.curs_set 0
106
- input.getbyte
107
- ensure
108
- FFI::NCurses.endwin
109
- end
110
- end
116
+ rescue LoadError # If our first choice fails, try using JLine
117
+ if JRUBY # if we are on JRuby. JLine is bundled with JRuby.
118
+ CHARACTER_MODE = "jline" # For Debugging purposes only.
111
119
 
112
- rescue LoadError # If the ffi-ncurses choice fails, try using stty
113
- CHARACTER_MODE = "stty" # For Debugging purposes only.
114
-
115
- #
116
- # Unix savvy getc(). (Second choice.)
117
- #
118
- # *WARNING*: This method requires the external "stty" program!
119
- #
120
- def get_character( input = STDIN )
121
- raw_no_echo_mode
122
-
123
- begin
124
- input.getbyte
125
- ensure
126
- restore_mode
127
- end
120
+ def terminal_size
121
+ [ @java_terminal.getTerminalWidth, @java_terminal.getTerminalHeight ]
128
122
  end
129
123
 
130
- #
131
- # Switched the input mode to raw and disables echo.
132
- #
133
- # *WARNING*: This method requires the external "stty" program!
134
- #
135
124
  def raw_no_echo_mode
136
- @state = `stty -g`
137
- system "stty raw -echo -icanon isig"
125
+ @state = @java_console.getEchoCharacter
126
+ @java_console.setEchoCharacter 0
138
127
  end
139
128
 
140
- #
141
- # Restores a previously saved input mode.
142
- #
143
- # *WARNING*: This method requires the external "stty" program!
144
- #
145
129
  def restore_mode
146
- system "stty #{@state}"
130
+ @java_console.setEchoCharacter @state
147
131
  end
148
- end
149
- end
150
- if CHARACTER_MODE == 'ncurses'
151
- #
152
- # A ncurses savvy method to fetch the console columns, and rows.
153
- #
154
- def terminal_size
155
- size = [80, 40]
156
- FFI::NCurses.initscr
132
+ else # If we are not on JRuby, try ncurses
157
133
  begin
158
- size = FFI::NCurses.getmaxyx(stdscr).reverse
159
- ensure
160
- FFI::NCurses.endwin
134
+ require 'ffi-ncurses'
135
+ CHARACTER_MODE = "ncurses" # For Debugging purposes only.
136
+
137
+ def raw_no_echo_mode
138
+ FFI::NCurses.initscr
139
+ FFI::NCurses.cbreak
140
+ end
141
+
142
+ def restore_mode
143
+ FFI::NCurses.endwin
144
+ end
145
+
146
+ #
147
+ # A ncurses savvy method to fetch the console columns, and rows.
148
+ #
149
+ def terminal_size
150
+ size = [80, 40]
151
+ FFI::NCurses.initscr
152
+ begin
153
+ size = FFI::NCurses.getmaxyx(stdscr).reverse
154
+ ensure
155
+ FFI::NCurses.endwin
156
+ end
157
+ size
158
+ end
159
+ rescue LoadError # Finally, if all else fails, use stty
160
+ # *WARNING*: This requires the external "stty" program!
161
+ CHARACTER_MODE = "stty" # For Debugging purposes only.
162
+
163
+ def raw_no_echo_mode
164
+ @state = `stty -g`
165
+ system "stty raw -echo -icanon isig"
166
+ end
167
+
168
+ def restore_mode
169
+ system "stty #{@state}"
170
+ end
161
171
  end
162
- size
163
172
  end
164
- elsif JRUBY
165
- # JRuby running on Unix can fetch the number of columns and rows from the builtin Jline library
166
- require 'java'
167
- java_import 'jline.Terminal'
168
- def terminal_size
169
- java_terminal = @java_terminal || Terminal.getTerminal
170
- [ java_terminal.getTerminalWidth, java_terminal.getTerminalHeight ]
171
- end
172
- else
173
- # A Unix savvy method using stty that to fetch the console columns, and rows.
173
+ end
174
+
175
+ # For termios and stty
176
+ if not defined?(terminal_size)
177
+ # A Unix savvy method using stty to fetch the console columns, and rows.
174
178
  # ... stty does not work in JRuby
175
179
  def terminal_size
176
180
  if /solaris/ =~ RUBY_PLATFORM and
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: highline
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.13
4
+ version: 1.6.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-15 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'A high-level IO library that provides validation, type conversion,
15
15
  and more for