termbox-banana-0.2.0: reactive-banana + termbox

Safe HaskellNone
LanguageHaskell2010

Termbox.Banana

Contents

Synopsis

Documentation

See the bottom of this module for a simple, runnable example to get started.

Here's how to run the examples with cabal:

cabal v2-run --constraint "termbox-banana +build-examples" termbox-banana-example-echo
cabal v2-run --constraint "termbox-banana +build-examples" termbox-banana-example-hoogle

This module is intended to be imported qualified.

import qualified Termbox.Banana as Termbox

type TermboxEvent = Event Source #

A termbox event. This type alias exists only for Haddock readability; in code, you are encouraged to use

  • Event for reactive-banana events
  • Termbox.Event for termbox events

Since: 0.1.0

run Source #

Run a termbox program with the specified input and output modes.

Given

  • the terminal event stream
  • the time-varying terminal size (width, then height)

return

  • a time-varying scene to render
  • an event stream of arbitrary values, only the first of which is relevant, which ends the termbox program and returns from the main action.

Since: 0.2.0

run_ Source #

Arguments

:: InputMode 
-> OutputMode 
-> (Event TermboxEvent -> Behavior (Int, Int) -> MomentIO (Behavior Scene, Event a)) 
-> IO a 

Like run, but throws InitErrors as IO exceptions.

Since: 0.2.0

set :: Int -> Int -> Cell -> Cells Source #

Set a single cell's value (column, then row).

Since: 0.1.0

data Scene Source #

A scene to render; a grid of cells and a cursor.

Since: 0.1.0

Constructors

Scene !Cells !Cursor 

data Cells Source #

A grid of cells.

Create a Cells with set or mempty and combine them with (<>).

Since: 0.1.0

Instances
Semigroup Cells Source # 
Instance details

Defined in Termbox.Banana

Methods

(<>) :: Cells -> Cells -> Cells #

sconcat :: NonEmpty Cells -> Cells #

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

Monoid Cells Source # 
Instance details

Defined in Termbox.Banana

Methods

mempty :: Cells #

mappend :: Cells -> Cells -> Cells #

mconcat :: [Cells] -> Cells #

data Cursor Source #

A cursor.

Since: 0.1.0

Constructors

Cursor !Int !Int

Column, then row

NoCursor 

Re-exports

black :: Attr #

black = 1.

red :: Attr #

red = 2.

green :: Attr #

green = 3.

yellow :: Attr #

yellow = 4.

blue :: Attr #

blue = 5.

magenta :: Attr #

magenta = 6.

cyan :: Attr #

cyan = 7.

white :: Attr #

white = 8.

bold :: Attr #

Bold modifier attribute.

underline :: Attr #

Underline modifier attribute.

reverse :: Attr #

Reverse modifier attribute.

data Attr #

A cell attribute, which includes its color, and whether or not it is bold, underlined, and/or reversed.

A cell can only have one color, but may be (for example) bold and underlined. The Monoid instance combines Attrs this way, with a right bias.

Instances
Eq Attr 
Instance details

Defined in Termbox

Methods

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

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

Num Attr

Provided for numeric literals.

Instance details

Defined in Termbox

Methods

(+) :: Attr -> Attr -> Attr #

(-) :: Attr -> Attr -> Attr #

(*) :: Attr -> Attr -> Attr #

negate :: Attr -> Attr #

abs :: Attr -> Attr #

signum :: Attr -> Attr #

fromInteger :: Integer -> Attr #

Semigroup Attr

Left-biased color; attributes are merged.

Instance details

Defined in Termbox

Methods

(<>) :: Attr -> Attr -> Attr #

sconcat :: NonEmpty Attr -> Attr #

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

Monoid Attr 
Instance details

Defined in Termbox

Methods

mempty :: Attr #

mappend :: Attr -> Attr -> Attr #

mconcat :: [Attr] -> Attr #

data Cell #

A Cell contains a character, foreground attribute, and background attribute.

Constructors

Cell !Char !Attr !Attr 
Instances
Eq Cell 
Instance details

Defined in Termbox

Methods

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

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

Show Cell 
Instance details

Defined in Termbox

Methods

showsPrec :: Int -> Cell -> ShowS #

show :: Cell -> String #

showList :: [Cell] -> ShowS #

Storable Cell 
Instance details

Defined in Termbox

Methods

sizeOf :: Cell -> Int #

alignment :: Cell -> Int #

peekElemOff :: Ptr Cell -> Int -> IO Cell #

