clojure-lanterna

Reference

Here's the deep dive.

If you haven't read the terminal and screen documentation you should read those to wrap your brain around the structure of things first.

Constants

clojure-lanterna uses Clojure keywords where you need to supply constants. It will handle the filthy details of converting them to the appropriate Java enum elements when needed so you don't need to worry about it.

Colors

Lanterna (and thus clojure-lanterna) supports the 8 common terminal colors, as well as a "default" color:

Styles

Lanterna (and thus clojure-lanterna) supports 4 common styles:

Key Codes

When you get a key of user input from clojure-lanterna it will be one of two things: a Character like \a or \$ representing what the user typed, or a keyword for special keys like Delete or Page Up.

Note that the Tab key is returned as :tab and not \tab.

Here are the keywords for special keys that may be returned:

There are also two other special keywords:

Charsets

Currently there's only one charset clojure-lanterna constant defines. Open an issue as a feature request if you want others -- I'll be happy to add the constants.

Consoles

When creating a Terminal or Screen, you can optionally specify a specific kind of Terminal or Screen to create.

If it's not supported (e.g.: trying to create a Swing Terminal on a system without X) then who knows what will happen. Make sure you know what you're doing if you use anything other than :auto.

Palettes

When creating a Swing Terminal or Screen, you can choose the color palette to use. Text-based Terminals and Screens will use the user's color scheme, of course.

The following palettes are supported:

Font Names

When giving a font name, it should be a string naming a font family on your system. For example: "Consolas", "Courier New", or "Monaco".

To see a the fonts available on your system you can call get-available-fonts.

Terminals

The terminal layer is the lowest-level layer. Read the terminal documentation for an overview.

lanterna.terminal/get-terminal

(get-terminal)
(get-terminal kind)
(get-terminal kind options)

Get a terminal object.

kind is a console constant describing the type of terminal you want. If unspecified it defaults to :auto.

The options map can contain any of the following mappings:

The :rows, :cols, :font, :font-size, :palette and :charset options are really just a suggestion!

The text-based terminals will ignore rows, columns, fonts and palettes. They will be determined by the user's terminal window.

The Swing terminal will start out at the given size but can be resized later by the user, and will ignore the charset entirely.

God only know what Cygwin will do.

Your application needs to be flexible and handle sizes on the fly.

lanterna.terminal/start

(start terminal)

Start the given terminal. Terminals must be started before they can be used.

Consider using in-terminal instead if you don't need detailed control of the starting and stopping.

lanterna.terminal/stop

(stop terminal)

Stop the given terminal. Terminals must be stopped after you're done with them, otherwise you risk corrupting the user's console.

Don't try to do anything to the Terminal after you stop it.

I'm not sure if you can "restart" a terminal once it's been stopped. TODO: Find out.

Consider using in-terminal instead if you don't need detailed control of the starting and stopping.

lanterna.terminal/in-terminal

(in-terminal terminal & body)

Start the given terminal, perform the body of expressions, and stop the terminal afterward.

This is a macro.

The stopping will be done in a try/finally block, so you can be confident it will actually happen.

Use this if you don't need detailed control of the terminal starting and stopping process.

lanterna.terminal/get-size

(get-size terminal)

Return the current size of the terminal as [cols rows].

lanterna.terminal/move-cursor

(move-cursor terminal x y)

Move the cursor to a specific location on the screen.

lanterna.terminal/put-character

(put-character terminal ch)

Draw the character at the current cursor location.

Also moves the cursor one character to the right, so a sequence of calls will output next to each other.

(put-character terminal ch x y)

Draw the character at the specified cursor location.

Also moves the cursor one character to the right.

lanterna.terminal/put-string

(put-string terminal s)

Draw the string at the current cursor location.

The cursor will end up at the position directly after the string.

(put-string terminal s x y)

Draw the string at the specified cursor location.

The cursor will end up at the position directly after the string.

lanterna.terminal/clear

(clear terminal)

Clear the given terminal.

The cursor will be at the coordinates 0, 0 after the clearing.

lanterna.terminal/set-fg-color

(set-fg-color terminal color)

Set the foreground color for text drawn by subsequent put-character and put-string calls.

Color is a color constant like :red.

lanterna.terminal/set-bg-color

(set-bg-color terminal color)

Set the background color for text drawn by subsequent put-character and put-string calls.

Color is a color constant like :red.

lanterna.terminal/set-style

Broken right now, sorry.

lanterna.terminal/remove-style

Broken right now, sorry.

lanterna.terminal/reset-styles

Broken right now, sorry.

lanterna.terminal/get-key

(get-key terminal)

Get the next keypress from the user, or nil if none are buffered.

If there is one or more keystroke buffered, that key will be returned (and popped off the buffer of input). The returned key will be a key code constant.

If there are no keystrokes buffered, nil will be returned immediately.

