README.md

# ElementTui

An Elixir library to create terminal user interfaces (tui), using [termbox2](https://github.com/termbox/termbox2) under the hood. Elementtui provides the basic building blocks for a TUI. There is a companion library available with more complicated [components](https://codeberg.org/edwinvanl/elementtui_components). Code examples are available in a separate [repository](https://codeberg.org/edwinvanl/elementtui_examples).

## Installation

The package can be installed by adding `elementtui` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:elementtui, "~> 0.5"}
  ]
end
```

## Usage

There are some simple examples available on [codeberg](https://codeberg.org/edwinvanl/elementtui_examples) that should help you get started. Also see the documentation on [hexdocs](https://hexdocs.pm/elementtui/api-reference.html).

Minimal `hello world` example:

```elixir
ElementTui.run_loop(
  fn _, ev -> case ev do
      {:key,  _, _, ?q} ->
        # Exit the programme when q is pressed
        {:halt, nil}

      _ ->
        ElementTui.Element.text("Hello World")
        |> ElementTui.render(5, 5, 100, 1)

        ElementTui.present()

        {:cont, nil}
    end
  end,
  nil,
  timeout: 1000
)
```

Note that when starting your own project you need to make sure elixir/erlang does not listen for input, by setting [-noinput](https://codeberg.org/edwinvanl/elementtui_examples/src/branch/main/mix.exs#L14) in the `mix.exs` file. 

Two panels with a dividing line:

```elixir
ElementTui.run_loop(
  fn _, ev -> case ev do
      {:key,  _, _, ?q} ->
        # Exit the programme when q is pressed
        {:halt, nil}

      _ ->
        [
          Element.text("Text in pane 1") |> Element.wrap() |> Element.flex(:horizontal),
          Element.text("Text in pane 2") |> Element.wrap() |> Element.flex(:horizontal)
        ]
        # Dividing line
        |> Enum.intersperse(Element.text("┃") |> Element.vfill())
        |> Element.hbox()
        |> ElementTui.render(0, 0, ElementTui.width(), ElementTui.height())

        ElementTui.present()

        {:cont, nil}
    end
  end,
  nil,
  timeout: 1000
)
```

## Examples

For the code to create these examples see [codeberg/elementtui_examples](https://codeberg.org/edwinvanl/elementtui_examples)

### Performance example 

![performance](https://codeberg.org/edwinvanl/elementtui/wiki/raw/uploads/performance.gif)

### Popup example

![popup](https://codeberg.org/edwinvanl/elementtui/wiki/raw/uploads/c95e9becc78aff37ec867ad64925082d/popup.gif)


### Component example

Example of the tab and xygraph components. See the [examples](https://codeberg.org/edwinvanl/elementtui_examples) repository for the code.

![component](https://codeberg.org/edwinvanl/elementtui/wiki/raw/uploads/component.gif)

### Rtttex

[rtttex](https://codeberg.org/edwinvanl/rtttex) is a full fledged tui build using the elementtui library.