Causing a refresh on printing to a window
[nfoiled.git] / lib / nfoiled / window.rb
blob8a9914b4c5f10d716939a642fe2649dcf05befd5
1 module Nfoiled
2   ##
3   # An `Nfoiled::Window` is a "box" in the terminal to which output can be
4   # printed and from which input can be received. A basic Nfoiled application
5   # will utilize only one of these, a single `Window` covering the entirety
6   # of the `Terminal`'s available area.
7   class Window
8     
9     class <<self
10       # This is simply an accessor for all the windows on the current Terminal.
11       attr_reader :windows
12       def windows; Terminal.current.windows; end
13     end
14     
15     # The Y co-ordinate of the top left corner of this `Window`'s bounding box
16     attr_reader :top
17     # The X co-ordinate of the top left corner of this `Window`'s bounding box
18     attr_reader :left
19     # The height in lines of this `Window`'s bounding box
20     attr_reader :height
21     # The width in columns of this `Window`'s bounding box
22     attr_reader :width
23     
24     # The actual window object as returned by Ncurses
25     attr_reader :wrapee
26     
27     # The `Terminal` that this `Window` pertains to
28     attr_reader :owner
29     
30     ##
31     # Responsible for creating a new `Window`, this will also take care of
32     # initializing Ncurses if necessary.
33     def initialize opts = Hash.new
34       Nfoiled::initialize
35       
36       @wrapee = ::Ncurses.newwin(
37         opts[:height] ? @height = opts[:height] : 0,
38         opts[:width]  ? @width =  opts[:width]  : 0,
39         opts[:top]    ? @top =    opts[:top]    : 0,
40         opts[:left]   ? @left =   opts[:left]   : 0)
41       
42       (@owner = Terminal.current).windows << self
43     end
44     
45     ##
46     # Prints a string to the window
47     def print string
48       @wrapee.printw string
49       @wrapee.wnoutrefresh
50     end
51     
52     ##
53     # Destroys the `wrapee` of this `Window`, and removes this `Window`
54     # from its owning `Terminal`'s `#windows`.
55     def destroy!
56       ::Ncurses.delwin(@wrapee)
57       @wrapee = nil
58       @owner.windows.delete self
59     end
60   end
61 end