examples/example.rb (4533B) - raw
1 #!/usr/bin/env ruby
2
3 # $Id: example.rb,v 1.4 2002-03-04 13:24:29 t-peters Exp $
4
5 # This file provides an example for the usage of the ncurses-ruby module.
6 # Copyright (C) 2002 Tobias Peters <t-peters@users.berlios.de>
7 #
8 # The following license applys only to this file. It is less restrictive
9 # than the license for the rest of the ncurses-ruby distribution.
10 # I've adapted this file from someone else, see below.
11 #
12 # Permission is hereby granted, free of charge, to any person
13 # obtaining a copy of this file
14 # (the "Software"), to deal in the Software without restriction,
15 # including without limitation the rights to use, copy, modify, merge,
16 # publish, distribute, sublicense, and/or sell copies of the Software,
17 # and to permit persons to whom the Software is furnished to do so,
18 # subject to the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be
21 # included in all copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 # SOFTWARE.
31
32
33
34 # this file is adapted from an example of the python ncurses binding
35 # pyncurses (http://pyncurses.sf.net/), which has the following copyright
36 # statement:
37
38 # Copyright (c) 2000 by Harry Henry Gebel
39 #
40 # Permission is hereby granted, free of charge, to any person
41 # obtaining a copy of this software and associated documentation files
42 # (the "Software"), to deal in the Software without restriction,
43 # including without limitation the rights to use, copy, modify, merge,
44 # publish, distribute, sublicense, and/or sell copies of the Software,
45 # and to permit persons to whom the Software is furnished to do so,
46 # subject to the following conditions:
47 #
48 # The above copyright notice and this permission notice shall be
49 # included in all copies or substantial portions of the Software.
50 #
51 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
55 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
56 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
57 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58 # SOFTWARE.
59
60
61
62 require "ncursesw"
63
64 def moving(scr)
65 scr.clear() # clear screen
66 scr.move(5,5) # move cursor
67 scr.addstr("move(5,5)")
68 scr.refresh() # update screen
69 sleep(2)
70 scr.move(2,2)
71 scr.addstr("move(2,2)")
72 scr.refresh()
73 sleep(2)
74 scr.move(10, 2)
75 scr.addstr("Press a key to continue")
76 scr.getch()
77 end
78
79 def border(scr)
80 scr.clear()
81 scr.border(*([0]*8)) # calls WINDOW#border(0, 0, 0, 0, 0, 0, 0, 0)
82 scr.move(3,3)
83 scr.addstr("Press a key to continue")
84 scr.getch()
85 end
86
87 def two_borders()
88 # make a new window as tall as the screen and half as wide, in the left half
89 # of the screen
90 one = Ncurses::WINDOW.new(0, Ncurses.COLS() / 2, 0, 0)
91 # make one for the right half
92 two = Ncurses::WINDOW.new(0, Ncurses.COLS() - (Ncurses.COLS() / 2),
93 0, Ncurses.COLS() / 2)
94 one.border(*([0]*8))
95 two.border(*([0]*8))
96 one.move(3,3)
97 two.move(2,5)
98 one.addstr("move(3,3)")
99 two.addstr("move(2,5)")
100 two.move(5,3)
101 two.addstr("Press a key to continue")
102 one.noutrefresh() # copy window to virtual screen, don't update real screen
103 two.noutrefresh()
104 Ncurses.doupdate() # update read screen
105 two.getch()
106 end
107
108 begin
109 # initialize ncurses
110 Ncurses.initscr
111 Ncurses.cbreak # provide unbuffered input
112 Ncurses.noecho # turn off input echoing
113 Ncurses.nonl # turn off newline translation
114 Ncurses.stdscr.intrflush(false) # turn off flush-on-interrupt
115 Ncurses.stdscr.keypad(true) # turn on keypad mode
116
117 Ncurses.stdscr.addstr("Press a key to continue") # output string
118 Ncurses.stdscr.getch # get a charachter
119
120 moving(Ncurses.stdscr) # demo of moving cursor
121 border(Ncurses.stdscr) # demo of borders
122 two_borders() # demo of two windows with borders
123
124 ensure
125 Ncurses.echo
126 Ncurses.nocbreak
127 Ncurses.nl
128 Ncurses.endwin
129 end