pokeElemOff :: Ptr Cell -> Int -> Cell -> IO () #

peekByteOff :: Ptr b -> Int -> IO Cell #

pokeByteOff :: Ptr b -> Int -> Cell -> IO () #

peek :: Ptr Cell -> IO Cell #

poke :: Ptr Cell -> Cell -> IO () #

data Event #

A input event.

Constructors

EventKey !Key !Bool

Key event. The bool indicates the alt modifier.

EventResize !Int !Int

Resize event (width, then height)

EventMouse !Mouse !Int !Int

Mouse event (column, then row)

Instances
Eq Event 
Instance details

Defined in Termbox

Methods

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

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

Show Event 
Instance details

Defined in Termbox

Methods

showsPrec :: Int -> Event -> ShowS #

show :: Event -> String #

showList :: [Event] -> ShowS #

data InitError #

Termbox initialization errors that can be returned by run.

Instances
Show InitError 
Instance details

Defined in Termbox

Exception InitError 
Instance details

Defined in Termbox

data InputMode #

The input modes.

  • Esc. When ESC sequence is in the buffer and it doesn't match any known sequence, ESC means KeyEsc.
  • Alt. When ESC sequence is in the buffer and it doesn't match any known sequence, ESC enables the alt modifier for the next keyboard event.
Instances
Eq InputMode 
Instance details

Defined in Termbox

Ord InputMode 
Instance details

Defined in Termbox

Show InputMode 
Instance details

Defined in Termbox

data Mouse #

A mouse event.

Instances
Eq Mouse 
Instance details

Defined in Termbox

Methods

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

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

Ord Mouse 
Instance details

Defined in Termbox

Methods

compare :: Mouse -> Mouse -> Ordering #

(<) :: Mouse -> Mouse -> Bool #

(<=) :: Mouse -> Mouse -> Bool #

(>) :: Mouse -> Mouse -> Bool #

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

max :: Mouse -> Mouse -> Mouse #

min :: Mouse -> Mouse -> Mouse #

Show Mouse 
Instance details

Defined in Termbox

Methods

showsPrec :: Int -> Mouse -> ShowS #

show :: Mouse -> String #

showList :: [Mouse] -> ShowS #

data MouseMode #

The mouse mode.

  • No. Don't handle mouse events.
  • Yes. Handle mouse events.

Constructors

MouseModeNo

Default.

MouseModeYes 
Instances
Eq MouseMode 
Instance details

Defined in Termbox

Ord MouseMode 
Instance details

Defined in Termbox

Show MouseMode 
Instance details

Defined in Termbox

data OutputMode #

The output modes.

  • Normal. Supports colors 0..8, which includes all named color attributes exported by this library, e.g. red.
  • Grayscale. Supports colors 0..23.
  • 216. Supports colors 0..216.
  • 256. Supports colors 0..255.
Instances
Eq OutputMode 
Instance details

Defined in Termbox

Ord OutputMode 
Instance details

Defined in Termbox

Show OutputMode 
Instance details

Defined in Termbox

Example

Below is a sample program that simply displays the last key pressed, and quits on Esc:

{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import Reactive.Banana
import Reactive.Banana.Frameworks

import qualified Termbox.Banana as Termbox

main :: IO ()
main =
  Termbox.run_
    (Termbox.InputModeEsc Termbox.MouseModeNo)
    Termbox.OutputModeNormal
    moment

moment
  :: Event Termbox.Event
  -> Behavior (Int, Int)
  -> MomentIO (Behavior Termbox.Scene, Event ())
moment eEvent _bSize = do
  let
    eQuit :: Event ()
    eQuit =
      () <$ filterE isKeyEsc eEvent

  bLatestEvent :: Behavior (Maybe Termbox.Event) <-
    stepper
      Nothing
      (Just <$> eEvent)

  let
    bCells :: Behavior Termbox.Cells
    bCells =
      maybe mempty renderEvent <$> bLatestEvent

  let
    bScene :: Behavior Termbox.Scene
    bScene =
      Termbox.Scene
        <$> bCells
        <*> pure Termbox.NoCursor

  pure (bScene, eQuit)

renderEvent :: Termbox.Event -> Termbox.Cells
renderEvent =
  foldMap ((i, c) -> Termbox.set i 0 (Termbox.Cell c mempty mempty))
    . zip [0..]
    . show

isKeyEsc :: Termbox.Event -> Bool
isKeyEsc = case
  Termbox.EventKey Termbox.KeyEsc _ -> True
  _ -> False