Skip to content

Commit 028b672

Browse files
committed
Fix scrolling problems when output is redirected
Alternative implementation of the Arduino IDE fix contributed by @earlephilhower in #482
1 parent 9e5fd48 commit 028b672

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

esptool.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ def byte(bitstr, index):
136136
basestring = str
137137

138138

139+
def print_overwrite(message, last_line=False):
140+
""" Print a message, overwriting the currently printed line.
141+
142+
If last_line is False, don't append a newline at the end (expecting another subsequent call will overwrite this one.)
143+
144+
After a sequence of calls with last_line=False, call once with last_line=True.
145+
146+
If output is not a TTY (for example redirected a pipe), no overwriting happens and this function is the same as print().
147+
"""
148+
if sys.stdout.isatty():
149+
print("\r%s" % message, end='\n' if last_line else '')
150+
else:
151+
print(message)
152+
153+
139154
def _mask_to_shift(mask):
140155
""" Return the index of the least significant bit in the mask """
141156
shift = 0
@@ -2369,10 +2384,10 @@ def dump_mem(esp, args):
23692384
d = esp.read_reg(args.address + (i * 4))
23702385
f.write(struct.pack(b'<I', d))
23712386
if f.tell() % 1024 == 0:
2372-
print('\r%d bytes read... (%d %%)' % (f.tell(),
2373-
f.tell() * 100 // args.size),
2374-
end=' ')
2387+
print_overwrite('%d bytes read... (%d %%)' % (f.tell(),
2388+
f.tell() * 100 // args.size))
23752389
sys.stdout.flush()
2390+
print_overwrite("Read %d bytes" % f.tell(), last_line=True)
23762391
print('Done!')
23772392

23782393

@@ -2507,7 +2522,7 @@ def write_flash(esp, args):
25072522
written = 0
25082523
t = time.time()
25092524
while len(image) > 0:
2510-
print('\rWriting at 0x%08x... (%d %%)' % (address + seq * esp.FLASH_WRITE_SIZE, 100 * (seq + 1) // blocks), end='')
2525+
print_overwrite('Writing at 0x%08x... (%d %%)' % (address + seq * esp.FLASH_WRITE_SIZE, 100 * (seq + 1) // blocks))
25112526
sys.stdout.flush()
25122527
block = image[0:esp.FLASH_WRITE_SIZE]
25132528
if args.compress:
@@ -2527,11 +2542,11 @@ def write_flash(esp, args):
25272542
if args.compress:
25282543
if t > 0.0:
25292544
speed_msg = " (effective %.1f kbit/s)" % (uncsize / t * 8 / 1000)
2530-
print('\rWrote %d bytes (%d compressed) at 0x%08x in %.1f seconds%s...' % (uncsize, written, address, t, speed_msg))
2545+
print_overwrite('Wrote %d bytes (%d compressed) at 0x%08x in %.1f seconds%s...' % (uncsize, written, address, t, speed_msg), last_line=True)
25312546
else:
25322547
if t > 0.0:
25332548
speed_msg = " (%.1f kbit/s)" % (written / t * 8 / 1000)
2534-
print('\rWrote %d bytes at 0x%08x in %.1f seconds%s...' % (written, address, t, speed_msg))
2549+
print_overwrite('Wrote %d bytes at 0x%08x in %.1f seconds%s...' % (written, address, t, speed_msg), last_line=True)
25352550

25362551
if not args.encrypt:
25372552
try:
@@ -2694,8 +2709,8 @@ def flash_progress(progress, length):
26942709
t = time.time()
26952710
data = esp.read_flash(args.address, args.size, flash_progress)
26962711
t = time.time() - t
2697-
print('\rRead %d bytes at 0x%x in %.1f seconds (%.1f kbit/s)...'
2698-
% (len(data), args.address, t, len(data) / t * 8 / 1000))
2712+
print_overwrite('Read %d bytes at 0x%x in %.1f seconds (%.1f kbit/s)...'
2713+
% (len(data), args.address, t, len(data) / t * 8 / 1000), last_line=True)
26992714
with open(args.filename, 'wb') as f:
27002715
f.write(data)
27012716

0 commit comments

Comments
 (0)