From f2af25c5c12f2c8fe7212f2ee060ec33f7bad918 Mon Sep 17 00:00:00 2001 From: abdulras Date: Mon, 2 Mar 2020 17:18:05 +0000 Subject: [PATCH] port to Windows This adjusts the imports to enable building for Windows. The major change here is that `_terminalHeight` and `_terminalWidth` have been merged into a `_terminalSize` which returns a tuple of the width and the height. This is then implemented for Windows as well. --- .../Parsable Types/ParsableArguments.swift | 5 +++- .../ArgumentParser/Usage/HelpGenerator.swift | 29 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift b/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift index 9e48df80b..32fd0e583 100644 --- a/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift +++ b/Sources/ArgumentParser/Parsable Types/ParsableArguments.swift @@ -12,9 +12,12 @@ #if canImport(Glibc) import Glibc let _exit: (Int32) -> Never = Glibc.exit -#else +#elseif canImport(Darwin) import Darwin let _exit: (Int32) -> Never = Darwin.exit +#elseif canImport(MSVCRT) +import MSVCRT +let _exit: (Int32) -> Never = ucrt._exit #endif /// A type that can be parsed from a program's command-line arguments. diff --git a/Sources/ArgumentParser/Usage/HelpGenerator.swift b/Sources/ArgumentParser/Usage/HelpGenerator.swift index ca7dc1610..3d9acc718 100644 --- a/Sources/ArgumentParser/Usage/HelpGenerator.swift +++ b/Sources/ArgumentParser/Usage/HelpGenerator.swift @@ -13,7 +13,7 @@ internal struct HelpGenerator { static var helpIndent = 2 static var labelColumnWidth = 26 static var screenWidth: Int { - _screenWidthOverride ?? _terminalWidth() + _screenWidthOverride ?? _terminalSize().width } internal static var _screenWidthOverride: Int? = nil @@ -252,20 +252,27 @@ import Glibc func ioctl(_ a: Int32, _ b: Int32, _ p: UnsafeMutableRawPointer) -> Int32 { ioctl(CInt(a), UInt(b), p) } -#else +#elseif canImport(Darwin) import Darwin +#elseif canImport(MSVCRT) +import MSVCRT +import WinSDK #endif -func _terminalWidth() -> Int { - var w = winsize() - let err = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) - let result = Int(w.ws_col) - return err == 0 && result > 0 ? result : 80 -} +func _terminalSize() -> (width: Int, height: Int) { +#if os(Windows) + var csbi: CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO() -func _terminalHeight() -> Int { + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) + return (width: Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1, + height: Int(csbi.srWindow.Bottom - csbi.srWindow.Top) + 1) +#else var w = winsize() let err = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) - let result = Int(w.ws_row) - return err == 0 && result > 0 ? result : 25 + let width = Int(w.ws_col) + let height = Int(w.ws_row) + guard err == 0 else { return (80, 25) } + return (width: width > 0 ? width : 80, + height: height > 0 ? height : 25) +#endif }