Sha256: 7619a3208f37b81cd365fa13465d4bff19f2f3aca7de6fb77f2cd9d2fe8544e2

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'git'
require 'tty-prompt'
require 'logger'
require_relative './treeish_extractor'
require_relative './monkey_patches/tty/prompt.rb'

class GitPrompt
  SELECT_OPTIONS_PER_PAGE = 10

  attr_reader :git, :extractor, :prompt, :logger

  def initialize(git: nil)
    @git = git || Git.open(Dir.pwd)
    @extractor = TreeishExtractor.new(git: git)

    create_prompt
    define_key_events
    display_select(:branch, extractor.recent_branch_names)
  end

  private

  def define_key_events
    prompt.on(:keypress) do |event|
      if event.value == 'q'
        exit
      end

      # move up/down in the list

      if event.value == 'j'
        prompt.trigger(:keydown)
      end

      if event.value == 'k'
        prompt.trigger(:keyup)
      end

      # Select a mode

      if event.value == 'b'
        prompt.clear!
        display_select(:branch, extractor.recent_branch_names)
      end

      if event.value == 't'
        prompt.clear!
        display_select(:tag, extractor.recent_tag_names)
      end
    end
  end

  def create_prompt
    @prompt = TTY::Prompt.new(quiet: false)
  end

  def display_select(treeish_type, treeishes)
    message = prompt.decorate("Select #{treeish_type.to_s.capitalize} > ", :green)
    message += <<~MSG.chomp
      [b] branch view [t] tag view
      j: down, k: up, q: quit, Enter: choose tag
    MSG

    if treeishes.size > 0
      prompt.select(message, filter: false, per_page: SELECT_OPTIONS_PER_PAGE) do |menu|
        treeishes.each do |treeish|
          menu.choice(treeish[:name], -> { checkout(treeish[:value]) })
        end
      end
    else
      prompt.ok 'No tags were found'
    end
  rescue TTY::Reader::InputInterrupt => e
    # exit automatically
  end

  def checkout(treeish_name)
    git.checkout(treeish_name)
    exit
  rescue Git::GitExecuteError => e
    prompt.error(e.message)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gnav-0.4.0 lib/git_prompt.rb
gnav-0.3.2 lib/git_prompt.rb
gnav-0.3.1 lib/git_prompt.rb
gnav-0.3.0 lib/git_prompt.rb