highline 1.7.10 → 2.0.0.pre.develop.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.simplecov +5 -0
  4. data/.travis.yml +11 -6
  5. data/Changelog.md +112 -20
  6. data/Gemfile +8 -7
  7. data/README.rdoc +3 -0
  8. data/Rakefile +7 -2
  9. data/appveyor.yml +19 -0
  10. data/examples/asking_for_arrays.rb +3 -0
  11. data/examples/basic_usage.rb +3 -0
  12. data/examples/get_character.rb +3 -0
  13. data/examples/limit.rb +3 -0
  14. data/examples/menus.rb +3 -0
  15. data/examples/overwrite.rb +3 -0
  16. data/examples/password.rb +3 -0
  17. data/examples/repeat_entry.rb +4 -1
  18. data/lib/highline.rb +182 -704
  19. data/lib/highline/builtin_styles.rb +109 -0
  20. data/lib/highline/color_scheme.rb +4 -1
  21. data/lib/highline/compatibility.rb +2 -0
  22. data/lib/highline/custom_errors.rb +19 -0
  23. data/lib/highline/import.rb +4 -2
  24. data/lib/highline/list.rb +93 -0
  25. data/lib/highline/list_renderer.rb +232 -0
  26. data/lib/highline/menu.rb +20 -20
  27. data/lib/highline/paginator.rb +43 -0
  28. data/lib/highline/question.rb +157 -97
  29. data/lib/highline/question/answer_converter.rb +84 -0
  30. data/lib/highline/question_asker.rb +147 -0
  31. data/lib/highline/simulate.rb +5 -1
  32. data/lib/highline/statement.rb +58 -0
  33. data/lib/highline/string.rb +34 -0
  34. data/lib/highline/string_extensions.rb +3 -28
  35. data/lib/highline/style.rb +18 -8
  36. data/lib/highline/template_renderer.rb +38 -0
  37. data/lib/highline/terminal.rb +78 -0
  38. data/lib/highline/terminal/io_console.rb +98 -0
  39. data/lib/highline/terminal/ncurses.rb +38 -0
  40. data/lib/highline/terminal/unix_stty.rb +94 -0
  41. data/lib/highline/version.rb +3 -1
  42. data/lib/highline/wrapper.rb +43 -0
  43. data/test/acceptance/acceptance.rb +62 -0
  44. data/test/acceptance/acceptance_test.rb +69 -0
  45. data/test/acceptance/at_color_output_using_erb_templates.rb +17 -0
  46. data/test/acceptance/at_echo_false.rb +23 -0
  47. data/test/acceptance/at_readline.rb +37 -0
  48. data/test/io_console_compatible.rb +37 -0
  49. data/test/string_methods.rb +3 -0
  50. data/test/test_answer_converter.rb +26 -0
  51. data/test/{tc_color_scheme.rb → test_color_scheme.rb} +7 -9
  52. data/test/test_helper.rb +26 -0
  53. data/test/{tc_highline.rb → test_highline.rb} +193 -136
  54. data/test/{tc_import.rb → test_import.rb} +5 -2
  55. data/test/test_list.rb +60 -0
  56. data/test/{tc_menu.rb → test_menu.rb} +6 -3
  57. data/test/test_paginator.rb +73 -0
  58. data/test/test_question_asker.rb +20 -0
  59. data/test/test_simulator.rb +24 -0
  60. data/test/test_string_extension.rb +72 -0
  61. data/test/{tc_string_highline.rb → test_string_highline.rb} +7 -3
  62. data/test/{tc_style.rb → test_style.rb} +70 -35
  63. data/test/test_wrapper.rb +188 -0
  64. metadata +57 -22
  65. data/lib/highline/system_extensions.rb +0 -254
  66. data/test/tc_simulator.rb +0 -33
  67. data/test/tc_string_extension.rb +0 -33
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "test_helper"
5
+
6
+ require "highline/wrapper"
7
+
8
+ class TestHighLineWrapper < Minitest::Test
9
+ def setup
10
+ @wrap_at = 80
11
+ end
12
+
13
+ def wrap(text)
14
+ HighLine::Wrapper.wrap text, @wrap_at
15
+ end
16
+
17
+ def test_dont_wrap_if_line_is_shorter_than_wrap_at
18
+ wrapped = wrap("This is a very short line.\n")
19
+ assert_equal "This is a very short line.\n", wrapped
20
+ end
21
+
22
+ def test_wrap_long_lines_correctly
23
+ long_line =
24
+ "This is a long flowing paragraph meant to span " +
25
+ "several lines. This text should definitely be " +
26
+ "wrapped at the set limit, in the result. Your code " +
27
+ "does well with things like this.\n\n"
28
+
29
+ wrapped_long_line =
30
+ "This is a long flowing paragraph meant to span " +
31
+ "several lines. This text should\n" +
32
+
33
+ "definitely be wrapped at the set limit, in the " +
34
+ "result. Your code does well with\n" +
35
+
36
+ "things like this.\n\n"
37
+
38
+ wrapped = wrap(long_line)
39
+ assert_equal wrapped_long_line, wrapped
40
+ end
41
+
42
+ def test_dont_wrap_already_well_wrapped_text
43
+ well_formatted_text =
44
+ " * This is a simple embedded list.\n" +
45
+ " * You're code should not mess with this...\n" +
46
+ " * Because it's already formatted correctly and does not\n" +
47
+ " exceed the limit!\n"
48
+
49
+ wrapped = wrap(well_formatted_text)
50
+ assert_equal well_formatted_text, wrapped
51
+ end
52
+
53
+ def test_wrap_single_word_longer_than_wrap_at
54
+ wrapped = wrap("-=" * 50)
55
+ assert_equal(("-=" * 40 + "\n") + ("-=" * 10), wrapped)
56
+ end
57
+
58
+ def test_wrap_plain_text
59
+ line = "123 567 901 345"
60
+
61
+ 1.upto(25) do |wrap_at|
62
+ wrapped = HighLine::Wrapper.wrap(line, wrap_at)
63
+
64
+ case wrap_at
65
+ when 1
66
+ assert_equal "1\n2\n3\n5\n6\n7\n9\n0\n1\n3\n4\n5", wrapped
67
+ when 2
68
+ assert_equal "12\n3\n56\n7\n90\n1\n34\n5", wrapped
69
+ when 3..6
70
+ assert_equal "123\n567\n901\n345", wrapped
71
+ when 7..10
72
+ assert_equal "123 567\n901 345", wrapped
73
+ when 11..14
74
+ assert_equal "123 567 901\n345", wrapped
75
+ when 15..25
76
+ assert_equal "123 567 901 345", wrapped
77
+ end
78
+ end
79
+ end
80
+
81
+ def test_wrap_whole_colored_text
82
+ skip "TODO: Implement whole colored text wrapping!"
83
+ line = "\e[31m123 567 901 345\e[0m"
84
+
85
+ 1.upto(25) do |wrap_at|
86
+ wrapped = HighLine::Wrapper.wrap(line, wrap_at)
87
+
88
+ case wrap_at
89
+ when 1
90
+ assert_equal "\e[31m1\n2\n3\n5\n6\n7\n9\n0\n1\n3\n4\n5\e[0m", wrapped
91
+ when 2
92
+ assert_equal "\e[31m12\n3\n56\n7\n90\n1\n34\n5\e[0m", wrapped
93
+ when 3..6
94
+ assert_equal "\e[31m123\n567\n901\n345\e[0m", wrapped
95
+ when 7..10
96
+ assert_equal "\e[31m123 567\n901 345\e[0m", wrapped
97
+ when 11..14
98
+ assert_equal "\e[31m123 567 901\n345\e[0m", wrapped
99
+ when 15..25
100
+ assert_equal "\e[31m123 567 901 345\e[0m", wrapped
101
+ end
102
+ end
103
+ end
104
+
105
+ def test_wrap_partially_colored_text
106
+ skip "TODO: Implement middle colored text wrapping!"
107
+ line = "123 567 \e[31m901\e[0m 345"
108
+
109
+ 1.upto(25) do |wrap_at|
110
+ wrapped = HighLine::Wrapper.wrap(line, wrap_at)
111
+
112
+ case wrap_at
113
+ when 1
114
+ assert_equal "1\n2\n3\n5\n6\n7\n\e[31m9\n0\n1\e[0m\n3\n4\n5", wrapped
115
+ when 2
116
+ assert_equal "12\n3\n56\n7\n\e[31m90\n1\e[0m\n34\n5", wrapped
117
+ when 3..6
118
+ assert_equal "123\n567\n\e[31m901\e[0m\n345", wrapped
119
+ when 7..10
120
+ assert_equal "123 567\n\e[31m901\e[0m 345", wrapped
121
+ when 11..14
122
+ assert_equal "123 567 \e[31m901\e[0m\n345", wrapped
123
+ when 15..25
124
+ assert_equal "123 567 \e[31m901\e[0m 345", wrapped
125
+ end
126
+ end
127
+ end
128
+
129
+ def test_wrap_text_with_partially_colored_word_in_the_middle
130
+ skip "TODO: Implement middle partially colored text wrapping!"
131
+ line = "123 567 9\e[31m0\e[0m1 345"
132
+
133
+ 1.upto(25) do |wrap_at|
134
+ wrapped = HighLine::Wrapper.wrap(line, wrap_at)
135
+
136
+ case wrap_at
137
+ when 1
138
+ assert_equal "1\n2\n3\n5\n6\n7\n9\n\e[31m0\e[0m\n1\n3\n4\n5", wrapped
139
+ when 2
140
+ assert_equal "12\n3\n56\n7\n9\e[31m0\e[0m\n1\n34\n5", wrapped
141
+ when 3..6
142
+ assert_equal "123\n567\n9\e[31m0\e[0m1\n345", wrapped
143
+ when 7..10
144
+ assert_equal "123 567\n9\e[31m0\e[0m1 345", wrapped
145
+ when 11..14
146
+ assert_equal "123 567 9\e[31m0\e[0m1\n345", wrapped
147
+ when 15..25
148
+ assert_equal "123 567 9\e[31m0\e[0m1 345", wrapped
149
+ end
150
+ end
151
+ end
152
+
153
+ def test_wrap_when_multibyte_characters_present
154
+ line_ascii = "Sera um passaro?"
155
+ line_utf8 = "Será um pássaro?"
156
+
157
+ assert_equal 16, line_ascii.size
158
+ assert_equal 16, line_ascii.bytesize
159
+
160
+ assert_equal 16, line_utf8.size
161
+ assert_equal 18, line_utf8.bytesize
162
+
163
+ 1.upto(18) do |wrap_at|
164
+ wrapped = HighLine::Wrapper.wrap(line_utf8, wrap_at)
165
+
166
+ case wrap_at
167
+ when 1
168
+ assert_equal "S\ne\nr\ná\nu\nm\np\ná\ns\ns\na\nr\no\n?", wrapped
169
+ when 2
170
+ assert_equal "Se\nrá\num\npá\nss\nar\no?", wrapped
171
+ when 3
172
+ assert_equal "Ser\ná\num\npás\nsar\no?", wrapped
173
+ when 4
174
+ assert_equal "Será\num\npáss\naro?", wrapped
175
+ when 5
176
+ assert_equal "Será\num\npássa\nro?", wrapped
177
+ when 6
178
+ assert_equal "Será\num\npássar\no?", wrapped
179
+ when 7
180
+ assert_equal "Será um\npássaro\n?", wrapped
181
+ when 15..8
182
+ assert_equal "Será um\npássaro?", wrapped
183
+ when 16..18
184
+ assert_equal "Será um pássaro?", wrapped
185
+ end
186
+ end
187
+ end
188
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: highline
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.10
4
+ version: 2.0.0.pre.develop.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Edward Gray II
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-24 00:00:00.000000000 Z
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: code_statistics
@@ -40,6 +40,7 @@ extra_rdoc_files:
40
40
  - LICENSE
