class HighLine::Terminal
Basic Terminal
class which HighLine
will direct input and output to. The specialized Terminals all decend from this HighLine::Terminal
class
Attributes
@return [IO] input stream
@return [IO] output stream
Public Class Methods
Probe for and return a suitable Terminal
instance @param input [IO] input stream @param output [IO] output stream
# 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
Creates a terminal instance based on given input and output streams. @param input [IO] input stream @param output [IO] output stream
# File lib/highline/terminal.rb, line 46 def initialize(input, output) @input = input @output = output end
Public Instance Methods
Returns the class name as String
. Useful for debuggin. @return [String] class name. Ex: “HighLine::Terminal::IOConsole”
# File lib/highline/terminal.rb, line 164 def character_mode self.class.name end
Get one character from the terminal @return [String] one character
# File lib/highline/terminal.rb, line 78 def get_character; end
Get one line from the terminal and format accordling. Use readline if question has readline mode set. @param question [HighLine::Question] @param highline [HighLine]
# 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 one line from terminal using default gets method. @param highline (see get_line
)
# 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 one line using readline_read
@param (see get_line
)
# 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
An initialization callback. It is called by {.get_terminal}.
# File lib/highline/terminal.rb, line 53 def initialize_system_extensions; end
Running on JRuby?
# File lib/highline/terminal.rb, line 146 def jruby? defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" end
Enter Raw No Echo mode.
# File lib/highline/terminal.rb, line 62 def raw_no_echo_mode; end
Yieds a block to be executed in Raw No Echo mode and then restore the terminal state.
# File lib/highline/terminal.rb, line 66 def raw_no_echo_mode_exec raw_no_echo_mode yield ensure restore_mode end
Use readline to read one line @param question [HighLine::Question] question from where to get
autocomplete candidate strings
# 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 terminal to its default mode
# File lib/highline/terminal.rb, line 74 def restore_mode; end
Running on Rubinius?
# File lib/highline/terminal.rb, line 151 def rubinius? defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx" end
@return [Array<Integer, Integer>] two value terminal
size like [columns, lines]
# File lib/highline/terminal.rb, line 57 def terminal_size [80, 24] end
Running on Windows?
# File lib/highline/terminal.rb, line 156 def windows? defined?(RUBY_PLATFORM) && (RUBY_PLATFORM =~ /mswin|mingw|cygwin/) end
Private Instance Methods
Restores terminal state using shell stty command.
# File lib/highline/terminal.rb, line 188 def restore_stty system("stty", @stty_save) if @stty_save end
Yield a block using stty shell commands to preserve the terminal state.
# File lib/highline/terminal.rb, line 171 def run_preserving_stty save_stty yield ensure restore_stty end
Saves terminal state using shell stty command.
# File lib/highline/terminal.rb, line 179 def save_stty @stty_save = begin `stty -g`.chomp if input.tty? rescue StandardError nil end end