Added a proc to deal with keys to `Window`
[nfoiled.git] / lib / nfoiled.rb
blobdc70db14c2c38f3e40fdabcb8f2b2069ae2ff108
1 require 'nfoiled/terminal'
2 require 'nfoiled/window'
4 require 'ncurses'
6 ##
7 # See `README.markdown`.
8 module Nfoiled
9   Version = 0
10   
11   class <<self
12     attr_accessor :initialized; alias_method :initialized?, :initialized
13     
14     ##
15     # This module method is responsible for setting up the entirety of Nfoiled's
16     # overall environment. It will be called before any other Nfoiled
17     # functionality is allowed. In most cases, this will be called for you.
18     # 
19     # This method also schedules `Nfoiled::finalize` to be automatically run
20     # `at_exit`.
21     def initialize!
22       self.initialized = true
23       Terminal.default = Terminal.new unless Terminal.current
24       at_exit { Nfoiled.finalize }
25     end
26     
27     ##
28     # This module method ensures that Nfoiled is initialized. It simply calls
29     # `Nfoiled::initialize!` if Nfoiled hasn't already been initialized.
30     def initialize
31       initialize! unless initialized?
32     end
33     public :initialize
34     
35     ##
36     # Causes an cycling of the physical window with the virtual window.
37     # 
38     # Warning: You have to update the virtual window first!
39     def update!
40       ::Ncurses.doupdate
41     end
42     
43     ##
44     # This method is responsible for tearing down any environment set up by the
45     # `Ncurses::initialize!` method.
46     def finalize!
47       self.initialized = false
48       ::Ncurses.endwin
49       Terminal.terminals.each {|t| t.destroy! }
50       Terminal.current, Terminal.default = nil
51     end
52     
53     ##
54     # This module method ensures that Nfoiled is finalize. It simply calls
55     # `Nfoiled::finalize!` if Nfoiled hasn't already been finalized.
56     def finalize
57       # TODO: Ensure finalization on fatal errors or interrupts
58       finalize! if initialized?
59     end
60   end
61   
62 end