From bf203337f44e65740750c8f047fd49e555de0b66 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 16 Jun 2021 01:58:44 +0200 Subject: [PATCH] std_utils: add clip() / tldr() --- src/std_utils.cc | 26 +++++++++++++++++++++++++- src/std_utils.hh | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/std_utils.cc b/src/std_utils.cc index 89e308d..c357a5b 100644 --- a/src/std_utils.cc +++ b/src/std_utils.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -227,6 +228,28 @@ namespace pEp { return ret; } + std::string clip(const std::string &str, const size_t len) + { + std::string ret{ str }; + if (str.length() > len) { + ret = str.substr(0, len) + "..."; + } + return ret; + } + + std::string tldr(const std::string &str, const size_t len) + { + std::string decoration = "..."; + int trunclen = len - decoration.length(); + + std::string ret{ str }; + if (str.length() > len) { + ret = "\n" + str.substr(0, (int)floor(trunclen / 2.0)) + "\n... tldr ...\n"; + ret += str.substr(str.length() - (int)floor(trunclen / 2.0), str.length()); + } + return ret; + } + string to_termcol(const Color &col) { switch (col) { @@ -253,7 +276,8 @@ namespace pEp { } } - void sleep_millis(int milis) { + void sleep_millis(int milis) + { std::chrono::milliseconds timespan(milis); std::this_thread::sleep_for(timespan); } diff --git a/src/std_utils.hh b/src/std_utils.hh index 91c3848..654b365 100644 --- a/src/std_utils.hh +++ b/src/std_utils.hh @@ -54,6 +54,8 @@ namespace pEp { //String formatting std::string padTo(const std::string &str, const size_t num, const char paddingChar); + std::string clip(const std::string &str, const size_t len); + std::string tldr(const std::string &str, const size_t len); enum class Color {