Browse Source

std_utils: add clip() / tldr()

LIB-11
heck 4 years ago
parent
commit
bf203337f4
  1. 26
      src/std_utils.cc
  2. 2
      src/std_utils.hh

26
src/std_utils.cc

@ -6,6 +6,7 @@
#include <fstream> #include <fstream>
#include <cstdio> #include <cstdio>
#include <cerrno> #include <cerrno>
#include <cmath>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <algorithm> #include <algorithm>
@ -227,6 +228,28 @@ namespace pEp {
return ret; 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) string to_termcol(const Color &col)
{ {
switch (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::chrono::milliseconds timespan(milis);
std::this_thread::sleep_for(timespan); std::this_thread::sleep_for(timespan);
} }

2
src/std_utils.hh

@ -54,6 +54,8 @@ namespace pEp {
//String formatting //String formatting
std::string padTo(const std::string &str, const size_t num, const char paddingChar); 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 enum class Color
{ {

Loading…
Cancel
Save