View RSS Feed

Hibernate

Linux terminal magick, part 2

Rate this Entry
by , 08-12-2011 at 06:05 AM (1651 Views)
In the previous part if ”Linux terminal magick” we show how to let use user use ^C and ^\ in the terminal, that was the last part of creating a graphical terminal user interface. The parts are [almost] not ordered!

For this part we add two more functions:
Java Code:
/**
 * Sets the the value of the ECHO flag; iff on the input to the terminal will be echoed back while typing.
 * This can be used to hide what the user is type, for example when a password is requested.
 *
 * @param   on           Whether the flag should be on.
 * @throws  IOException  Should not be thrown in GNU.
 */
public static void setEchoFlag(final boolean on) throws IOException
{
    (new ProcessBuilder("/bin/sh", "-c", "stty " + (on ? "echo" : "-echo") + " < " + TerminalMagick.tty + " > /dev/null")).start();
}

/**
 * Sets the the value of the ICANON flag; iff on the input will wait to be sent onto a line feed is given.
 * This can be used to read single characters instead of lines.
 *
 * @param   on           Whether the flag should be on.
 * @throws  IOException  Should not be thrown in GNU.
 */
public static void setBufferFlag(final boolean on) throws IOException
{
    (new ProcessBuilder("/bin/sh", "-c", "stty " + (on ? "icanon" : "-icanon") + " < " + TerminalMagick.tty + " > /dev/null")).start();
}

To your shutdown hook (or finally clause in the main method, as in part 1) you may want to add:
TerminalMagick.setEchoFlag(true)
TerminalMagick.setBufferFlag(true)


TerminalMagick.setEchoFlag(true) will allow the user to see what he is writing, it terminal will print it out.
TerminalMagick.setEchoFlag(false) will hide the typed text from the user, this is preferable if you are asking for a password. It is almost like press ScrollLock, except it will not become visible. It is also usable for graphical interfaces in the terminal.

TerminalMagick.setBufferFlag(false) lets the program retrieve the typed character the instant it is typed, instead of when it user presses Enter.
This is usable when you want to retrieve mouse clicks or single key presses. It is also usable if you want to create a method jumps to the last
entry when the user presses the ↑-key, just like when you are not running a program in the console. But backspace, delete, the ←-key and the →-key
will lose there functionally, and which most be implemented by hand.
TerminalMagick.setBufferFlag(true) will undo what is described above.


In next part: Getting the width and height, of the terminal, character units.

Submit "Linux terminal magick, part 2" to Facebook Submit "Linux terminal magick, part 2" to Digg Submit "Linux terminal magick, part 2" to del.icio.us Submit "Linux terminal magick, part 2" to StumbleUpon Submit "Linux terminal magick, part 2" to Google

Updated 08-12-2011 at 06:42 AM by Hibernate

Tags: None Add / Edit Tags
Categories
Terminal cues , Linux

Comments