class BBS::AI::Typewriter
Types streamed text onto a FakeOS console, word-wrapped to the caller’s terminal under a prefix:
ACP: The bus is quiet tonight, and I am
listening to it.
Words are held back until they are whole, and whitespace is written only before the next word, so a reply never ends on a dangling blank line.
Public Class Methods
# File lib/bbs/ai.rb, line 402 def initialize(io, prefix: '', style: :text, width: nil) @io = io @style = style @indent = ' ' * prefix.size @width = [(width || (io.cols - 1)) - prefix.size, 20].max @col = 0 @gap = nil @pending = +'' @io.print prefix, style: @style, newline: 0 end
Public Instance Methods
Source
# File lib/bbs/ai.rb, line 425 def finish(newlines: 2) word(@pending) @pending = +'' @io.newline(newlines) end
Writes the last word and newlines line breaks.
Source
# File lib/bbs/ai.rb, line 415 def write(piece) @pending << piece while (m = @pending.match(Text::WHITESPACE)) word(m.pre_match) @gap = "#{@gap}#{m[0]}" @pending = m.post_match end end
Adds piece of the streamed text, printing every word it completes and keeping the unfinished one back.