DEFINITIONS
This source file includes following functions.
1 #!/usr/local/bin/ruby
2
3 require "curses"
4 include Curses
5
6 #
7 # main
8 #
9
10 if ARGV.size != 1 then
11 printf("usage: view file\n");
12 exit
13 end
14 begin
15 fp = open(ARGV[0], "r")
16 rescue
17 raise "cannot open file: #{ARGV[1]}"
18 end
19
20 # signal(SIGINT, finish)
21
22 init_screen
23 #keypad(stdscr, TRUE)
24 nonl
25 cbreak
26 noecho
27 #scrollok(stdscr, TRUE)
28
29 # slurp the file
30 data_lines = []
31 fp.each_line { |l|
32 data_lines.push(l)
33 }
34 fp.close
35
36
37 lptr = 0
38 while TRUE
39 i = 0
40 while i < lines
41 setpos(i, 0)
42 #clrtoeol
43 addstr(data_lines[lptr + i]) #if data_lines[lptr + i]
44 i += 1
45 end
46 refresh
47
48 explicit = FALSE
49 n = 0
50 while TRUE
51 c = getch.chr
52 if c =~ "[0-9]" then
53 n = 10 * n + c.to_i
54 else
55 break
56 end
57 end
58
59 n = 1 if !explicit && n == 0
60
61 case c
62 when "n" #when KEY_DOWN
63 i = 0
64 while i < n
65 if lptr + lines < data_lines.size then
66 lptr += 1
67 else
68 break
69 end
70 i += 1
71 end
72 #wscrl(i)
73
74 when "p" #when KEY_UP
75 i = 0
76 while i < n
77 if lptr > 0 then
78 lptr -= 1
79 else
80 break
81 end
82 i += 1
83 end
84 #wscrl(-i)
85
86 when "q"
87 break
88 end
89
90 end
91 close_screen