ffi-ncurses 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/COPYING +22 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile.lock +13 -0
  4. data/History.txt +32 -3
  5. data/README.rdoc +118 -58
  6. data/Rakefile +30 -0
  7. data/examples/acs_chars.rb +53 -0
  8. data/examples/acs_chars.rbc +1502 -0
  9. data/examples/{example-attributes.rb → attributes.rb} +0 -0
  10. data/examples/color.rb +63 -0
  11. data/examples/cursor.rb +27 -0
  12. data/examples/example.rb +17 -17
  13. data/examples/getkey.rb +212 -0
  14. data/examples/{example-getsetsyx.rb → getsetsyx.rb} +2 -2
  15. data/examples/globals.rb +38 -0
  16. data/examples/hello.rb +34 -0
  17. data/examples/hello.rbc +638 -0
  18. data/examples/hellowide.rb +59 -0
  19. data/examples/keys.rb +27 -0
  20. data/examples/{example-mouse.rb → mouse.rb} +2 -2
  21. data/examples/multiterm.rb +120 -0
  22. data/examples/ncurses/LICENSES_for_examples +26 -0
  23. data/examples/{ncurses-example.rb → ncurses/example.rb} +13 -80
  24. data/examples/ncurses/hello_ncurses.rb +57 -0
  25. data/examples/ncurses/rain.rb +220 -0
  26. data/examples/ncurses/read_line.rb +67 -0
  27. data/examples/ncurses/subwin.rb +71 -0
  28. data/examples/ncurses/tclock.rb +227 -0
  29. data/examples/newterm.rb +65 -0
  30. data/examples/panel_simple.rb +82 -0
  31. data/examples/{example-printw-variadic.rb → printw-variadic.rb} +1 -1
  32. data/examples/ripoffline.rb +86 -0
  33. data/examples/run-all.sh +14 -0
  34. data/examples/{example-softkeys.rb → softkeys.rb} +0 -0
  35. data/examples/{example-stdscr.rb → stdscr.rb} +2 -1
  36. data/examples/temp_leave.rb +99 -0
  37. data/examples/viewer.rb +350 -0
  38. data/examples/wacs_chars.rb +64 -0
  39. data/examples/windows.rb +73 -0
  40. data/ffi-ncurses.gemspec +39 -52
  41. data/lib/ffi-ncurses.rb +214 -474
  42. data/lib/ffi-ncurses/acs.rb +150 -0
  43. data/lib/ffi-ncurses/bool_wrappers.rb +66 -0
  44. data/lib/ffi-ncurses/functions.rb +450 -0
  45. data/lib/ffi-ncurses/keydefs.rb +136 -99
  46. data/lib/ffi-ncurses/mouse.rb +106 -106
  47. data/lib/ffi-ncurses/ncurses.rb +176 -0
  48. data/lib/ffi-ncurses/{ord-shim.rb → ord_shim.rb} +0 -0
  49. data/lib/ffi-ncurses/panel.rb +21 -0
  50. data/lib/ffi-ncurses/typedefs.rb +35 -0
  51. data/lib/ffi-ncurses/version.rb +7 -0
  52. data/lib/ffi-ncurses/widechars.rb +137 -0
  53. data/lib/ffi-ncurses/winstruct.rb +55 -33
  54. data/spec/attached_functions_spec.rb +42 -0
  55. metadata +95 -85
  56. data/examples/example-colour.rb +0 -63
  57. data/examples/example-cursor.rb +0 -22
  58. data/examples/example-hello.rb +0 -24
  59. data/examples/example-jruby.rb +0 -14
  60. data/examples/example-keys.rb +0 -25
  61. data/examples/example-windows.rb +0 -24
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sean O'Halpin, 2009-02-15
4
+ #
5
+ require 'ffi-ncurses'
6
+ require 'ffi-ncurses/ord_shim' # for 1.8.6 compatibility
7
+ include FFI::NCurses
8
+
9
+ initscr
10
+ begin
11
+ # turn cursor off
12
+ curs_set 0
13
+
14
+ # initialize colour
15
+ start_color
16
+
17
+ # set up colour pairs
18
+ # Background Foreground
19
+ init_pair(0, Color::BLACK, Color::BLACK)
20
+ init_pair(1, Color::RED, Color::BLACK)
21
+ init_pair(2, Color::GREEN, Color::BLACK)
22
+ init_pair(3, Color::YELLOW, Color::BLACK)
23
+ init_pair(4, Color::BLUE, Color::BLACK)
24
+ init_pair(5, Color::MAGENTA, Color::BLACK)
25
+ init_pair(6, Color::CYAN, Color::BLACK)
26
+ init_pair(7, Color::WHITE, Color::BLACK)
27
+
28
+ init_pair(8, Color::BLACK, Color::BLACK)
29
+ init_pair(9, Color::BLACK, Color::RED)
30
+ init_pair(10, Color::BLACK, Color::GREEN)
31
+ init_pair(11, Color::BLACK, Color::YELLOW)
32
+ init_pair(12, Color::BLACK, Color::BLUE)
33
+ init_pair(13, Color::BLACK, Color::MAGENTA)
34
+ init_pair(14, Color::BLACK, Color::CYAN)
35
+ init_pair(15, Color::BLACK, Color::WHITE)
36
+
37
+ 0.upto(15) do |i|
38
+ attr_set A_NORMAL, i, nil
39
+ addch("A"[0].ord + i)
40
+ end
41
+
42
+ # add character and attribute together
43
+ addch("Z"[0].ord | COLOR_PAIR(1)) # red
44
+
45
+ # reset attribute and colour to default
46
+ attr_set A_NORMAL, 0, nil
47
+
48
+ # start new line
49
+ addstr "\n"
50
+
51
+ # how to add a single space
52
+ addch(' '[0].ord)
53
+ # or
54
+ addstr(" ")
55
+
56
+ addstr "Press any key"
57
+
58
+ # display and pause for key press
59
+ refresh
60
+ ch = getch
61
+ ensure
62
+ endwin
63
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sean O'Halpin, 2009-02-15
4
+ #
5
+ require 'ffi-ncurses'
6
+ include FFI::NCurses
7
+
8
+ def try_cursor(n)
9
+ addstr "\ncurs_set #{n} "
10
+ curs_set n
11
+ getch
12
+ end
13
+
14
+ begin
15
+ initscr
16
+ noecho
17
+ addstr "Press any key to cycle through the cursor types:\n"
18
+ addstr "Default "
19
+ getch
20
+ try_cursor 0
21
+ # Note that there probably won't be any difference between cursors 1
22
+ # and 2 on an X terminal. Try in the Linux console to see a difference.
23
+ try_cursor 1
24
+ try_cursor 2
25
+ ensure
26
+ FFI::NCurses.endwin
27
+ end
@@ -5,7 +5,7 @@
5
5
  # Sean O'Halpin, 2009-01-18
