authorgravatar for emekankurumeh@outlook.comemekoi <emekankurumeh@outlook.com> 2018-11-07 23:36:36-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-08 00:36:36-05:00
log8e69a18d8c01aaf2bf4f0c0f8495e47cb36c284f
treec1b44cc9f694148f8ef25b752181c67d59705c15
parentac8898e68150fc9cd2bc04ee6c4ef4d3005b8c27

made colored output more consistent (#1706)

* made colored output more consistent * added os.supportsAnsiEscapeCodes

4 files changed, 69 insertions(+), 36 deletions(-)

src/os.cpp-4
...@@ -1128,9 +1128,6 @@ Error os_get_cwd(Buf *out_cwd) {...@@ -1128,9 +1128,6 @@ Error os_get_cwd(Buf *out_cwd) {
1128#define is_wprefix(s, prefix) \1128#define is_wprefix(s, prefix) \
1129 (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)1129 (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
1130static bool is_stderr_cyg_pty(void) {1130static bool is_stderr_cyg_pty(void) {
1131#if defined(__MINGW32__)
1132 return false;
1133#else
1134 HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);1131 HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
1135 if (stderr_handle == INVALID_HANDLE_VALUE)1132 if (stderr_handle == INVALID_HANDLE_VALUE)
1136 return false;1133 return false;
...@@ -1182,7 +1179,6 @@ static bool is_stderr_cyg_pty(void) {...@@ -1182,7 +1179,6 @@ static bool is_stderr_cyg_pty(void) {
1182 }1179 }
1183 free(nameinfo);1180 free(nameinfo);
1184 return (p != NULL);1181 return (p != NULL);
1185#endif
1186}1182}
1187#endif1183#endif
11881184
std/debug/index.zig+56-31
...@@ -171,7 +171,9 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c...@@ -171,7 +171,9 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
171 os.abort();171 os.abort();
172}172}
173173
174const RED = "\x1b[31;1m";
174const GREEN = "\x1b[32;1m";175const GREEN = "\x1b[32;1m";
176const CYAN = "\x1b[36;1m";
175const WHITE = "\x1b[37;1m";177const WHITE = "\x1b[37;1m";
176const DIM = "\x1b[2m";178const DIM = "\x1b[2m";
177const RESET = "\x1b[0m";179const RESET = "\x1b[0m";
...@@ -454,38 +456,61 @@ const TtyColor = enum.{...@@ -454,38 +456,61 @@ const TtyColor = enum.{
454456
455/// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt457/// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt
456fn setTtyColor(tty_color: TtyColor) void {458fn setTtyColor(tty_color: TtyColor) void {
457 const S = struct.{459 if (os.supportsAnsiEscapeCodes(stderr_file.handle)) {
458 var attrs: windows.WORD = undefined;460 switch (tty_color) {
459 var init_attrs = false;461 TtyColor.Red => {
460 };462 stderr_file.write(RED) catch return;
461 if (!S.init_attrs) {463 },
462 S.init_attrs = true;464 TtyColor.Green => {
463 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;465 stderr_file.write(GREEN) catch return;
464 // TODO handle error466 },
465 _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info);467 TtyColor.Cyan => {
466 S.attrs = info.wAttributes;468 stderr_file.write(CYAN) catch return;
467 }469 },
470 TtyColor.White, TtyColor.Bold => {
471 stderr_file.write(WHITE) catch return;
472 },
473 TtyColor.Dim => {
474 stderr_file.write(DIM) catch return;
475 },
476 TtyColor.Reset => {
477 stderr_file.write(RESET) catch return;
478 },
479 }
480 } else {
481 const S = struct.{
482 var attrs: windows.WORD = undefined;
483 var init_attrs = false;
484 };
485 if (!S.init_attrs) {
486 S.init_attrs = true;
487 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
488 // TODO handle error
489 _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info);
490 S.attrs = info.wAttributes;
491 }
468492
469 // TODO handle errors493 // TODO handle errors
470 switch (tty_color) {494 switch (tty_color) {
471 TtyColor.Red => {495 TtyColor.Red => {
472 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY);496 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY);
473 },497 },
474 TtyColor.Green => {498 TtyColor.Green => {
475 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY);499 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY);
476 },500 },
477 TtyColor.Cyan => {501 TtyColor.Cyan => {
478 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);502 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);
479 },503 },
480 TtyColor.White, TtyColor.Bold => {504 TtyColor.White, TtyColor.Bold => {
481 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);505 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);
482 },506 },
483 TtyColor.Dim => {507 TtyColor.Dim => {
484 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY);508 _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY);
485 },509 },
486 TtyColor.Reset => {510 TtyColor.Reset => {
487 _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs);511 _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs);
488 },512 },
513 }
489 }514 }
490}515}
491516
std/os/index.zig+12
...@@ -2272,6 +2272,18 @@ pub fn isTty(handle: FileHandle) bool {...@@ -2272,6 +2272,18 @@ pub fn isTty(handle: FileHandle) bool {
2272 }2272 }
2273}2273}
22742274
2275pub fn supportsAnsiEscapeCodes(handle: FileHandle) bool {
2276 if (is_windows) {
2277 return windows_util.windowsIsCygwinPty(handle);
2278 } else {
2279 if (builtin.link_libc) {
2280 return c.isatty(handle) != 0;
2281 } else {
2282 return posix.isatty(handle);
2283 }
2284 }
2285}
2286
2275pub const PosixSocketError = error.{2287pub const PosixSocketError = error.{
2276 /// Permission to create a socket of the specified type and/or2288 /// Permission to create a socket of the specified type and/or
2277 /// pro‐tocol is denied.2289 /// pro‐tocol is denied.
std/os/windows/util.zig+1-1
...@@ -91,7 +91,7 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {...@@ -91,7 +91,7 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
91 @ptrCast(*c_void, &name_info_bytes[0]),91 @ptrCast(*c_void, &name_info_bytes[0]),
92 @intCast(u32, name_info_bytes.len),92 @intCast(u32, name_info_bytes.len),
93 ) == 0) {93 ) == 0) {
94 return true;94 return false;
95 }95 }
9696
97 const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]);97 const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]);