class BBS::Gateway::AsciiBridge
The translation a gateway puts on the line when a PETSCII caller (a Commodore 64) dials a board made for ASCII and ANSI terminals. The board’s bytes go down as PETSCII: text in UTF-8, or CP437 from the first bytes that are not UTF-8 on, through a Petscii::Encoder, so letters swap case, ANSI colours and cursor moves become C64 control codes and line drawing the nearest glyph. The C64’s keys go up as an ANSI terminal sends them.
bridge = BBS::Gateway::AsciiBridge.new bridge.down("Hello\r\n") # PETSCII bytes for the C64 bridge.up("\x91".b) # => "\e[A" (CRSR up)
Constants
- IAC
-
The telnet
IACbyte, doubled in what goes down so the caller’s telnet client takes it as data. - KEYS
-
The C64’s special keys (
BBS::Petscii.key) as an ANSI terminal sends them; a key not listed (F1-F8) is not sent. - SIGNS
-
Signs the C64 has a key for and ASCII has not, as their ASCII stand-in.
- UTF8_LEADS
-
The first byte of a UTF-8 sequence of two, three and four bytes.
Public Class Methods
Source
# File lib/bbs/gateway/ascii_bridge.rb, line 30 def initialize @encoder = Petscii::Encoder.new @pending = +''.b @cp437 = false end
Creates a bridge with a fresh encoder and no partial character.
Public Instance Methods
Source
# File lib/bbs/gateway/ascii_bridge.rb, line 40 def down(bytes) data = @pending + bytes.b @pending = @cp437 ? +''.b : cut_character(data) whole = data.byteslice(0, data.bytesize - @pending.bytesize) @encoder.encode(text(whole)).gsub(IAC, IAC + IAC) end
Returns the PETSCII bytes for bytes the board sent (telnet commands already taken out). While the board writes UTF-8, a character cut at the end of bytes waits for the next call; the encoder’s column and colour carry over too.
Source
# File lib/bbs/gateway/ascii_bridge.rb, line 50 def up(bytes) = bytes.b.each_byte.filter_map { |byte| key(Petscii.key(byte)) }.join.b
Returns the bytes an ANSI terminal would send for bytes, the keys the C64 sent: letters in ASCII case, RETURN as CR, DEL as backspace, the cursor keys as ANSI sequences, RUN/STOP as ESC.