File: test_color.h

package info (click to toggle)
keyman 18.0.246-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,316 kB
  • sloc: python: 52,784; cpp: 21,289; sh: 7,633; ansic: 4,823; xml: 3,617; perl: 959; makefile: 139; javascript: 138
file content (83 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Keyman is copyright (C) SIL International. MIT License.
 *
 * Keyman Core - test console color helpers
 */

#pragma once

#include <ostream>

#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif

namespace console_color {

enum ansi_code {
    BLACK    = 0,
    RED      = 1,
    GREEN    = 2,
    YELLOW   = 3,
    BLUE     = 4,
    MAGENTA  = 5,
    CYAN     = 6,
    WHITE    = 7,
    GREY     = 8,
    BRIGHT_RED = 196
};

extern bool enabled;

class fg {
    ansi_code code;
public:
    fg(ansi_code pCode) : code(pCode) {}

    inline friend std::wostream&
    operator<<(std::wostream& os, const fg& mod) {
        if(!enabled) return os;
        return os << "\033[38;5;" << mod.code << "m";
    }
};

class bg {
    ansi_code code;
public:
    bg(ansi_code pCode) : code(pCode) {}

    inline friend std::wostream&
    operator<<(std::wostream& os, const bg& mod) {
        if(!enabled) return os;
        return os << "\033[48;5;" << mod.code << "m";
    }
};

#define __define_ansi_code__(name,value) class name { \
    inline friend std::wostream& \
    operator<<(std::wostream& os, const name& mod) { \
        if(!enabled) return os; \
        return os << "\033[" value "m"; \
    } \
}

__define_ansi_code__(reset, "0");
__define_ansi_code__(bold, "1");
__define_ansi_code__(underline, "4");
__define_ansi_code__(reversed, "7");

#undef __define_ansi_code__

#ifdef _MSC_VER
inline bool isaterminal() {
  return _isatty(_fileno(stdout));
}
#else
inline bool isaterminal() {
  return isatty(STDOUT_FILENO);
}
#endif

}