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.
10 # An array of known `Terminal` instances
11 attr_reader :terminals
12 def terminals; @terminals ||= Array.new; end
14 # The currently active `Terminal` instance
15 attr_accessor :current
17 # The initial `Terminal` instance, if Nfoiled is initialized without an
18 # existing `Terminal`.
19 attr_accessor :default
22 # The IO object to which output will be managed
25 # The IO object on which input will be watched
28 # The type of the terminal ('vt102', 'xterm', etc)
31 # The actual terminal object as returned by Ncurses
34 # An array of known windows belonging to this Terminal
36 def windows; @windows ||= Array.new; end
38 # The `Window` responsible for accepting input to this Terminal. Defaults
39 # to the last created `Window`, unless one has been defined.
40 attr_accessor :acceptor
41 def acceptor; @acceptor || windows.last; end
47 # Responsible for creating a new `Terminal`. See `newterm(3X)`.
48 def initialize opts = Hash.new
49 { :output => STDOUT, :input => STDIN }.merge opts
50 @output = opts[:output]
54 @wrapee = ::Ncurses.newterm(opts[:term], opts[:out], opts[:in])
55 Terminal.current = self
56 Terminal.terminals << self
61 # 'Activates' a `Terminal`, destroying all windows and environment from
62 # the current `Terminal` and replacing it with those of this one. See
65 ::Ncurses.set_term(wrapee)
66 Terminal.current = self
70 # Simply calls `#activate!` if this `Terminal` isn't already active.
72 activate! unless active?
76 # Simply cheks if `Terminal.current == self`
78 Terminal.current == self
82 # Destroys the `wrapee` of this `Terminal`, and removes this `Terminal`
83 # from `Terminal.terminals`. See `endwin(3X)` and `delscreen(3X)`.
85 previous = Terminal.current
88 ::Ncurses.delscreen(wrapee)
90 Terminal.terminals.delete self
91 previous.activate if previous