From 19d3c8293b1f61acbe2dab1d49a17950fd788a4a Mon Sep 17 00:00:00 2001 From: Eric Curtin Date: Sun, 9 Feb 2025 10:34:49 +0000 Subject: [PATCH] There's a better way of clearing lines (#11756) Use the ANSI escape code for clearing a line. Signed-off-by: Eric Curtin --- common/log.h | 1 + examples/run/run.cpp | 15 ++++----------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/common/log.h b/common/log.h index 85dd4393b323b..4ebc6314b25d6 100644 --- a/common/log.h +++ b/common/log.h @@ -2,6 +2,7 @@ #include "ggml.h" // for ggml_log_level +#define LOG_CLR_TO_EOL "\033[K\r" #define LOG_COL_DEFAULT "\033[0m" #define LOG_COL_BOLD "\033[1m" #define LOG_COL_RED "\033[31m" diff --git a/examples/run/run.cpp b/examples/run/run.cpp index eab60cad1fc95..9362da22083d3 100644 --- a/examples/run/run.cpp +++ b/examples/run/run.cpp @@ -535,8 +535,7 @@ class HttpClient { static void print_progress(const std::string & progress_prefix, const std::string & progress_bar, const std::string & progress_suffix) { - printe("\r%*s\r%s%s| %s", get_terminal_width(), " ", progress_prefix.c_str(), progress_bar.c_str(), - progress_suffix.c_str()); + printe("\r" LOG_CLR_TO_EOL "%s%s| %s", progress_prefix.c_str(), progress_bar.c_str(), progress_suffix.c_str()); } // Function to write data to a file static size_t write_data(void * ptr, size_t size, size_t nmemb, void * stream) { @@ -797,16 +796,13 @@ class LlamaData { llama_model_ptr initialize_model(Opt & opt) { ggml_backend_load_all(); resolve_model(opt.model_); - printe( - "\r%*s" - "\rLoading model", - get_terminal_width(), " "); + printe("\r" LOG_CLR_TO_EOL "Loading model"); llama_model_ptr model(llama_model_load_from_file(opt.model_.c_str(), opt.model_params)); if (!model) { printe("%s: error: unable to load model from file: %s\n", __func__, opt.model_.c_str()); } - printe("\r%*s\r", static_cast(sizeof("Loading model")), " "); + printe("\r" LOG_CLR_TO_EOL); return model; } @@ -969,10 +965,7 @@ static int generate(LlamaData & llama_data, const std::string & prompt, std::str static int read_user_input(std::string & user_input) { static const char * prompt_prefix = "> "; #ifdef WIN32 - printf( - "\r%*s" - "\r" LOG_COL_DEFAULT "%s", - get_terminal_width(), " ", prompt_prefix); + printf("\r" LOG_CLR_TO_EOL LOG_COL_DEFAULT "%s", prompt_prefix); std::getline(std::cin, user_input); if (std::cin.eof()) {