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

def initialize(file_context)
  Dispel::Screen.open(:colors => true) do |screen|
    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)
    set_repo_content

    @status = StatusWindow.new
    set_status_message

    screen.draw *draw
    Dispel::Keyboard.output :timeout => 0.30 do |key|
      handle_key(key)
      screen.draw *draw
    end
  end
end

Instance Method Details

#after_navigationObject



63
64
65
66
67
68
# File 'lib/git_spelunk/ui.rb', line 63

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

#calculate_heights!Object



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

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

#drawObject



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

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



178
179
180
181
182
183
184
185
# File 'lib/git_spelunk/ui.rb', line 178

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



105
106
107
108
109
110
111
112
113
114
115
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
# File 'lib/git_spelunk/ui.rb', line 105

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 'q'
        exit
      end
    end
  end
end

#history_backObject



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

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



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/git_spelunk/ui.rb', line 93

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



58
59
60
61
# File 'lib/git_spelunk/ui.rb', line 58

def set_repo_content
  @repo.content = @file_context.get_line_commit_info(@pager.blame_line)
  @repo.draw
end

#set_status_messageObject



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

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

#typing?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/git_spelunk/ui.rb', line 174

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