Last active
January 20, 2022 15:36
-
-
Save ChristopherDavenport/de53c1a34f9d351639e7dde344e59417 to your computer and use it in GitHub Desktop.
Functional Term Size
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
object Winch { | |
// https://nodejs.org/api/tty.html | |
case class Size(cols: Int, lines: Int) | |
def build[F[_]: Async]: Resource[F, fs2.concurrent.Signal[F, Size]] = | |
Resource.eval(getCurrentSizeExternal).flatMap{ | |
Stream.repeatEval(singleEvent >> getCurrentSizeExternal) | |
.holdResource(_) | |
} | |
def getCurrentSizeExternal[F[_]: Sync]: F[Size] = { | |
Sync[F].delay{ | |
// Side-effect to call tput cols/lines | |
val cols = TTY.consoleDim("cols") | |
val lines = TTY.consoleDim("lines") | |
Size(cols, lines) | |
} | |
} | |
import sun.misc.{Signal, SignalHandler} | |
def singleEvent[F[_]: Async]: F[Unit] = Async[F].async{ cb => | |
Async[F].delay{ | |
val terminalSizeChangedHandler: SignalHandler = { | |
new SignalHandler { | |
override def handle(sig: Signal): Unit = { | |
cb(Right(())) | |
} | |
} | |
} | |
Signal.handle(new Signal("WINCH"), terminalSizeChangedHandler) | |
None | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment