Skip to content

TSCBasic: implement support for width in the terminal #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/TSCBasic/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ public final class TerminalController {
///
/// - Returns: Current width of terminal if it was determinable.
public static func terminalWidth() -> Int? {
#if os(Windows)
var csbi: CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO()
if !GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) {
// GetLastError()
return nil
}
return Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1
#else
// Try to get from environment.
if let columns = ProcessEnv.vars["COLUMNS"], let width = Int(columns) {
return width
Expand All @@ -130,13 +138,14 @@ public final class TerminalController {
// Following code does not compile on ppc64le well. TIOCGWINSZ is
// defined in system ioctl.h file which needs to be used. This is
// a temporary arrangement and needs to be fixed.
#if !(arch(powerpc64le) || os(Windows))
#if !arch(powerpc64le)
var ws = winsize()
if ioctl(1, UInt(TIOCGWINSZ), &ws) == 0 {
return Int(ws.ws_col)
}
#endif
return nil
#endif
}

/// Flushes the stream.
Expand Down