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?
If you leave the cursor at the end of the line (ie, 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 CI/O, this would be fflush(stdout)
; I'm not sure what the Swift equivalent is, but I'm sure it exists.
Add
\u{1B}[1A\u{1B}[k
to your string. for example:
print("\u{1B}[1A\u{1B}[kDownloading \(myProgress)%")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.