41
41
  files:
42
42
  - ".gitignore"
43
+ - ".simplecov"
43
44
  - ".travis.yml"
44
45
  - AUTHORS
45
46
  - COPYING
@@ -50,6 +51,7 @@ files:
50
51
  - README.rdoc
51
52
  - Rakefile
52
53
  - TODO
54
+ - appveyor.yml
53
55
  - doc/.cvsignore
54
56
  - examples/ansi_colors.rb
55
57
  - examples/asking_for_arrays.rb
@@ -66,30 +68,56 @@ files:
66
68
  - examples/using_readline.rb
67
69
  - highline.gemspec
68
70
  - lib/highline.rb
71
+ - lib/highline/builtin_styles.rb
69
72
  - lib/highline/color_scheme.rb
70
73
  - lib/highline/compatibility.rb
74
+ - lib/highline/custom_errors.rb
71
75
  - lib/highline/import.rb
76
+ - lib/highline/list.rb
77
+ - lib/highline/list_renderer.rb
72
78
  - lib/highline/menu.rb
79
+ - lib/highline/paginator.rb
73
80
  - lib/highline/question.rb
81
+ - lib/highline/question/answer_converter.rb
82
+ - lib/highline/question_asker.rb
74
83
  - lib/highline/simulate.rb
84
+ - lib/highline/statement.rb
85
+ - lib/highline/string.rb
75
86
  - lib/highline/string_extensions.rb
