mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-06-26 10:54:28 +02:00
Common: Added ::leftJustify and ::rightJustify variants
This commit is contained in:
parent
33dce5de7b
commit
4a2ac6f4fc
2 changed files with 48 additions and 0 deletions
|
@ -27,6 +27,7 @@
|
||||||
#include <cmake.h>
|
#include <cmake.h>
|
||||||
#include <text.h>
|
#include <text.h>
|
||||||
#include <utf8.h>
|
#include <utf8.h>
|
||||||
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
@ -310,3 +311,44 @@ void replace_positional (
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string leftJustify (const int input, const int width)
|
||||||
|
{
|
||||||
|
std::stringstream s;
|
||||||
|
s << input;
|
||||||
|
std::string output = s.str ();
|
||||||
|
return output + std::string (width - output.length (), ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string leftJustify (const std::string& input, const int width)
|
||||||
|
{
|
||||||
|
return input + std::string (width - utf8_text_width (input), ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string rightJustifyZero (const int input, const int width)
|
||||||
|
{
|
||||||
|
std::stringstream s;
|
||||||
|
s << std::setw (width) << std::setfill ('0') << input;
|
||||||
|
return s.str ();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string rightJustify (const int input, const int width)
|
||||||
|
{
|
||||||
|
std::stringstream s;
|
||||||
|
s << std::setw (width) << std::setfill (' ') << input;
|
||||||
|
return s.str ();
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
std::string rightJustify (const std::string& input, const int width)
|
||||||
|
{
|
||||||
|
unsigned int len = utf8_text_width (input);
|
||||||
|
return (((unsigned int) width > len)
|
||||||
|
? std::string (width - len, ' ')
|
||||||
|
: "")
|
||||||
|
+ input;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -75,6 +75,12 @@ const std::string format (const std::string& fmt, Args... args)
|
||||||
return format (1, fmt, args...);
|
return format (1, fmt, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string leftJustify (const int, const int);
|
||||||
|
std::string leftJustify (const std::string&, const int);
|
||||||
|
std::string rightJustifyZero (const int, const int);
|
||||||
|
std::string rightJustify (const int, const int);
|
||||||
|
std::string rightJustify (const std::string&, const int);
|
||||||
|
|
||||||
// List operations.
|
// List operations.
|
||||||
template <class T> void listDiff (
|
template <class T> void listDiff (
|
||||||
const T& left, const T& right, T& leftOnly, T& rightOnly)
|
const T& left, const T& right, T& leftOnly, T& rightOnly)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue