-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move shell methods to a general helper
The helpers that interact with the user will need to be called from outside of the `Cli` namespace in the future. I need classes loaded before the `Cli` classes to be able to require a module like the `Shell` one to access those methods. I've added `module_function` to the module. This makes it possible to call the methods on the module directly, without having to include the module. ``` Shell.ask_for_input Shell.required_input "Your name?" Shell.yes_or_no "Cake?" ```
- Loading branch information
Showing
4 changed files
with
40 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
module Mono | ||
module Cli | ||
class Init | ||
include Helpers | ||
include Shell | ||
|
||
def execute | ||
config = {} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mono | ||
module Shell | ||
module_function | ||
|
||
def ask_for_input | ||
value = $stdin.gets | ||
value ? value.chomp : "" | ||
rescue Interrupt | ||
puts "\nExiting..." | ||
exit 1 | ||
end | ||
|
||
def required_input(prompt) | ||
loop do | ||
print prompt | ||
value = ask_for_input | ||
return value unless value.empty? | ||
end | ||
end | ||
|
||
def yes_or_no(prompt, options = {}) | ||
loop do | ||
print prompt | ||
input = ask_for_input.strip | ||
input = options[:default] if input.empty? && options[:default] | ||
case input | ||
when "y", "Y", "yes" | ||
return true | ||
when "n", "N", "no" | ||
return false | ||
end | ||
end | ||
end | ||
end | ||
end |