Pulling in `Window`.
[nfoiled.git] / lib / nfoiled / terminal.rb
blob0516a248b109bd16997088c320d90003634de96e
1 module Nfoiled
2   ##
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.
7   class Terminal
8     
9     class <<self
10       # An array of known `Terminal` instances
11       attr_reader :terminals
12       def terminals; @terminals ||= Array.new; end
13       
14       # The currently active `Terminal` instance
15       attr_accessor :current
16       
17       # The initial `Terminal` instance, if Nfoiled is initialized without an
18       # existing `Terminal`.
19       attr_accessor :default
20     end
21     
22     # The IO object to which output will be managed
23     attr_reader :output
24     
25     # The IO object on which input will be watched
26     attr_reader :input
27     
28     # TODO: Figure out what this does!
29     attr_reader :term
30     
31     # The actual terminal object as returned by Ncurses
32     attr_reader :wrapee
33     
34     # An array of known windows belonging to this Terminal
35     attr_reader :windows
36     def windows; @windows ||= Array.new; end
37     
38     ##
39     # Responsible for creating a new `Terminal`. See `newterm(3X)`.
40     def initialize opts = Hash.new
41       { :output => STDOUT, :input => STDIN }.merge opts
42       @output = opts[:output]
43       @input = opts[:input]
44       @term = opts[:term]
45       
46       @wrapee = ::Ncurses.newterm(opts[:term], opts[:out], opts[:in])
47       Terminal.current = self
48       Terminal.terminals << self
49     end
50     
51     ##
52     # Destroys the `wrapee` of this `Terminal`, and removes this `Terminal`
53     # from `Terminal.terminals`.
54     def destroy!
55       ::Ncurses.delscreen(@wrapee)
56       Terminal.terminals.delete self
57     end
58   end
59 end