6
6
  #
7
7
  require 'ffi-ncurses'
8
- require 'ffi-ncurses/ord-shim' # for 1.8.6 compatibility
8
+ require 'ffi-ncurses/ord_shim' # for 1.8.6 compatibility
9
9
 
10
10
  begin
11
11
  # the methods can be called as module methods
@@ -34,23 +34,23 @@ begin
34
34
  #addstr(sprintf("paryx %d %d\n", *getparyx(FFI::NCurses.stdscr)))
35
35
  standend
36
36
 
37
- init_pair(1, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::BLACK)
38
- init_pair(2, FFI::NCurses::Colour::RED, FFI::NCurses::Colour::BLACK)
39
- init_pair(3, FFI::NCurses::Colour::GREEN, FFI::NCurses::Colour::BLACK)
40
- init_pair(4, FFI::NCurses::Colour::YELLOW, FFI::NCurses::Colour::BLACK)
41
- init_pair(5, FFI::NCurses::Colour::BLUE, FFI::NCurses::Colour::BLACK)
42
- init_pair(6, FFI::NCurses::Colour::MAGENTA, FFI::NCurses::Colour::BLACK)
43
- init_pair(7, FFI::NCurses::Colour::CYAN, FFI::NCurses::Colour::BLACK)
44
- init_pair(8, FFI::NCurses::Colour::WHITE, FFI::NCurses::Colour::BLACK)
37
+ init_pair(1, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::BLACK)
38
+ init_pair(2, FFI::NCurses::Color::RED, FFI::NCurses::Color::BLACK)
39
+ init_pair(3, FFI::NCurses::Color::GREEN, FFI::NCurses::Color::BLACK)
40
+ init_pair(4, FFI::NCurses::Color::YELLOW, FFI::NCurses::Color::BLACK)
41
+ init_pair(5, FFI::NCurses::Color::BLUE, FFI::NCurses::Color::BLACK)
42
+ init_pair(6, FFI::NCurses::Color::MAGENTA, FFI::NCurses::Color::BLACK)
43
+ init_pair(7, FFI::NCurses::Color::CYAN, FFI::NCurses::Color::BLACK)
44
+ init_pair(8, FFI::NCurses::Color::WHITE, FFI::NCurses::Color::BLACK)
45
45
 
