Skip to content

Commit 12eaa66

Browse files
authored
Merge pull request #62 from coezbek/master
Added samples for addch, attron, mouse tracking and colors
2 parents 21a1bd8 + dc411a6 commit 12eaa66

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

sample/addch.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require_relative "../lib/curses"
2+
include Curses
3+
4+
init_screen
5+
begin
6+
addstr("The following letter A should be BOLD and UNDERLINED by using addch:\n")
7+
addch('A'.ord | A_BOLD | A_UNDERLINE)
8+
9+
addstr("\nIt should look the same as when using attron and addstr:\n")
10+
attron(A_BOLD | A_UNDERLINE)
11+
addstr("A")
12+
getch
13+
14+
ensure
15+
close_screen
16+
end

sample/attr_demo.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "curses"
2+
include Curses
3+
4+
init_screen
5+
begin
6+
attrs = {
7+
A_NORMAL => 'Normal display (no highlight)',
8+
A_STANDOUT => 'Best highlighting mode of the terminal',
9+
A_UNDERLINE => 'Underlining',
10+
A_REVERSE => 'Reverse video',
11+
A_BLINK => 'Blinking',
12+
A_DIM => 'Half bright',
13+
A_BOLD => 'Extra bright or bold',
14+
A_PROTECT => 'Protected mode',
15+
A_INVIS => 'Invisible or blank mode',
16+
A_ALTCHARSET => 'Alternate character set',
17+
}
18+
19+
longest_description = attrs.values.map(&:size).max
20+
attrs.each { |attribute, description|
21+
22+
attrset(A_NORMAL)
23+
addstr("#{description.ljust(longest_description)}: ")
24+
25+
attrset(attribute)
26+
addstr([*('a'..'z'), *('0'..'9')].join + "\n")
27+
}
28+
getch
29+
30+
ensure
31+
close_screen
32+
end

sample/colors.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require "curses"
2+
include Curses
3+
4+
begin
5+
init_screen
6+
7+
if !Curses.has_colors?
8+
addstr "This Terminal does not support colors!"
9+
else
10+
start_color
11+
12+
addstr "This Terminal supports #{colors} colors.\n"
13+
14+
Curses.colors.times { |i|
15+
Curses.init_pair(i, i, 0)
16+
attrset(color_pair(i))
17+
addstr("#{i.to_s.rjust(3)} ")
18+
addstr("\n") if i == 15 || (i > 16 && (i - 15) % 36 == 0)
19+
}
20+
end
21+
22+
getch
23+
24+
ensure
25+
close_screen
26+
end

sample/mouse_move.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env ruby
2+
#
3+
# Mouse movement tracking using CSI 1003 and 1006 (SGR). Will print a cursor that moves with the mouse.
4+
#
5+
6+
require "curses"
7+
include Curses
8+
9+
@positions = []
10+
11+
def draw_cursor(y, x, button, state)
12+
13+
# Keep a trail of 10 previous positions visible on the screen
14+
if @positions.size >= 10
15+
y_old, x_old = @positions.shift
16+
setpos(y_old, x_old)
17+
addstr(" ")
18+
end
19+
20+
setpos(2, 1)
21+
addstr("Mouse is at y=#{y.to_s.ljust(3)} x=#{x.to_s.ljust(3)} button=#{button.to_s.ljust(3)} #{'%08b' % button}")
22+
23+
setpos(y, x)
24+
addstr("+")
25+
@positions << [y, x]
26+
end
27+
28+
init_screen
29+
crmode
30+
noecho
31+
stdscr.box('|', "-")
32+
setpos(1,1)
33+
addstr("Please move your mouse. Press 'q' to close.")
34+
35+
begin
36+
# Switch on mouse continous position reporting
37+
print("\x1b[?1003h")
38+
39+
# Also enable SGR extended reporting, because otherwise we can only
40+
# receive values up to 160x94. Everything else confuses Ruby Curses.
41+
print("\x1b[?1006h")
42+
43+
loop do
44+
c = get_char
45+
case c
46+
when "q"
47+
return
48+
when "\e" # ESC
49+
case get_char
50+
when '['
51+
csi = ""
52+
loop do
53+
d = get_char
54+
csi += d
55+
if d.ord >= 0x40 && d.ord <= 0x7E
56+
break
57+
end
58+
end
59+
if /<(\d+);(\d+);(\d+)(m|M)/ =~ csi
60+
button = $1.to_i
61+
x = $2.to_i
62+
y = $3.to_i
63+
state = $4
64+
draw_cursor(y, x, button, state)
65+
end
66+
end
67+
end
68+
end
69+
70+
ensure
71+
print("\x1b[?1003l")
72+
print("\x1b[?1006l")
73+
close_screen
74+
end
75+

0 commit comments

Comments
 (0)