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

Defined Under Namespace

Classes: PagerWindow, RepoWindow, StatusWindow

Instance Method Summary collapse

Constructor Details

#initialize(file_context) ⇒ UI

Returns a new instance of UI.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/git_spelunk/ui.rb', line 7

def initialize(file_context)
  Dispel::Screen.open(:colors => true) do |screen|
    @file_context = file_context
    @history = [file_context]

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

    @repo = RepoWindow.new(0)
    set_repo_content

    @status = StatusWindow.new
    set_status_message

    calculate_heights!(screen)
    screen.draw *draw
    Dispel::Keyboard.output(timeout: 0.5) do |key|
      handle_key(key)
      calculate_heights!(screen)
      screen.draw *draw
    end
  end
end

Instance Method Details

#after_navigationObject



73
74
75
76
77
78
# File 'lib/git_spelunk/ui.rb', line 73

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

#calculate_heights!(screen) ⇒ Object



49
50
51
52
53
# File 'lib/git_spelunk/ui.rb', line 49

def calculate_heights!(screen)
  status_height = 1
  @repo.height = [(screen.lines.to_f * 0.20).to_i, 6].max
  @pager.height = screen.lines  - @repo.height - status_height
end

#drawObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/git_spelunk/ui.rb', line 31

def draw
  view1, style1 = @pager.draw
  view2, style2 = @repo.draw
  view3, style3 = @status.draw

  cursor = if typing?
    [@pager.height + @repo.height, @status.command_buffer.size + 1]
  else
    [Curses.lines-1, Curses.cols]
  end

  [
    [view1, view2, view3].join("\n"),
    style1 + style2 + style3,
    cursor
  ]
end

#execute_gotoObject



192
193
194
195
196
197
198
199
# File 'lib/git_spelunk/ui.rb', line 192

def execute_goto
  if @status.command_buffer != ''
    @pager.go_to(@status.command_buffer.to_i)
  else
    @pager.go_bottom
  end
  after_navigation
end

#handle_key(key) ⇒ Object



116
117
118
119
120
121
122
123
124
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
184
185
186
# File 'lib/git_spelunk/ui.rb', line 116

def handle_key(key)
  case key
  when :"Ctrl+d", :page_down
    @pager.pagedown
    after_navigation
  when :"Ctrl+u", :page_up
    @pager.pageup
    after_navigation
  when :"Ctrl+c"
    exit
  when :escape
    @pager.search_term = nil
    @status.exit_command_mode!
    @typing = false
  else
    if typing?
      case key
      when String
        if key == "G" && @typing == :goto
          execute_goto
        else
          @status.command_buffer << key
        end
      when :backspace then @status.command_buffer[-1..-1] = ""
      when :enter
        if @typing == :search
          typed = @status.command_buffer
          @pager.search(typed[1..-1], false, typed[0] == '?')
        elsif @typing == :goto
          execute_goto
        end
        @typing = false
        @status.command_buffer = ""
      end
    else
      case key
      when :down, 'j'
        @pager.cursordown
        after_navigation
      when :up, '-', 'k'
        @pager.cursorup
        after_navigation
      when *(0..9).to_a.map(&:to_s)
        @status.command_buffer = key
        @typing = :goto
      when '['
        history_back
      when ']'
        history_forward
      when 's'
        sha = @pager.blame_line.sha
        Curses.close_screen
        system("git -p --git-dir='#{@file_context.repo.path}' show #{sha} | less")
      when '/', '?'
        @status.command_buffer = key
        @typing = :search
      when 'n'
        @pager.search(nil, true, false)
        after_navigation
      when 'N'
        @pager.search(nil, true, true)
        after_navigation
      when ' '
        @pager.pagedown
        after_navigation
      when 'q'
        exit
      end
    end
  end
end

#history_backObject



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

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


    @file_context = @file_context.clone_for_blame_line(@pager.blame_line)
    @pager.data = @file_context.get_blame
    @pager.go_to(goto)

    set_repo_content
    @status.clear_onetime_message!
    set_status_message
  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



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

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

    @status.clear_onetime_message!
    set_status_message
  end
end

#set_repo_contentObject



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

def set_repo_content
  info = @file_context.get_line_commit_info(@pager.blame_line)
  content = <<-EOL
commit: #{info[:commit]}
Author: #{info[:author]}
Date: #{info[:date]}

  EOL
  message_lines = info[:message].split(/\r?\n/)
  content += message_lines[0..(@repo.height - 6)].join("\n")
  @repo.content = content
  @repo.draw
end

#set_status_messageObject



55
56
57
# File 'lib/git_spelunk/ui.rb', line 55

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

#typing?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/git_spelunk/ui.rb', line 188

def typing?
  @status.command_buffer.size > 0
end