Class: HighLine::Terminal

Inherits:
Object show all
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

Direct Known Subclasses

IOConsole, NCurses, UnixStty

Defined Under Namespace

Classes: IOConsole, NCurses, UnixStty

Instance Attribute Summary collapse

Enviroment queries collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ Terminal

Creates a terminal instance based on given input and output streams.

Parameters:

  • input (IO)

    input stream

  • output (IO)

    output stream


46
47
48
49
# File 'lib/highline/terminal.rb', line 46

def initialize(input, output)
  @input  = input
  @output = output
end

Instance Attribute Details

#inputIO (readonly)

Returns input stream.

Returns:

  • (IO)

    input stream


38
39
40
# File 'lib/highline/terminal.rb', line 38

def input
  @input
end

#outputIO (readonly)

Returns output stream.

Returns:

  • (IO)

    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

Parameters:

  • input (IO)

    input stream

  • output (IO)

    output stream


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_modeString

Returns the class name as String. Useful for debuggin.

Returns:

  • (String)

    class name. Ex: “HighLine::Terminal::IOConsole”


164
165
166
# File 'lib/highline/terminal.rb', line 164

def character_mode
  self.class.name
end

#get_characterString

Get one character from the terminal

Returns:


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.

Parameters:


85
86
87
88
89
90
91
92
93
94
# File 'lib/highline/terminal.rb', line 85

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.

Parameters:

Raises:

  • (EOFError)

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

Parameters:


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/highline/terminal.rb', line 98

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_extensionsObject

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?

Returns:

  • (Boolean)

146
147
148
# File 'lib/highline/terminal.rb', line 146

def jruby?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
end

#raw_no_echo_modeObject

Enter Raw No Echo mode.


62
# File 'lib/highline/terminal.rb', line 62

def raw_no_echo_mode; end

#raw_no_echo_mode_execObject

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) ⇒ Object

Use readline to read one line

Parameters:

  • prompt (String)

    Readline prompt

  • question (HighLine::Question)

    question from where to get autocomplete candidate strings


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 114

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_modeObject

Restore terminal to its default mode


74
# File 'lib/highline/terminal.rb', line 74

def restore_mode; end

#rubinius?Boolean

Running on Rubinius?

Returns:

  • (Boolean)

151
152
153
# File 'lib/highline/terminal.rb', line 151

def rubinius?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
end

#terminal_sizeArray<Integer, Integer>

Returns two value terminal size like [columns, lines].

Returns:

  • (Array<Integer, Integer>)

    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?

Returns:

  • (Boolean)

156
157
158
# File 'lib/highline/terminal.rb', line 156

def windows?
  defined?(RUBY_PLATFORM) && (RUBY_PLATFORM =~ /mswin|mingw|cygwin/)
end