| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | OutputChannel.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Byte sink used by Terminal. Each WriteString call is a single WriteConsoleW |
| 12 | or fwprintf. For console destinations the channel owns VT enablement (RAII). |
| 13 | |
| 14 | --*/ |
| 15 | #pragma once |
| 16 | |
| 17 | #include "VTSupport.h" |
| 18 | #include "defs.h" |
| 19 | |
| 20 | #include <cstdio> |
| 21 | #include <optional> |
| 22 | #include <string_view> |
| 23 | #include <Windows.h> |
| 24 | |
| 25 | namespace wsl::windows::wslc { |
| 26 | |
| 27 | class OutputChannel |
| 28 | { |
| 29 | public: |
| 30 | NON_COPYABLE(OutputChannel); |
| 31 | NON_MOVABLE(OutputChannel); |
| 32 | |
| 33 | // Console path: probes handle, enables VT; falls back to fallbackFile when redirected. |
| 34 | OutputChannel(HANDLE consoleHandle, FILE* fallbackFile); |
| 35 | |
| 36 | // FILE* path with explicit VT override (for tests). |
| 37 | OutputChannel(FILE* file, bool vtOverride); |
| 38 | |
| 39 | void WriteString(std::wstring_view text) const; |
| 40 | |
| 41 | // Flushes buffered output so a prompt is visible before a blocking read. No-op for |
| 42 | // console destinations (WriteConsoleW is unbuffered); flushes the CRT stream otherwise. |
| 43 | void Flush() const; |
| 44 | |
| 45 | // Console write width minus one (autowrap guard), or nullopt when redirected. |
| 46 | std::optional<int> GetConsoleWidth() const; |
| 47 | |
| 48 | bool IsVTEnabled() const noexcept; |
| 49 | |
| 50 | private: |
| 51 | HANDLE m_consoleHandle = nullptr; |
| 52 | FILE* m_file = nullptr; |
| 53 | bool m_vtOverride = false; |
| 54 | std::optional<wsl::windows::common::vt::EnableVirtualTerminal> m_vtMode; |
| 55 | }; |
| 56 | |
| 57 | } // namespace wsl::windows::wslc |