import sys
import socket
import os

def read_char():
    import sys, termios, tty
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


def read_view(f):
    view = f.readline()
    if not view:
        return
    for x in range(2, len(view)):
        line = f.readline()
        if not line:
            return
        view += line
    return view


def main(host='localhost', port=62688):
    s = socket.socket()
    s.connect((host, port))
    f = s.makefile()

    commands = {'w':'^', 'a':'<', 's':'v', 'd':'>', 'W':'^', 'A':'<', 'S':'v', 'D':'>' }


    while True:
        try:
            view = read_view(f)
            if not view:
                break
            os.system('clear')
            sys.stdout.write(view)
            cmd = read_char()
            if cmd == 'q':
                break
            cmd = commands[cmd]
            s.send(cmd if sys.version_info[0] < 3 else str.encode(cmd))
        except Exception as e:
            print(e)
            break
    s.close()



if __name__ == '__main__':
    main( * sys.argv[1:])