Created
August 1, 2016 14:55
-
-
Save wojtha/3d4fc9133b9e7b6f87a68243fc72a1c0 to your computer and use it in GitHub Desktop.
Ruby CLI menu prompts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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