Class: GitSpelunk::UI::PagerWindow

Inherits:
Window
  • Object
show all
Defined in:
lib/git_spelunk/ui/pager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Window

#line_remainder, #with_highlighting

Constructor Details

#initialize(height) ⇒ PagerWindow

Returns a new instance of PagerWindow.



7
8
9
10
11
12
13
# File 'lib/git_spelunk/ui/pager.rb', line 7

def initialize(height)
  @window = Curses::Window.new(height, Curses.cols, 0, 0)
  @height = height
  @cursor = 1
  @top = 1
  @highlight_sha = true
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



16
17
18
# File 'lib/git_spelunk/ui/pager.rb', line 16

def cursor
  @cursor
end

#dataObject

Returns the value of attribute data.



15
16
17
# File 'lib/git_spelunk/ui/pager.rb', line 15

def data
  @data
end

#highlight_shaObject

Returns the value of attribute highlight_sha.



15
16
17
# File 'lib/git_spelunk/ui/pager.rb', line 15

def highlight_sha
  @highlight_sha
end

#topObject

Returns the value of attribute top.



16
17
18
# File 'lib/git_spelunk/ui/pager.rb', line 16

def top
  @top
end

Instance Method Details

#adjust_top!Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/git_spelunk/ui/pager.rb', line 136

def adjust_top!
  if @top < 1
    @top = 1
  end

  if @top > @cursor
    @top = @cursor
  end

  while @cursor > bufbottom
    @top += 1
  end
end

#bufbottomObject



81
82
83
# File 'lib/git_spelunk/ui/pager.rb', line 81

def bufbottom
  @top + (@height - 1)
end

#cursordownObject



91
92
93
94
95
# File 'lib/git_spelunk/ui/pager.rb', line 91

def cursordown
  return if @cursor >= data.size
  @cursor += 1
  adjust_top!
end

#cursorupObject



85
86
87
88
89
# File 'lib/git_spelunk/ui/pager.rb', line 85

def cursorup
  return if @cursor == 1
  @cursor -= 1
  adjust_top!
end

#drawObject



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
# File 'lib/git_spelunk/ui/pager.rb', line 18

def draw
  @window.clear
  @window.setpos(0,0)
  line_number_width = (data.size + 1).to_s.size

  active_sha = data[@cursor - 1][0]

  data[@top - 1,@height].each_with_index do |b, i|
    sha, content = *b
    line_number = i + @top

    if sha == active_sha && highlight_sha
      @window.attron(Curses::color_pair(ACTIVE_SHA_COLOR))
    end

    if @cursor == line_number
      with_highlighting { @window.addstr(sha) }
    else
      @window.addstr(sha)
    end

    @window.addstr(" %*s " % [line_number_width, line_number])
    if @search_term
      content.split(/(#{@search_term})/).each do |t|
        if t == @search_term
          @window.attron(Curses::A_STANDOUT)
        end
        @window.addstr(t[0,line_remainder])
        @window.attroff(Curses::A_STANDOUT)
      end
    else
      @window.addstr(content[0,line_remainder])
    end
    @window.addstr("\n")
    @window.attroff(Curses::color_pair(ACTIVE_SHA_COLOR))
  end
  @window.refresh
  @window.setpos(0,0)
end

#go_bottomObject



131
132
133
134
# File 'lib/git_spelunk/ui/pager.rb', line 131

def go_bottom
  @cursor = data.size
  @top = data.size - (@height - 1)
end

#go_to(l) ⇒ Object



123
124
125
126
127
128
# File 'lib/git_spelunk/ui/pager.rb', line 123

def go_to(l)
  previous_offset = @cursor - @top
  @cursor = l
  @top = @cursor - previous_offset
  adjust_top!
end

#go_topObject



119
120
121
# File 'lib/git_spelunk/ui/pager.rb', line 119

def go_top
  @top = @cursor = 1
end

#pagedownObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/git_spelunk/ui/pager.rb', line 108

def pagedown
  previous_offset = @cursor - @top
  @cursor += @height / 2
  if @cursor > data.size
    @cursor = data.size
  end

  @top = @cursor - previous_offset
  adjust_top!
end

#pageupObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/git_spelunk/ui/pager.rb', line 97

def pageup
  previous_offset = @cursor - @top
  @cursor -= @height / 2
  if @cursor < 1
    @cursor = 1
  end

  @top = @cursor - previous_offset
  adjust_top!
end

#search(term, skip_current_line) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/git_spelunk/ui/pager.rb', line 60

def search(term, skip_current_line)
  @search_term = term
  return unless term
  save_cursor = @cursor
  search_data = data.map { |d| d[1] }
  initial_position = save_cursor - (skip_current_line ? 0 : 1)
  search_data[initial_position..-1].each_with_index do |d, i|
    if d =~ /#{term}/
      go_to(initial_position + i + 1)
      return
    end
  end

  search_data[0..initial_position].each_with_index do |d, i|
    if d =~ /#{term}/
      go_to(i + 1)
      return
    end
  end
end