Class: GitSpelunk::UI::PagerWindow

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

Constant Summary collapse

ACTIVE_SHA_COLOR =
["#00ff00", "#000000"]
FOUND_COLOR =
:reverse
CURRENT_COLOR =
["#000000", "#00ff00"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height) ⇒ PagerWindow

Returns a new instance of PagerWindow.



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

def initialize(height)
  @height = height
  @cursor = 1
  @top = 1
  @highlight_sha = true
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



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

def cursor
  @cursor
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#highlight_shaObject

Returns the value of attribute highlight_sha.



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

def highlight_sha
  @highlight_sha
end

#search_termObject

Returns the value of attribute search_term.



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

def search_term
  @search_term
end

#topObject

Returns the value of attribute top.



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

def top
  @top
end

Instance Method Details

#adjust_top!Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/git_spelunk/ui/pager.rb', line 158

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

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

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

#blame_lineObject



19
20
21
# File 'lib/git_spelunk/ui/pager.rb', line 19

def blame_line
  @data[@cursor - 1]
end

#bufbottomObject



98
99
100
# File 'lib/git_spelunk/ui/pager.rb', line 98

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

#cursordownObject



108
109
110
111
112
# File 'lib/git_spelunk/ui/pager.rb', line 108

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

#cursorupObject



102
103
104
105
106
# File 'lib/git_spelunk/ui/pager.rb', line 102

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

#drawObject



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

def draw
  styles = Dispel::StyleMap.new(@height)

  line_number_width = (data.size + 1).to_s.size

  active_sha = blame_line.sha

  view = Array.new(@height)

  data[@top - 1, @height].each_with_index do |b, i|
    line = ""
    sha, content = b.sha, b.content

    line_number = i + @top

    if sha == active_sha && highlight_sha
      styles.add(ACTIVE_SHA_COLOR, i, 0...999)
    end

    sha_abbrev = sha[0..5]
    if @cursor == line_number
      styles.add(CURRENT_COLOR, i, 0..(sha_abbrev.size - 1))
    end

    line << sha_abbrev

    line << " %*s " % [line_number_width, line_number]
    line << content.gsub(/\r/, '')


    content_start = (sha_abbrev.size + line_number_width + 2)

    if @search_term
      Dispel::Tools.indexes(content, @search_term).each do |found|
        found = content_start + found
        styles.add(FOUND_COLOR, i, found...(found + @search_term.size))
      end
    end
    view[i] = line
  end
  [view, styles]
end

#find_next_index(term, start, reverse) ⇒ Object

returns position in data set, not cursor position



69
70
71
72
73
74
75
76
77
78
# File 'lib/git_spelunk/ui/pager.rb', line 69

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

#go_bottomObject



153
154
155
156
# File 'lib/git_spelunk/ui/pager.rb', line 153

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

#go_to(l) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/git_spelunk/ui/pager.rb', line 140

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



136
137
138
# File 'lib/git_spelunk/ui/pager.rb', line 136

def go_top
  @top = @cursor = 1
end

#pagedownObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/git_spelunk/ui/pager.rb', line 125

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



114
115
116
117
118
119
120
121
122
123
# File 'lib/git_spelunk/ui/pager.rb', line 114

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/git_spelunk/ui/pager.rb', line 80

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