ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
terminal.hpp
Go to the documentation of this file.
1#ifndef _terminal_hpp_INCLUDED
2#define _terminal_hpp_INCLUDED
3
4#include "global.h"
5
7
8namespace CaDiCaL {
9
10class Terminal {
11
12 FILE *file; // 'stdout' or 'stderr'
13 bool connected; // Connected to terminal.
14 bool use_colors; // Use colors.
15 bool reset_on_exit; // Reset on exit.
16
17 void escape () {
18 CADICAL_assert (connected);
19 fputs ("\033[", file);
20 }
21
22 void color (int color, bool bright) {
23 if (!use_colors)
24 return;
25 CADICAL_assert (connected);
26 escape ();
27 fputc (bright ? '1' : '0', file);
28 fprintf (file, ";%dm", color);
29 fflush (file);
30 }
31
32 void code (const char *str) {
33 if (!use_colors)
34 return;
35 if (!connected)
36 return;
37 escape ();
38 fputs (str, file);
39 fflush (file);
40 }
41
42public:
43 Terminal (FILE *file);
44 ~Terminal ();
45
46 void disable (); // Assume disconnected in any case.
47 void force_colors ();
48 void force_no_colors ();
49 void force_reset_on_exit ();
50
51 bool colors () { return use_colors; }
52
53 operator bool () const { return connected; }
54
55 void red (bool bright = false) { color (31, bright); }
56 void green (bool bright = false) { color (32, bright); }
57 void yellow (bool bright = false) { color (33, bright); }
58 void blue (bool bright = false) { color (34, bright); }
59 void magenta (bool bright = false) { color (35, bright); }
60 void black (bool bright = false) { color (90, bright); }
61 void cyan (bool bright = false) { color (96, bright); }
62
63 void bold () { code ("1m"); }
64 void normal () { code ("0m"); }
65 void inverse () { code ("7m"); }
66 void underline () { code ("4m"); }
67
68#define MODIFY(CODE) (use_colors ? "\033[" CODE "m" : "")
69
70 const char *bright_magenta_code () { return MODIFY ("1;35"); }
71 const char *magenta_code () { return MODIFY ("0;35"); }
72 const char *blue_code () { return MODIFY ("0;34"); }
73 const char *bright_blue_code () { return MODIFY ("1;34"); }
74 const char *yellow_code () { return MODIFY ("0;33"); }
75 const char *bright_yellow_code () { return MODIFY ("1;33"); }
76 const char *green_code () { return MODIFY ("0;32"); }
77 const char *red_code () { return MODIFY ("0;31"); }
78 const char *cyan_code () { return MODIFY ("0;96"); }
79 const char *bright_red_code () { return MODIFY ("1;31"); }
80 const char *normal_code () { return MODIFY ("0"); }
81 const char *bold_code () { return MODIFY ("1"); }
82
83 void cursor (bool on) { code (on ? "?25h" : "?25l"); }
84
85 void erase_until_end_of_line () { code ("K"); }
86
88 if (connected)
89 code ("1G");
90 else
91 fputc ('\n', file), fflush (file);
92 }
93
94 void reset ();
95};
96
97extern Terminal tout; // Terminal of 'stdout' (file descriptor '1')
98extern Terminal terr; // Terminal of 'stderr' (file descriptor '2')
99
100} // namespace CaDiCaL
101
103
104#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define CADICAL_assert(ignore)
Definition global.h:14
const char * magenta_code()
Definition terminal.hpp:71
const char * bright_blue_code()
Definition terminal.hpp:73
const char * normal_code()
Definition terminal.hpp:80
void blue(bool bright=false)
Definition terminal.hpp:58
void magenta(bool bright=false)
Definition terminal.hpp:59
const char * bold_code()
Definition terminal.hpp:81
void red(bool bright=false)
Definition terminal.hpp:55
const char * red_code()
Definition terminal.hpp:77
void erase_until_end_of_line()
Definition terminal.hpp:85
void cursor(bool on)
Definition terminal.hpp:83
const char * bright_magenta_code()
Definition terminal.hpp:70
void black(bool bright=false)
Definition terminal.hpp:60
const char * cyan_code()
Definition terminal.hpp:78
const char * green_code()
Definition terminal.hpp:76
void cyan(bool bright=false)
Definition terminal.hpp:61
const char * bright_red_code()
Definition terminal.hpp:79
const char * bright_yellow_code()
Definition terminal.hpp:75
void erase_line_if_connected_otherwise_new_line()
Definition terminal.hpp:87
const char * blue_code()
Definition terminal.hpp:72
const char * yellow_code()
Definition terminal.hpp:74
void green(bool bright=false)
Definition terminal.hpp:56
void yellow(bool bright=false)
Definition terminal.hpp:57
#define bool
Definition espresso.h:254
Terminal terr(stderr)
Definition terminal.hpp:98
Terminal tout(stdout)
Definition terminal.hpp:97
#define MODIFY(CODE)
Definition terminal.hpp:68