Skip to content

Commit bb03afd

Browse files
committed
skip "\n" from $stdin before showing prompt
Any input into `$stdin` before showing prompt will be appear on the next prompt, and entering new empty line will dispatch previous command. This patch consumes empty lines ("\n"+) from $stdin just before showing the prompt.
1 parent 43f80ff commit bb03afd

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/debug/console.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def readline_setup prompt
9797
end
9898

9999
def readline prompt
100+
clear_stdin
100101
readline_setup prompt do
101102
Reline.readmultiline(prompt, true){ true }
102103
end
@@ -132,6 +133,7 @@ def readline_setup
132133
end
133134

134135
def readline prompt
136+
clear_stdin
135137
readline_setup
136138
Readline.readline(prompt, true)
137139
end
@@ -142,6 +144,7 @@ def history
142144

143145
rescue LoadError
144146
def readline prompt
147+
clear_stdin
145148
print prompt
146149
$stdin.gets
147150
end
@@ -152,6 +155,22 @@ def history
152155
end
153156
end
154157

158+
def clear_stdin
159+
# consume all STDIN input just before prompt
160+
loop do
161+
case r = $stdin.read_nonblock(1, exception: false)
162+
when "\n"
163+
# drop \n
164+
next
165+
when nil, :wait_readable
166+
break
167+
else
168+
$stdin.ungetc r
169+
break
170+
end
171+
end
172+
end
173+
155174
def history_file
156175
case
157176
when (path = CONFIG[:history_file]) && !path.empty?

0 commit comments

Comments
 (0)