Commit 8e69a18d8c

emekoi <emekankurumeh@outlook.com>
2018-11-08 06:36:36
made colored output more consistent (#1706)
* made colored output more consistent * added os.supportsAnsiEscapeCodes
1 parent ac8898e
Changed files (4)
src
std
src/os.cpp
@@ -1128,9 +1128,6 @@ Error os_get_cwd(Buf *out_cwd) {
 #define is_wprefix(s, prefix) \
     (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
 static bool is_stderr_cyg_pty(void) {
-#if defined(__MINGW32__)
-    return false;
-#else
     HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
     if (stderr_handle == INVALID_HANDLE_VALUE)
         return false;
@@ -1182,7 +1179,6 @@ static bool is_stderr_cyg_pty(void) {
     }
     free(nameinfo);
     return (p != NULL);
-#endif
 }
 #endif
 
std/debug/index.zig
@@ -171,7 +171,9 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
     os.abort();
 }
 
+const RED = "\x1b[31;1m";
 const GREEN = "\x1b[32;1m";
+const CYAN = "\x1b[36;1m";
 const WHITE = "\x1b[37;1m";
 const DIM = "\x1b[2m";
 const RESET = "\x1b[0m";
@@ -454,38 +456,61 @@ const TtyColor = enum.{
 
 /// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt
 fn setTtyColor(tty_color: TtyColor) void {
-    const S = struct.{
-        var attrs: windows.WORD = undefined;
-        var init_attrs = false;
-    };
-    if (!S.init_attrs) {
-        S.init_attrs = true;
-        var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
-        // TODO handle error
-        _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info);
-        S.attrs = info.wAttributes;
-    }
+    if (os.supportsAnsiEscapeCodes(stderr_file.handle)) {
+        switch (tty_color) {
+            TtyColor.Red => {
+                stderr_file.write(RED) catch return;
+            },
+            TtyColor.Green => {
+                stderr_file.write(GREEN) catch return;
+            },
+            TtyColor.Cyan => {
+                stderr_file.write(CYAN) catch return;
+            },
+            TtyColor.White, TtyColor.Bold => {
+                stderr_file.write(WHITE) catch return;
+            },
+            TtyColor.Dim => {
+                stderr_file.write(DIM) catch return;
+            },
+            TtyColor.Reset => {
+                stderr_file.write(RESET) catch return;
+            },
+        }
+    } else {
+        const S = struct.{
+            var attrs: windows.WORD = undefined;
+            var init_attrs = false;
+        };
+        if (!S.init_attrs) {
+            S.init_attrs = true;
+            var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
+            // TODO handle error
+            _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info);
+            S.attrs = info.wAttributes;
+        }
 
-    // TODO handle errors
-    switch (tty_color) {
-        TtyColor.Red => {
-            _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY);
-        },
-        TtyColor.Green => {
-            _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY);
-        },
-        TtyColor.Cyan => {
-            _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);
-        },
-        TtyColor.White, TtyColor.Bold => {
-            _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);
-        },
-        TtyColor.Dim => {
-            _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY);
-        },
-        TtyColor.Reset => {
-            _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs);
-        },
+        // TODO handle errors
+        switch (tty_color) {
+            TtyColor.Red => {
+                _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY);
+            },
+            TtyColor.Green => {
+                _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY);
+            },
+            TtyColor.Cyan => {
+                _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);
+            },
+            TtyColor.White, TtyColor.Bold => {
+                _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY);
+            },
+            TtyColor.Dim => {
+                _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY);
+            },
+            TtyColor.Reset => {
+                _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs);
+            },
+        }
     }
 }
 
std/os/windows/util.zig
@@ -91,7 +91,7 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
         @ptrCast(*c_void, &name_info_bytes[0]),
         @intCast(u32, name_info_bytes.len),
     ) == 0) {
-        return true;
+        return false;
     }
 
     const name_info = @ptrCast(*const windows.FILE_NAME_INFO, &name_info_bytes[0]);
std/os/index.zig
@@ -2272,6 +2272,18 @@ pub fn isTty(handle: FileHandle) bool {
     }
 }
 
+pub fn supportsAnsiEscapeCodes(handle: FileHandle) bool {
+    if (is_windows) {
+        return windows_util.windowsIsCygwinPty(handle);
+    } else {
+        if (builtin.link_libc) {
+            return c.isatty(handle) != 0;
+        } else {
+            return posix.isatty(handle);
+        }
+    }
+}
+
 pub const PosixSocketError = error.{
     /// Permission to create a socket of the specified type and/or
     /// pro‐tocol is denied.