layoutz-0.3.2.0: Simple, beautiful CLI output
Copyright(c) 2025 Matthieu Court
LicenseApache-2.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Layoutz

Description

A simple Haskell port of the layoutz library for creating structured terminal layouts.

Synopsis

Core Types

class Element a where Source #

Minimal complete definition

renderElement

Methods

renderElement :: a -> String Source #

width :: a -> Int Source #

height :: a -> Int Source #

Instances

Instances details
Element L Source # 
Instance details

Defined in Layoutz

Element Tree Source # 
Instance details

Defined in Layoutz

class HasBorder a where Source #

Typeclass for elements that support customizable borders

Methods

setBorder :: Border -> a -> a Source #

Set the border style for an element

Instances

Instances details
HasBorder L Source # 
Instance details

Defined in Layoutz

Methods

setBorder :: Border -> L -> L Source #

data Color Source #

Instances

Instances details
Show Color Source # 
Instance details

Defined in Layoutz

Methods

showsPrec :: Int -> Color -> ShowS #

show :: Color -> String #

showList :: [Color] -> ShowS #

Eq Color Source # 
Instance details

Defined in Layoutz

Methods

(==) :: Color -> Color -> Bool #

(/=) :: Color -> Color -> Bool #

data Style Source #

Instances

Instances details
Monoid Style Source # 
Instance details

Defined in Layoutz

Methods

mempty :: Style #

mappend :: Style -> Style -> Style #

mconcat :: [Style] -> Style #

Semigroup Style Source #

Combine styles using ++

Instance details

Defined in Layoutz

Methods

(<>) :: Style -> Style -> Style #

sconcat :: NonEmpty Style -> Style #

stimes :: Integral b => b -> Style -> Style #

Show Style Source # 
Instance details

Defined in Layoutz

Methods

showsPrec :: Int -> Style -> ShowS #

show :: Style -> String #

showList :: [Style] -> ShowS #

Eq Style Source # 
Instance details

Defined in Layoutz

Methods

(==) :: Style -> Style -> Bool #

(/=) :: Style -> Style -> Bool #

data L Source #

L is the universal layout element type - a type-erased wrapper for the DSL.

This allows mixing different element types in layouts while providing a common interface. Uses existential quantification to store any Element type inside L.

Constructors: * L a - Wraps any Element (Text, Box, Table, etc.) * UL [L] - Special case for unordered lists (allows nesting) * AutoCenter L - Smart centering that adapts to layout context width * LBox, LStatusCard, LTable - Specialized constructors for bordered elements

Example usage: layout [text "title", box "content" [...], center (text "footer")] All different types unified as L, so they can be composed together.

Instances

Instances details
IsString L Source #

Enable string literals to be used directly as elements with OverloadedStrings

With OverloadedStrings enabled, you can write: layout [Hello, World] instead of layout [text Hello, text World]

Instance details

Defined in Layoutz

Methods

fromString :: String -> L #

Show L Source # 
Instance details

Defined in Layoutz

Methods

showsPrec :: Int -> L -> ShowS #

show :: L -> String #

showList :: [L] -> ShowS #

Element L Source # 
Instance details

Defined in Layoutz

HasBorder L Source # 
Instance details

Defined in Layoutz

Methods

setBorder :: Border -> L -> L Source #

data Tree Source #

Tree structure for hierarchical data

Constructors

Tree String [Tree] 

Instances

Instances details
Element Tree Source # 
Instance details

Defined in Layoutz

Basic Elements

layout :: [L] -> L Source #

Layout Functions

center :: Element a => a -> L Source #

center' :: Element a => Int -> a -> L Source #

Center element within specified width

row :: [L] -> L Source #

tightRow :: [L] -> L Source #

Create horizontal row with no spacing between elements (for gradients, etc.)

underline :: Element a => a -> L Source #

underline' :: Element a => String -> a -> L Source #

Add underline with custom character

underlineColored :: Element a => String -> Color -> a -> L Source #

Add colored underline with custom character and color

Example usage: underlineColored "=" ColorRed $ text "Error Section" underlineColored "~" ColorGreen $ text Success underlineColored "─" ColorBrightCyan $ text Info

alignLeft :: Int -> String -> L Source #

Align text to the left within specified width

alignRight :: Int -> String -> L Source #

Align text to the right within specified width

alignCenter :: Int -> String -> L Source #

Align text to the center within specified width

