mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
new methods and constants for using the mouse, character attributes,
colors and key codes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1676 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7e97ab23f8
commit
9eded5fa13
6 changed files with 1168 additions and 28 deletions
|
@ -5,3 +5,5 @@ extconf.rb
|
|||
hello.rb
|
||||
rain.rb
|
||||
view.rb
|
||||
view2.rb
|
||||
mouse.rb
|
||||
|
|
1016
ext/curses/curses.c
1016
ext/curses/curses.c
File diff suppressed because it is too large
Load diff
|
@ -1,8 +1,8 @@
|
|||
require 'mkmf'
|
||||
|
||||
dir_config('curses')
|
||||
dir_config('ncurses')
|
||||
dir_config('termcap')
|
||||
if( ! $CPPFLAGS )
|
||||
$CPPFLAGS = ""
|
||||
end
|
||||
|
||||
make=false
|
||||
have_library("mytinfo", "tgetent") if /bow/ =~ RUBY_PLATFORM
|
||||
|
@ -20,7 +20,7 @@ else
|
|||
end
|
||||
|
||||
if make
|
||||
for f in %w(isendwin ungetch beep doupdate flash deleteln wdeleteln)
|
||||
for f in %w(isendwin ungetch beep doupdate flash deleteln wdeleteln keypad init_color)
|
||||
have_func(f)
|
||||
end
|
||||
create_makefile("curses")
|
||||
|
|
|
@ -7,7 +7,7 @@ def show_message(message)
|
|||
width = message.length + 6
|
||||
win = Window.new(5, width,
|
||||
(lines - 5) / 2, (cols - width) / 2)
|
||||
win.box(?|, ?=)
|
||||
win.box(?|, ?-)
|
||||
win.setpos(2, 3)
|
||||
win.addstr(message)
|
||||
win.refresh
|
||||
|
|
53
ext/curses/mouse.rb
Normal file
53
ext/curses/mouse.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
#!/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
|
115
ext/curses/view2.rb
Normal file
115
ext/curses/view2.rb
Normal file
|
@ -0,0 +1,115 @@
|
|||
#!/usr/local/bin/ruby
|
||||
|
||||
require "curses"
|
||||
|
||||
if ARGV.size != 1 then
|
||||
printf("usage: view file\n");
|
||||
exit
|
||||
end
|
||||
begin
|
||||
fp = open(ARGV[0], "r")
|
||||
rescue
|
||||
raise "cannot open file: #{ARGV[1]}"
|
||||
end
|
||||
|
||||
# signal(SIGINT, finish)
|
||||
|
||||
Curses.init_screen
|
||||
Curses.nonl
|
||||
Curses.cbreak
|
||||
Curses.noecho
|
||||
|
||||
$screen = Curses.stdscr
|
||||
|
||||
$screen.scrollok(true)
|
||||
#$screen.keypad(true)
|
||||
|
||||
# slurp the file
|
||||
$data_lines = []
|
||||
fp.each_line { |l|
|
||||
$data_lines.push(l.chop)
|
||||
}
|
||||
fp.close
|
||||
|
||||
$top = 0
|
||||
$data_lines[0..$screen.maxy-1].each_with_index{|line, idx|
|
||||
$screen.setpos(idx, 0)
|
||||
$screen.addstr(line)
|
||||
}
|
||||
$screen.setpos(0,0)
|
||||
$screen.refresh
|
||||
|
||||
def scroll_up
|
||||
if( $top > 0 )
|
||||
$screen.scrl(-1)
|
||||
$top -= 1
|
||||
str = $data_lines[$top]
|
||||
if( str )
|
||||
$screen.setpos(0, 0)
|
||||
$screen.addstr(str)
|
||||
end
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def scroll_down
|
||||
if( $top + $screen.maxy < $data_lines.length )
|
||||
$screen.scrl(1)
|
||||
$top += 1
|
||||
str = $data_lines[$top + $screen.maxy - 1]
|
||||
if( str )
|
||||
$screen.setpos($screen.maxy - 1, 0)
|
||||
$screen.addstr(str)
|
||||
end
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
while true
|
||||
result = true
|
||||
c = Curses.getch
|
||||
case c
|
||||
when Curses::KEY_DOWN, Curses::KEY_CTRL_N
|
||||
result = scroll_down
|
||||
when Curses::KEY_UP, Curses::KEY_CTRL_P
|
||||
result = scroll_up
|
||||
when Curses::KEY_NPAGE, ? # white space
|
||||
for i in 0..($screen.maxy - 2)
|
||||
if( ! scroll_down )
|
||||
if( i == 0 )
|
||||
result = false
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
when Curses::KEY_PPAGE
|
||||
for i in 0..($screen.maxy - 2)
|
||||
if( ! scroll_up )
|
||||
if( i == 0 )
|
||||
result = false
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
when Curses::KEY_LEFT, Curses::KEY_CTRL_T
|
||||
while( scroll_up )
|
||||
end
|
||||
when Curses::KEY_RIGHT, Curses::KEY_CTRL_B
|
||||
while( scroll_down )
|
||||
end
|
||||
when ?q
|
||||
break
|
||||
else
|
||||
$screen.setpos(0,0)
|
||||
$screen.addstr("[unknown key `#{Curses.keyname(c)}'=#{c}] ")
|
||||
end
|
||||
if( !result )
|
||||
Curses.beep
|
||||
end
|
||||
$screen.setpos(0,0)
|
||||
end
|
||||
Curses.close_screen
|
Loading…
Reference in a new issue