highline 1.6.1 → 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.
@@ -1,5 +1,3 @@
1
- #!/usr/local/bin/ruby -w
2
-
3
1
  # system_extensions.rb
4
2
  #
5
3
  # Created by James Edward Gray II on 2006-06-14.
@@ -11,10 +9,42 @@ require "highline/compatibility"
11
9
 
12
10
  class HighLine
13
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
+
14
42
  module_function
15
43
 
16
- JRUBY = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
17
-
44
+ def get_character( input = STDIN )
45
+ input.getbyte
46
+ end
47
+
18
48
  #
19
49
  # This section builds character reading and terminal size functions
20
50
  # to suit the proper platform we're running on. Be warned: Here be
@@ -30,14 +60,23 @@ class HighLine
30
60
 
31
61
  #
32
62
  # Windows savvy getc().
33
- #
63
+ #
34
64
  # *WARNING*: This method ignores <tt>input</tt> and reads one
35
65
  # character from +STDIN+!
36
- #
66
+ #
37
67
  def get_character( input = STDIN )
68
+ Win32API.new("msvcrt", "_getch", [ ], "L").Call
69
+ rescue Exception
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',
@@ -51,124 +90,91 @@ class HighLine
51
90
  format = 'SSSSSssssSS'
52
91
  buf = ([0] * format.size).pack(format)
53
92
  stdout_handle = m_GetStdHandle.call(0xFFFFFFF5)
54
-
93
+
55
94
  m_GetConsoleScreenBufferInfo.call(stdout_handle, buf)
56
- bufx, bufy, curx, cury, wattr,
57
- left, top, right, bottom, maxx, maxy = buf.unpack(format)
95
+ _, _, _, _, _,
96
+ left, top, right, bottom, _, _ = buf.unpack(format)
58
97
  return right - left + 1, bottom - top + 1
59
98
  end
60
99
  rescue LoadError # If we're not on Windows try...
61
100
  begin
62
- raise LoadError
63
101
  require "termios" # Unix, first choice termios.
64
102
 
65
103
  CHARACTER_MODE = "termios" # For Debugging purposes only.
66
104
 
67
- #
68
- # Unix savvy getc(). (First choice.)
69
- #
70
- # *WARNING*: This method requires the "termios" library!
71
- #
72
- def get_character( input = STDIN )
73
- old_settings = Termios.getattr(input)
74
-
75
- new_settings = old_settings.dup
105
+ def raw_no_echo_mode
106
+ @state = Termios.getattr(input)
107
+ new_settings = @state.dup
76
108
  new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
77
109
  new_settings.c_cc[Termios::VMIN] = 1
110
+ Termios.setattr(input, Termios::TCSANOW, new_settings)
111
+ end
78
112
 
79
- begin
80
- Termios.setattr(input, Termios::TCSANOW, new_settings)
81
- input.getbyte
82
- ensure
83
- Termios.setattr(input, Termios::TCSANOW, old_settings)
84
- end
113
+ def restore_mode
114
+ Termios.setattr(input, Termios::TCSANOW, @state)
85
115
  end
86
- rescue LoadError # If our first choice fails, try using ffi-ncurses.
87
- begin
88
- require 'ffi-ncurses' # The ffi gem is builtin to JRUBY and because stty does not
89
- # work correctly in JRuby manually installing the ffi-ncurses
90
- # gem is the only way to get highline to operate correctly in
91
- # JRuby. The ncurses library is only present on unix platforms
92
- # so this is not a solution for using highline in JRuby on
93
- # windows.
94
-
95
- CHARACTER_MODE = "ncurses" # For Debugging purposes only.
96
-
97
- #
98
- # ncurses savvy getc(). (JRuby choice.)
99
- #
100
- def get_character( input = STDIN )
101
- FFI::NCurses.initscr
102
- FFI::NCurses.cbreak
103
- begin
104
- FFI::NCurses.curs_set 0
105
- input.getbyte
106
- ensure
107
- FFI::NCurses.endwin
108
- end
109
- 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.
110
119
 
111
- rescue LoadError => e # If the ffi-ncurses choice fails, try using stty
112
- if JRUBY
113
- if e.message =~ /^no such file to load/
114
- STDERR.puts "\n*** Using highline effectively in JRuby requires manually installing the ffi-ncurses gem.\n*** jruby -S gem install ffi-ncurses"
115
- else
116
- raise
117
- end
118
- end
119
- CHARACTER_MODE = "stty" # For Debugging purposes only.
120
-
121
- #
122
- # Unix savvy getc(). (Second choice.)
123
- #
124
- # *WARNING*: This method requires the external "stty" program!
125
- #
126
- def get_character( input = STDIN )
127
- raw_no_echo_mode
128
-
129
- begin
130
- input.getbyte
131
- ensure
132
- restore_mode
133
- end
120
+ def terminal_size
121
+ [ @java_terminal.getTerminalWidth, @java_terminal.getTerminalHeight ]
134
122
  end
135
123
 
136
- #
137
- # Switched the input mode to raw and disables echo.
138
- #
139
- # *WARNING*: This method requires the external "stty" program!
140
- #
141
124
  def raw_no_echo_mode
142
- @state = `stty -g`
143
- system "stty raw -echo -icanon isig"
125
+ @state = @java_console.getEchoCharacter
126
+ @java_console.setEchoCharacter 0
144
127
  end
145
128
 
146
- #
147
- # Restores a previously saved input mode.
148
- #
149
- # *WARNING*: This method requires the external "stty" program!
150
- #
151
129
  def restore_mode
152
- system "stty #{@state}"
130
+ @java_console.setEchoCharacter @state
153
131
  end
154
- end
155
- end
156
- if CHARACTER_MODE == 'ncurses'
157
- #
158
- # A ncurses savvy method to fetch the console columns, and rows.
159
- #
160
- def terminal_size
161
- size = [80, 40]
162
- FFI::NCurses.initscr
132
+ else # If we are not on JRuby, try ncurses
163
133
  begin
164
- size = FFI::NCurses.getmaxyx(stdscr).reverse
165
- ensure
166
- 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
167
171
  end
168
- size
169
172
  end
170
- else
171
- # 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.
172
178
  # ... stty does not work in JRuby
173
179
  def terminal_size
174
180
  if /solaris/ =~ RUBY_PLATFORM and