76
87
  - lib/highline/style.rb
77
- - lib/highline/system_extensions.rb
88
+ - lib/highline/template_renderer.rb
89
+ - lib/highline/terminal.rb
90
+ - lib/highline/terminal/io_console.rb
91
+ - lib/highline/terminal/ncurses.rb
92
+ - lib/highline/terminal/unix_stty.rb
78
93
  - lib/highline/version.rb
94
+ - lib/highline/wrapper.rb
79
95
  - setup.rb
80
96
  - site/.cvsignore
81
97
  - site/highline.css
82
98
  - site/images/logo.png
83
99
  - site/index.html
100
+ - test/acceptance/acceptance.rb
101
+ - test/acceptance/acceptance_test.rb
102
+ - test/acceptance/at_color_output_using_erb_templates.rb
103
+ - test/acceptance/at_echo_false.rb
104
+ - test/acceptance/at_readline.rb
105
+ - test/io_console_compatible.rb
84
106
  - test/string_methods.rb
85
- - test/tc_color_scheme.rb
86
- - test/tc_highline.rb
87
- - test/tc_import.rb
88
- - test/tc_menu.rb
89
- - test/tc_simulator.rb
90
- - test/tc_string_extension.rb
91
- - test/tc_string_highline.rb
92
- - test/tc_style.rb
107
+ - test/test_answer_converter.rb
108
+ - test/test_color_scheme.rb
109
+ - test/test_helper.rb
110
+ - test/test_highline.rb
111
+ - test/test_import.rb
112
+ - test/test_list.rb
113
+ - test/test_menu.rb
114
+ - test/test_paginator.rb
115
+ - test/test_question_asker.rb
116
+ - test/test_simulator.rb
117
+ - test/test_string_extension.rb
118
+ - test/test_string_highline.rb
119
+ - test/test_style.rb
120
+ - test/test_wrapper.rb
93
121
  homepage: https://github.com/JEG2/highline
