Sha256: a27f5da0382373e5896abe114b63d914bed3df6be3a2d87a965f9e8bd45aaac1

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'git'
require 'tty/prompt/vim'
require 'logger'
require_relative './tag_extractor'

class Selector
  attr_reader :git, :extractor, :prompt, :logger

  def initialize(git: nil)
    @git = git || Git.open(Dir.pwd)
    @extractor = TagExtractor.new(git: git)
    @prompt = TTY::Prompt.new

    define_key_events
  end

  def select_tag
    message = 'j/↓: down, k/↑: up, Enter: choose tag, Type to filter tags'
    prompt.select(message, filter: true, per_page: 10) do |menu|
      extractor.recent_tag_names.each do |tag_name|
        menu.choice tag_name, -> { checkout(tag_name) }
      end
    end
  rescue TTY::Reader::InputInterrupt => e
    exit
  end

  def define_key_events
    prompt.on(:keypress) do |event|
      if event.value == "j"
        prompt.trigger(:keydown)
      end

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

      if event.value == "q"
        exit
      end
    end
  end

  private

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
git_selector-0.1.4 lib/selector.rb
git_selector-0.1.3 lib/selector.rb
git_selector-0.1.2 lib/selector.rb
git_selector-0.1.1 lib/selector.rb
git_selector-0.1.0 lib/selector.rb