0
votes

I'm writing a simple mac os (10.11) command line app in Swift to download a file from a valid remote URL and I'd like to show the % of file downloaded in place on the console.

It would start with:

0% of 10Mb file downloaded

Then when I have some percentage of the file downloaded, the above line would get replaced with:

18% of 10Mb file downloaded

Finally, when it finishes, the string gets replaced in line with:

100% of 10Mb file downloaded

print(..) will keep append to existing text - is there another function that will do what I need?

2

2 Answers

0
votes

If you leave the cursor at the end of the line (i.e, by not outputting a newline '\n' at the end), you can return to the start of the line by outputting a carriage return ('\r'). Further output will overwrite what is already on the line.

I'm not super familiar with Swift, but I suspect you may need to explicitly flush output to get a partial line to show up. If you were using standard C I/O, this would be fflush(stdout); I'm not sure what the Swift equivalent is, but I'm sure it exists.

0
votes

Add

\u{1B}[1A\u{1B}[k

to your string. for example:

print("\u{1B}[1A\u{1B}[kDownloading \(myProgress)%")