Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Last active May 28, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otobrglez/d2f757827fa7a1e047743db69371aa05 to your computer and use it in GitHub Desktop.
Save otobrglez/d2f757827fa7a1e047743db69371aa05 to your computer and use it in GitHub Desktop.
Spawning VIM
import cats.effect._
import cats.implicits._
import java.io.PrintWriter
import java.nio.file.Path
object Editor {
type Editor = String
sealed trait EditorError
final case class EditorNotSet(message: String = "The EDITOR environment variable is not set!")
extends Exception(message) with EditorError
private val get: IO[Editor] = IO.fromEither(sys.env.get("EDITOR").toRight(EditorNotSet()))
private def spawnEditor(path: Path)(editor: Editor): IO[(ExitCode, Path)] =
IO.blocking {
val process = Runtime.getRuntime.exec("/bin/bash", null, null)
val printWriter: PrintWriter = new java.io.PrintWriter(process.getOutputStream) {
println(s"$editor $path < /dev/tty > /dev/tty")
close()
}
process.waitFor()
}.map(exitCode => (ExitCode(exitCode), path))
val spawn: Path => IO[(ExitCode, Path)] =
path => get >>= spawnEditor(path)
}
// Usage (returns IO (blocking))
Editor.spawn(Path.of("/tmp/test.md"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment