Shellminator V3.0.1
Simple Terminal
|
Of course, a terminal interface isn’t very useful if it can’t bridge the gap between the user and the system. In nearly every system, there are situations where an event requires user interaction. The simplest way to handle this is by allowing the user to input text. This can be handy in a question-and-answer scenario, or even for implementing a simple yes/no query.
First, you’ll need a buffer to store the text that the user types in after pressing Enter. It’s a good idea to make the buffer large enough to fit a few words, but not so large that it uses up too much memory. Based on experience, a size of around 30 characters is usually sufficient for this purpose.
The input works by asking the terminal to create an input field, which remains active until the user presses Enter. While the input is active, no other actions can take place. Once the user presses Enter, a callback function is triggered. This function is customizable and needs to be implemented by the user.
The callback should have a return type of void
and the following arguments: char* buffer
, int bufferSize
, and Shellminator* parent
. In the example, the callback simply outputs the text entered by the user.
The final step is to ask the terminal to create the input for us. For this, you’ll need the buffer you created earlier, its size, a short prompt to guide the user on what the system expects them to do, and, finally, the callback function.
Here are a few tips for working with input: If you need to collect something highly confidential from the user, you can hide the characters, similar to how passwords work. To do this, simply include an optional argument to indicate that the input should be hidden. For example, you can modify the previous example like this:
Here’s another helpful tip: You can interrupt an input at any time by pressing the Ctrl-C
key combination.