Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 073f34b51202bd3c87c3c28b16c52dadf88cc779 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
   sys/consoleinterface.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "sys/consoleinterface.h"

#include <iostream>

#include <stdlib.h>

namespace sys
{

ConsoleInterface *ConsoleInterface::consoleinterface_instance = 0;

ConsoleInterface::ConsoleInterface()
{
	consoleinterface_ansi = true;
	if (consoleinterface_instance) {
		std::cerr << "multiple sys::ConsoleInterface instances!" << std::endl;
		sys::quit(2);
	}

	consoleinterface_rcon = false;

	consoleinterface_instance = this;
	consoleinterface_text.clear();
	consoleinterface_buffer.clear();
}

ConsoleInterface::~ConsoleInterface()
{
	consoleinterface_instance = 0;
}

ConsoleInterface *ConsoleInterface::instance()
{
	return consoleinterface_instance;
}

std::ostream & ConsoleInterface::messagestream()
{
	flush();
	return (consoleinterface_buffer << "^N");
}

std::ostream & ConsoleInterface::warningstream()
{
	flush();
	return (consoleinterface_buffer << "^W");
}

std::ostream & ConsoleInterface::errorstream()
{
	flush();
	return (consoleinterface_buffer << "^R");
}

std::ostream & ConsoleInterface::debugstream()
{
	flush();
	return (consoleinterface_buffer << "^D");
}

void ConsoleInterface::flush()
{
	if (rcon())
		return;

	char line[MAXCMDSIZE];

	while(consoleinterface_buffer.getline(line, MAXCMDSIZE-1)) {	
		while (consoleinterface_text.size() >= MAXCONLINES) {
			consoleinterface_text.pop_front();
		}
		consoleinterface_text.push_back(std::string(line));

		// print to stdout
		print_ansi(line);
		std::cout << std::endl;
	}
	
	consoleinterface_buffer.clear();
}

void ConsoleInterface::resize()
{
}

void ConsoleInterface::print_ansi(const char *line)
{
	if (consoleinterface_ansi)
		std::cout << "\033[0;39m";

	const char *c = line;
	while (*c) {
		
		if ((*c == '^')) {
			bool is_code = true;
			int bold = 0;
			int color = 39; 

			switch (*(c+1)) {
				case '0': // black
					color = 0;
                                	bold = 1;
                                        break;
                                case '1': // red
                                	color = 31;
                                        break;
                                case '2': // green
                                        color = 32;
                                        break;
                                case '3': // yellow
					bold = 1;
                                        color = 33;
                                        break;
                                case '4': // blue
                                        color = 34;
                                        break;
                                case '5': // cyan
                                        color = 36;
                                        break;
                                case '6': // magenta
                                        color = 35;
                                        break;
                                case '7': // white is mapped to foreground color
					bold = 1;
                                        color = 39;
					break;

				case 'N': // normal
					bold = 0;
					color = 39;
					break;
				case 'B': // bold
					bold = 1;
					color = 39;
					break;
				case 'D': // debug
					bold = 0;
					color = 39;
					break;
				case 'R': // error
					bold = 0;
					color = 31;
					break;
				case 'W': // warning
					bold = 1;
					color = 33;
					break;
				case 'F': // fancy
					bold = 0;
					color = 32;
					break;
				default:
					is_code = false;
			}
			if (is_code) {
				c++;
				if (consoleinterface_ansi)
					std::cout <<  "\033[" << bold << ";" << color << "m";
			} else
				std::cout << *c;
		} else {
			std::cout << *c;
		}
		c++;
	}

	if (consoleinterface_ansi)
		std::cout << "\033[0;39m";
}

void ConsoleInterface::set_ansi(bool enable)
{
	consoleinterface_ansi = enable;
}

void ConsoleInterface::set_rcon(bool enable)
{
	if (enable) {
		flush();
	}

	consoleinterface_rcon = enable;
}

} // namespace sys