llvm.org GIT mirror llvm / 7d31d75
Add reverseColor to raw_ostream. To be used in printing unprintable source in clang diagnostics. Patch by Seth Cantrell! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154800 91177308-0d34-0410-b5e6-96231b3b80d8 Argyrios Kyrtzidis 8 years ago
5 changed file(s) with 59 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
135135 /// Same as OutputColor, but only enables the bold attribute.
136136 static const char *OutputBold(bool bg);
137137
138 /// This function returns the escape sequence to reverse forground and
139 /// background colors.
140 static const char *OutputReverse();
141
138142 /// Resets the terminals colors, or returns an escape sequence to do so.
139143 static const char *ResetColor();
140144 /// @}
221221 /// outputting colored text, or before program exit.
222222 virtual raw_ostream &resetColor() { return *this; }
223223
224 /// Reverses the forground and background colors.
225 virtual raw_ostream &reverseColor() { return *this; }
226
224227 /// This function determines if this stream is connected to a "tty" or
225228 /// "console" window. That is, the output would be displayed to the user
226229 /// rather than being put on a pipe or stored in a file.
378381 bool bg=false);
379382 virtual raw_ostream &resetColor();
380383
384 virtual raw_ostream &reverseColor();
385
381386 virtual bool is_displayed() const;
382387
383388 /// has_error - Return the value of the flag in this raw_fd_ostream indicating
289289 return "\033[1m";
290290 }
291291
292 const char *Process::OutputReverse() {
293 return "\033[7m";
294 }
295
292296 const char *Process::ResetColor() {
293297 return "\033[0m";
294298 }
214214 return 0;
215215 }
216216
217 WORD GetConsoleTextAttribute(__in HANDLE hConsoleOutput)
218 {
219 CONSOLE_SCREEN_BUFFER_INFO info;
220 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
221 return info.wAttributes;
222 }
223
224 const char *Process::OutputReverse() {
225 const WORD attributes
226 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
227
228 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
229 FOREGROUND_RED | FOREGROUND_INTENSITY;
230 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
231 BACKGROUND_RED | BACKGROUND_INTENSITY;
232 const WORD color_mask = foreground_mask | background_mask;
233
234 WORD new_attributes =
235 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
236 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
237 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
238 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
239 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
240 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
241 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
242 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
243 0;
244 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
245
246 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),new_attributes);
247 return 0;
248 }
249
217250 const char *Process::ResetColor() {
218251 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
219252 return 0;
632632 return *this;
633633 }
634634
635 raw_ostream &raw_fd_ostream::reverseColor() {
636 if (sys::Process::ColorNeedsFlush())
637 flush();
638 const char *colorcode = sys::Process::OutputReverse();
639 if (colorcode) {
640 size_t len = strlen(colorcode);
641 write(colorcode, len);
642 // don't account colors towards output characters
643 pos -= len;
644 }
645 return *this;
646 }
647
635648 bool raw_fd_ostream::is_displayed() const {
636649 return sys::Process::FileDescriptorIsDisplayed(FD);
637650 }