justify :: Int -> String -> L Source #

Justify text (spread words evenly to fill width)

wrap :: Int -> String -> L Source #

Wrap text to multiple lines with specified width

Containers

box :: String -> [L] -> L Source #

Widgets

ul :: [L] -> L Source #

ol :: [L] -> L Source #

table :: [String] -> [[L]] -> L Source #

Create table with headers and rows

section :: String -> [L] -> L Source #

Create section with title and content

section' :: String -> String -> [L] -> L Source #

Create section with custom glyph

section'' :: String -> String -> Int -> [L] -> L Source #

Create section with custom glyph and flanking chars

kv :: [(String, String)] -> L Source #

Create key-value pairs

tree :: String -> [Tree] -> L Source #

Create tree structure

leaf :: String -> Tree Source #

Create leaf tree node (no children)

branch :: String -> [Tree] -> Tree Source #

Create branch tree node with children

Visual Elements

margin :: String -> [L] -> L Source #

Create margin with custom prefix

Example usage: margin "[error]" [text "Something went wrong"] margin "[info]" [text "FYI: Check the logs"]

hr :: L Source #

Horizontal rule with default character and width

hr' :: String -> L Source #

Horizontal rule with custom character

hr'' :: String -> Int -> L Source #

Horizontal rule with custom character and width

vr :: L Source #

Vertical rule with default character and height

vr' :: String -> L Source #

Vertical rule with custom character

vr'' :: String -> Int -> L Source #

Vertical rule with custom character and height

pad :: Element a => Int -> a -> L Source #

Add padding around element

chart :: [(String, Double)] -> L Source #

Create horizontal bar chart

Spinners

spinner :: String -> Int -> SpinnerStyle -> L Source #

Create an animated spinner

Example usage: spinner Loading 5 SpinnerDots -- Shows the 5th frame of dots spinner spinner Processing 0 SpinnerLine -- Shows first frame with label

Increment the frame number each render to animate: layout [spinner Working (tickCount mod 10) SpinnerDots]

data SpinnerStyle Source #

Spinner style with animation frames

Instances

Instances details
Show SpinnerStyle Source # 
Instance details

Defined in Layoutz

Eq SpinnerStyle Source # 
Instance details

Defined in Layoutz

Visualizations

plotSparkline :: [Double] -> L Source #

Create a sparkline from a list of values

data Series Source #

A data series for line plots: points, label, color

Constructors

Series [(Double, Double)] String Color 

plotLine :: Int -> Int -> [Series] -> L Source #

Create a braille line plot

data Slice Source #

A slice of a pie chart: value, label, color

Constructors

Slice Double String Color 

plotPie :: Int -> Int -> [Slice] -> L Source #

Create a braille pie chart

data BarItem Source #

A bar item: value, label, color

Constructors

BarItem Double String Color 

plotBar :: Int -> Int -> [BarItem] -> L Source #

Create a vertical bar chart

data StackedBarGroup Source #

A group of stacked bars: segments and group label

plotStackedBar :: Int -> Int -> [StackedBarGroup] -> L Source #

Create a stacked vertical bar chart

data HeatmapData Source #

Heatmap data: grid of values, row labels, column labels

Constructors

HeatmapData [[Double]] [String] [String] 

plotHeatmap :: HeatmapData -> L Source #

Create a heatmap with default cell width (6)

plotHeatmap' :: Int -> HeatmapData -> L Source #

Create a heatmap with custom cell width

Border utilities

withBorder :: Border -> L -> L Source #

Apply a border style to elements that support borders

Elements that support borders: box, statusCard, table Other elements are returned unchanged

Example usage: withBorder BorderDouble $ table [Name] [[text Alice]]

Color utilities

withColor :: Color -> L -> L Source #

Apply a color to an element

Example usage: withColor ColorBrightYellow $ box Warning [text "Check logs"]

Style utilities

withStyle :: Style -> L -> L Source #

Apply a style to an element Example usage: withStyle StyleBold $ text "Important!"

Rendering

TUI Runtime

data LayoutzApp state msg Source #

The core application structure - Elm Architecture style

Build interactive TUI apps by defining: * Initial state and startup commands * How to update state based on messages * What events to subscribe to * How to render state to UI

Example: @ data CounterMsg = Inc | Dec

