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

input[R]

@return [IO] input stream

output[R]

@return [IO] output stream

Public Class Methods

get_terminal(input, output) click to toggle source

Probe for and return a suitable Terminal instance @param input [IO] input stream @param output [IO] output stream

# File lib/highline/terminal.rb, line 21
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
new(input, output) click to toggle source

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 45
def initialize(input, output)
  @input  = input
  @output = output
end

Public Instance Methods

character_mode() click to toggle source

Returns the class name as String. Useful for debuggin. @return [String] class name. Ex: “HighLine::Terminal::IOConsole”

# File lib/highline/terminal.rb, line 161
def character_mode
  self.class.name
end
get_character() click to toggle source

Get one character from the terminal @return [String] one character

# File lib/highline/terminal.rb, line 77
def get_character; end
get_line(question, highline) click to toggle source

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 83
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) click to toggle source

Get one line from terminal using default gets method. @param highline (see get_line)

# File lib/highline/terminal.rb, line 134
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) click to toggle source

Get one line using readline_read @param (see get_line)

# File lib/highline/terminal.rb, line 96
def get_line_with_readline(question, highline)
  require "readline" # load only if needed

  raw_answer = readline_read(question)

  if !raw_answer && highline.track_eof?
    raise EOFError, "The input stream is exhausted."
  end

  raw_answer || ""
end
initialize_system_extensions() click to toggle source

An initialization callback. It is called by {.get_terminal}.

# File lib/highline/terminal.rb, line 52
def initialize_system_extensions; end
jruby?() click to toggle source

Running on JRuby?

# File lib/highline/terminal.rb, line 143
def jruby?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
end
raw_no_echo_mode() click to toggle source

Enter Raw No Echo mode.

# File lib/highline/terminal.rb, line 61
def raw_no_echo_mode; end
raw_no_echo_mode_exec() { || ... } click to toggle source

Yieds a block to be executed in Raw No Echo mode and then restore the terminal state.

# File lib/highline/terminal.rb, line 65
def raw_no_echo_mode_exec
  raw_no_echo_mode
  yield
ensure
  restore_mode
end
readline_read(question) click to toggle source

Use readline to read one line @param question [HighLine::Question] question from where to get

autocomplete candidate strings
# File lib/highline/terminal.rb, line 111
def readline_read(question)
  # prep auto-completion
  unless question.selection.empty?
    Readline.completion_proc = lambda do |str|
      question.selection.grep(/\A#{Regexp.escape(str)}/)
    end
  end

  # work-around ugly readline() warnings
  old_verbose = $VERBOSE
  $VERBOSE    = nil

  raw_answer  = run_preserving_stty do
    Readline.readline("", true)
  end

  $VERBOSE = old_verbose

  raw_answer
end
restore_mode() click to toggle source

Restore terminal to its default mode

# File lib/highline/terminal.rb, line 73
def restore_mode; end
rubinius?() click to toggle source

Running on Rubinius?

# File lib/highline/terminal.rb, line 148
def rubinius?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
end
terminal_size() click to toggle source

@return [Array<Integer, Integer>] two value terminal

size like [columns, lines]
# File lib/highline/terminal.rb, line 56
def terminal_size
  [80, 24]
end
windows?() click to toggle source

Running on Windows?

# File lib/highline/terminal.rb, line 153
def windows?
  defined?(RUBY_PLATFORM) && (RUBY_PLATFORM =~ /mswin|mingw|cygwin/)
end

Private Instance Methods

restore_stty() click to toggle source

Restores terminal state using shell stty command.

# File lib/highline/terminal.rb, line 185
def restore_stty
  system("stty", @stty_save) if @stty_save
end
run_preserving_stty() { || ... } click to toggle source

Yield a block using stty shell commands to preserve the terminal state.

# File lib/highline/terminal.rb, line 168
def run_preserving_stty
  save_stty
  yield
ensure
  restore_stty
end
save_stty() click to toggle source

Saves terminal state using shell stty command.

# File lib/highline/terminal.rb, line 176
def save_stty
  @stty_save = begin
                 %xstty -g`.chomp
               rescue StandardError
                 nil
               end
end