Commit 3d9200f9 by maarten

laatste hand aan pepflip, voor het weekeinde dan. Met coloured voor gekleurde tekst naar een tty

git-svn-id: svn+ssh://gitlab/srv/svn-repos/pdb-redo/trunk@287 a1961a4f-ab94-4bcc-80e8-33b5a54de466
parent 0af4eca1
...@@ -59,6 +59,77 @@ std::vector<std::string> wordWrap(const std::string& text, unsigned int width); ...@@ -59,6 +59,77 @@ std::vector<std::string> wordWrap(const std::string& text, unsigned int width);
uint32 get_terminal_width(); uint32 get_terminal_width();
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// some manipulators to write coloured text to terminals
enum StringColour {
scBLACK = 0, scRED, scGREEN, scYELLOW, scBLUE, scMAGENTA, scCYAN, scWHITE, scNONE = 9 };
template<typename String, typename CharT>
struct ColouredString
{
static_assert(std::is_reference<String>::value or std::is_pointer<String>::value, "String type must be pointer or reference");
ColouredString(String s, StringColour fore, StringColour back, bool bold = true)
: m_s(s), m_fore(fore), m_back(back), m_bold(bold) {}
ColouredString& operator=(const ColouredString&) = delete;
String m_s;
StringColour m_fore, m_back;
bool m_bold;
};
template<typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const ColouredString<const CharT*,CharT>& s)
{
if (isatty(STDOUT_FILENO))
{
std::basic_ostringstream<CharT, Traits> ostr;
ostr << "\033[" << (30 + s.m_fore) << ';' << (s.m_bold ? "1" : "22") << ';' << (40 + s.m_back) << 'm'
<< s.m_s
<< "\033[0m";
return os << ostr.str();
}
else
return os << s.m_s;
}
template<typename CharT, typename Traits, typename String>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const ColouredString<String,CharT>& s)
{
if (isatty(STDOUT_FILENO))
{
std::basic_ostringstream<CharT, Traits> ostr;
ostr << "\033[" << (30 + s.m_fore) << ';' << (s.m_bold ? "1" : "22") << ';' << (40 + s.m_back) << 'm'
<< s.m_s
<< "\033[0m";
return os << ostr.str();
}
else
return os << s.m_s;
}
template<typename CharT>
inline auto coloured(const CharT* s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
{
return ColouredString<const CharT*, CharT>(s, fore, back, bold);
}
template<typename CharT, typename Traits, typename Alloc>
inline auto coloured(const std::basic_string<CharT, Traits, Alloc>& s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
{
return ColouredString<const std::basic_string<CharT, Traits, Alloc>, CharT>(s, fore, back, bold);
}
template<typename CharT, typename Traits, typename Alloc>
inline auto coloured(std::basic_string<CharT, Traits, Alloc>& s, StringColour fore = scWHITE, StringColour back = scRED, bool bold = true)
{
return ColouredString<std::basic_string<CharT, Traits, Alloc>, CharT>(s, fore, back, bold);
}
// --------------------------------------------------------------------
// A progress bar // A progress bar
class Progress class Progress
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "cif++/AtomType.h" #include "cif++/AtomType.h"
#include "cif++/Compound.h" #include "cif++/Compound.h"
#include "cif++/PDB2CifRemark3.h" #include "cif++/PDB2CifRemark3.h"
#include "cif++/CifUtils.h"
using namespace std; using namespace std;
namespace ba = boost::algorithm; namespace ba = boost::algorithm;
...@@ -21,9 +22,6 @@ using cif::Row; ...@@ -21,9 +22,6 @@ using cif::Row;
using cif::Key; using cif::Key;
using cif::iequals; using cif::iequals;
static const char* kRedOn = "\033[37;1;41m";
static const char* kRedOff = "\033[0m";
// -------------------------------------------------------------------- // --------------------------------------------------------------------
struct TemplateLine struct TemplateLine
...@@ -911,7 +909,7 @@ bool Remark3Parser::match(const char* expr, int nextState) ...@@ -911,7 +909,7 @@ bool Remark3Parser::match(const char* expr, int nextState)
if (result) if (result)
mState = nextState; mState = nextState;
else if (VERBOSE >= 3) else if (VERBOSE >= 3)
cerr << kRedOn << "No match:" << kRedOff << " '" << expr << '\'' << endl; cerr << cif::coloured("No match:", cif::scWHITE, cif::scRED) << " '" << expr << '\'' << endl;
return result; return result;
} }
...@@ -971,7 +969,7 @@ float Remark3Parser::parse() ...@@ -971,7 +969,7 @@ float Remark3Parser::parse()
} }
if (VERBOSE >= 2) if (VERBOSE >= 2)
cerr << kRedOn << "Dropping line:" << kRedOff << " '" << mLine << '\'' << endl; cerr << cif::coloured("Dropping line:", cif::scWHITE, cif::scRED) << " '" << mLine << '\'' << endl;
++dropped; ++dropped;
} }
......
...@@ -49,9 +49,6 @@ namespace c = mmcif; ...@@ -49,9 +49,6 @@ namespace c = mmcif;
namespace cif namespace cif
{ {
static const char* kRedOn = "\033[37;1;41m";
static const char* kRedOff = "\033[0m";
const int const int
kResidueNrWildcard = numeric_limits<int>::min(), kResidueNrWildcard = numeric_limits<int>::min(),
kNoSeqNum = numeric_limits<int>::max() - 1; kNoSeqNum = numeric_limits<int>::max() - 1;
...@@ -151,9 +148,9 @@ void DumpSelection(const vector<TLSResidue>& selected, int indentLevel) ...@@ -151,9 +148,9 @@ void DumpSelection(const vector<TLSResidue>& selected, int indentLevel)
if (first) if (first)
{ {
if (isatty(STDOUT_FILENO)) if (isatty(STDOUT_FILENO))
cout << indent << kRedOn << "Empty selection" << kRedOff << endl; cout << indent << cif::coloured("Empty selection") << endl;
else else
cout << indent << kRedOn << "Empty selection" << kRedOff << endl; cout << indent << cif::coloured("Empty selection") << endl;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment