#!/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!