Class: HighLine::Terminal
- Defined in:
- lib/highline/terminal.rb,
lib/highline/terminal/ncurses.rb,
lib/highline/terminal/unix_stty.rb,
lib/highline/terminal/io_console.rb
Overview
Basic Terminal class which HighLine will direct input and output to. The specialized Terminals all decend from this HighLine::Terminal class
Defined Under Namespace
Classes: IOConsole, NCurses, UnixStty
Instance Attribute Summary collapse
-
#input ⇒ IO
readonly
Input stream.
-
#output ⇒ IO
readonly
Output stream.
Enviroment queries collapse
-
#jruby? ⇒ Boolean
Running on JRuby?.
-
#rubinius? ⇒ Boolean
Running on Rubinius?.
-
#windows? ⇒ Boolean
Running on Windows?.
Class Method Summary collapse
-
.get_terminal(input, output) ⇒ Object
Probe for and return a suitable Terminal instance.
Instance Method Summary collapse
-
#character_mode ⇒ String
Returns the class name as String.
-
#get_character ⇒ String
Get one character from the terminal.
-
#get_line(question, highline) ⇒ Object
Get one line from the terminal and format accordling.
-
#get_line_default(highline) ⇒ Object
Get one line from terminal using default #gets method.
-
#get_line_with_readline(question, highline) ⇒ Object
Get one line using #readline_read.
-
#initialize(input, output) ⇒ Terminal
constructor
Creates a terminal instance based on given input and output streams.
-
#initialize_system_extensions ⇒ Object
An initialization callback.
-
#raw_no_echo_mode ⇒ Object
Enter Raw No Echo mode.
-
#raw_no_echo_mode_exec ⇒ Object
Yieds a block to be executed in Raw No Echo mode and then restore the terminal state.
-
#readline_read(question, highline) ⇒ Object
Use readline to read one line.
-
#restore_mode ⇒ Object
Restore terminal to its default mode.
-
#terminal_size ⇒ Array<Integer, Integer>
Two value terminal size like [columns, lines].
Constructor Details
#initialize(input, output) ⇒ Terminal
Creates a terminal instance based on given input and output streams.
46 47 48 49 |
# File 'lib/highline/terminal.rb', line 46 def initialize(input, output) @input = input @output = output end |
Instance Attribute Details
#input ⇒ IO (readonly)
Returns input stream.
38 39 40 |
# File 'lib/highline/terminal.rb', line 38 def input @input end |
#output ⇒ IO (readonly)
Returns output stream.
41 42 43 |
# File 'lib/highline/terminal.rb', line 41 def output @output end |
Class Method Details
.get_terminal(input, output) ⇒ Object
Probe for and return a suitable Terminal instance
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/highline/terminal.rb', line 22 def self.get_terminal(input, output) # First of all, probe for io/console begin require "io/console" require "highline/terminal/io_console" terminal = HighLine::Terminal::IOConsole.new(input, output) rescue LoadError require "highline/terminal/unix_stty" terminal = HighLine::Terminal::UnixStty.new(input, output) end terminal.initialize_system_extensions terminal end |
Instance Method Details
#character_mode ⇒ String
Returns the class name as String. Useful for debuggin.
164 165 166 |
# File 'lib/highline/terminal.rb', line 164 def character_mode self.class.name end |
#get_character ⇒ String
Get one character from the terminal
78 |
# File 'lib/highline/terminal.rb', line 78 def get_character; end |
#get_line(question, highline) ⇒ Object
Get one line from the terminal and format accordling. Use readline if question has readline mode set.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/highline/terminal.rb', line 84 def get_line(question, highline) raw_answer = if question.readline get_line_with_readline(question, highline) else get_line_default(highline) end question.format_answer(raw_answer) end |
#get_line_default(highline) ⇒ Object
Get one line from terminal using default #gets method.
137 138 139 140 141 |
# File 'lib/highline/terminal.rb', line 137 def get_line_default(highline) raise EOFError, "The input stream is exhausted." if highline.track_eof? && highline.input.eof? highline.input.gets end |
#get_line_with_readline(question, highline) ⇒ Object
Get one line using #readline_read
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/highline/terminal.rb', line 97 def get_line_with_readline(question, highline) require "reline" # load only if needed raw_answer = readline_read(question, highline) if !raw_answer && highline.track_eof? raise EOFError, "The input stream is exhausted." end raw_answer || "" end |
#initialize_system_extensions ⇒ Object
An initialization callback. It is called by get_terminal.
53 |
# File 'lib/highline/terminal.rb', line 53 def initialize_system_extensions; end |
#jruby? ⇒ Boolean
Running on JRuby?
146 147 148 |
# File 'lib/highline/terminal.rb', line 146 def jruby? defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" end |
#raw_no_echo_mode ⇒ Object
Enter Raw No Echo mode.
62 |
# File 'lib/highline/terminal.rb', line 62 def raw_no_echo_mode; end |
#raw_no_echo_mode_exec ⇒ Object
Yieds a block to be executed in Raw No Echo mode and then restore the terminal state.
66 67 68 69 70 71 |
# File 'lib/highline/terminal.rb', line 66 def raw_no_echo_mode_exec raw_no_echo_mode yield ensure restore_mode end |
#readline_read(question, highline) ⇒ Object
Use readline to read one line
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/highline/terminal.rb', line 112 def readline_read(question, highline) # prep auto-completion unless question.selection.empty? Reline.completion_proc = lambda do |str| question.selection.grep(/\A#{Regexp.escape(str)}/) end end # TODO: Check if this is still needed after Reline # work-around ugly readline() warnings old_verbose = $VERBOSE $VERBOSE = nil raw_answer = run_preserving_stty do prompt = highline.render_and_ident_statement(question) Reline.readline(prompt, true) end $VERBOSE = old_verbose raw_answer end |
#restore_mode ⇒ Object
Restore terminal to its default mode
74 |
# File 'lib/highline/terminal.rb', line 74 def restore_mode; end |
#rubinius? ⇒ Boolean
Running on Rubinius?
151 152 153 |
# File 'lib/highline/terminal.rb', line 151 def rubinius? defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx" end |
#terminal_size ⇒ Array<Integer, Integer>
Returns two value terminal size like [columns, lines].
57 58 59 |
# File 'lib/highline/terminal.rb', line 57 def terminal_size [80, 24] end |
#windows? ⇒ Boolean
Running on Windows?
156 157 158 |
# File 'lib/highline/terminal.rb', line 156 def windows? defined?(RUBY_PLATFORM) && (RUBY_PLATFORM =~ /mswin|mingw|cygwin/) end |