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.
10 # This is simply an accessor for all the windows on the current Terminal.
12 def windows; Terminal.current.windows; end
19 # The Y co-ordinate of the top left corner of this `Window`'s bounding box
21 # The X co-ordinate of the top left corner of this `Window`'s bounding box
23 # The height in lines of this `Window`'s bounding box
25 # The width in columns of this `Window`'s bounding box
28 # The actual window object as returned by Ncurses
31 # The `Terminal` that this `Window` pertains to
34 # The proc to be run when a character is received
37 # ====================
38 # = Setup / Teardown =
39 # ====================
42 # Responsible for creating a new `Window`, this will also take care of
43 # initializing Ncurses if necessary. See `newwin(3X)`.
44 def initialize opts = Hash.new
47 @wrapee = ::Ncurses.newwin(
48 opts[:height] ? @height = opts[:height] : ::Ncurses.LINES,
49 opts[:width] ? @width = opts[:width] : ::Ncurses.COLS,
50 opts[:top] ? @top = opts[:top] : 0,
51 opts[:left] ? @left = opts[:left] : 0)
53 ::Ncurses.wtimeout(wrapee, 0) # Prevents ncurses from blocking for input
55 (@owner = Terminal.current).windows << self
59 # Destroys the `wrapee` of this `Window`, and removes this `Window`
60 # from its owning `Terminal`'s `#windows`. See `delwin(3X)`.
62 ::Ncurses.delwin(wrapee)
64 owner.windows.delete self
72 # Gets a single character from the input buffer for this window. Returns
73 # nil if there are no new characters in the buffer. See `wgetch(3X)`.
75 chr = Key.process ::Ncurses.wgetch(wrapee)
80 # This sets this `Window` as the current `Terminal.acceptor`.
87 # Defines a block that controls how the global input loop from
88 # `Nfoiled::read!` handles input when this window has focus.
90 # This acts as both a getter & setter, depending on whether a block is
93 block_given? ? @on_key = Proc.new : @on_key
101 # Prints a string to the window
108 # Updates the virtual screen associated with this window. See `wnoutrefresh(3X)`.
114 # Prints a string, followed by a newline, to the window
116 self.print string.to_s + "\n"