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



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/git_spelunk/ui/pager.rb', line 151

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

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

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

#bufbottomObject



90
91
92
# File 'lib/git_spelunk/ui/pager.rb', line 90

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

#cursordownObject



100
101
102
103
104
# File 'lib/git_spelunk/ui/pager.rb', line 100

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

#cursorupObject



94
95
96
97
98
# File 'lib/git_spelunk/ui/pager.rb', line 94

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

#find_next_index(term, start, reverse) ⇒ Object

returns position in data set, not cursor position



61
62
63
64
65
66
67
68
69
70
# File 'lib/git_spelunk/ui/pager.rb', line 61

def find_next_index(term, start, reverse)
  i = start
  while i < data.size && i >= 0
    if data[i][1] =~ /#{term}/
      return i
    end
    i += reverse ? -1 : 1
  end
  nil
end

#go_bottomObject



146
147
148
149
# File 'lib/git_spelunk/ui/pager.rb', line 146

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

#go_to(l) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/git_spelunk/ui/pager.rb', line 132

def go_to(l)
  if l > @data.size
    l = @data.size
  elsif l < 1
    l = 1
  end

  previous_offset = @cursor - @top
  @cursor = l
  @top = @cursor - previous_offset
  adjust_top!
end

#go_topObject



128
129
130
# File 'lib/git_spelunk/ui/pager.rb', line 128

def go_top
  @top = @cursor = 1
end

#pagedownObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/git_spelunk/ui/pager.rb', line 117

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



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

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, reverse) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/git_spelunk/ui/pager.rb', line 72

def search(term, skip_current_line, reverse)
  if term
    @search_term = term
  else
    term = @search_term # nil indicates 'use-last-term'
  end

  search_from = @cursor - 1
  if skip_current_line
    search_from += reverse ? -1 : 1
  end

  p = find_next_index(term, search_from, reverse) ||
        find_next_index(term, reverse ? data.size - 1 : 0, reverse)

  go_to(p + 1) if p
end