Picosmos: Writing a simple single-line text-editor with brick.

Using the cursors as defined in the previous posts about list cursors and text cursors we can now take the first step toward writing a Purely Functional Semantic Editor.
In this post we will write a purely functional text editor for a single line of text using brick.

Picosmos: editing a line.

In the previous two posts: cursors: list and cursors: text we explored the cursors that we will need to be able to write an editor for a single line of text.
Now it is time to put all the pices together, and write a simple editor for a single line of text.
We will be using brick to interface with the terminal interface, but you can use these concepts for a GUI as well, as long as you can somehow fit in the ELM architecture.

A brick app

The central piece of the editor is a brickApp.
It has three type variables:

s: The state of the application. In our case, that will be a text cursor.

e: The type for custom events. We will not use these, so we will leave it a paramter.

n: The type for names of pieces of the display. For this toy example, we will use Text.

appDraw: A way to draw the state onto the screen. We do this by turning it into a brickWidget.

appChooseCursor: Not relevant for us now.

appHandleEvent: A way to change the state, given an event.

appStartEvent: Not relevant for us now.

appAttrMap: Not relevant for us now.

Drawing a text cursor

To implement the draw :: TextCursor -> [Widget Text] function, we have to explain to brick how to render a TextCursor to the terminal screen.
We do this by turning a TextCursor into a Widget Text.
The reason why the result is a list, is so that you could draw multiple layers, but we will only use one layer.

_ -> continue tc: Do nothing if anything else happens, but do not stop the application.

References

That is it.
Now we have a a functional line-of-text editor.
The full code can be found on github.
Text cursors are available in the cursor package on Hackage.
This post is part of an effort to encourage contributions to Smos.
The simplest contribution could be to just try out smos and provide feedback on the experience.
Smos is a purely functional semantic editor of a subset of YAML that is intended to replace Emacs' Org-mode for Getting Things Done.