A simple selection control in the Terminal
Building from the last article, we can extend our state
so that it now stores an option selection:
struct state {
int cursor;
int selection;
};
Each grid cell now represents a distinct option.
The switch
statement in our update
function must now include a clause that handles a selection being made.
We can effectively do this by setting the value of the selection
to that of the cursor
when the Enter
key is typed.
case 10:
s.selection = s.cursor;
Note the 10
- The ncurses
documentation details that:
depending on whether nl or nonl was called, pressing "Enter" on the regular keyboard may return either a carriage-return or line feed,
Therefore we handle a pressed Enter
key by handling a typed line feed, which has an ASCII code equal to 10
.
In our view
function, we now have two more attribute settings that we want to handle when re-drawing our cells.
The first is when the number of the cell being drawn is equal to both the cursor
and selection
state - we will use A_BOLD
for this.
The second is when the cell number is equal to the selection
state only - again, we use A_BOLD
.
What really matters is that both these settings are equal, and different to the attribute setting for when the cell number is equal to the cursor
state.
Lastly, we initialize our state before starting the event loop:
struct state s = { 0, 1 };
References
“The Elm Architecture” https://guide.elm-lang.org/architecture/
The ncurses
documentation, which can be obtained on a Debian system by installing the ncurses-doc
package, or on a CentOS/Redhat/Fedora systems by installing the ncurses-devel
package.