-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Its own set of implementations for obtaining the terminal size on eac…
…h platform
- Loading branch information
Showing
11 changed files
with
103 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
mosaic-runtime/src/appleMain/kotlin/com/jakewharton/mosaic/platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import com.jakewharton.mosaic.ui.unit.IntSize | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.memScoped | ||
import platform.posix.STDIN_FILENO | ||
import platform.posix.TIOCGWINSZ | ||
import platform.posix.ioctl | ||
import platform.posix.winsize | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual fun getPlatformTerminalSize(): IntSize = memScoped { | ||
val size = alloc<winsize>() | ||
if (ioctl(STDIN_FILENO, TIOCGWINSZ, size) < 0) { | ||
IntSize.Zero | ||
} else { | ||
IntSize(width = size.ws_col.toInt(), height = size.ws_row.toInt()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import com.jakewharton.mosaic.ui.unit.IntSize | ||
|
||
internal expect fun platformDisplay(chars: CharSequence) | ||
|
||
internal expect fun getPlatformTerminalSize(): IntSize |
14 changes: 14 additions & 0 deletions
14
mosaic-runtime/src/jsMain/kotlin/com/jakewharton/mosaic/platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import com.jakewharton.mosaic.ui.unit.IntSize | ||
|
||
private external val process: dynamic | ||
|
||
internal actual fun getPlatformTerminalSize(): IntSize { | ||
val size = process.stdout.getWindowSize() | ||
return if (size == undefined) { | ||
IntSize.Zero | ||
} else { | ||
IntSize(width = size[0] as Int, height = size[1] as Int) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
mosaic-runtime/src/linuxMain/kotlin/com/jakewharton/mosaic/platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import com.jakewharton.mosaic.ui.unit.IntSize | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.memScoped | ||
import platform.linux.ioctl | ||
import platform.posix.STDIN_FILENO | ||
import platform.posix.TIOCGWINSZ | ||
import platform.posix.winsize | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual fun getPlatformTerminalSize(): IntSize = memScoped { | ||
val size = alloc<winsize>() | ||
if (ioctl(STDIN_FILENO, TIOCGWINSZ.toULong(), size) < 0) { | ||
IntSize.Zero | ||
} else { | ||
IntSize(width = size.ws_col.toInt(), height = size.ws_row.toInt()) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
mosaic-runtime/src/mingwMain/kotlin/com/jakewharton/mosaic/platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.jakewharton.mosaic | ||
|
||
import com.jakewharton.mosaic.ui.unit.IntSize | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.alloc | ||
import kotlinx.cinterop.memScoped | ||
import kotlinx.cinterop.ptr | ||
import platform.windows.CONSOLE_SCREEN_BUFFER_INFO | ||
import platform.windows.GetConsoleScreenBufferInfo | ||
import platform.windows.GetStdHandle | ||
import platform.windows.INVALID_HANDLE_VALUE | ||
import platform.windows.STD_OUTPUT_HANDLE | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal actual fun getPlatformTerminalSize(): IntSize = memScoped { | ||
val csbi = alloc<CONSOLE_SCREEN_BUFFER_INFO>() | ||
val stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE) | ||
if (stdoutHandle == INVALID_HANDLE_VALUE) { | ||
return@memScoped IntSize.Zero | ||
} | ||
if (GetConsoleScreenBufferInfo(stdoutHandle, csbi.ptr) == 0) { | ||
return@memScoped IntSize.Zero | ||
} | ||
csbi.srWindow.run { IntSize(width = Right - Left + 1, height = Bottom - Top + 1) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters