module MiniTerm

A simple, portable terminal interface object. (Common Code)

MiniTerm needs to put text on the screen! (ANSI Specific Code)

Get input from the user in raw mode. (ANSI Specific Code)

Give MiniTerm control of the cursor position. (ANSI Specific Code)

Get size info about the console window. (ANSI Specific Code)

Common control character definitions. (Common Code)

Get input from the user in mapped mode. (Common Code)

A input text mapper. (Common Code)

Get input from the user in raw mode. (Common Code)

Common Terminal Info. (Common Code)

A simple, portable terminal interface object.

Link Ruby to the Windows API calls needed by MiniTerm

MiniTerm needs to put text on the screen! (Windows Specific Code)

Get input from the user in raw mode. (Windows Specific Code)

Give MiniTerm control of the cursor position. (Windows Specific Code)

Get size info about the console window. (Windows Specific Code)

Replacement support for deprecated win_32_api gem, with thanks to the ConnorAtherton/rb-readline project from where this code originates.

Constants

BELL
CARRIAGE_RETURN
DESCRIPTION
ENABLE_LINE_INPUT
ENABLE_PROCESSED_INPUT
ESCAPE
FLUSH_COUNT
FLUSH_SLEEP

Control values for flush.

LINE_FEED
PREFIX_00

These are mostly used in Windows maps.

PREFIX_E0
STDIN_ID

The magic numbers for the handles.

STDOUT_ID
TERM_JAVA

Is Java present in the environment?

TERM_MODE_MASK
TERM_PLATFORM

What operating platform is in effect?

TERM_TYPE
VALID_OPTIONS

What options are supported in this ANSI mode?

VERSION
WAIT_SLEEP

The sleep interval waiting for a key to be pressed.

Public Class Methods

add_map(type, &defn_block) click to toggle source

Add a terminal mapping.

# File lib/mini_term/common/mapped_input.rb, line 17
def self.add_map(type, &defn_block)
  @maps[type] = Mapper.new(&defn_block)
end
ansi?() click to toggle source

Is this ANSI?

# File lib/mini_term.rb, line 44
def self.ansi?
  TERM_TYPE == :ansi
end
beep() click to toggle source

Sound a beep

# File lib/mini_term/ansi/output.rb, line 13
def self.beep
  STDERR.write(BELL)
  STDERR.flush
  self
end
clear_screen() click to toggle source

Clear the screen and home the cursor

# File lib/mini_term/ansi/output.rb, line 20
def self.clear_screen
  STDOUT.print("\e[f\e[2J")
  self
end
close() click to toggle source
# File lib/mini_term/common/term_info.rb, line 15
def self.close
  end_raw_input if raw?
  @term_open = false
  @options   = nil
end
flush() click to toggle source

Flush the keyboard buffer.

# File lib/mini_term/common/raw_input.rb, line 27
def self.flush
  result = ""

  raw do |input|
    FLUSH_COUNT.times do
      sleep(FLUSH_SLEEP)
      break unless input.has_raw_char?
      result << input.get_raw_char
    end
  end

  result
end
get_mapped_char(&block) click to toggle source

Get a mapped character. Note: The block is for testing only.

# File lib/mini_term/common/mapped_input.rb, line 22
def self.get_mapped_char(&block)
  mapper = @maps[MiniTerm::TERM_TYPE]
  proc = block_given? ? block : Proc.new { get_raw_char }
  raw { mapper.get_mapped_char(&proc) }
end
get_raw_char() click to toggle source

Get a uncooked character keystroke.

# File lib/mini_term/ansi/raw_input.rb, line 12
def self.get_raw_char
  fail MiniTermNotRaw, "Not in raw mode." unless raw?
  STDIN.getch
end
has_raw_char?() click to toggle source

Is there a character waiting?

# File lib/mini_term/ansi/raw_input.rb, line 7
def self.has_raw_char?
  raw { STDIN.ready? }
end
height() click to toggle source

What is the terminal height in rows?

# File lib/mini_term/common/term_info.rb, line 40
def self.height
  term_info[0]
