summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-05-03 09:01:06 +0430
committerAndreas Kling <kling@serenityos.org>2020-05-03 12:59:26 +0200
commit06cf9d3fb7fd712c50a720e22309b91084b9e5e9 (patch)
treefd226b3e5fbdd0bc32715503378db18278477c6b /Userland
parentc6825a96c74bb6222aaa7e82c9950290f5e7471b (diff)
downloadserenity-06cf9d3fb7fd712c50a720e22309b91084b9e5e9.zip
ProtocolServer: Implement and handle download progress
Also updates `pro` to display download progress and speed on stderr
Diffstat (limited to 'Userland')
-rw-r--r--Userland/pro.cpp27
1 files changed, 24 insertions, 3 deletions
diff --git a/Userland/pro.cpp b/Userland/pro.cpp
index 51d0b4912f..8edc0b2fd5 100644
--- a/Userland/pro.cpp
+++ b/Userland/pro.cpp
@@ -24,8 +24,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <AK/URL.h>
+#include <AK/NumberFormat.h>
#include <AK/SharedBuffer.h>
+#include <AK/URL.h>
#include <LibCore/EventLoop.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
@@ -53,10 +54,30 @@ int main(int argc, char** argv)
fprintf(stderr, "Failed to start download for '%s'\n", url_string.characters());
return 1;
}
- download->on_progress = [](u32 total_size, u32 downloaded_size) {
- dbgprintf("download progress: %u / %u\n", downloaded_size, total_size);
+ u32 previous_downloaded_size { 0 };
+ timeval prev_time, current_time, time_diff;
+ gettimeofday(&prev_time, nullptr);
+
+ download->on_progress = [&](Optional<u32> maybe_total_size, u32 downloaded_size) {
+ fprintf(stderr, "\r\033[2K");
+ if (maybe_total_size.has_value())
+ fprintf(stderr, "Download progress: %s / %s", human_readable_size(downloaded_size).characters(), human_readable_size(maybe_total_size.value()).characters());
+ else
+ fprintf(stderr, "Download progress: %s / ???", human_readable_size(downloaded_size).characters());
+
+ gettimeofday(&current_time, nullptr);
+ timersub(&current_time, &prev_time, &time_diff);
+
+ auto time_diff_ms = time_diff.tv_sec * 1000 + time_diff.tv_usec / 1000;
+ auto size_diff = downloaded_size - previous_downloaded_size;
+
+ fprintf(stderr, " at %s/s", human_readable_size(((float)size_diff / (float)time_diff_ms) * 1000).characters());
+
+ previous_downloaded_size = downloaded_size;
+ prev_time = current_time;
};
download->on_finish = [&](bool success, auto& payload, auto) {
+ fprintf(stderr, "\n");
if (success)
write(STDOUT_FILENO, payload.data(), payload.size());
else