From 9c0d5620f798abfb35069142c7958fddf111796e Mon Sep 17 00:00:00 2001 From: reibitto Date: Wed, 9 Feb 2022 18:22:29 +0900 Subject: [PATCH] Add more docs (#233) --- docs/overview/index.md | 1 + docs/overview/interactive_processes.md | 30 ++++++++++++++++++++++++++ website/sidebars.json | 1 + 3 files changed, 32 insertions(+) create mode 100644 docs/overview/interactive_processes.md diff --git a/docs/overview/index.md b/docs/overview/index.md index e5b86870..e3cc1b59 100644 --- a/docs/overview/index.md +++ b/docs/overview/index.md @@ -8,6 +8,7 @@ Here's list of contents available: - **[Basics](basics.md)** — Creating a description of a command and transforming its output - **[Piping](piping.md)** — Creating a pipeline of commands + - **[Interactive Processes](interactive_processes.md)** — Communicating with an interactive process - **[Other](other.md)** — Miscellaneous operations such as settings the working direction, inheriting I/O, etc. ## Installation diff --git a/docs/overview/interactive_processes.md b/docs/overview/interactive_processes.md new file mode 100644 index 00000000..4778be3f --- /dev/null +++ b/docs/overview/interactive_processes.md @@ -0,0 +1,30 @@ +--- +id: overview_interactive_processes +title: "Interactive Processes" +--- + +Sometimes you want to interact with a process in a back-and-forth manner by sending requests to the process and receiving responses back. For example, interacting with a repl-like process like `node -i`, `python -i`, etc. or an ssh server. + +Here is an example of communicating with an interactive Python shell: + +```scala mdoc:invisible +import zio._ +import zio.process._ +import java.nio.charset.StandardCharsets +``` + +```scala mdoc:silent +for { + commandQueue <- Queue.unbounded[Chunk[Byte]] + process <- Command("python", "-qi").stdin(ProcessInput.fromQueue(commandQueue)).run + _ <- process.stdout.linesStream.foreach { response => + ZIO.debug(s"Response from REPL: $response") + }.forkDaemon + _ <- commandQueue.offer(Chunk.fromArray("1+1\n".getBytes(StandardCharsets.UTF_8))) + _ <- commandQueue.offer(Chunk.fromArray("2**8\n".getBytes(StandardCharsets.UTF_8))) + _ <- commandQueue.offer(Chunk.fromArray("import random\nrandom.randint(1, 100)\n".getBytes(StandardCharsets.UTF_8))) + _ <- commandQueue.offer(Chunk.fromArray("exit()\n".getBytes(StandardCharsets.UTF_8))) +} yield () +``` + +You would probably want to create a helper for the repeated code, but this just a minimal example to help get you started. \ No newline at end of file diff --git a/website/sidebars.json b/website/sidebars.json index 4bc3cded..17bdfbac 100755 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -4,6 +4,7 @@ "overview/overview_index", "overview/overview_basics", "overview/overview_piping", + "overview/overview_interactive_processes", "overview/overview_other" ] },