coupdebol / lanterna

Automatically exported from code.google.com/p/lanterna
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Suggestion: add ANSI ENQ/Answer #103

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
We found it useful to enquire the terminal for configured info using the 
standard ANSI ENQ/Answer. This could contain specific device info, such as 
unique ID, environment variables and so on. Many clients allow you to configure 
the Answer field to a string of your choosing.

Here's a quick method sketch which should be called right after initializing 
the terminal but before rendering the first screen:

    private Map<String, String> enquireTerminalInfo() {
        final Terminal terminal = getGuiScreen().getScreen().getTerminal();

        terminal.putCharacter(ENQ_CHAR); // ENQ char: \u005
        final StringBuilder sb = new StringBuilder();
        Key key;
        final Stopwatch stopWatch = new Stopwatch();
        stopWatch.start();
        while ((key = terminal.readInput()) != null
                || (sb.length() == 0 && stopWatch.elapsedTime(TimeUnit.SECONDS) <= 2)) {
            if (key != null) {
                sb.append(key.getCharacter());
            }
        }
        stopWatch.stop();

        final Map<String, String> params = new HashMap<>();
        // Expected format: comma delimited key=value pairs
        final Matcher matcher = Pattern.compile("(\\p{Alnum}+)=(\\p{Alnum}+),?").matcher(sb.toString());
        while (matcher.find()) {
            params.put(matcher.group(1), matcher.group(2));
        }
        return params;
    }

Original issue reported on code.google.com by sivan.mo...@gmail.com on 20 Feb 2014 at 12:20

GoogleCodeExporter commented 9 years ago
Interesting! I've never heard of this feature before. Do you have a link to any 
documentation about how this works?

Original comment by mab...@gmail.com on 30 Mar 2014 at 1:16

GoogleCodeExporter commented 9 years ago
It's a standard ANSI code and is well documented: 
https://en.wikipedia.org/wiki/Enquiry_character

In the PuTTy configuration, see the setting: ‘Answerback to ^E’. TerraTerm 
has something similar.

Original comment by sivan.mo...@gmail.com on 1 Apr 2014 at 6:13

GoogleCodeExporter commented 9 years ago
Ok, that should be doable. I believe there's no way of knowing when the 
answerback is done, I suppose just read to the end of the input buffer?

Original comment by mab...@gmail.com on 13 Apr 2014 at 12:59