Skip to content

Instantly share code, notes, and snippets.

@ChristopherDavenport
Last active January 20, 2022 15:36
Show Gist options
  • Save ChristopherDavenport/de53c1a34f9d351639e7dde344e59417 to your computer and use it in GitHub Desktop.
Save ChristopherDavenport/de53c1a34f9d351639e7dde344e59417 to your computer and use it in GitHub Desktop.
Functional Term Size
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