Skip to content

Commit 202009b

Browse files
David MaloneyDavid Maloney
authored andcommitted
Land rapid7#7570, async print fix
Land's jennamagius' fix for async console printing outoupt from jobs no longers screws the console prompt up. w00t!
2 parents 8d1c718 + 739c9c1 commit 202009b

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/rex/ui/text/input/readline.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def reset_tab_completion(tab_complete_proc = nil)
4040
::Readline.completion_proc = tab_complete_proc || @rl_saved_proc
4141
end
4242

43+
44+
#
45+
# Retrieve the line buffer
46+
#
47+
def line_buffer
48+
if defined? RbReadline
49+
RbReadline.rl_line_buffer
50+
else
51+
::Readline.line_buffer
52+
end
53+
end
54+
55+
attr_accessor :prompt
56+
4357
#
4458
# Whether or not the input medium supports readline.
4559
#
@@ -124,12 +138,13 @@ def readline_with_output(prompt, add_history=false)
124138
# to reimplement []`Readline.readline`](https://github.com/luislavena/rb-readline/blob/ce4908dae45dbcae90a6e42e3710b8c3a1f2cd64/lib/readline.rb#L36-L58)
125139
# for rb-readline to support setting input and output. Output needs to be set so that colorization works for the
126140
# prompt on Windows.
141+
self.prompt = prompt
127142
if defined? RbReadline
128143
RbReadline.rl_instream = fd
129144
RbReadline.rl_outstream = output
130145

131146
begin
132-
line = RbReadline.readline(prompt)
147+
line = RbReadline.readline("\001\r\033[K\002" + prompt)
133148
rescue ::Exception => exception
134149
RbReadline.rl_cleanup_after_signal()
135150
RbReadline.rl_deprep_terminal()
@@ -143,7 +158,7 @@ def readline_with_output(prompt, add_history=false)
143158

144159
line.try(:dup)
145160
else
146-
::Readline.readline(prompt, true)
161+
::Readline.readline("\001\r\033[K\002" + prompt, true)
147162
end
148163
end
149164

lib/rex/ui/text/output.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def initialize
2929
super
3030
end
3131
attr_reader :config
32+
attr_accessor :input
3233

3334
def disable_color
3435
@config[:color] = false
@@ -60,7 +61,14 @@ def print_status(msg = '')
6061
end
6162

6263
def print_line(msg = '')
63-
print(msg + "\n")
64+
print("\033[s") # Save cursor position
65+
print("\r\033[K" + msg + "\n")
66+
if input and input.prompt
67+
print("\r\033[K")
68+
print(input.prompt)
69+
print(input.line_buffer)
70+
print("\033[u\033[B") # Restore cursor, move down one line
71+
end
6472
end
6573

6674
def print_warning(msg = '')

lib/rex/ui/text/shell.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ def run(&block)
184184
self.init_prompt = input.prompt
185185
end
186186

187+
output.input = input
187188
line = input.pgets()
189+
output.input = nil
188190
log_output(input.prompt)
189191

190192
# If a block was passed in, pass the line to it. If it returns true,

0 commit comments

Comments
 (0)