@@ -136,6 +136,21 @@ def byte(bitstr, index):
136
136
basestring = str
137
137
138
138
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
+
139
154
def _mask_to_shift (mask ):
140
155
""" Return the index of the least significant bit in the mask """
141
156
shift = 0
@@ -2369,10 +2384,10 @@ def dump_mem(esp, args):
2369
2384
d = esp .read_reg (args .address + (i * 4 ))
2370
2385
f .write (struct .pack (b'<I' , d ))
2371
2386
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 ))
2375
2389
sys .stdout .flush ()
2390
+ print_overwrite ("Read %d bytes" % f .tell (), last_line = True )
2376
2391
print ('Done!' )
2377
2392
2378
2393
@@ -2507,7 +2522,7 @@ def write_flash(esp, args):
2507
2522
written = 0
2508
2523
t = time .time ()
2509
2524
while len (image ) > 0 :
2510
- print ( ' \r Writing 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 ))
2511
2526
sys .stdout .flush ()
2512
2527
block = image [0 :esp .FLASH_WRITE_SIZE ]
2513
2528
if args .compress :
@@ -2527,11 +2542,11 @@ def write_flash(esp, args):
2527
2542
if args .compress :
2528
2543
if t > 0.0 :
2529
2544
speed_msg = " (effective %.1f kbit/s)" % (uncsize / t * 8 / 1000 )
2530
- print ( ' \r Wrote %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 )
2531
2546
else :
2532
2547
if t > 0.0 :
2533
2548
speed_msg = " (%.1f kbit/s)" % (written / t * 8 / 1000 )
2534
- print ( ' \r Wrote %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 )
2535
2550
2536
2551
if not args .encrypt :
2537
2552
try :
@@ -2694,8 +2709,8 @@ def flash_progress(progress, length):
2694
2709
t = time .time ()
2695
2710
data = esp .read_flash (args .address , args .size , flash_progress )
2696
2711
t = time .time () - t
2697
- print ( ' \r Read %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 )
2699
2714
with open (args .filename , 'wb' ) as f :
2700
2715
f .write (data )
2701
2716
0 commit comments