summaryrefslogtreecommitdiff
path: root/sample/view.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/view.rb')
-rwxr-xr-xsample/view.rb61
1 files changed, 23 insertions, 38 deletions
diff --git a/sample/view.rb b/sample/view.rb
index 300b3b9..66e861a 100755
--- a/sample/view.rb
+++ b/sample/view.rb
@@ -3,54 +3,41 @@
require "curses"
include Curses
-#
-# main
-#
-
-if ARGV.size != 1 then
- printf("usage: view file\n");
+unless ARGV.size == 1
+ puts "usage: #{$0} file"
exit
end
+
begin
- fp = open(ARGV[0], "r")
+ data_lines = File.readlines(ARGV[0])
rescue
- raise "cannot open file: #{ARGV[1]}"
+ raise "cannot open file: #{ARGV[0]}"
end
-# signal(SIGINT, finish)
-
init_screen
-#keypad(stdscr, TRUE)
+#keypad(stdscr, true)
nonl
cbreak
noecho
-#scrollok(stdscr, TRUE)
-
-# slurp the file
-data_lines = []
-fp.each_line { |l|
- data_lines.push(l)
-}
-fp.close
-
+#scrollok(stdscr, true)
lptr = 0
-while TRUE
- i = 0
- while i < lines
+loop do
+ lines.times do |i|
setpos(i, 0)
#clrtoeol
- addstr(data_lines[lptr + i] || '')
- i += 1
+ addstr(data_lines[lptr + i] || "")
end
refresh
- explicit = FALSE
+ explicit = false
n = 0
- while TRUE
+ c = nil
+ loop do
c = getch
if c =~ /[0-9]/
n = 10 * n + c.to_i
+ explicit = true
else
break
end
@@ -61,31 +48,29 @@ while TRUE
case c
when "n" #when KEY_DOWN
i = 0
- while i < n
- if lptr + lines < data_lines.size then
- lptr += 1
+ n.times do
+ if lptr + lines < data_lines.size
+ lptr += 1
else
- break
+ break
end
i += 1
end
#wscrl(i)
-
when "p" #when KEY_UP
i = 0
- while i < n
- if lptr > 0 then
- lptr -= 1
+ n.times do
+ if lptr > 0
+ lptr -= 1
else
- break
+ break
end
i += 1
end
#wscrl(-i)
-
when "q"
break
end
-
end
+
close_screen