end
java?() click to toggle source

Are we running under Java?

# File lib/mini_term.rb, line 49
def self.java?
  TERM_JAVA
end
map_types() click to toggle source

What terminal types are mapped?

# File lib/mini_term/common/mapped_input.rb, line 12
def self.map_types
  @maps.keys
end
open(options = {}) click to toggle source
# File lib/mini_term/common/term_info.rb, line 9
def self.open(options = {})
  @term_open = true
  @options = options
  validate_options
end
print(text) click to toggle source

Put some text onto the screen.

raw() { |self| ... } click to toggle source

Execute the block with input in raw mode.

# File lib/mini_term/common/raw_input.rb, line 19
def self.raw
  begin_raw_input unless (already_raw = raw?)
  yield(self)
ensure
  end_raw_input unless already_raw
end
raw?() click to toggle source

Is raw mode in effect?

# File lib/mini_term/common/raw_input.rb, line 14
def self.raw?
  @raw_input
end
set_posn(row: nil, column:) click to toggle source

Set the row (optional) and column of the cursor.

# File lib/mini_term/ansi/set_posn.rb, line 7
def self.set_posn(row: nil, column:)
  if row
    STDOUT.print("\e[#{row};#{column}f")
  else
    STDOUT.print("\e#{column}G")
  end

  self
end
stdin_handle() click to toggle source

Well, stdin's handle in particular.

# File lib/mini_term/windows/link.rb, line 23
def self.stdin_handle
  get_handle(STDIN_ID)
end
stdout_handle() click to toggle source

Well, stdout's handle in particular.

# File lib/mini_term/windows/link.rb, line 18
def self.stdout_handle
  get_handle(STDOUT_ID)
end
term_info() click to toggle source

Get term_info [rows, cols]

# File lib/mini_term/ansi/term_info.rb, line 7
def self.term_info
  IO.console.winsize
end
term_open?() click to toggle source

Is the mini_term open just now?

# File lib/mini_term/common/term_info.rb, line 22
def self.term_open?
  @term_open
end
width() click to toggle source

What is the terminal width in characters?

# File lib/mini_term/common/term_info.rb, line 35
def self.width
  term_info[1]
end
windows?() click to toggle source

Is this Windows?

# File lib/mini_term.rb, line 39
def self.windows?
  TERM_TYPE == :windows
end

Private Class Methods

begin_raw_input() click to toggle source

Get user input uncooked, with no echo or buffering.

# File lib/mini_term/ansi/raw_input.rb, line 20
def self.begin_raw_input
  @raw_input = true
  STDIN.raw!
end
end_raw_input() click to toggle source

Done with raw mode for now.

# File lib/mini_term/ansi/raw_input.rb, line 26
def self.end_raw_input
  STDIN.cooked!
  @raw_input = false
end
get_cursor_row() click to toggle source

Get the current row of the cursor.

# File lib/mini_term/windows/term_info.rb, line 32
def self.get_cursor_row
  raw_buffer = 0.chr * 24
  get_screen_info(raw_buffer)
  (raw_buffer[6,2].unpack('S'))[0]
end
get_term_mode() click to toggle source

Get the current terminal mode.

# File lib/mini_term/windows/term_info.rb, line 20
def self.get_term_mode
  raw_buffer = 0.chr * 8
  get_console_mode(raw_buffer)
  raw_buffer[0,4].unpack('L')[0]
end
set_term_mode(new_mode) click to toggle source

Get the current terminal mode.

# File lib/mini_term/windows/term_info.rb, line 27
def self.set_term_mode(new_mode)
  set_console_mode(new_mode)
end
validate_options() click to toggle source
# File lib/mini_term/common/term_info.rb, line 46
def self.validate_options
  return if (bad = @options.keys - VALID_OPTIONS).empty?

  msg = "MiniTerm.open, Invalid options: #{bad.join(", ")}"
  puts msg unless @options[:quiet]
  fail MiniTermStrict, msg if @options[:strict]
end