Sha256: 1d211df19fac15dab3e8d0dfed9c22e41cf5def424eac245ad94413702070104

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 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(extractor.recent_branch_names)
  end

  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'
        clear_prompt
        display_select(extractor.recent_branch_names)
      end

      if event.value == 't'
        clear_prompt
        display_select(extractor.recent_tag_names)
      end
    end
  end

  private

  def clear_prompt
    prompt.clear_list
  end

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

  def display_select(treeish_names)
    message = <<~MSG.chomp
      j: down, k: up, q: quit, Enter: choose tag
      [b] branch mode [t] tag mode
    MSG

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git_selector-0.2.6 lib/git_prompt.rb
git_selector-0.2.5 lib/git_prompt.rb