Skip to content

Commit

Permalink
Add more docs (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
reibitto authored Feb 9, 2022
1 parent 9a83d0e commit 9c0d562
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/overview/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions docs/overview/interactive_processes.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"overview/overview_index",
"overview/overview_basics",
"overview/overview_piping",
"overview/overview_interactive_processes",
"overview/overview_other"
]
},
Expand Down

0 comments on commit 9c0d562

Please sign in to comment.