Wednesday, April 1, 2015

Lanterna Scroll

This was my second attempt to build something with lanterna library from:
https://code.google.com/p/lanterna/
For the very first time I did slightly different project. This time I focused on producing something completely useless like old school scroll but on terminal output and via telnet on the net.

You can find my scroll on github:
https://github.com/piotrpietrzak/lanternaScroll
It looks something like this:
this is scroll rendered on telnet localhost 1024 with atari characters set. This is very tricky way of rendering this text.
To understand how it works first you have to visit: http://www.atariarchives.org/c3ba/charset_full.php
Now you are ready to read code - especially this:
boolean bitForCharAtXY(String symbol, int x, int y) {
    return charset[symbol][y] >> x & 1}
<sad_jokes>
This time bit shift operator was used to ... well this time this was sad bit shift. Of course our & operator ... oh wait - this is and.
No operator overloading - is this groovy or what?
</sad_jokes>

This was so '80 to encode chars as bits and then shift them and apply bit mask to check if they are 1 or 0. But how to encode entire message?
def computeBit(String message, int i, int j) {
    return atariFont.bitForCharAtXY(message[(int) Math.floor(i / 8) % message.length()], 7 - i % 8, j)
}
So - we can provide message and then at any (i,j) position of bits we can find if we should flash or turn off our "pixel".

Lets go "forward" to the early internet. Telnet was the king. Hackers was too busy to hack other people and plain text passwords in public net was accepted by admins so they use telnet even to manage their servers.

Lanterna in new version allows us to provide telnet service. This is really easy - we have to setup server:
TelnetTerminalServer server = new TelnetTerminalServer(1024, Charset.forName("utf-8"));
Then we can accept connections:
TelnetTerminal telnetTerminal = server.acceptConnection();
Then we can start new thread per connection:
new ConnectionHandler().handleNewConnection(telnetTerminal, {
    String message, Exception e ->
        log.error(message,e)
})
In this thread we can run our scroll:
new AtariFontScroll().scroll(terminal, screen, writer);
or even modern ascii art version:
new SimpleScroll().scroll(terminal, screen, writer);

This project was just for fun, but I plan to use lanterna in gradle plugin to show progress bar via net - directly from your CI to your laptop. I hope security will not cool down my enthusiasm...
OK, with apache mina I can upgrade telnet to ssh.

No comments:

Post a Comment