Drafter.Terminal.RawMode (drafter v0.6.0)

Copy Markdown View Source

Puts the controlling terminal into raw mode and takes it out again.

enter/0 returns how raw mode was entered, and leave/1 takes that value back to undo it:

{:ok, mode} = Drafter.Terminal.RawMode.enter()
Drafter.Terminal.RawMode.leave(mode)

The termios NIF is used where it loads and stty otherwise. Off unix neither runs, enter/0 returns {:ok, nil}, and the terminal mode is left alone.

In raw mode input arrives unbuffered and unechoed, control characters such as Ctrl+C arrive as input instead of raising signals, and a line feed written to the terminal no longer returns the cursor to the first column.

Summary

Types

How raw mode was entered: by the NIF, by stty with the saved settings, or not at all.

Functions

Enter raw mode.

Restore the terminal settings saved by the enter/0 that returned mode.

Types

mode()

@type mode() :: :nif | {:stty, binary()} | nil

How raw mode was entered: by the NIF, by stty with the saved settings, or not at all.

Functions

enter()

@spec enter() :: {:ok, mode()} | {:error, term()}

Enter raw mode.

{:error, :not_a_terminal} means standard input is not a terminal, and {:error, {:stty_failed, output}} that stty refused.

leave(arg1)

@spec leave(mode()) :: :ok

Restore the terminal settings saved by the enter/0 that returned mode.