Skip to content

Commit 84bf56b

Browse files
committed
Turn Interaction#display into a dedicated in-place prompt method and use it thru-out
1 parent 19f3ff7 commit 84bf56b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lib/remedy/interaction.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ def initialize message = nil
1313
# A simple way to ask a user to confirm an action.
1414
# @return [Boolean] `true` if the use presses `y`, otherwise `false`
1515
def confirm message = 'Confirm?'
16-
ANSI.cursor.home!
17-
ANSI.command.clear_line!
18-
19-
print message, ' y/n '
16+
display message, ' y/n '
2017
if Keyboard.get === :y then
2118
yield if block_given?
2219
true
@@ -45,11 +42,25 @@ def quit!
4542
def debug!
4643
require 'pry'
4744
binding.pry
45+
rescue LoadError => ex
46+
warn 'Unable to load Pry!'
47+
warn ex.full_message(true)
4848
end
4949

50-
def display key
50+
# Display a response to the user.
51+
#
52+
# It displays the message *in place* (eg overwriting the current line) rather than on the next line.
53+
#
54+
# @return `nil`
55+
def display message
5156
ANSI.command.clear_line!
52-
print " -- You pressed: #{key.inspect}"
57+
print "#{message}"
58+
end
59+
60+
# Display a {Key}'s internals for debugging purpose.
61+
# @return `nil`
62+
def display_key key
63+
display " -- You pressed: #{key.inspect}"
5364
end
5465

5566
# A quick-and-dirty way to integrate {Remedy} and interactivity into your Ruby command line application.
@@ -99,30 +110,31 @@ def loop
99110
Keyboard.raise_on_control_c!
100111

101112
super do
102-
print " -- #{message}" if message
113+
display " -- #{message}" if message
103114

104115
ANSI.cursor.hide!
105116
key = Keyboard.get
106117

107118
if key == ?\C-q then
108-
display key
119+
display_key key
109120
quit!
110121
elsif key == ?\C-d and defined? Pry then
111-
display key
122+
display_key key
112123
debug!
113124
end
114125

115126
yield key
116127
end
117128
end
118129

119-
# @note If you called {initialize} with a `message` parameter, that same `message` will be displayed as a prompt.
130+
# @note If you called {initialize} with a `message` parameter, that same `message` will be displayed
131+
# to the user before it reads the user's key press.
120132
#
121133
# Get a single key press from the user.
122134
#
123135
# @return [Key] the key the user pressed
124136
def get_key
125-
print " -- #{message}" if message
137+
display " -- #{message}" if message
126138

127139
ANSI.cursor.hide!
128140
Keyboard.get

0 commit comments

Comments
 (0)