@@ -119,7 +119,7 @@ pub fn detectTTYConfig() TTY.Config {
119119 if (stderr_file .supportsAnsiEscapeCodes ()) {
120120 return .escape_codes ;
121121 } else if (native_os == .windows and stderr_file .isTty ()) {
122- return .windows_api ;
122+ return .{ . windows_api = stderr_file } ;
123123 } else {
124124 return .no_color ;
125125 }
@@ -384,14 +384,6 @@ pub fn panicImpl(trace: ?*const std.builtin.StackTrace, first_trace_addr: ?usize
384384 os .abort ();
385385}
386386
387- const RED = "\x1b [31;1m" ;
388- const GREEN = "\x1b [32;1m" ;
389- const CYAN = "\x1b [36;1m" ;
390- const WHITE = "\x1b [37;1m" ;
391- const BOLD = "\x1b [1m" ;
392- const DIM = "\x1b [2m" ;
393- const RESET = "\x1b [0m" ;
394-
395387pub fn writeStackTrace (
396388 stack_trace : std.builtin.StackTrace ,
397389 out_stream : anytype ,
@@ -415,9 +407,9 @@ pub fn writeStackTrace(
415407 if (stack_trace .index > stack_trace .instruction_addresses .len ) {
416408 const dropped_frames = stack_trace .index - stack_trace .instruction_addresses .len ;
417409
418- tty_config .setColor (out_stream , .Bold );
410+ tty_config .setColor (out_stream , .Bold ) catch {} ;
419411 try out_stream .print ("({d} additional stack frames skipped...)\n " , .{dropped_frames });
420- tty_config .setColor (out_stream , .Reset );
412+ tty_config .setColor (out_stream , .Reset ) catch {} ;
421413 }
422414}
423415
@@ -605,59 +597,39 @@ pub const TTY = struct {
605597 Reset ,
606598 };
607599
608- pub const Config = enum {
600+ pub const Config = union ( enum ) {
609601 no_color ,
610602 escape_codes ,
611- // TODO give this a payload of file handle
612- windows_api ,
603+ windows_api : File ,
613604
614- pub fn setColor (conf : Config , out_stream : anytype , color : Color ) void {
605+ pub fn setColor (conf : Config , out_stream : anytype , color : Color ) ! void {
615606 nosuspend switch (conf ) {
616607 .no_color = > return ,
617- .escape_codes = > switch (color ) {
618- .Red = > out_stream .writeAll (RED ) catch return ,
619- .Green = > out_stream .writeAll (GREEN ) catch return ,
620- .Cyan = > out_stream .writeAll (CYAN ) catch return ,
621- .White = > out_stream .writeAll (WHITE ) catch return ,
622- .Dim = > out_stream .writeAll (DIM ) catch return ,
623- .Bold = > out_stream .writeAll (BOLD ) catch return ,
624- .Reset = > out_stream .writeAll (RESET ) catch return ,
608+ .escape_codes = > {
609+ const color_string = switch (color ) {
610+ .Red = > "\x1b [31;1m" ,
611+ .Green = > "\x1b [32;1m" ,
612+ .Cyan = > "\x1b [36;1m" ,
613+ .White = > "\x1b [37;1m" ,
614+ .Bold = > "\x1b [1m" ,
615+ .Dim = > "\x1b [2m" ,
616+ .Reset = > "\x1b [0m" ,
617+ };
618+ try out_stream .writeAll (color_string );
625619 },
626- .windows_api = > if (native_os == .windows ) {
627- const stderr_file = io .getStdErr ();
628- const S = struct {
629- var attrs : windows.WORD = undefined ;
630- var init_attrs = false ;
620+ .windows_api = > | file | if (native_os == .windows ) {
621+ var info : windows.CONSOLE_SCREEN_BUFFER_INFO = undefined ;
622+ if (windows .kernel32 .GetConsoleScreenBufferInfo (file .handle , & info ) != windows .TRUE )
623+ return error .FailedRetrievingTerminalInfo ;
624+ const attributes = switch (color ) {
625+ .Red = > windows .FOREGROUND_RED | windows .FOREGROUND_INTENSITY ,
626+ .Green = > windows .FOREGROUND_GREEN | windows .FOREGROUND_INTENSITY ,
627+ .Cyan = > windows .FOREGROUND_GREEN | windows .FOREGROUND_BLUE | windows .FOREGROUND_INTENSITY ,
628+ .White , .Bold = > windows .FOREGROUND_RED | windows .FOREGROUND_GREEN | windows .FOREGROUND_BLUE | windows .FOREGROUND_INTENSITY ,
629+ .Dim = > windows .FOREGROUND_INTENSITY ,
630+ .Reset = > info .wAttributes ,
631631 };
632- if (! S .init_attrs ) {
633- S .init_attrs = true ;
634- var info : windows.CONSOLE_SCREEN_BUFFER_INFO = undefined ;
635- // TODO handle error
636- _ = windows .kernel32 .GetConsoleScreenBufferInfo (stderr_file .handle , & info );
637- S .attrs = info .wAttributes ;
638- }
639-
640- // TODO handle errors
641- switch (color ) {
642- .Red = > {
643- _ = windows .SetConsoleTextAttribute (stderr_file .handle , windows .FOREGROUND_RED | windows .FOREGROUND_INTENSITY ) catch {};
644- },
645- .Green = > {
646- _ = windows .SetConsoleTextAttribute (stderr_file .handle , windows .FOREGROUND_GREEN | windows .FOREGROUND_INTENSITY ) catch {};
647- },
648- .Cyan = > {
649- _ = windows .SetConsoleTextAttribute (stderr_file .handle , windows .FOREGROUND_GREEN | windows .FOREGROUND_BLUE | windows .FOREGROUND_INTENSITY ) catch {};
650- },
651- .White , .Bold = > {
652- _ = windows .SetConsoleTextAttribute (stderr_file .handle , windows .FOREGROUND_RED | windows .FOREGROUND_GREEN | windows .FOREGROUND_BLUE | windows .FOREGROUND_INTENSITY ) catch {};
653- },
654- .Dim = > {
655- _ = windows .SetConsoleTextAttribute (stderr_file .handle , windows .FOREGROUND_INTENSITY ) catch {};
656- },
657- .Reset = > {
658- _ = windows .SetConsoleTextAttribute (stderr_file .handle , S .attrs ) catch {};
659- },
660- }
632+ try windows .SetConsoleTextAttribute (file .handle , attributes );
661633 } else {
662634 unreachable ;
663635 },
@@ -751,19 +723,19 @@ fn printLineInfo(
751723 comptime printLineFromFile : anytype ,
752724) ! void {
753725 nosuspend {
754- tty_config .setColor (out_stream , .Bold );
726+ try tty_config .setColor (out_stream , .Bold );
755727
756728 if (line_info ) | * li | {
757729 try out_stream .print ("{s}:{d}:{d}" , .{ li .file_name , li .line , li .column });
758730 } else {
759731 try out_stream .writeAll ("???:?:?" );
760732 }
761733
762- tty_config .setColor (out_stream , .Reset );
734+ try tty_config .setColor (out_stream , .Reset );
763735 try out_stream .writeAll (": " );
764- tty_config .setColor (out_stream , .Dim );
736+ try tty_config .setColor (out_stream , .Dim );
765737 try out_stream .print ("0x{x} in {s} ({s})" , .{ address , symbol_name , compile_unit_name });
766- tty_config .setColor (out_stream , .Reset );
738+ try tty_config .setColor (out_stream , .Reset );
767739 try out_stream .writeAll ("\n " );
768740
769741 // Show the matching source code line if possible
@@ -774,9 +746,9 @@ fn printLineInfo(
774746 const space_needed = @intCast (usize , li .column - 1 );
775747
776748 try out_stream .writeByteNTimes (' ' , space_needed );
777- tty_config .setColor (out_stream , .Green );
749+ try tty_config .setColor (out_stream , .Green );
778750 try out_stream .writeAll ("^" );
779- tty_config .setColor (out_stream , .Reset );
751+ try tty_config .setColor (out_stream , .Reset );
780752 }
781753 try out_stream .writeAll ("\n " );
782754 } else | err | switch (err ) {
@@ -789,14 +761,12 @@ fn printLineInfo(
789761 }
790762}
791763
792- // TODO use this
793764pub const OpenSelfDebugInfoError = error {
794765 MissingDebugInfo ,
795- OutOfMemory ,
796766 UnsupportedOperatingSystem ,
797767};
798768
799- pub fn openSelfDebugInfo (allocator : mem.Allocator ) anyerror ! DebugInfo {
769+ pub fn openSelfDebugInfo (allocator : mem.Allocator ) OpenSelfDebugInfoError ! DebugInfo {
800770 nosuspend {
801771 if (builtin .strip_debug_info )
802772 return error .MissingDebugInfo ;
@@ -813,7 +783,7 @@ pub fn openSelfDebugInfo(allocator: mem.Allocator) anyerror!DebugInfo {
813783 .windows ,
814784 .solaris ,
815785 = > return DebugInfo .init (allocator ),
816- else = > return error .UnsupportedDebugInfo ,
786+ else = > return error .UnsupportedOperatingSystem ,
817787 }
818788 }
819789}
0 commit comments