94
122
  licenses:
95
123
  - Ruby
@@ -109,22 +137,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
137
  version: 1.9.3
110
138
  required_rubygems_version: !ruby/object:Gem::Requirement
111
139
  requirements:
112
- - - ">="
140
+ - - ">"
113
141
  - !ruby/object:Gem::Version
114
- version: '0'
142
+ version: 1.3.1
115
143
  requirements: []
116
144
  rubyforge_project: highline
117
- rubygems_version: 2.6.14
145
+ rubygems_version: 2.4.5.1
118
146
  signing_key:
119
147
  specification_version: 4
120
148
  summary: HighLine is a high-level command-line IO library.
121
149
  test_files:
150
+ - test/io_console_compatible.rb
122
151
  - test/string_methods.rb
123
- - test/tc_color_scheme.rb
124
- - test/tc_highline.rb
125
- - test/tc_import.rb
126
- - test/tc_menu.rb
127
- - test/tc_simulator.rb
128
- - test/tc_string_extension.rb
129
- - test/tc_string_highline.rb
130
- - test/tc_style.rb
152
+ - test/test_answer_converter.rb
153
+ - test/test_color_scheme.rb
154
+ - test/test_helper.rb
155
+ - test/test_highline.rb
156
+ - test/test_import.rb
157
+ - test/test_list.rb
158
+ - test/test_menu.rb
159
+ - test/test_paginator.rb
160
+ - test/test_question_asker.rb
161
+ - test/test_simulator.rb
162
+ - test/test_string_extension.rb
163
+ - test/test_string_highline.rb
164
+ - test/test_style.rb
165
+ - test/test_wrapper.rb
@@ -1,254 +0,0 @@
1
- # system_extensions.rb
2
- #
3
- # Created by James Edward Gray II on 2006-06-14.
4
- # Copyright 2006 Gray Productions. All rights reserved.
5
- #
6
- # This is Free Software. See LICENSE and COPYING for details.
7
-
8
- require "highline/compatibility"
9
-
10
- class HighLine
11
- module SystemExtensions
12
- JRUBY = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
13
-
14
- if JRUBY
15
- JRUBY_OVER_17 = JRUBY_VERSION =~ /^1.7/ || JRUBY_VERSION =~ /^9/
16
-
17
- def initialize_system_extensions
18
- require 'java'
19
- require 'readline'
20
- if JRUBY_OVER_17
21
- java_import 'jline.console.ConsoleReader'
22
-
23
- input = @input && @input.to_inputstream
24
- output = @output && @output.to_outputstream
25
-
26
- @java_console = ConsoleReader.new(input, output)
27
- @java_console.set_history_enabled(false)
28
- @java_console.set_bell_enabled(true)
29
- @java_console.set_pagination_enabled(false)
30
- @java_terminal = @java_console.getTerminal
31
- elsif JRUBY_VERSION =~ /^1.6/
32
- java_import 'java.io.OutputStreamWriter'
33
- java_import 'java.nio.channels.Channels'
34
- java_import 'jline.ConsoleReader'
35
- java_import 'jline.Terminal'
36
-
37
- @java_input = Channels.newInputStream(@input.to_channel)
38
- @java_output = OutputStreamWriter.new(Channels.newOutputStream(@output.to_channel))
39
- @java_terminal = Terminal.getTerminal
40
- @java_console = ConsoleReader.new(@java_input, @java_output)
41
- @java_console.setUseHistory(false)
42
- @java_console.setBellEnabled(true)
43
- @java_console.setUsePagination(false)
44
- end
45
- end
46
- end
47
-
48
- extend self
49
-
50
- #
51
- # This section builds character reading and terminal size functions
52
- # to suit the proper platform we're running on. Be warned: Here be
53
- # dragons!
54
- #
55
- if RUBY_PLATFORM =~ /mswin(?!ce)|mingw|bccwin/i
56
- begin
57
- require "fiddle"
58
-
59
- module WinAPI
60
- include Fiddle
61
- Handle = RUBY_VERSION >= "2.0.0" ? Fiddle::Handle : DL::Handle
62
- Kernel32 = Handle.new("kernel32")
63
- Crt = Handle.new("msvcrt") rescue Handle.new("crtdll")
64
-
65
- def self._getch
66
- @@_m_getch ||= Function.new(Crt["_getch"], [], TYPE_INT)
67
- @@_m_getch.call
68
- end
69
-
70
- def self.GetStdHandle(handle_type)
71
- @@get_std_handle ||= Function.new(Kernel32["GetStdHandle"], [-TYPE_INT], -TYPE_INT)
72
- @@get_std_handle.call(handle_type)
73
- end
74
-
75
- def self.GetConsoleScreenBufferInfo(cons_handle, lp_buffer)
76
- @@get_console_screen_buffer_info ||=
77
- Function.new(Kernel32["GetConsoleScreenBufferInfo"], [TYPE_LONG, TYPE_VOIDP], TYPE_INT)
78
- @@get_console_screen_buffer_info.call(cons_handle, lp_buffer)
79
- end
80
- end
81
- rescue LoadError
82
- require "dl/import"
83
-
84
- module WinAPI
85
- if defined?(DL::Importer)
86
- # Ruby 1.9
87
- extend DL::Importer
88
- else
89
- # Ruby 1.8
90
- extend DL::Importable
91
- end
92
- begin
93
- dlload "msvcrt", "kernel32"
94
- rescue DL::DLError
95
- dlload "crtdll", "kernel32"
96
- end
97
- extern "unsigned long _getch()"
98
- extern "unsigned long GetConsoleScreenBufferInfo(unsigned long, void*)"
99
- extern "unsigned long GetStdHandle(unsigned long)"
100
-
101
- # Ruby 1.8 DL::Importable.import does mname[0,1].downcase so FooBar becomes fooBar
102
- if defined?(getConsoleScreenBufferInfo)
103
- alias_method :GetConsoleScreenBufferInfo, :getConsoleScreenBufferInfo
104
- module_function :GetConsoleScreenBufferInfo
105
- end
106
- if defined?(getStdHandle)
107
- alias_method :GetStdHandle, :getStdHandle
108
- module_function :GetStdHandle
109
- end
110
- end
111
- end
112
-
113
- CHARACTER_MODE = "Win32API" # For Debugging purposes only.
114
-
115
- #
116
- # Windows savvy getc().
117
- #
118
- # *WARNING*: This method ignores <tt>input</tt> and reads one
119
- # character from +STDIN+!
120
- #
121
- def get_character( input = STDIN )
122
- WinAPI._getch
123
- end
124
-
125
- # We do not define a raw_no_echo_mode for Windows as _getch turns off echo
126
- def raw_no_echo_mode
127
- end
128
-
129
- def restore_mode
130
- end
131
-
132
- # A Windows savvy method to fetch the console columns, and rows.
133
- def terminal_size
134
- format = 'SSSSSssssSS'
135
- buf = ([0] * format.size).pack(format)
136
- stdout_handle = WinAPI.GetStdHandle(0xFFFFFFF5)
137
-
138
- WinAPI.GetConsoleScreenBufferInfo(stdout_handle, buf)
139
- _, _, _, _, _,
140
- left, top, right, bottom, _, _ = buf.unpack(format)
141
- return right - left + 1, bottom - top + 1
142
- end
143
- else # If we're not on Windows try...
144
- begin
145
- require "termios" # Unix, first choice termios.
146
-
147
- CHARACTER_MODE = "termios" # For Debugging purposes only.
148
-
149
- def raw_no_echo_mode
150
- @state = Termios.getattr(@input)
151
- new_settings = @state.dup
152
- new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
153
- new_settings.c_cc[Termios::VMIN] = 1
154
- Termios.setattr(@input, Termios::TCSANOW, new_settings)
155
- end
156
-
157
- def restore_mode
158
- Termios.setattr(@input, Termios::TCSANOW, @state)
159
- end
160
- rescue LoadError # If our first choice fails, try using JLine
161
- if JRUBY # if we are on JRuby. JLine is bundled with JRuby.
162
- CHARACTER_MODE = "jline" # For Debugging purposes only.
163
-
164
- def terminal_size
165
- if JRUBY_OVER_17
166
- [ @java_terminal.get_width, @java_terminal.get_height ]
167
- else
168
- [ @java_terminal.getTerminalWidth, @java_terminal.getTerminalHeight ]
169
- end
170
- end
171
-
172
- def raw_no_echo_mode
173
- @state = @java_console.getEchoCharacter
174
- @java_console.setEchoCharacter 0
175
- end
176
-
177
- def restore_mode
178
- @java_console.setEchoCharacter @state
179
- end
180
- else # If we are not on JRuby, try ncurses
181
- begin
182
- require 'ffi-ncurses'
183
- CHARACTER_MODE = "ncurses" # For Debugging purposes only.
184
-
185
- def raw_no_echo_mode
186
- FFI::NCurses.initscr
187
- FFI::NCurses.cbreak
188
- end
189
-
190
- def restore_mode
191
- FFI::NCurses.endwin
192
- end
193
-
194
- #
195
- # A ncurses savvy method to fetch the console columns, and rows.
196
- #
197
- def terminal_size
198
- size = [80, 40]
199
- FFI::NCurses.initscr
200
- begin
201
- size = FFI::NCurses.getmaxyx(FFI::NCurses.stdscr).reverse
202
- ensure
203
- FFI::NCurses.endwin
204
- end
205
- size
206
- end
207
- rescue LoadError # Finally, if all else fails, use stty
208
- # *WARNING*: This requires the external "stty" program!
209
- CHARACTER_MODE = "stty" # For Debugging purposes only.
210
-
211
- def raw_no_echo_mode
212
- @state = `stty -g`
213
- system "stty raw -echo -icanon isig"
214
- end
215
-
216
- def restore_mode
217
- system "stty #{@state}"
218
- print "\r"
219
- end
220
- end
221
- end
222
- end
223
-
224
- # For termios and stty
225
- if not method_defined?(:terminal_size)
226
- # A Unix savvy method using stty to fetch the console columns, and rows.
227
- # ... stty does not work in JRuby
228
- def terminal_size
229
- begin
230
- require "io/console"
231
- winsize = IO.console.winsize.reverse rescue nil
232
- return winsize if winsize
233
- rescue LoadError
234
- end
235
-
236
- if /solaris/ =~ RUBY_PLATFORM and
237
- `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
238
- [$2, $1].map { |x| x.to_i }
239
- elsif `stty size` =~ /^(\d+)\s(\d+)$/
240
- [$2.to_i, $1.to_i]
241
- else
242
- [ 80, 24 ]
243
- end
244
- end
245
- end
246
- end
247
-
248
- if not method_defined?(:get_character)
249
- def get_character( input = STDIN )
250
- input.getbyte
251
- end
252
- end
253
- end
254
- end