Packages
ElementTuiComponents: TUI component companion library for ElementTui.
Current section
Files
Jump to
Current section
Files
lib/components/tablist.ex
defmodule ElementTui.Components.Tab do
alias ElementTui.Components.Tab
defstruct id: nil, render: nil
def new(id, func) do
%Tab{id: id, render: func}
end
end
defmodule ElementTui.Components.TabList do
alias ElementTui.Element
alias ElementTui.FocusStream
@side_chars {"│", "│", "┃", "┃"}
def new(tabs) do
FocusStream.new(tabs)
end
def next(state) do
FocusStream.next(state, bound: true)
end
def prev(state) do
FocusStream.prev(state, bound: true)
end
def render(state, x, y, width, height) do
# Render the tabs
xs =
state
|> FocusStream.map(fn tb, is_cursor ->
case is_cursor do
true ->
Element.text_line(elem(@side_chars, 2) <> tb.id <> elem(@side_chars, 3))
|> Element.bold()
false ->
Element.text_line(elem(@side_chars, 0) <> tb.id <> elem(@side_chars, 1))
end
end)
ElementTui.render(Element.hbox(xs), x, y, width, 1)
# Render the active tab
tb = FocusStream.peek(state)
tb.render.(x, y + 1, width, height - 1)
end
end