Sha256: ed7453ef38c62ded08d307f13f8fa2de34e34079c486527aaa6eb9ecb9307fe6

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

#!/usr/bin/env ruby

lib = File.expand_path('../lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'git/switcher'

def putm(message, color = :to_s)
  return if message.strip.empty?

  puts '', Rainbow(message).send(color)
  print Rainbow('Press ⏎ to continue ... ').italic
  $stdin.gets
end

repo = Rugged::Repository.new(Dir.pwd)
menu = Git::Switcher::Menu.for(repo)

# (optional) script to run after every checkout
switcher = File.join(repo.workdir, '.switcher')

run_switcher = true

loop do
  print "\e[H\e[2J", menu, "\n", "\n" # clear the screen first (^L)

  if run_switcher && File.executable?(switcher)
    print Rainbow('Switcher message:').italic.underline, "\n"

    cmd_msg = `2>&1 #{switcher}`
    putm(cmd_msg, :green)

    run_switcher = false
    next # re-start loop
  end

  if (default_menu_item = menu.default_menu_item)
    default_shortcut = format('⏎ [%2s]', default_menu_item.shortcut)
    print Rainbow(default_shortcut).bold.steelblue, ' '
  end

  print "\t", Rainbow('your selection =>').steelblue, ' '

  response = begin
               $stdin.gets&.strip
             rescue Interrupt
               nil
             end

  break unless response # ^C and ^D

  menu_item = if response.empty?
                default_menu_item
              else
                menu[response]
              end

  next unless menu_item

  err_msg = `2>&1 git checkout --quiet #{menu_item.reference.name}`

  if $CHILD_STATUS.success?
    run_switcher = true
  else
    putm(err_msg, :red)
  end
end

# That's all Folks!

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git-switcher-1.1.0 exe/git-switcher
git-switcher-1.0.7 exe/git-switcher