Skip to content

Commit c916dbd

Browse files
authored
port to Windows (#42)
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.
1 parent 4d57c8c commit c916dbd

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
#if canImport(Glibc)
1313
import Glibc
1414
let _exit: (Int32) -> Never = Glibc.exit
15-
#else
15+
#elseif canImport(Darwin)
1616
import Darwin
1717
let _exit: (Int32) -> Never = Darwin.exit
18+
#elseif canImport(MSVCRT)
19+
import MSVCRT
20+
let _exit: (Int32) -> Never = ucrt._exit
1821
#endif
1922

2023
/// A type that can be parsed from a program's command-line arguments.

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal struct HelpGenerator {
1313
static var helpIndent = 2
1414
static var labelColumnWidth = 26
1515
static var screenWidth: Int {
16-
_screenWidthOverride ?? _terminalWidth()
16+
_screenWidthOverride ?? _terminalSize().width
1717
}
1818

1919
internal static var _screenWidthOverride: Int? = nil
@@ -226,20 +226,27 @@ import Glibc
226226
func ioctl(_ a: Int32, _ b: Int32, _ p: UnsafeMutableRawPointer) -> Int32 {
227227
ioctl(CInt(a), UInt(b), p)
228228
}
229-
#else
229+
#elseif canImport(Darwin)
230230
import Darwin
231+
#elseif canImport(MSVCRT)
232+
import MSVCRT
233+
import WinSDK
231234
#endif
232235

233-
func _terminalWidth() -> Int {
234-
var w = winsize()
235-
let err = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)
236-
let result = Int(w.ws_col)
237-
return err == 0 && result > 0 ? result : 80
238-
}
236+
func _terminalSize() -> (width: Int, height: Int) {
237+
#if os(Windows)
238+
var csbi: CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO()
239239

240-
func _terminalHeight() -> Int {
240+
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)
241+
return (width: Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1,
242+
height: Int(csbi.srWindow.Bottom - csbi.srWindow.Top) + 1)
243+
#else
241244
var w = winsize()
242245
let err = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)
243-
let result = Int(w.ws_row)
244-
return err == 0 && result > 0 ? result : 25
246+
let width = Int(w.ws_col)
247+
let height = Int(w.ws_row)
248+
guard err == 0 else { return (80, 25) }
249+
return (width: width > 0 ? width : 80,
250+
height: height > 0 ? height : 25)
251+
#endif
245252
}

0 commit comments

Comments
 (0)