Owl.LiveScreen (Owl v0.1.0) View Source
A server that handles live updates in terminal.
It partially implements The Erlang I/O Protocol,
so it is possible to use Owl.LiveScreen as an I/O-device in Logger.Backends.Console
and functions like Owl.IO.puts/2, IO.puts/2. When used as I/O-device, then output is printed above dynamic blocks.
Example
require Logger
Logger.configure_backend(:console, device: Owl.LiveScreen)
Owl.LiveScreen.add_block(:dependency,
state: :init,
render: fn
:init -> "init..."
dependency -> ["dependency: ", Owl.Tag.new(dependency, :yellow)]
end
)
Owl.LiveScreen.add_block(:compiling,
render: fn
:init -> "init..."
filename -> ["compiling: ", Owl.Tag.new(to_string(filename), :cyan)]
end
)
["ecto", "phoenix", "ex_doc", "broadway"]
|> Enum.each(fn dependency ->
Owl.LiveScreen.update(:dependency, dependency)
1..5
|> Enum.map(&"filename#{&1}.ex")
|> Enum.each(fn filename ->
Owl.LiveScreen.update(:compiling, filename)
Process.sleep(1000)
Logger.debug("#{filename} compiled for dependency #{dependency}")
end)
end)
Link to this section Summary
Link to this section Types
Specs
add_block_option() ::
{:state, any()} | {:render, (block_state :: any() -> Owl.Data.t())}
Specs
block_id() :: any()
Specs
start_option() ::
{:name, GenServer.name()}
| {:refresh_every, pos_integer()}
| {:terminal_width, pos_integer() | :auto}
Link to this section Functions
Specs
add_block(GenServer.server(), block_id(), add_block_option()) :: :ok
Adds a sticky block to the bottom of the screen that can be updated using update/3.
Options
:render- a function that acceptsstateand returns a view of the block. Defaults toFunction.identity/1, which means that state has to have typeOwl.Data.t/0.:state- initial state of the block. Defaults tonil.
Example
Owl.LiveScreen.add_block(:footer, state: "starting...")
# which is equivalent to
Owl.LiveScreen.add_block(:footer, render: fn
nil -> "starting..."
data -> data
end)
Specs
flush(GenServer.server()) :: :ok
Renders data in buffer and detaches blocks.
Specs
start_link([start_option()]) :: GenServer.on_start()
Starts a server.
Server is started automatically by :owl application as a named process.
Options
:name- used for name registration as described in the "Name registration" section in the documentation forGenServer. Defaults toOwl.LiveScreen:refresh_every- a period of refreshing a screen in milliseconds. Defaults to 100.:terminal_width- a width of terminal in symbols. Defaults to:auto, which gets value fromOwl.IO.columns/0. If terminal is now a available, then the server won't be started.
Specs
stop(GenServer.server()) :: :ok
Renders data in buffer and terminates a server.
Specs
update(GenServer.server(), block_id(), block_state :: any()) :: :ok
Updates a state of the block for using it in the next render iteration.
Example
Owl.LiveScreen.add_block(:footer, "starting...")
Process.sleep(1000)
Owl.LiveScreen.update(:footer, "...almost done...")
Process.sleep(1000)
Owl.LiveScreen.update(:footer, "done!!!")