46
- init_pair(9, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::BLACK)
47
- init_pair(10, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::RED)
48
- init_pair(11, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::GREEN)
49
- init_pair(12, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::YELLOW)
50
- init_pair(13, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::BLUE)
51
- init_pair(14, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::MAGENTA)
52
- init_pair(15, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::CYAN)
53
- init_pair(16, FFI::NCurses::Colour::BLACK, FFI::NCurses::Colour::WHITE)
46
+ init_pair(9, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::BLACK)
47
+ init_pair(10, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::RED)
48
+ init_pair(11, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::GREEN)
49
+ init_pair(12, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::YELLOW)
50
+ init_pair(13, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::BLUE)
51
+ init_pair(14, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::MAGENTA)
52
+ init_pair(15, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::CYAN)
53
+ init_pair(16, FFI::NCurses::Color::BLACK, FFI::NCurses::Color::WHITE)
54
54
 
55
55
  1.upto(16) do |i|
56
56
  attr_set FFI::NCurses::A_NORMAL, i, nil
@@ -0,0 +1,212 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ #
4
+ # Note: terminal set to:
5
+ # LANG=en_GB.utf8
6
+ #
7
+ # Sean O'Halpin, 2010-08-29
8
+
9
+ require 'ffi-ncurses'
10
+
11
+ include FFI::NCurses
12
+
13
+ $APP_DEBUG = ARGV.delete("--debug")
14
+
15
+ def log(*a)
16
+ if $APP_DEBUG
17
+ File.open("log.txt", "ab") do |file|
18
+ file.puts a.inspect
19
+ end
20
+ end
21
+ end
22
+
23
+ def line(row, txt = nil)
24
+ move row, 2
25
+ if txt
26
+ addstr(txt)
27
+ end
28
+ end
29
+
30
+ def column(col, txt = nil)
31
+ y, x = getyx(stdscr)
32
+ move y, col
33
+ if txt
34
+ addstr(txt)
35
+ end
36
+ end
37
+
38
+ $APP_USE_WIDECHARS = ARGV.delete("--no-widechars") ? false : true
39
+ def use_widechars?
40
+ $APP_USE_WIDECHARS
41
+ end
42
+
43
+ @last_keys = []
44
+
45
+ def getkey(win, input_buffer)
46
+ if use_widechars?
47
+ log :wget_wch
48
+ # Note: 1.8.x stalls in this function without setlocale(LC_ALL, "")
49
+ rv = FFI::NCurses.wget_wch(win, input_buffer)
50
+ log :rv, rv
51
+ ch = input_buffer.read_int # assumes wint_t is an int (which in all cases I've seen it is...)
52
+ log :ch, ch
53
+
54
+ # flag if a function key code
55
+ if rv == KEY_CODE_YES
56
+ fkey = true
57
+ else
58
+ fkey = false
59
+ end
60
+ else
61
+ # use 8-bit input - won't handle Unicode properly
62
+ log :wgetch
63
+ ch = FFI::NCurses.wgetch(win)
64
+ if (KEY_CODE_YES..KEY_MAX).include?(ch)
65
+ fkey = true
66
+ else
67
+ fkey = false
68
+ end
69
+ end
70
+ # convert char code to UTF-8 string
71
+ char = [ch].pack("U")
72
+ log :getkey, :fkey, fkey, :ch, ch, :char, char
73
+ @last_keys.unshift(ch)
74
+ while @last_keys.size > 10
75
+ @last_keys.pop
76
+ end
77
+ [fkey, ch, char]
78
+ end
79
+
80
+ def header
81
+ clear
82
+ line 1, "Press any key to display codes in various interpretations"
83
+ line 2, "Press Ctrl-Q to quit."
84
+ line 3, "widechar input %s. Press w to toggle" % [use_widechars? ? "on" : "off"]
85
+ line 4, "keypad %s. Press k to toggle" % [@keypad_toggle ? "on" : "off"]
86
+ line 5, "meta %s. Press m to toggle (only makes a difference in non-widechar mode)" % [@meta_toggle ? "on" : "off"]
87
+ line 6, "meta works only on terminal emulators that allow 8-bit input - use xterm to see effect"
88
+ end
89
+
90
+ begin
91
+ # initialize screen and input params
92
+ win = initscr
93
+ curs_set 0
94
+ raw
95
+ set_escdelay(1000) if respond_to?(:set_escdelay)
96
+ # turn on meta - ncurses default = true - but here to remind me that you can
97
+ # detect Alt keys using keyname (e.g. Alt-q == "M-q")
98
+ @meta_toggle = ARGV.delete("--no-meta") ? false : true
99
+ @keypad_toggle = ARGV.delete("--no-keypad") ? false : true
100
+ meta(stdscr, @meta_toggle)
101
+ keypad stdscr, @keypad_toggle
102
+ noecho
103
+
104
+ # initialize colour
105
+ start_color
106
+
107
+ # Define a couple of custom keys. The easiest way to find out the sequences is to type
108
+ #
109
+ # $ cat -v
110
+ #
111
+ # at the command line then press the key combination you want.
112
+ #
113
+ # Note that these key definitions may not be portable.
114
+ define_key("\e[1;5C", KEY_MAX - 1) # C-Right
115
+ define_key("\e[1;5D", KEY_MAX - 2) # C-Left
116
+ define_key("\e[1;5A", KEY_MAX - 3) # C-Up
117
+ define_key("\e[1;5B", KEY_MAX - 3) # C-Down
118
+
119
+
120
+ # set up colour pairs
121
+ # Background Foreground
122
+ init_pair(0, Color::BLACK, Color::BLACK)
123
+ init_pair(1, Color::RED, Color::BLACK)
124
+ init_pair(2, Color::GREEN, Color::BLACK)
125
+ init_pair(3, Color::YELLOW, Color::BLACK)
126
+ init_pair(4, Color::BLUE, Color::BLACK)
127
+ init_pair(5, Color::MAGENTA, Color::BLACK)
128
+ init_pair(6, Color::CYAN, Color::BLACK)
129
+ init_pair(7, Color::WHITE, Color::BLACK)
130
+
131
+ init_pair(8, Color::BLACK, Color::BLACK)
132
+ init_pair(9, Color::BLACK, Color::RED)
133
+ init_pair(10, Color::BLACK, Color::GREEN)
134
+ init_pair(11, Color::BLACK, Color::YELLOW)
135
+ init_pair(12, Color::BLACK, Color::BLUE)
136
+ init_pair(13, Color::BLACK, Color::MAGENTA)
137
+ init_pair(14, Color::BLACK, Color::CYAN)
138
+ init_pair(15, Color::BLACK, Color::WHITE)
139
+
140
+ # set up input buffer
141
+ buffer = FFI::Buffer.new(FFI::NCurses.find_type(:wint_t))
142
+ log :buffer_size, buffer.size, FFI.find_type(:int).size
143
+
144
+ # set up output wide char struct
145
+ cchar = WinStruct::CCharT.new
146
+ cchar[:attr] = 0
147
+
148
+ char_col = 55
149
+
150
+ header
151
+ row = 8
152
+ loop do
153
+ # read a key
154
+ fkey, ch, char = getkey(win, buffer)
155
+ break if ch == KEY_CTRL_Q
156
+
157
+ case char
158
+ when "k"
159
+ @keypad_toggle = !@keypad_toggle
160
+ keypad stdscr, @keypad_toggle # NB. win arg ignored
161
+ when "m"
162
+ @meta_toggle = !@meta_toggle
163
+ meta stdscr, @meta_toggle # NB. win arg ignored
164
+ when "w"
165
+ $APP_USE_WIDECHARS = !$APP_USE_WIDECHARS
166
+ end
167
+
168
+ header
169
+ mvwaddstr(win, 20, 2, @last_keys.inspect)
170
+
171
+ attr_on(A_BOLD, nil) if fkey
172
+ line row + 1, "#{fkey ? "Function" : "Normal"} key"
173
+ attr_off(A_BOLD, nil) if fkey
174
+
175
+ line row + 2, "keycode:"
176
+ column char_col, "#{"0x%04x" % ch} (#{ch}) "
177
+
178
+ line row + 3, "keyname: "
179
+ column char_col, "[#{keyname(ch)}]"
180
+
181
+ line row + 4, "key_name: "
182
+ column char_col, "[#{key_name(ch)}]"
183
+
184
+ line row + 5, "output as raw keycode using waddch (non-widechar):"
185
+ column char_col, "["
186
+ waddch win, char[0].ord
187
+ addstr "]"
188
+
189
+ line row + 6, "output as UTF-8 string using waddstr (non-widechar):"
190
+ column char_col, "["
191
+ attr_on(COLOR_PAIR(5) | A_BOLD, nil)
192
+ waddstr win, char
193
+ attr_off(COLOR_PAIR(5) | A_BOLD, nil)
194
+ addstr "]"
195
+
196
+ line row + 7, "output Unicode character using wadd_wch (widechar):"
197
+ cchar[:attr] = COLOR_PAIR(6) | A_BOLD
198
+ cchar[:chars][0] = ch # ch == Unicode codepoint
199
+ column char_col, "["
200
+ wadd_wch win, cchar
201
+ addstr "]"
202
+
203
+ refresh
204
+ end
205
+ rescue => saved_exception
206
+ ensure
207
+ flushinp
208
+ endwin
209
+ end
210
+ if saved_exception
211
+ raise saved_exception
212
+ end
@@ -30,7 +30,7 @@ begin
30
30
  refresh
