Hello, I've been learning nim for the last week or so and to practice I am trying to recreate Linux command line tools in nim. I'm having issues with implementing a wget-like program. Admittedly, I've never managed to properly understand asynchronous programming, so please bear with me.
I want to display the progress as a percentage to track the status of downloads. But I'm clueless on how to execute both of these functions concurrently.
As far as I understand, I could make this application threaded, but wget works on single core systems.
proc progress(fileSize: int, fileProgress: BiggestFloat, downloadPath: string) {.async.}=
let percentProgress = int(floor(100 * fileProgress / fileSize.toFloat))
stdout.write(&"{percentProgress}%")
cursorUp 1
proc download(p: string) {.async.} =
var client = newAsyncHttpClient()
let headReq = await client.head(p)
var fileSize = contentLength(headReq)
while getFileSize("ubuntu.iso") != fileSize:
await client.downloadFile("https://releases.ubuntu.com/22.04/ubuntu-22.04-desktop-amd64.iso", "ubuntu.iso")
await progress(fileSize, (getFileSize("ubuntu.iso").toBiggestFloat), p)
Thank you.
Hello and welcome!
For this you don't even need async. You can just use the blocking http client. You should use the onProgressChanged callback for this. Here is an example, it uses async but the same should work for the blocking HttpClient: https://nim-lang.org/docs/httpclient.html#progress-reporting