aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/patched/replxx/src/windows.cxx
blob: 6a0e425e483a5269944e8d4b936082cbd0168c79 (plain) (blame)
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
#ifdef _WIN32

#include <iostream>

#include "windows.hxx"
#include "conversion.hxx"
#include "terminal.hxx"

using namespace std;

namespace replxx {

WinAttributes WIN_ATTR;

template<typename T>
T* HandleEsc(HANDLE out_, T* p, T* end) {
	if (*p == '[') {
		int code = 0;

		int thisBackground( WIN_ATTR._defaultBackground );
		for (++p; p < end; ++p) {
			char32_t c = *p;

			if ('0' <= c && c <= '9') {
				code = code * 10 + (c - '0');
			} else if (c == 'm' || c == ';') {
				switch (code) {
					case 0:
						WIN_ATTR._consoleAttribute = WIN_ATTR._defaultAttribute;
						WIN_ATTR._consoleColor = WIN_ATTR._defaultColor | thisBackground;
						break;
					case 1:	// BOLD
					case 5:	// BLINK
						WIN_ATTR._consoleAttribute = (WIN_ATTR._defaultAttribute ^ FOREGROUND_INTENSITY) & INTENSITY;
						break;
					case 22:
						WIN_ATTR._consoleAttribute = WIN_ATTR._defaultAttribute;
						break;
					case 30:
					case 90:
						WIN_ATTR._consoleColor = thisBackground;
						break;
					case 31:
					case 91:
						WIN_ATTR._consoleColor = FOREGROUND_RED | thisBackground;
						break;
					case 32:
					case 92:
						WIN_ATTR._consoleColor = FOREGROUND_GREEN | thisBackground;
						break;
					case 33:
					case 93:
						WIN_ATTR._consoleColor = FOREGROUND_RED | FOREGROUND_GREEN | thisBackground;
						break;
					case 34:
					case 94:
						WIN_ATTR._consoleColor = FOREGROUND_BLUE | thisBackground;
						break;
					case 35:
					case 95:
						WIN_ATTR._consoleColor = FOREGROUND_BLUE | FOREGROUND_RED | thisBackground;
						break;
					case 36:
					case 96:
						WIN_ATTR._consoleColor = FOREGROUND_BLUE | FOREGROUND_GREEN | thisBackground;
						break;
					case 37:
					case 97:
						WIN_ATTR._consoleColor = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | thisBackground;
						break;
					case 101:
						thisBackground = BACKGROUND_RED;
						break;
				}

				if ( ( code >= 90 ) && ( code <= 97 ) ) {
					WIN_ATTR._consoleAttribute = (WIN_ATTR._defaultAttribute ^ FOREGROUND_INTENSITY) & INTENSITY;
				}

				code = 0;
			}

			if (*p == 'm') {
				++p;
				break;
			}
		}
	} else {
		++p;
	}

	SetConsoleTextAttribute(
		out_,
		WIN_ATTR._consoleAttribute | WIN_ATTR._consoleColor
	);

	return p;
}

int win_write( HANDLE out_, bool autoEscape_, char const* str_, int size_ ) {
	int count( 0 );
	if ( tty::out ) {
		DWORD nWritten( 0 );
		if ( autoEscape_ ) {
			WriteConsoleA( out_, str_, size_, &nWritten, nullptr );
			count = nWritten;
		} else {
			char const* s( str_ );
			char const* e( str_ + size_ );
			while ( str_ < e ) {
				if ( *str_ == 27 ) {
					if ( s < str_ ) {
						int toWrite( static_cast<int>( str_ - s ) );
						WriteConsoleA( out_, s, static_cast<DWORD>( toWrite ), &nWritten, nullptr );
						count += nWritten;
						if ( static_cast<int>( nWritten ) != toWrite ) {
							s = str_ = nullptr;
							break;
						}
					}
					s = HandleEsc( out_, str_ + 1, e );
					int escaped( static_cast<int>( s - str_ ) );
					count += escaped;
					str_ = s;
				} else {
					++ str_;
				}
			}

			if ( s < str_ ) {
				WriteConsoleA( out_, s, static_cast<DWORD>( str_ - s ), &nWritten, nullptr );
				count += nWritten;
			}
		}
	} else {
		count = _write( 1, str_, size_ );
	}
	return ( count );
}

}

#endif