Class: GitSpelunk::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/git_spelunk/ui.rb,
lib/git_spelunk/ui/repo.rb,
lib/git_spelunk/ui/pager.rb,
lib/git_spelunk/ui/status.rb,
lib/git_spelunk/ui/window.rb

Defined Under Namespace

Classes: PagerWindow, RepoWindow, StatusWindow, Window

Instance Method Summary collapse

Constructor Details

#initialize(file_context) ⇒ UI

Returns a new instance of UI.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/git_spelunk/ui.rb', line 9

def initialize(file_context)
  init_curses

  calculate_heights!
  @file_context = file_context
  @history = [file_context]

  @pager = PagerWindow.new(@pager_height)
  @pager.data = @file_context.get_blame

  @repo = RepoWindow.new(@repo_height, @pager_height)

  @status = StatusWindow.new(1, Curses.lines - 1)
  set_status_message
end

Instance Method Details

#after_navigationObject



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

def after_navigation
  @pager.highlight_sha = true
  @status.exit_command_mode!
  @status.clear_onetime_message!
end

#calculate_heights!Object



38
39
40
41
42
# File 'lib/git_spelunk/ui.rb', line 38

def calculate_heights!
  @repo_height = [(Curses.lines.to_f * 0.20).to_i, 6].max
  @pager_height = Curses.lines  - @repo_height - 1
  @status_height = 1
end

#getlineObject

you’d really think there was a better way



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/git_spelunk/ui.rb', line 186

def getline
  while ch = Curses.getch
    case ch
    when Curses::KEY_CTRL_C
      @status.command_buffer = ''
      return
    when Curses::KEY_CTRL_M
      return @status.command_buffer[1..-1]
    when Curses::KEY_BACKSPACE, Curses::KEY_CTRL_H, 127
      if @status.command_buffer == "/"
        return
      end
      @status.command_buffer.chop!
    else
      if ch.is_a?(String)
        @status.command_buffer += ch
      end
    end
    @status.draw
  end
end

#handle_key(key) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/git_spelunk/ui.rb', line 125

def handle_key(key)
  @heartbeat = Time.now
  case key
  when Curses::KEY_DOWN, 'j'
    @pager.cursordown
    after_navigation
  when Curses::KEY_UP, '-', 'k'
    @pager.cursorup
    after_navigation
  when Curses::KEY_CTRL_D, ' '
    @pager.pagedown
    after_navigation
  when Curses::KEY_CTRL_U
    @pager.pageup
    after_navigation
  when *(0..9).to_a.map(&:to_s)
    @status.command_buffer += key
  when Curses::KEY_CTRL_M
    if @status.command_buffer != ''
      @pager.go_to(@status.command_buffer.to_i)
    end
    after_navigation
  when 'G'
    if @status.command_buffer != ''
      @pager.go_to(@status.command_buffer.to_i)
    else
      @pager.go_bottom
    end
    after_navigation
  when '['
    history_back
  when ']'
    history_forward
  when 's'
    @heartbeat = nil
    sha = @file_context.sha_for_line(@pager.cursor)
    Curses.close_screen
    system("git -p --git-dir='#{@file_context.repo.path}' show #{sha} | less")
    Curses.stdscr.refresh
  when '/', '?'
    @heartbeat = nil
    @status.command_buffer = key
    @status.draw

    line = getline
    if line
      @pager.search(line, false, key == '?')
    end
    @status.exit_command_mode!
  when 'n'
    @pager.search(nil, true, false)
    after_navigation
  when 'N'
    @pager.search(nil, true, true)
    after_navigation
  when 'q', Curses::KEY_CTRL_C
    exit
  end
end

#heartbeat_expired?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/git_spelunk/ui.rb', line 77

def heartbeat_expired?
  @heartbeat && (Time.now - @heartbeat).to_f > 0.30
end

#history_backObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/git_spelunk/ui.rb', line 87

def history_back
  @status.set_onetime_message("Rewinding...")
  goto = @file_context.get_line_for_sha_parent(@pager.cursor)
  if goto.is_a?(Fixnum)
    @file_context.line_number = @pager.cursor
    @history.push(@file_context)

    @file_context = @file_context.clone_for_parent_sha(@pager.cursor)
    @pager.data = @file_context.get_blame
    @pager.go_to(goto)

    # force commit info update
    @status.clear_onetime_message!
    set_status_message
    @last_line = nil
  elsif goto == :at_beginning_of_time
    @status.set_onetime_message("At beginning of repository history!")
  elsif goto == :unable_to_trace
    @status.set_onetime_message("Unable to trace lineage of file line")
  elsif goto == :first_commit_for_file
    @status.set_onetime_message("At first appearance of file")
  end
end

#history_forwardObject



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

def history_forward
  if @history.last
    @file_context = @history.pop
    @pager.data = @file_context.get_blame
    @pager.go_to(@file_context.line_number)

    @status.clear_onetime_message!
    set_status_message

    # force commit info update
    @last_line = nil
  end
end

#init_cursesObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/git_spelunk/ui.rb', line 25

def init_curses
  Curses.init_screen
  Curses.start_color
  Curses.raw
  Curses.nonl
  Curses.noecho
  Curses.curs_set(0)
  screen = Curses.stdscr
  screen.refresh
  screen.keypad(1)
  Curses.init_pair(ACTIVE_SHA_COLOR, Curses::COLOR_GREEN, Curses::COLOR_BLACK)
end

#pause_threadObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/git_spelunk/ui.rb', line 57

def pause_thread
  Thread.abort_on_exception = true
  Thread.new do
    while true
      if heartbeat_expired? && @last_line != @pager.cursor
        current_line = @pager.cursor
        content = @file_context.get_line_commit_info(current_line)
        if heartbeat_expired? && @pager.cursor == current_line
          @repo.content = content
          @repo.draw
          @last_line = current_line
        else
          @heartbeat = Time.now
        end
      end
      sleep 0.05
    end
  end
end

#runObject



44
45
46
47
48
49
50
51
# File 'lib/git_spelunk/ui.rb', line 44

def run
  @repo.content = @file_context.get_line_commit_info(@pager.cursor)
  pause_thread
  begin
    [@pager, @repo, @status].each(&:draw)
    handle_key(Curses.getch)
  end while true
end

#set_status_messageObject



53
54
55
# File 'lib/git_spelunk/ui.rb', line 53

def set_status_message
  @status.status_message = "#{@file_context.file} @ #{@file_context.sha}"
end