Drafter.Terminal.Stdin (drafter v0.6.0)

Copy Markdown View Source

The one reader of standard input on this node.

Reading starts at the first subscribe/2 and continues for the life of the node. Each chunk read is sent to the subscriber as {:stdin, binary}, and :stdin_closed is sent when standard input ends. One process is subscribed at a time, and a new subscription replaces the previous one. A subscriber that exits is unsubscribed.

Bytes read while no process is subscribed are held. The next subscriber receives them as one {:stdin, binary} when it subscribes with :keep, and they are dropped when it subscribes with :discard.

Drafter.Terminal.Stdin.subscribe()

receive do
  {:stdin, bytes} -> bytes
end

Once reading has started, every byte of standard input passes through this process. A process that reads standard input another way after that point competes with it.

Summary

Functions

Returns a specification to start this module under a supervisor.

Start the reader's owner and register it under this module's name.

Send standard input to subscriber from now on, replacing any other subscriber.

Stop sending standard input to subscriber.

Types

held()

@type held() :: :keep | :discard

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the reader's owner and register it under this module's name.

subscribe(subscriber \\ self(), held \\ :keep)

@spec subscribe(pid(), held()) :: :ok

Send standard input to subscriber from now on, replacing any other subscriber.

held says what happens to bytes read while nobody was subscribed: :keep (the default) delivers them first, :discard drops them. A subscriber that joins after standard input ended is sent :stdin_closed at once.

unsubscribe(subscriber \\ self())

@spec unsubscribe(pid()) :: :ok

Stop sending standard input to subscriber.

Does nothing when subscriber is not the current subscriber. Reading continues and what is read is held for the next subscriber.