If you want to wait for user input, use get-key-blocking instead.

lanterna.terminal/get-key-blocking

(get-key-blocking terminal)
(get-key-blocking terminal options)

Get the next keypress from the user.

If there is one or more keystroke buffered, that key will be returned (and popped off the buffer of input). The returned key will be a key code constant.

If there are no keystrokes buffered the function will sleep, checking every 50 milliseconds for input. Once there is a character buffered it will be popped off and returned as normal.

If you want to return immediately instead of blocking when no input is buffered, use get-key instead.

The options map can contain any of the following mappings:

lanterna.terminal/add-resize-listener

(add-resize-listener terminal listener-fn)

Create a listener that will call the supplied function when the terminal is resized.

The function must take two arguments: the new number of columns and the new number of rows.

You probably don't need this because you can specify a resize listener function when you call get-terminal. It's here if you do need it though.

lanterna.terminal/remove-resize-listener

(remove-resize-listener terminal listener)

Remove the given resize listener from the given terminal.

lanterna.terminal/get-available-fonts

(get-available-fonts)

Return a set of strings of the names of available fonts on the current system.

Screens

The screen layer is an abstraction that provides buffering on top of the terminal layer. Read the screen documentation for an overview.

lanterna.screen/get-screen

(get-screen)
(get-screen kind)
(get-screen kind options)

Get a screen object.

kind is a console constant describing the type of screen you want. If unspecified it defaults to :auto.

The options map can contain any of the following mappings:

The :rows, :cols, and :charset options are really just a suggestion!

The text-based screens will ignore rows and columns and will be the size of the user's window.

The Swing screen will start out at this size but can be resized later by the user, and will ignore the charset entirely.

God only know what Cygwin will do.

Your application needs to be flexible and handle sizes on the fly.

lanterna.screen/start

(start screen)

Start the given screen. Screens must be started before they can be used.

Consider using in-screen instead if you don't need detailed control of the starting and stopping.

lanterna.screen/stop

(stop screen)

Stop the given screen. Screens must be stopped after you're done with them, otherwise you risk corrupting the user's console.

Don't try to do anything to the screen after you stop it.

I'm not sure if you can "restart" a screen once it's been stopped. TODO: Find out.

Consider using in-screen instead if you don't need detailed control of the starting and stopping.

lanterna.screen/in-screen

(in-screen screen & body)

Start the given screen, perform the body of expressions, and stop the screen afterward.

This is a macro.

The stopping will be done in a try/finally block, so you can be confident it will actually happen.

Use this if you don't need detailed control of the screen starting and stopping process.

lanterna.screen/get-size

(get-size screen)

Return the current size of the screen as [cols rows].

lanterna.screen/redraw

(redraw screen)

Redraw the given screen.

This is how you actually flush any changes to the user's display.

lanterna.screen/get-cursor

(get-cursor screen x y)

Retrieve the current location of the cursor on the screen as [x y].

lanterna.screen/move-cursor

(move-cursor screen x y)
(move-cursor screen [x y])

Move the cursor to a specific location on the screen.

You'll need to redraw the screen to actually see it happen.

The cursor will stay where you move it, even if you later draw some text in a different place and redraw. If you want it to move, you need to call this function again.

lanterna.screen/put-string

(put-string screen x y s)
(put-string screen x y s options)

Put a string on the screen buffer, ready to be drawn at the next redraw.

x and y are the column and row to start the string.

s is the actual string to draw.

The options map can contain any of the following mappings:

lanterna.screen/clear

(clear screen)

Clear the given screen.

Note that this is buffered just like every other screen-related action. You need to redraw to actually see it happen.

lanterna.screen/get-key

(get-key screen)

Get the next keypress from the user, or nil if none are buffered.

If there is one or more keystroke buffered, that key will be returned (and popped off the buffer of input). The returned key will be a key code constant.

If there are no keystrokes buffered, nil will be returned immediately.

If you want to wait for user input, use get-key-blocking instead.

lanterna.screen/get-key-blocking

(get-key-blocking screen)

Get the next keypress from the user.

If there is one or more keystroke buffered, that key will be returned (and popped off the buffer of input). The returned key will be a key code constant.

If there are no keystrokes buffered the function will sleep, checking every 50 milliseconds for input. Once there is a character buffered it will be popped off and returned as normal.

If you want to return immediately instead of blocking when no input is buffered, use get-key instead.

The options map can contain any of the following mappings:

lanterna.screen/add-resize-listener

(add-resize-listener screen listener-fn)

Create a listener that will call the supplied function when the screen is resized.

The function must take two arguments: the new number of columns and the new number of rows.

You probably don't need this because you can specify a resize listener function when you call get-screen. It's here if you do need it though.

lanterna.screen/remove-resize-listener

(remove-resize-listener screen listener)

Remove the given resize listener from the given screen.