Skip to content

Commit 913fac8

Browse files
committed
swiftlint: add a Windows port
This enables building the swiftlint command on Windows. There is no system ioctl for terminal access, instead, we can use the Win32 Console API surface to query the console size. In the case of a failure, assume the width to be 80-columns (as the standard VGA console is 80x25).
1 parent 10caa70 commit 913fac8

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Source/SwiftLintFramework/Configuration+CommandLine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private func fileCount(from envVar: String) throws(SwiftLintError) -> Int {
6969
return count
7070
}
7171

72-
#if os(Linux)
72+
#if os(Linux) || os(Windows)
7373
private func autoreleasepool<T>(block: () -> T) -> T { block() }
7474
#endif
7575

Source/SwiftLintFramework/LintOrAnalyzeCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ private actor CorrectionsBuilder {
444444
}
445445

446446
private func memoryUsage() -> String? {
447-
#if os(Linux)
447+
#if os(Linux) || os(Windows)
448448
return nil
449449
#else
450450
var info = mach_task_basic_info()

Source/SwiftLintFramework/ProgressBar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ actor ProgressBar {
5252
}
5353
}
5454

55-
#if os(Linux)
55+
#if os(Linux) || os(Windows)
5656
// swiftlint:disable:next identifier_name
5757
private let NSEC_PER_SEC = 1_000_000_000
5858
#endif

Source/swiftlint/Commands/Rules.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import ArgumentParser
22
import Foundation
33
import SwiftLintFramework
44
import SwiftyTextTable
5+
#if os(Windows)
6+
import WinSDK
7+
#endif
58

69
private typealias SortedRules = [(String, any Rule.Type)]
710

@@ -141,12 +144,20 @@ private extension TextTable {
141144

142145
private struct Terminal {
143146
static func currentWidth() -> Int {
147+
#if os(Windows)
148+
var csbi = CONSOLE_SCREEN_BUFFER_INFO()
149+
guard GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) else {
150+
return 80
151+
}
152+
return Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1
153+
#else
144154
var size = winsize()
145155
#if os(Linux)
146156
_ = ioctl(CInt(STDOUT_FILENO), UInt(TIOCGWINSZ), &size)
147157
#else
148158
_ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)
149159
#endif
150160
return Int(size.ws_col)
161+
#endif
151162
}
152163
}

0 commit comments

Comments
 (0)