Class: FileViewer

Inherits:
Object
  • Object
show all
Defined in:
view2.rb

Overview

A curses based file viewer

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileViewer

Create a new fileviewer, and view the file.



10
11
12
13
14
15
16
17
# File 'view2.rb', line 10

def initialize(filename)
  @data_lines = []
  @screen = nil
  @top = nil
  init_curses
  load_file(filename)
  interact
end

Instance Method Details

#init_cursesObject

Perform the curses setup



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'view2.rb', line 20

def init_curses
  # signal(SIGINT, finish)

  Curses.init_screen
  Curses.nonl
  Curses.cbreak
  Curses.noecho

  @screen = Curses.stdscr

  @screen.scrollok(true)
  #$screen.keypad(true)
end

#interactObject

Allow the user to interact with the display. This uses EMACS-like keybindings, and also vi-like keybindings as well, except that left and right move to the beginning and end of the file, respectively.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'view2.rb', line 92

def interact
  while true
    result = true
    c = Curses.getch
    case c
    when Curses::KEY_DOWN, Curses::KEY_CTRL_N, ?j
      result = scroll_down
    when Curses::KEY_UP, Curses::KEY_CTRL_P, ?k
      result = scroll_up
    when Curses::KEY_NPAGE, ?\s  # 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, ?h
      while( scroll_up )
      end
    when Curses::KEY_RIGHT, Curses::KEY_CTRL_B, ?l
      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
end

#load_file(filename) ⇒ Object

Load the file into memory, and put the first part on the curses display.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'view2.rb', line 36

def load_file(filename)
  fp = open(filename, "r") do |fp|
  # slurp the file
  fp.each_line { |l|
    @data_lines.push(l.chop)
  }
  end
  @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
rescue
  raise "cannot open file '#{filename}' for reading"
end

#scroll_downObject

Scroll the display down by one line



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'view2.rb', line 72

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

#scroll_upObject

Scroll the display up by one line



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'view2.rb', line 56

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