hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
cio.H
Go to the documentation of this file.
1 
2 #ifndef HI_REPL_CIO_HPP_INCLUDED
3 #define HI_REPL_CIO_HPP_INCLUDED
4 
5 #include <iostream>
6 
7 namespace hi {
8 
9 // controls display options
10 typedef unsigned char color;
11 
12 DEFINE_STRUCT(ConsoleColors,
13  (color, promptfg),
14  (color, stdtextfg),
15  (color, errorfg),
16  (color, hlfg),
17  (color, divfg),
18  (color, evalfg),
19  (color, unsweetfg),
20  (color, typefg),
21  (color, llvmfg),
22 
23  (color, instfg),
24  (color, argdelimfg),
25  (color, registerfg),
26  (color, xnumfg),
27  (color, xvalfg),
28  (color, linenumfg),
29  (color, linenumdelimfg),
30  (color, evenlinebg),
31  (color, oddlinebg)
32 );
33 
34 // controls whether or not we bother to send terminal control sequences
36 
37 extern ConsoleColors colors;
38 
39 inline void sendCmd(std::ostream& out, char c) {
40  if (extConsoleCmdsEnabled()) {
41  out << (char)0x1B << '[' << c << std::flush;
42  }
43 }
44 
45 inline void sendCmd(std::ostream& out, int n0, char c) {
46  if (extConsoleCmdsEnabled()) {
47  out << (char)0x1B << '[' << n0 << c << std::flush;
48  }
49 }
50 
51 inline void sendCmd(std::ostream& out, int n0, int n1, char c) {
52  if (extConsoleCmdsEnabled()) {
53  out << (char)0x1B << '[' << n0 << ';' << n1 << c << std::flush;
54  }
55 }
56 
57 inline void sendCmd(std::ostream& out, int n0, int n1, int n2, char c) {
58  if (extConsoleCmdsEnabled()) {
59  out << (char)0x1B << '[' << n0 << ';' << n1 << ';' << n2 << c << std::flush;
60  }
61 }
62 
63 struct setbold { setbold() { } };
64 inline std::ostream& operator<<(std::ostream& lhs, const setbold&) {
65  sendCmd(lhs, 1, 'm');
66  return lhs;
67 }
68 
69 struct setunderline { setunderline() { } };
70 inline std::ostream& operator<<(std::ostream& lhs, const setunderline&) {
71  sendCmd(lhs, 4, 'm');
72  return lhs;
73 }
74 
75 struct setfont {
76  setfont(unsigned char f) : f(f) { }
77  unsigned char f;
78 };
79 inline std::ostream& operator<<(std::ostream& lhs, const setfont& f) {
80  sendCmd(lhs, 10 + f.f, 'm');
81  return lhs;
82 }
83 
84 struct setfgc {
85  setfgc(unsigned char x) : x(x) { }
86  unsigned char x;
87 };
88 
89 inline std::ostream& operator<<(std::ostream& lhs, const setfgc& c) {
90  sendCmd(lhs, 38, 5, (int)c.x, 'm');
91  return lhs;
92 }
93 
94 struct setbgc {
95  setbgc(unsigned char x) : x(x) { }
96  unsigned char x;
97 };
98 
99 inline std::ostream& operator<<(std::ostream& lhs, const setbgc& c) {
100  sendCmd(lhs, 48, 5, (int)c.x, 'm');
101  return lhs;
102 }
103 
104 struct resetfmt {
105  resetfmt() { }
106 };
107 
108 inline std::ostream& operator<<(std::ostream& lhs, const resetfmt& c) {
109  sendCmd(lhs, 0, 'm');
110  return lhs;
111 }
112 
113 struct clearscr {
114  clearscr() { }
115 };
116 
117 inline std::ostream& operator<<(std::ostream& lhs, const clearscr& c) {
118  sendCmd(lhs, 2, 'J');
119  return lhs;
120 }
121 
122 struct setcursor {
123  setcursor(int x, int y) : x(x), y(y) { }
124  int x, y;
125 };
126 
127 inline std::ostream& operator<<(std::ostream& lhs, const setcursor& sc) {
128  sendCmd(lhs, sc.y, sc.x, 'H');
129  return lhs;
130 }
131 
132 struct movecursor {
133  movecursor(int dx, int dy) : dx(dx), dy(dy) { }
134  int dx, dy;
135 };
136 inline std::ostream& operator<<(std::ostream& lhs, const movecursor& mc) {
137  if (mc.dy < 0) {
138  sendCmd(lhs, -mc.dy, 'A');
139  } else if (mc.dy > 0) {
140  sendCmd(lhs, mc.dy, 'B');
141  }
142 
143  if (mc.dx < 0) {
144  sendCmd(lhs, -mc.dx, 'D');
145  } else if (mc.dx > 0) {
146  sendCmd(lhs, mc.dx, 'C');
147  }
148 
149  return lhs;
150 }
151 
152 struct eraseToEOL { eraseToEOL() { } };
153 inline std::ostream& operator<<(std::ostream& lhs, const eraseToEOL&) {
154  sendCmd(lhs, 0, 'K');
155  return lhs;
156 }
157 
158 struct clearline { clearline() { } };
159 inline std::ostream& operator<<(std::ostream& lhs, const clearline&) {
160  sendCmd(lhs, 2, 'K');
161  return lhs;
162 }
163 
164 }
165 
166 #endif
int dy
Definition: cio.H:134
resetfmt()
Definition: cio.H:105
Definition: cio.H:113
void sendCmd(std::ostream &out, char c)
Definition: cio.H:39
Definition: cio.H:158
int x
Definition: cio.H:124
Definition: cio.H:63
setfont(unsigned char f)
Definition: cio.H:76
Definition: cio.H:75
setbold()
Definition: cio.H:63
Definition: cio.H:122
Definition: cio.H:132
Definition: cio.H:69
Definition: cio.H:7
unsigned char f
Definition: cio.H:77
Definition: cio.H:84
Definition: cio.H:94
int dx
Definition: cio.H:134
setunderline()
Definition: cio.H:69
unsigned char x
Definition: cio.H:86
int y
Definition: cio.H:124
setcursor(int x, int y)
Definition: cio.H:123
ConsoleColors colors
Definition: main.C:25
std::ostream & operator<<(std::ostream &lhs, const setbold &)
Definition: cio.H:64
bool extConsoleCmdsEnabled()
Definition: main.C:50
Definition: cio.H:152
movecursor(int dx, int dy)
Definition: cio.H:133
clearscr()
Definition: cio.H:114
setfgc(unsigned char x)
Definition: cio.H:85
setbgc(unsigned char x)
Definition: cio.H:95
#define out
Definition: netio.H:19
clearline()
Definition: cio.H:158
unsigned char color
Definition: cio.H:10
eraseToEOL()
Definition: cio.H:152
Definition: cio.H:104
DEFINE_STRUCT(ConsoleColors,(color, promptfg),(color, stdtextfg),(color, errorfg),(color, hlfg),(color, divfg),(color, evalfg),(color, unsweetfg),(color, typefg),(color, llvmfg),(color, instfg),(color, argdelimfg),(color, registerfg),(color, xnumfg),(color, xvalfg),(color, linenumfg),(color, linenumdelimfg),(color, evenlinebg),(color, oddlinebg))
unsigned char x
Definition: cio.H:96