Skip to content

Commit eeeca0a

Browse files
authored
Merge pull request #105 from compnerd/wide
TSCBasic: implement support for width in the terminal
2 parents 803f9b9 + 5b34700 commit eeeca0a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Sources/TSCBasic/TerminalController.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ public final class TerminalController {
121121
///
122122
/// - Returns: Current width of terminal if it was determinable.
123123
public static func terminalWidth() -> Int? {
124+
#if os(Windows)
125+
var csbi: CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO()
126+
if !GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) {
127+
// GetLastError()
128+
return nil
129+
}
130+
return Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1
131+
#else
124132
// Try to get from environment.
125133
if let columns = ProcessEnv.vars["COLUMNS"], let width = Int(columns) {
126134
return width
@@ -130,13 +138,14 @@ public final class TerminalController {
130138
// Following code does not compile on ppc64le well. TIOCGWINSZ is
131139
// defined in system ioctl.h file which needs to be used. This is
132140
// a temporary arrangement and needs to be fixed.
133-
#if !(arch(powerpc64le) || os(Windows))
141+
#if !arch(powerpc64le)
134142
var ws = winsize()
135143
if ioctl(1, UInt(TIOCGWINSZ), &ws) == 0 {
136144
return Int(ws.ws_col)
137145
}
138146
#endif
139147
return nil
148+
#endif
140149
}
141150

142151
/// Flushes the stream.

0 commit comments

Comments
 (0)