Last active
May 28, 2021 21:30
-
-
Save otobrglez/d2f757827fa7a1e047743db69371aa05 to your computer and use it in GitHub Desktop.
Spawning VIM
This file contains hidden or 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
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