examples/form_get_wch.rb (3750B) - raw
1 #!/usr/bin/ruby
2 # Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
3 # Example from the NCurses Programming HOWTO
4 # This example uses module functions that are documented in the ncurses man page.
5 # For a more rubyish approach that uses Ruby objects see form2.rb
6 #
7 # The original example contained the following copyright:
8 # Copyright (c) 2001 by Pradeep Padala. This document may be distributed
9 # under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
10
11 require 'ncursesw.rb'
12
13 begin
14 scr = Ncurses.initscr()
15 Ncurses.cbreak()
16 Ncurses.noecho()
17 Ncurses.keypad(scr, true)
18
19 #create some fields
20 fields = Array.new
21 fields.push(Ncurses::Form.new_field(1,10,4,18,0,0))
22 fields.push(Ncurses::Form.new_field(1,10,6,18,0,0))
23
24 # set field options
25 Ncurses::Form.set_field_back(fields[0], Ncurses::A_UNDERLINE)
26 Ncurses::Form.field_opts_off(fields[0], Ncurses::Form::O_AUTOSKIP)
27
28 Ncurses::Form.set_field_back(fields[1], Ncurses::A_UNDERLINE)
29 Ncurses::Form.field_opts_off(fields[1], Ncurses::Form::O_AUTOSKIP)
30
31
32 # create a form
33 form = Ncurses::Form.new_form(fields)
34
35 # post the form and refresh the screen
36 Ncurses::Form.post_form(form)
37 scr.refresh()
38
39 Ncurses.mvprintw(4, 10, "Value 1:")
40 Ncurses.mvprintw(6, 10, "Value 2:")
41 scr.refresh()
42
43 buf = ""
44
45 if not defined? Ncurses::Form.form_driver_w
46 raise NotImplementedError, "This example needs a ncurses version with the form_driver_w method"
47 end
48
49 # Loop through to get user requests
50 while(true) do
51 ret = Ncurses.get_wch()
52
53 ch = ret[1]
54
55 Ncurses.mvprintw(8, 10, "Got: #{ch.inspect} (#{[ch].pack("U")})")
56
57 case (ret[0])
58 when Ncurses::OK
59 # If this is a normal character, it gets Printed
60 #prbuf = fields[0].field_buffer 0
61 ##buf = buf.force_encoding('utf-8') + [ch].pack('U')
62 #buf = prbuf + [ch].pack('U')
63
64
65 ##buf = sbuf.force_encoding('utf-8') + [ch].pack('U')
66 #fields[0].set_field_buffer(0, buf)
67 #sbuf = fields[0].field_buffer 0
68 ##fields[0].working.add_wch 'a'
69 #Ncurses.mvprintw(12, 10, "buf: #{sbuf}")
70 #Ncurses.mvprintw(13, 10, "pbuf: #{prbuf}")
71 #Ncurses.mvprintw(14, 10, "mbuf: #{buf}")
72
73 #Ncurses.wadd_wch(form.win, ch)
74
75 #buf = fields[0].field_buffer(0)
76 #buf = buf.force_encoding('utf-8') + [ch].pack("U")
77 #fields[0].set_field_buffer(0, buf)
78 #fields[0].set_field_buffer(0, [ch].pack("U"))
79
80 Ncurses::Form.form_driver_w(form, ret[0], ret[1])
81 #puts "{fields[0].methods}"
82
83 #puts "#{form.methods}"
84 #puts "#{fields[0].methods}"
85 #Ncurses.mvprintw(9, 10, "Type: OK ")
86 when Ncurses::KEY_CODE_YES
87 Ncurses.mvprintw(9, 10, "Type: KEY_CODE_YES")
88 case(ch)
89 when Ncurses::KEY_DOWN
90 # Go to next field
91 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_NEXT_FIELD)
92 # Go to the end of the present buffer
93 # Leaves nicely at the last character
94 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE)
95
96 when Ncurses::KEY_UP
97 #Go to previous field
98 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_PREV_FIELD)
99 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE);
100 end
101 else
102 Ncurses.mvprintw(9, 10, "Type: Unknown: #{ret[0].inspect} ")
103 end
104 end
105
106 # unpost and free form
107 Ncurses::Form.unpost_form(form);
108 Ncurses::Form.free_form(form)
109 Ncurses::Form.free_field(fields[0]);
110 Ncurses::Form.free_field(fields[1]);
111
112
113 #using class methods this time
114 # form = Ncurses::Form::FORM.new(fields)
115 # puts "Created form: #{form.inspect}\n"
116 # form.free()
117
118 ensure
119 # put the screen back in its normal state
120 Ncurses.echo()
121 Ncurses.nocbreak()
122 Ncurses.nl()
123 Ncurses.endwin()
124 end
125