Browse Source

Console colors support

master
Michael Uleysky 4 weeks ago
parent
commit
899c83c015
  1. 1
      include/actiondep.h
  2. 67
      include/conscolors.h
  3. 4
      src/conscolors.cpp
  4. 5
      src/odm.cpp

1
include/actiondep.h

@ -1,6 +1,7 @@
#pragma once
#include "ParseArgs.h"
#include "basedata.h"
#include "conscolors.h"
#include "mdatetime.h"
#include "mregex.h"
#include "traits.h"

67
include/conscolors.h

@ -0,0 +1,67 @@
#pragma once
#include "merrors.h"
using michlib::int_cast;
using michlib::MString;
using michlib::uint2;
class C
{
protected:
static bool tty;
MString command;
template<class... T> static MString Create(uint2 c, T... args) { return MString(c) + ";" + Create(args...); }
static MString Create(uint2 c) { return MString(c); }
template<class... T> C(T... args): command("\033[" + Create(args...) + "m") {}
public:
static void Init() { tty = isatty(michlib_internal::fd_std) == 1; }
static void Init(bool c) { tty = c; }
enum
{
BLACK = 30,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
BBLACK = 90,
BRED,
BGREEN,
BYELLOW,
BBLUE,
BMAGENTA,
BCYAN,
BWHITE
};
C(): C(0) {}
operator MString()
{
if(tty)
return command;
else
return "";
}
};
class B: public C
{
public:
B(): C(49) {}
B(decltype(C::RED) c): C(int_cast<uint2>(c) + 10) {}
};
class F: public C
{
public:
F(): C(39) {}
F(decltype(C::RED) c): C(int_cast<uint2>(c)) {}
};

4
src/conscolors.cpp

@ -0,0 +1,4 @@
#define MICHLIB_NOSOURCE
#include "conscolors.h"
bool C::tty = false;

5
src/odm.cpp

@ -69,6 +69,11 @@ int main(int argc, char** argv)
auto args = ParseArgs(argc, argv);
if((args.contains("color") && !args.contains("nocolor")) || (!args.contains("color") && args.contains("nocolor")))
C::Init(args.contains("color"));
else
C::Init();
MString action = args.contains("action") ? args["action"] : "info";
Data data;

Loading…
Cancel
Save