Ansi

Functions and strings to control the screen, cursor, and text in the terminal using ANSI escape sequences.

import Ansi
import Stream exposing (Stream)

coolGreeting : Stream -> Cmd msg
coolGreeting stdout =
    "Hello!"
        |> Ansi.wrapItalic
        |> Ansi.wrapColor Ansi.Green
        |> Stream.sendLine stdout

Text Color

wrapColor : Color -> String -> String

Display text in the given color.

This will return the string with a "set color" sequence at the beginning and a "no color" sequence at the end.

setColor : Color -> String

Change all subsequent terminal output to the given color.

This will persist even after program exit! So be sure to call setColor NoColor before exiting.

type Color
= NoColor
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White

Text Styling

wrapFontWeight : FontWeight -> String -> String

Display string with the given font weight.

setFontWeight : FontWeight -> String

Change all terminal output from this point to the given font weight (even after program exit).

wrapItalic : String -> String

Display the given string as italic.

wrapUnderline : String -> String

Display the given string as underline.

wrapStrikeThrough : String -> String

Display the given string with a strikethrough.

setItalic : String

Change all terminal output from this point to italic (even after program exit).

unsetItalic : String

Stop displaying terminal output as italic.

setUnderline : String

Change all terminal output from this point to underline (even after program exit).

unsetUnderline : String

Stop displaying terminal output as underline.

setStrikeThrough : String

Change all terminal output from this point with a strikethrough (even after program exit).

unsetStrikeThrough : String

Stop displaying terminal output with a strikethrough.

type FontWeight
= NormalWeight
| Bold
| Faint

Clearing the Screen

clearScreen : Direction -> String

Clear screen in the given Direction.

clearLine : Direction -> String

Clear the current line in the given direction.

type Direction
= Up
| Down
| Full

Direction for clear functions.

  • Down: Clear things after the cursor.
  • Up: Clear things before the cursor.
  • Full: Clear both directions.

Controlling the Cursor

moveUpLines : Int -> String

Move the cursor up the given number of lines.

moveDownLines : Int -> String

Move the cursor down the given number of lines.

moveTo : Int -> Int -> String

Move cursor to the given row and column number of the visible terminal.

showCursor : String

Show the cursor.

hideCursor : String

Hide the cursor.

You should call showCursor before exiting your program, or the cursor will still be gone after exiting (gren-tui will handle this automatically if someone ctrl-c's out of your program).

getCursorReport : String

Get the cursor position.

After sending this sequence to stdout, a new sequence containing the cursor's column and row will be output. It will look like this: ESC[n;mR where n is the row and m is the column.

To capture this, you probably want to be in raw mode, and be listening on stdin. escape will be helpful here when checking the captured string.

If you're using this in gren-tui you can include it in your view (or send it directly to an output stream). This will trigger an update with your onInput message holding a CursorReport row column.

Arrow Keys

Use these functions if you are reading stdin and want to see if an arrow key was pressed.

arrowUp : String

ANSI escape sequence for the up arrow key.

arrowDown : String

ANSI escape sequence for the down arrow key.

arrowLeft : String

ANSI escape sequence for the left arrow key.

arrowRight : String

ANSI escape sequence for the right arrow key.

"Escape" hatch

prefix : String

The beginning of most ANSI escape sequences (The \e[ part).

Use this to define your own ansi functions. For example, setItalic is defined like this:

setItalic : String
setItalic =
    prefix ++ "3m"

You can look up escape sequences here or here.

If you create functions you think should be included in the package I'd love it if you sent a pull request.

escape : String

Unicode character for Escape (Esc).

You would use this if you needed to compare or parse a string holding an escape sequence.