{% block extra_head %}
    <link rel="stylesheet" href="{{ url_for('static', filename="css/terminal.js/jquery.terminal.min.css") }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/term.css') }}">
{% endblock %}

{% extends 'local/layout.html' %}

{% block main %}
    <h4>Command Prompt</h4>
    <div id="terminal" style="overflow: scroll; height:500px">
    </div>
{% endblock %}

{% block extra_js %}
    <script src="{{ url_for('static', filename="vendor/terminal.js/jquery.terminal.min.js") }}"></script>
    <script>
    // copy from LIPS source code
    var pre_parse_re = /("(?:\\[\S\s]|[^"])*"|\/(?! )[^\/\\]*(?:\\[\S\s][^\/\\]*)*\/[gimy]*(?=\s|\(|\)|$)|;.*)/g;
    var tokens_re = /("(?:\\[\S\s]|[^"])*"|\/(?! )[^\/\\]*(?:\\[\S\s][^\/\\]*)*\/[gimy]*(?=\s|\(|\)|$)|\(|\)|'|"(?:\\[\S\s]|[^"])+|(?:\\[\S\s]|[^"])*"|;.*|(?:[-+]?(?:(?:\.[0-9]+|[0-9]+\.[0-9]+)(?:[eE][-+]?[0-9]+)?)|[0-9]+\.)[0-9]|\.|,@|,|`|[^(\s)]+)/gim;
    // ----------------------------------------------------------------------
    function tokenize(str) {
        var count = 0;
        var offset = 0;
        var tokens = [];
        str.split(pre_parse_re).filter(Boolean).forEach(function(string) {
            if (string.match(pre_parse_re)) {
                if (!string.match(/^;/)) {
                    var col = (string.split(/\n/), [""]).pop().length;
                    tokens.push({
                        token: string,
                        col,
                        offset: count + offset,
                        line: offset
                    });
                    count += string.length;
                }
                offset += (string.match("\n") || []).length;
                return;
            }
            string.split('\n').filter(Boolean).forEach(function(line, i) {
                var col = 0;
                line.split(tokens_re).filter(Boolean).forEach(function(token) {
                    var line = i + offset;
                    var result = {
                        col,
                        line,
                        token,
                        offset: count + line
                    };
                    col += token.length;
                    count += token.length;
                    tokens.push(result);
                });
            });
        });
        return tokens;
    }

    function balance(code) {
        // tokenize from previous example

        var tokens = tokenize(code);
        var count = 0;
        var token;
        var i = tokens.length;
        while ((token = tokens[--i])) {
            if (token.token === '(') {
                count++
            } else if (token.token === ')') {
                count--;
            }
        }
        return count;
    }

    $('#terminal').terminal([
        "{{ url_for('cmd.cmd') }}",
        {
            print: function(input) {
                this.echo(input);
                this.height(500)
            }
        },
        {
            ts: function(input) {
                this.set_prompt( "[[;gray;]" + input + "> ]");
            }
        }
        ],
        {
        keymap: {
        ENTER: function(e, original) {
            var my_cmd = this.get_command();

            this.set_command(my_cmd)
            if (balance(my_cmd) === 0) {
                original();
            } else {
                this.insert('\n');
            }
        }
        },
        prompt: "[[;gray;]> ]",
        completion: true,
        greetings: '{{ meta.APP_NAME }} ({{ meta.APP_PROFILE }}) v. {{ meta.APP_VERSION }} db model {{ meta.DB_VERSION }}\n\nType ? for help\n'
    });
    </script>
{% endblock %}