3 # An `Nfoiled::Terminal` is a specific set of Nfoiled windows and
4 # configuration. In the vast majority of cases, you only need one of these,
5 # and that one will be created for you by `Nfoiled::initialize`. A general
6 # user shouldn't need to deal with `Terminal` at all.
9 class MissingTerminalError < StandardError; end
12 # An array of known `Terminal` instances
13 attr_reader :terminals
14 def terminals; @terminals ||= Array.new; end
16 # The currently active `Terminal` instance
17 attr_accessor :current
19 # The initial `Terminal` instance, if Nfoiled is initialized without an
20 # existing `Terminal`.
21 attr_accessor :default
24 # The IO object to which output will be managed
27 # The IO object on which input will be watched
30 # The type of the terminal ('vt102', 'xterm', etc)
33 # The actual terminal object as returned by Ncurses
36 # An array of known windows belonging to this Terminal
38 def windows; @windows ||= Array.new; end
41 # Responsible for creating a new `Terminal`. See `newterm(3X)`.
42 def initialize opts = Hash.new
43 { :output => STDOUT, :input => STDIN }.merge opts
44 @output = opts[:output]
48 @wrapee = ::Ncurses.newterm(opts[:term], opts[:out], opts[:in])
49 Terminal.current = self
50 Terminal.terminals << self
55 # This method raises an error if it is called when this `Terminal` is no
58 raise MissingTerminalError, "Terminal not found! (did you `#destroy!` it?)" unless
59 @wrapee and Terminal.terminals.include?(self)
63 # Destroys the `wrapee` of this `Terminal`, and removes this `Terminal`
64 # from `Terminal.terminals`.
67 ::Ncurses.delscreen(@wrapee)
68 Terminal.terminals.delete self