mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/curses/curses.c: [DOC] Update location of samples
* samples/curses/*: Move Curses samples and refactor from mixin The samples are included in rdoc for module and use of mixin is confusing git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42241 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
16bf45bf38
commit
ffb9da9fa0
10 changed files with 163 additions and 162 deletions
|
@ -1,3 +1,10 @@
|
|||
Mon Jul 29 22:25:20 2013 Zachary Scott <e@zzak.io>
|
||||
|
||||
* ext/curses/curses.c: [DOC] Update location of samples
|
||||
* samples/curses/*: Move Curses samples and refactor from mixin
|
||||
The samples are included in rdoc for module and use of mixin is
|
||||
confusing
|
||||
|
||||
Mon Jul 29 22:16:11 2013 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* bignum.c (LOG2_KARATSUBA_BIG2STR_DIGITS): Renamed from
|
||||
|
|
|
@ -2643,11 +2643,11 @@ pad_noutrefresh(VALUE obj, VALUE pminrow, VALUE pmincol, VALUE sminrow,
|
|||
* == Examples
|
||||
*
|
||||
* * hello.rb
|
||||
* :include: hello.rb
|
||||
* :include: sample/curses/hello.rb
|
||||
*
|
||||
*
|
||||
* * rain.rb
|
||||
* :include: rain.rb
|
||||
* :include: sample/curses/rain.rb
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
@ -2680,7 +2680,7 @@ Init_curses(void)
|
|||
* == Example
|
||||
*
|
||||
* * mouse.rb
|
||||
* :include: mouse.rb
|
||||
* :include: sample/curses/mouse.rb
|
||||
*
|
||||
*/
|
||||
cMouseEvent = rb_define_class_under(mCurses,"MouseEvent",rb_cObject);
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
#!/usr/local/bin/ruby
|
||||
|
||||
require "curses"
|
||||
include Curses
|
||||
|
||||
def show_message(message)
|
||||
width = message.length + 6
|
||||
win = Window.new(5, width,
|
||||
(lines - 5) / 2, (cols - width) / 2)
|
||||
win.box('|', '-')
|
||||
win.setpos(2, 3)
|
||||
win.addstr(message)
|
||||
win.refresh
|
||||
win.getch
|
||||
win.close
|
||||
end
|
||||
|
||||
init_screen
|
||||
begin
|
||||
crmode
|
||||
# show_message("Hit any key")
|
||||
setpos((lines - 5) / 2, (cols - 10) / 2)
|
||||
addstr("Hit any key")
|
||||
refresh
|
||||
getch
|
||||
show_message("Hello, World!")
|
||||
refresh
|
||||
ensure
|
||||
close_screen
|
||||
end
|
|
@ -1,53 +0,0 @@
|
|||
#!/usr/local/bin/ruby
|
||||
|
||||
require "curses"
|
||||
include Curses
|
||||
|
||||
def show_message(*msgs)
|
||||
message = msgs.join
|
||||
width = message.length + 6
|
||||
win = Window.new(5, width,
|
||||
(lines - 5) / 2, (cols - width) / 2)
|
||||
win.keypad = true
|
||||
win.attron(color_pair(COLOR_RED)){
|
||||
win.box(?|, ?-, ?+)
|
||||
}
|
||||
win.setpos(2, 3)
|
||||
win.addstr(message)
|
||||
win.refresh
|
||||
win.getch
|
||||
win.close
|
||||
end
|
||||
|
||||
init_screen
|
||||
start_color
|
||||
init_pair(COLOR_BLUE,COLOR_BLUE,COLOR_WHITE)
|
||||
init_pair(COLOR_RED,COLOR_RED,COLOR_WHITE)
|
||||
crmode
|
||||
noecho
|
||||
stdscr.keypad(true)
|
||||
|
||||
begin
|
||||
mousemask(BUTTON1_CLICKED|BUTTON2_CLICKED|BUTTON3_CLICKED|BUTTON4_CLICKED)
|
||||
setpos((lines - 5) / 2, (cols - 10) / 2)
|
||||
attron(color_pair(COLOR_BLUE)|A_BOLD){
|
||||
addstr("click")
|
||||
}
|
||||
refresh
|
||||
while( true )
|
||||
c = getch
|
||||
case c
|
||||
when KEY_MOUSE
|
||||
m = getmouse
|
||||
if( m )
|
||||
show_message("getch = #{c.inspect}, ",
|
||||
"mouse event = #{'0x%x' % m.bstate}, ",
|
||||
"axis = (#{m.x},#{m.y},#{m.z})")
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
refresh
|
||||
ensure
|
||||
close_screen
|
||||
end
|
|
@ -1,76 +0,0 @@
|
|||
#!/usr/local/bin/ruby
|
||||
# rain for a curses test
|
||||
|
||||
require "curses"
|
||||
include Curses
|
||||
|
||||
def onsig(sig)
|
||||
close_screen
|
||||
exit sig
|
||||
end
|
||||
|
||||
def ranf
|
||||
rand(32767).to_f / 32767
|
||||
end
|
||||
|
||||
# main #
|
||||
for i in %w[HUP INT QUIT TERM]
|
||||
if trap(i, "SIG_IGN") != 0 then # 0 for SIG_IGN
|
||||
trap(i) {|sig| onsig(sig) }
|
||||
end
|
||||
end
|
||||
|
||||
init_screen
|
||||
nl
|
||||
noecho
|
||||
srand
|
||||
|
||||
xpos = {}
|
||||
ypos = {}
|
||||
r = lines - 4
|
||||
c = cols - 4
|
||||
for i in 0 .. 4
|
||||
xpos[i] = (c * ranf).to_i + 2
|
||||
ypos[i] = (r * ranf).to_i + 2
|
||||
end
|
||||
|
||||
i = 0
|
||||
while TRUE
|
||||
x = (c * ranf).to_i + 2
|
||||
y = (r * ranf).to_i + 2
|
||||
|
||||
|
||||
setpos(y, x); addstr(".")
|
||||
|
||||
setpos(ypos[i], xpos[i]); addstr("o")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
setpos(ypos[i], xpos[i]); addstr("O")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
setpos(ypos[i] - 1, xpos[i]); addstr("-")
|
||||
setpos(ypos[i], xpos[i] - 1); addstr("|.|")
|
||||
setpos(ypos[i] + 1, xpos[i]); addstr("-")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
setpos(ypos[i] - 2, xpos[i]); addstr("-")
|
||||
setpos(ypos[i] - 1, xpos[i] - 1); addstr("/ \\")
|
||||
setpos(ypos[i], xpos[i] - 2); addstr("| O |")
|
||||
setpos(ypos[i] + 1, xpos[i] - 1); addstr("\\ /")
|
||||
setpos(ypos[i] + 2, xpos[i]); addstr("-")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
setpos(ypos[i] - 2, xpos[i]); addstr(" ")
|
||||
setpos(ypos[i] - 1, xpos[i] - 1); addstr(" ")
|
||||
setpos(ypos[i], xpos[i] - 2); addstr(" ")
|
||||
setpos(ypos[i] + 1, xpos[i] - 1); addstr(" ")
|
||||
setpos(ypos[i] + 2, xpos[i]); addstr(" ")
|
||||
|
||||
|
||||
xpos[i] = x
|
||||
ypos[i] = y
|
||||
refresh
|
||||
sleep(0.5)
|
||||
end
|
||||
|
||||
# end of main
|
27
sample/curses/hello.rb
Normal file
27
sample/curses/hello.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
require "curses"
|
||||
|
||||
def show_message(message)
|
||||
width = message.length + 6
|
||||
win = Curses::Window.new(5, width,
|
||||
(Curses.lines - 5) / 2, (Curses.cols - width) / 2)
|
||||
win.box('|', '-')
|
||||
win.setpos(2, 3)
|
||||
win.addstr(message)
|
||||
win.refresh
|
||||
win.getch
|
||||
win.close
|
||||
end
|
||||
|
||||
Curses.init_screen
|
||||
begin
|
||||
Curses.crmode
|
||||
# show_message("Hit any key")
|
||||
Curses.setpos((Curses.lines - 5) / 2, (Curses.cols - 10) / 2)
|
||||
Curses.addstr("Hit any key")
|
||||
Curses.refresh
|
||||
char = Curses.getch
|
||||
show_message("You typed: #{char}")
|
||||
Curses.refresh
|
||||
ensure
|
||||
Curses.close_screen
|
||||
end
|
52
sample/curses/mouse.rb
Normal file
52
sample/curses/mouse.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require "curses"
|
||||
|
||||
def show_message(*msgs)
|
||||
message = msgs.join
|
||||
width = message.length + 6
|
||||
win = Curses::Window.new(5, width,
|
||||
(Curses.lines - 5) / 2, (Curses.cols - width) / 2)
|
||||
win.keypad = true
|
||||
win.attron(Curses.color_pair(Curses::COLOR_RED)){
|
||||
win.box(?|, ?-, ?+)
|
||||
}
|
||||
win.setpos(2, 3)
|
||||
win.addstr(message)
|
||||
win.refresh
|
||||
win.getch
|
||||
win.close
|
||||
end
|
||||
|
||||
Curses.init_screen
|
||||
Curses.start_color
|
||||
Curses.init_pair(Curses::COLOR_BLUE, Curses::COLOR_BLUE, Curses::COLOR_WHITE)
|
||||
Curses.init_pair(Curses::COLOR_RED, Curses::COLOR_RED, Curses::COLOR_WHITE)
|
||||
Curses.crmode
|
||||
Curses.noecho
|
||||
Curses.stdscr.keypad(true)
|
||||
|
||||
begin
|
||||
Curses.mousemask(
|
||||
Curses::BUTTON1_CLICKED|Curses::BUTTON2_CLICKED|Curses::BUTTON3_CLICKED|Curses::BUTTON4_CLICKED
|
||||
)
|
||||
Curses.setpos((Curses.lines - 5) / 2, (Curses.cols - 10) / 2)
|
||||
Curses.attron(Curses.color_pair(Curses::COLOR_BLUE)|Curses::A_BOLD){
|
||||
Curses.addstr("click")
|
||||
}
|
||||
Curses.refresh
|
||||
while( true )
|
||||
c = Curses.getch
|
||||
case c
|
||||
when Curses::KEY_MOUSE
|
||||
m = Curses::getmouse
|
||||
if( m )
|
||||
show_message("getch = #{c.inspect}, ",
|
||||
"mouse event = #{'0x%x' % m.bstate}, ",
|
||||
"axis = (#{m.x},#{m.y},#{m.z})")
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
Curses.refresh
|
||||
ensure
|
||||
Curses.close_screen
|
||||
end
|
74
sample/curses/rain.rb
Normal file
74
sample/curses/rain.rb
Normal file
|
@ -0,0 +1,74 @@
|
|||
# rain for a curses test
|
||||
|
||||
require "curses"
|
||||
|
||||
def onsig(sig)
|
||||
Curses.close_screen
|
||||
exit sig
|
||||
end
|
||||
|
||||
def ranf
|
||||
rand(32767).to_f / 32767
|
||||
end
|
||||
|
||||
# main #
|
||||
for i in %w[HUP INT QUIT TERM]
|
||||
if trap(i, "SIG_IGN") != 0 then # 0 for SIG_IGN
|
||||
trap(i) {|sig| onsig(sig) }
|
||||
end
|
||||
end
|
||||
|
||||
Curses.init_screen
|
||||
Curses.nl
|
||||
Curses.noecho
|
||||
srand
|
||||
|
||||
xpos = {}
|
||||
ypos = {}
|
||||
r = Curses.lines - 4
|
||||
c = Curses.cols - 4
|
||||
for i in 0 .. 4
|
||||
xpos[i] = (c * ranf).to_i + 2
|
||||
ypos[i] = (r * ranf).to_i + 2
|
||||
end
|
||||
|
||||
i = 0
|
||||
while TRUE
|
||||
x = (c * ranf).to_i + 2
|
||||
y = (r * ranf).to_i + 2
|
||||
|
||||
|
||||
Curses.setpos(y, x); Curses.addstr(".")
|
||||
|
||||
Curses.setpos(ypos[i], xpos[i]); Curses.addstr("o")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
Curses.setpos(ypos[i], xpos[i]); Curses.addstr("O")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
Curses.setpos(ypos[i] - 1, xpos[i]); Curses.addstr("-")
|
||||
Curses.setpos(ypos[i], xpos[i] - 1); Curses.addstr("|.|")
|
||||
Curses.setpos(ypos[i] + 1, xpos[i]); Curses.addstr("-")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
Curses.setpos(ypos[i] - 2, xpos[i]); Curses.addstr("-")
|
||||
Curses.setpos(ypos[i] - 1, xpos[i] - 1); Curses.addstr("/ \\")
|
||||
Curses.setpos(ypos[i], xpos[i] - 2); Curses.addstr("| O |")
|
||||
Curses.setpos(ypos[i] + 1, xpos[i] - 1); Curses.addstr("\\ /")
|
||||
Curses.setpos(ypos[i] + 2, xpos[i]); Curses.addstr("-")
|
||||
|
||||
i = if i == 0 then 4 else i - 1 end
|
||||
Curses.setpos(ypos[i] - 2, xpos[i]); Curses.addstr(" ")
|
||||
Curses.setpos(ypos[i] - 1, xpos[i] - 1); Curses.addstr(" ")
|
||||
Curses.setpos(ypos[i], xpos[i] - 2); Curses.addstr(" ")
|
||||
Curses.setpos(ypos[i] + 1, xpos[i] - 1); Curses.addstr(" ")
|
||||
Curses.setpos(ypos[i] + 2, xpos[i]); Curses.addstr(" ")
|
||||
|
||||
|
||||
xpos[i] = x
|
||||
ypos[i] = y
|
||||
Curses.refresh
|
||||
sleep(0.5)
|
||||
end
|
||||
|
||||
# end of main
|
Loading…
Reference in a new issue