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 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
Is this ANSI?
# File lib/mini_term.rb, line 44 def self.ansi? TERM_TYPE == :ansi end
Sound a beep
# File lib/mini_term/ansi/output.rb, line 13 def self.beep STDERR.write(BELL) STDERR.flush self end
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
# 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 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 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 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
Is there a character waiting?
# File lib/mini_term/ansi/raw_input.rb, line 7 def self.has_raw_char? raw { STDIN.ready? } end
What is the terminal height in rows?
# File lib/mini_term/common/term_info.rb, line 40 def self.height term_info[0] end
Are we running under Java?
# File lib/mini_term.rb, line 49 def self.java? TERM_JAVA end
What terminal types are mapped?
# File lib/mini_term/common/mapped_input.rb, line 12 def self.map_types @maps.keys end
# File lib/mini_term/common/term_info.rb, line 9 def self.open(options = {}) @term_open = true @options = options validate_options end
Put some text onto the screen.
# File lib/mini_term/ansi/output.rb, line 7 def self.print(text) STDOUT.print(text) self end
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
Is raw mode in effect?
# File lib/mini_term/common/raw_input.rb, line 14 def self.raw? @raw_input end
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
Well, stdin's handle in particular.
# File lib/mini_term/windows/link.rb, line 23 def self.stdin_handle get_handle(STDIN_ID) end
Well, stdout's handle in particular.
# File lib/mini_term/windows/link.rb, line 18 def self.stdout_handle get_handle(STDOUT_ID) end
Get term_info
[rows, cols]
# File lib/mini_term/ansi/term_info.rb, line 7 def self.term_info IO.console.winsize end
Is the mini_term open just now?
# File lib/mini_term/common/term_info.rb, line 22 def self.term_open? @term_open end
What is the terminal width in characters?
# File lib/mini_term/common/term_info.rb, line 35 def self.width term_info[1] end
Is this Windows?
# File lib/mini_term.rb, line 39 def self.windows? TERM_TYPE == :windows end
Private Class Methods
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
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 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 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
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
# 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