From 250cc7003788a51437e298f5ef416da44169b8a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Fri, 27 Jan 2017 03:55:22 +0100 Subject: Pull a progress bar out of our ass --- hex.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/hex.c b/hex.c index 5aa9220..7280629 100644 --- a/hex.c +++ b/hex.c @@ -149,6 +149,8 @@ static struct app_context lua_State *L; ///< Lua state int ref_format; ///< Reference to "string.format" struct str_map coders; ///< Map of coders by name + + int progress; ///< Decoding progressbar #endif // HAVE_LUA // Data: @@ -999,9 +1001,26 @@ app_lua_chunk_newindex (lua_State *L) return 0; } +static void +app_update_progress (int64_t real_offset) +{ + // Updating too often causes unnecessary slowdowns + int progress = real_offset * 20 / g_ctx.data_len; + if (progress <= g_ctx.progress) + return; + + g_ctx.progress = progress; + printf ("\rDecoding... %3d%%", progress * 5); + fflush (stdout); +} + static void app_lua_mark (int64_t offset, int64_t len, const char *desc) { + // XXX: not all decoders go sequentially over the file + if (!g_ctx.polling && g_ctx.data_len) + app_update_progress (offset - g_ctx.data_offset); + // That would cause stupid entries, making trouble in marks_by_offset if (len <= 0) return; @@ -2015,6 +2034,9 @@ main (int argc, char *argv[]) exit_fatal ("Lua: decoding failed: %s", lua_tostring (g_ctx.L, -1)); lua_pop (g_ctx.L, 1); + + if (g_ctx.progress) + fputc ('\n', stdout); #endif // HAVE_LUA app_flatten_marks (); -- cgit v1.2.3