counterApp :: LayoutzApp Int CounterMsg counterApp = LayoutzApp { appInit = (0, CmdNone) , appUpdate = \msg count -> case msg of Inc -> (count + 1, CmdNone) Dec -> (count - 1, CmdNone) , appSubscriptions = \_ -> subKeyPress $ \key -> case key of KeyChar + -> Just Inc KeyChar - -> Just Dec _ -> Nothing , appView = \count -> layout [text $ "Count: " <> show count] } @

Constructors

LayoutzApp 

Fields

  • appInit :: (state, Cmd msg)

    Initial state and startup commands

  • appUpdate :: msg -> state -> (state, Cmd msg)

    Update state with message, return new state and commands

  • appSubscriptions :: state -> Sub msg

    Declare event subscriptions based on current state

  • appView :: state -> L

    Render state to UI

data Key Source #

Keyboard input representation

Constructors

KeyChar Char

Regular character keys: a, '1', ' ', etc.

KeyCtrl Char

Ctrl+key: KeyCtrl C, KeyCtrl Q, etc.

KeyEnter

Enter/Return key

KeyBackspace

Backspace key

KeyTab

Tab key

KeyEscape

Escape key

KeyDelete

Delete key

KeyUp

Up arrow

KeyDown

Down arrow

KeyLeft

Left arrow

KeyRight

Right arrow

KeySpecial String

Other unrecognized escape sequences

Instances

Instances details
Show Key Source # 
Instance details

Defined in Layoutz

Methods

showsPrec :: Int -> Key -> ShowS #

show :: Key -> String #

showList :: [Key] -> ShowS #

Eq Key Source # 
Instance details

Defined in Layoutz

Methods

(==) :: Key -> Key -> Bool #

(/=) :: Key -> Key -> Bool #

data Cmd msg Source #

Commands - side effects the runtime will execute after each update

Constructors

CmdNone

No effect

CmdRun (IO (Maybe msg))

Run IO, optionally produce a message

CmdBatch [Cmd msg]

Combine multiple commands

cmdFire :: IO () -> Cmd msg Source #

Create a command from an IO action (fire and forget)

cmdTask :: IO msg -> Cmd msg Source #

Create a command that produces a message after IO completes

cmdAfterMs :: Int -> msg -> Cmd msg Source #

Create a command that fires a message after a delay

executeCmd :: Cmd msg -> IO (Maybe msg) Source #

Execute a command and return any resulting message

data Sub msg Source #

Subscriptions - event sources your app listens to

Constructors

SubNone

No subscriptions

SubKeyPress (Key -> Maybe msg)

Subscribe to keyboard input

SubEveryMs Int msg

Subscribe to periodic ticks (interval in ms + message)

SubBatch [Sub msg]

Combine multiple subscriptions

data AppOptions Source #

Options for running a LayoutzApp. Use defaultAppOptions and override the fields you care about:

runAppWith defaultAppOptions { optAlignment = AppAlignCenter } myApp

Constructors

AppOptions 

Fields

Instances

Instances details
Show AppOptions Source # 
Instance details

Defined in Layoutz

Eq AppOptions Source # 
Instance details

Defined in Layoutz

defaultAppOptions :: AppOptions Source #

Sensible defaults: left-aligned.

data AppAlignment Source #

App-level alignment within the terminal window

Instances

Instances details
Show AppAlignment Source # 
Instance details

Defined in Layoutz

Eq AppAlignment Source # 
Instance details

Defined in Layoutz

runApp :: LayoutzApp state msg -> IO () Source #

Run an interactive TUI application with default options.

This function: * Sets up raw terminal mode (no echo, no buffering) * Clears screen and hides cursor * Enters event loop that: - Listens to subscribed events (keyboard, ticks, etc.) - Dispatches messages to update function - Updates state and re-renders * Restores terminal on exit (ESC, Ctrl+C, or Ctrl+D)

Press ESC, Ctrl+C, or Ctrl+D to quit the application.

runAppWith :: AppOptions -> LayoutzApp state msg -> IO () Source #

Run an interactive TUI application with custom options.

runAppWith defaultAppOptions { optAlignment = AppAlignCenter } myApp

Subscriptions

subKeyPress :: (Key -> Maybe msg) -> Sub msg Source #

Subscribe to keyboard events

subEveryMs :: Int -> msg -> Sub msg Source #

Subscribe to periodic ticks with custom interval (milliseconds)

subBatch :: [Sub msg] -> Sub msg Source #

Combine multiple subscriptions