Skip to content

Instantly share code, notes, and snippets.

@wojtha
Created August 1, 2016 14:55
Show Gist options
  • Select an option

  • Save wojtha/3d4fc9133b9e7b6f87a68243fc72a1c0 to your computer and use it in GitHub Desktop.

Select an option

Save wojtha/3d4fc9133b9e7b6f87a68243fc72a1c0 to your computer and use it in GitHub Desktop.
Ruby CLI menu prompts
require 'highline'
cli = HighLine.new
cli.say "Cannot pull because Git repository is dirty"
cli.choose do |menu|
menu.prompt = "WHAT TO DO?"
menu.choice(:reset) { sh 'git reset --hard' }
menu.choice(:stash) { sh 'git stash' }
menu.choice(:quit, "Exit program") { abort 'Quiting. Nothing to do.' }
menu.default = :quit
end
end
# Cannot pull because Git repository is dirty. WHAT TO DO?
# 1. reset
# 2. stash
# 3. quit
reset = nil
loop do
puts "\e[33mCannot pull because Git repository is dirty. WHAT TO DO?\n [r]eset\n [s]tash\n [q]uit\e[0m"
reset = STDIN.gets.chomp
break if %w[r reset s stash q quit].include? make_reset
end
case reset
when 'r', 'reset'
sh 'git reset --hard'
when 's', 'stash'
sh 'git stash'
else
abort 'Quiting. Nothing to do.'
end
# Cannot pull because Git repository is dirty. WHAT TO DO?
# [r]eset
# [s]tash
# [q]uit
require 'tty-prompt'
prompt = TTY::Prompt.new
prompt.warn "Cannot pull because Git repository is dirty"
reset = prompt.select("What to do?") do |menu|
menu.choice 'Reset (changes will be lost)', 1
menu.choice 'Stash (changes will be saved as git stash)', 2
menu.choice 'Quit', 3
menu.default 3
end
case reset
when 1
sh 'git reset --hard'
when 2
sh 'git stash'
else
abort 'Quiting. Nothing to do.'
end
# Cannot pull because Git repository is dirty.
#
# What to do?
# Reset (changes will be lost)
# Stash (changes will be saved as git stash)
# > Quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment