< Summary

Line coverage
6%
Covered lines: 2
Uncovered lines: 28
Coverable lines: 30
Total lines: 138
Line coverage: 6.6%
Branch coverage
12%
Covered branches: 1
Total branches: 8
Branch coverage: 12.5%
Method coverage
25%
Covered methods: 1
Fully covered methods: 0
Total methods: 4
Method coverage: 25%
Full method coverage: 0%

Metrics

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

File(s)

/home/runner/work/dotnet-logging/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()
 30    {
 331        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 32        {
 333            return false;
 34        }
 35
 036        var handle = GetStdHandle(StdOutputHandle);
 037        if (handle == IntPtr.Zero)
 38        {
 039            return false;
 40        }
 41
 042        if (!GetConsoleMode(handle, out var mode))
 43        {
 044            return false;
 45        }
 46
 047        if ((mode & EnableVirtualTerminalProcessingHex) != 0)
 48        {
 049            return true;
 50        }
 51
 052        var newMode = mode | EnableVirtualTerminalProcessingHex | DisableNewlineAutoReturn;
 53
 054        return SetConsoleMode(handle, newMode);
 55    }
 56}

/home/runner/work/dotnet-logging/dotnet-logging/src/obj/Release/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.14.374
 7        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
 8        private static partial nint GetStdHandle(int nStdHandle)
 9        {
 10            int __lastError;
 11            nint __retVal;
 12            {
 013                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
 014                __retVal = __PInvoke(nStdHandle);
 015                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
 16            }
 17
 018            global::System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
 019            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);
 23        }
 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.14.374
 31        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
 32        private static partial bool GetConsoleMode(nint hConsoleHandle, out uint lpMode)
 33        {
 34            int __lastError;
 035            lpMode = default;
 36            bool __retVal;
 37            int __retVal_native;
 38            // Pin - Pin data in preparation for calling the P/Invoke.
 039            fixed (uint* __lpMode_native = &lpMode)
 40            {
 041                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
 042                __retVal_native = __PInvoke(hConsoleHandle, __lpMode_native);
 043                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
 44            }
 45
 46            // Unmarshal - Convert native data to managed data.
 047            __retVal = __retVal_native != 0;
 048            global::System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
 049            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);
 53        }
 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.14.374
 61        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
 62        private static partial bool SetConsoleMode(nint hConsoleHandle, uint dwMode)
 63        {
 64            int __lastError;
 65            bool __retVal;
 66            int __retVal_native;
 67            {
 068                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
 069                __retVal_native = __PInvoke(hConsoleHandle, dwMode);
 070                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
 71            }
 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);
 80        }
 81    }
 82}