Class: GitSpelunk::UI
- Inherits:
-
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]
= .new()
.data = @file_context.get_blame
@repo = RepoWindow.new(@repo_height, )
@status = StatusWindow.new(1, Curses.lines - 1)
set_status_message
end
|
Instance Method Details
#after_navigation ⇒ Object
81
82
83
84
85
|
# File 'lib/git_spelunk/ui.rb', line 81
def after_navigation
.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
= Curses.lines - @repo_height - 1
@status_height = 1
end
|
#getline ⇒ Object
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'
.cursordown
after_navigation
when Curses::KEY_UP, '-', 'k'
.cursorup
after_navigation
when Curses::KEY_CTRL_D, ' '
.pagedown
after_navigation
when Curses::KEY_CTRL_U
.pageup
after_navigation
when *(0..9).to_a.map(&:to_s)
@status.command_buffer += key
when Curses::KEY_CTRL_M
if @status.command_buffer != ''
.go_to(@status.command_buffer.to_i)
end
after_navigation
when 'G'
if @status.command_buffer != ''
.go_to(@status.command_buffer.to_i)
else
.go_bottom
end
after_navigation
when '['
history_back
when ']'
history_forward
when 's'
@heartbeat = nil
sha = @file_context.sha_for_line(.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
.search(line, false, key == '?')
end
@status.exit_command_mode!
when 'n'
.search(nil, true, false)
after_navigation
when 'N'
.search(nil, true, true)
after_navigation
when 'q', Curses::KEY_CTRL_C
exit
end
end
|
#heartbeat_expired? ⇒ 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_back ⇒ Object
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(.cursor)
if goto.is_a?(Fixnum)
@file_context.line_number = .cursor
@history.push(@file_context)
@file_context = @file_context.clone_for_parent_sha(.cursor)
.data = @file_context.get_blame
.go_to(goto)
@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_forward ⇒ Object
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
.data = @file_context.get_blame
.go_to(@file_context.line_number)
@status.clear_onetime_message!
set_status_message
@last_line = nil
end
end
|
#init_curses ⇒ Object
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_thread ⇒ Object
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 != .cursor
current_line = .cursor
content = @file_context.get_line_commit_info(current_line)
if heartbeat_expired? && .cursor == current_line
@repo.content = content
@repo.draw
@last_line = current_line
else
@heartbeat = Time.now
end
end
sleep 0.05
end
end
end
|
#run ⇒ Object
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(.cursor)
pause_thread
begin
[, @repo, @status].each(&:draw)
handle_key(Curses.getch)
end while true
end
|
#set_status_message ⇒ Object
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
|