< Summary

Line coverage
60%
Covered lines: 29
Uncovered lines: 19
Coverable lines: 48
Total lines: 138
Line coverage: 60.4%
Branch coverage
37%
Covered branches: 3
Total branches: 8
Branch coverage: 37.5%
Method coverage
75%
Covered methods: 3
Fully covered methods: 0
Total methods: 4
Method coverage: 75%
Full method coverage: 0%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: EnableVirtualTerminalProcessing()37.5%18847.05%
File 2: GetStdHandle(...)100%11100%
File 2: GetConsoleMode(...)100%11100%
File 2: SetConsoleMode(...)100%210%

File(s)

D:\Repositories\dotnet-logging\src\ConsoleAnsi.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using System.Runtime.InteropServices;
 3
 4[assembly:InternalsVisibleTo("ArturRios.Logging.Tests")]
 5
 6namespace ArturRios.Logging;
 7
 8internal static partial class ConsoleAnsi
 9{
 10    private const int StdOutputHandle = -11;
 11    private const uint EnableVirtualTerminalProcessingHex = 0x0004;
 12    private const uint DisableNewlineAutoReturn = 0x0008;
 13
 14    [LibraryImport("kernel32.dll", SetLastError = true)]
 15    private static partial IntPtr GetStdHandle(int nStdHandle);
 16
 17    [LibraryImport("kernel32.dll", SetLastError = true)]
 18    [return: MarshalAs(UnmanagedType.Bool)]
 19    private static partial bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
 20
 21    [LibraryImport("kernel32.dll", SetLastError = true)]
 22    [return: MarshalAs(UnmanagedType.Bool)]
 23    private static partial bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
 24
 25    /// <summary>
 26    /// Enables virtual terminal processing for the console to support ANSI escape sequences.
 27    /// </summary>
 28    /// <returns>True if virtual terminal processing was successfully enabled or already enabled; otherwise, false.</ret
 29    public static bool EnableVirtualTerminalProcessing()
 230    {
 231        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 032        {
 033            return false;
 34        }
 35
 236        var handle = GetStdHandle(StdOutputHandle);
 237        if (handle == IntPtr.Zero)
 038        {
 039            return false;
 40        }
 41
 242        if (!GetConsoleMode(handle, out var mode))
 243        {
 244            return false;
 45        }
 46
 047        if ((mode & EnableVirtualTerminalProcessingHex) != 0)
 048        {
 049            return true;
 50        }
 51
 052        var newMode = mode | EnableVirtualTerminalProcessingHex | DisableNewlineAutoReturn;
 53
 054        return SetConsoleMode(handle, newMode);
 255    }
 56}

D:\Repositories\dotnet-logging\src\obj\Debug\net10.0\generated\Microsoft.Interop.LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator\LibraryImports.g.cs

#LineLine coverage
 1// <auto-generated/>
 2namespace ArturRios.Logging
 3{
 4    internal static unsafe partial class ConsoleAnsi
 5    {
 6        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "10.0.13.700
 7        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
 8        private static partial nint GetStdHandle(int nStdHandle)
 29        {
 10            int __lastError;
 11            nint __retVal;
 212            {
 213                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
 214                __retVal = __PInvoke(nStdHandle);
 215                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
 216            }
 17
 218            global::System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
 219            return __retVal;
 20            // Local P/Invoke
 21            [global::System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "GetStdHandle", Exac
 22            static extern unsafe nint __PInvoke(int __nStdHandle_native);
 223        }
 24    }
 25}
 26namespace ArturRios.Logging
 27{
 28    internal static unsafe partial class ConsoleAnsi
 29    {
 30        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "10.0.13.700
 31        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
 32        private static partial bool GetConsoleMode(nint hConsoleHandle, out uint lpMode)
 233        {
 34            int __lastError;
 235            lpMode = default;
 36            bool __retVal;
 37            int __retVal_native;
 38            // Pin - Pin data in preparation for calling the P/Invoke.
 239            fixed (uint* __lpMode_native = &lpMode)
 240            {
 241                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
 242                __retVal_native = __PInvoke(hConsoleHandle, __lpMode_native);
 243                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
 244            }
 45
 46            // Unmarshal - Convert native data to managed data.
 247            __retVal = __retVal_native != 0;
 248            global::System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
 249            return __retVal;
 50            // Local P/Invoke
 51            [global::System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "GetConsoleMode", Ex
 52            static extern unsafe int __PInvoke(nint __hConsoleHandle_native, uint* __lpMode_native);
 253        }
 54    }
 55}
 56namespace ArturRios.Logging
 57{
 58    internal static unsafe partial class ConsoleAnsi
 59    {
 60        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "10.0.13.700
 61        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
 62        private static partial bool SetConsoleMode(nint hConsoleHandle, uint dwMode)
 063        {
 64            int __lastError;
 65            bool __retVal;
 66            int __retVal_native;
 067            {
 068                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
 069                __retVal_native = __PInvoke(hConsoleHandle, dwMode);
 070                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
 071            }
 72
 73            // Unmarshal - Convert native data to managed data.
 074            __retVal = __retVal_native != 0;
 075            global::System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
 076            return __retVal;
 77            // Local P/Invoke
 78            [global::System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetConsoleMode", Ex
 79            static extern unsafe int __PInvoke(nint __hConsoleHandle_native, uint __dwMode_native);
 080        }
 81    }
 82}