1998-01-16 07:13:05 -05:00
|
|
|
#!/usr/local/bin/ruby
|
|
|
|
|
|
|
|
require "curses"
|
|
|
|
include Curses
|
|
|
|
|
|
|
|
#
|
|
|
|
# main
|
|
|
|
#
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
init_screen
|
|
|
|
#keypad(stdscr, TRUE)
|
|
|
|
nonl
|
|
|
|
cbreak
|
|
|
|
noecho
|
|
|
|
#scrollok(stdscr, TRUE)
|
|
|
|
|
|
|
|
# slurp the file
|
|
|
|
data_lines = []
|
|
|
|
fp.each_line { |l|
|
|
|
|
data_lines.push(l)
|
|
|
|
}
|
|
|
|
fp.close
|
|
|
|
|
|
|
|
|
|
|
|
lptr = 0
|
|
|
|
while TRUE
|
|
|
|
i = 0
|
|
|
|
while i < lines
|
|
|
|
setpos(i, 0)
|
|
|
|
#clrtoeol
|
2012-11-07 10:36:14 -05:00
|
|
|
addstr(data_lines[lptr + i] || '')
|
1998-01-16 07:13:05 -05:00
|
|
|
i += 1
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
refresh
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
explicit = FALSE
|
|
|
|
n = 0
|
|
|
|
while TRUE
|
2008-11-07 16:42:24 -05:00
|
|
|
c = getch
|
2005-08-27 07:21:30 -04:00
|
|
|
if c =~ /[0-9]/
|
1998-01-16 07:13:05 -05:00
|
|
|
n = 10 * n + c.to_i
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
n = 1 if !explicit && n == 0
|
|
|
|
|
|
|
|
case c
|
|
|
|
when "n" #when KEY_DOWN
|
|
|
|
i = 0
|
|
|
|
while i < n
|
|
|
|
if lptr + lines < data_lines.size then
|
|
|
|
lptr += 1
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
i += 1
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
#wscrl(i)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
when "p" #when KEY_UP
|
|
|
|
i = 0
|
|
|
|
while i < n
|
|
|
|
if lptr > 0 then
|
|
|
|
lptr -= 1
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
i += 1
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
#wscrl(-i)
|
|
|
|
|
|
|
|
when "q"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
close_screen
|