File: view.rb

package info (click to toggle)
ruby-curses 1.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: ansic: 3,701; ruby: 774; makefile: 3
file content (76 lines) | stat: -rwxr-xr-x 1,002 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env ruby

require "curses"
include Curses

unless ARGV.size == 1
  puts "usage: #{$0} file"
  exit
end

begin
  data_lines = File.readlines(ARGV[0])
rescue
  raise "cannot open file: #{ARGV[0]}"
end

init_screen
#keypad(stdscr, true)
nonl
cbreak
noecho
#scrollok(stdscr, true)

lptr = 0
loop do
  lines.times do |i|
    setpos(i, 0)
    #clrtoeol
    addstr(data_lines[lptr + i] || "")
  end
  refresh

  explicit = false
  n = 0
  c = nil
  loop do
    c = getch
    if c =~ /[0-9]/
      n = 10 * n + c.to_i
      explicit = true
    else
      break
    end
  end

  n = 1 if !explicit && n == 0

  case c
  when "n"  #when KEY_DOWN
    i = 0
    n.times do
      if lptr + lines < data_lines.size
        lptr += 1
      else
        break
      end
      i += 1
    end
    #wscrl(i)
  when "p"  #when KEY_UP
    i = 0
    n.times do
      if lptr > 0
        lptr -= 1
      else
        break
      end
      i += 1
    end
    #wscrl(-i)
  when "q"
    break
  end
end

close_screen