31
31
  getch
32
32
 
33
- leaveok(win, FFI::NCurses::TRUE)
33
+ leaveok(win, true)
34
34
  wrefresh(win)
35
35
  winy, winx = getyx(win)
36
36
  vy, vx = getsyx
@@ -46,7 +46,7 @@ begin
46
46
  printw("virtual: %d, %d\n", :int, vy, :int, vx)
47
47
  refresh
48
48
  getch
49
-
49
+
50
50
  rescue Object => e
51
51
  FFI::NCurses.endwin
52
52
  raise
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sean O'Halpin, 2010-08-28
4
+ #
5
+ # Demo that global variables are not initialized until after
6
+ # `initscr`.
7
+ #
8
+ require 'ffi-ncurses'
9
+ include FFI::NCurses
10
+ begin
11
+ # symbols = [:acs_map, :curscr, :newscr, :stdscr, :ttytype, :COLORS, :COLOR_PAIRS, :COLS, :ESCDELAY, :LINES, :TABSIZE]
12
+ symbols = [
13
+ "acs_map",
14
+ "curscr",
15
+ "newscr",
16
+ "stdscr",
17
+ "ttytype",
18
+ "COLORS",
19
+ "COLOR_PAIRS",
20
+ "COLS",
21
+ "ESCDELAY",
22
+ "LINES",
23
+ "TABSIZE"
24
+ ]
25
+
26
+ # symbols = [:curscr, :newscr, :stdscr]
27
+ symbols.each do |sym|
28
+ p [sym, FFI::NCurses.send(sym)]
29
+ end
30
+ initscr
31
+ symbols.each do |sym|
32
+ addstr [sym, FFI::NCurses.send(sym)].inspect + "\n"
33
+ end
34
+ refresh
35
+ getch
36
+ ensure
37
+ endwin
38
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+ #
4
+ # Sean O'Halpin, 2009-02-15
5
+ #
6
+ require 'ffi-ncurses'
7
+
8
+ def centre(text)
9
+ col = (COLS() - text.strip.size)/2
10
+ y, x = getyx(stdscr)
11
+ mvaddstr y, col, text
12
+ end
13
+
14
+ include FFI::NCurses
15
+ begin
16
+ greeting = ARGV.shift || "World"
17
+ stdscr = initscr
18
+ raw
19
+ keypad stdscr, true
20
+ noecho
21
+ curs_set 0
22
+ clear
23
+ move (LINES() - 3)/3, 0
24
+ centre "Hello " + greeting + "\n\n"
25
+ centre "Press any key to continue\n"
26
+ refresh
27
+ ch = getch
28
+ flushinp
29
+ addstr sprintf("\nYou pressed %c (%d)", ch, ch)
30
+ refresh
31
+ sleep 1
32
+ ensure
33
+ endwin
34
+ end