Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline Input #22

Closed
ghost opened this issue Feb 25, 2012 · 6 comments
Closed

Multiline Input #22

ghost opened this issue Feb 25, 2012 · 6 comments

Comments

@ghost
Copy link

ghost commented Feb 25, 2012

Admittedly this is a niche case, but it would be a nice configurable feature for the plugin to count the number of opening braces/parens/brackets and continue to accept input until the count is zero for each.

@jcubic
Copy link
Owner

jcubic commented Feb 26, 2012

There is already this kind of code in Scheme Example (only on the page not in repo). It count if parents in S-Expressions are balanced. Check code (in the source) http://terminal.jcubic.pl/examples.php#dterm, the code check if parents/brackets are ballanced if no it accept more input and concatenate it and only then execute the code.

This feature is not for the terminal but for specific implementation, terminal can only help in implement it by the client. I can only make multiline input easier.

@Totktonada
Copy link

@jcubic
Copy link
Owner

jcubic commented Mar 27, 2019

IMHO better multiline is Shift+Enter, I've created better parenthesis match but in single input here https://jcubic.github.io/lips/ (source: https://github.com/jcubic/lips/blob/gh-pages/index.html#L192)

@jcubic
Copy link
Owner

jcubic commented Mar 27, 2019

But there is also an option to overwrite ENTER keymap and insert newline when parenthesis are not balanced, I'll try to create example or update my parenthesis matching.

@Totktonada
Copy link

Our case is when a backend is responsible for distinguish whether a command is incomplete. But I guess I'm able to implement it just on server side and allows a client to send incomplete commands (maybe prompting will need some tweaking).

@jcubic
Copy link
Owner

jcubic commented Mar 28, 2019

Here is better solution, for your case:

 var term;

 $(function() {
     term = $('<div/>').attr('id', 'term')
         .appendTo('body')
         .terminal(function(command) {
            this.echo('executing ' + command);
         }, {
             name: 'multiline',
             keymap: {
                ENTER: function(e, original) {
                    this.pause();
                    $.jrpc('service.php', 'enter', [this.get_command()],
                        (response) => {
                            if (response.result == '__ENTER__') {
                                this.insert('\n');
                            } else if (response.result == '__EXEC__') {
                                original();
                            } else if (response.error) {
                                // you need to decide what to do, user still
                                // can edit whole command
                                // you can highlight all parenthesis using jQuery
                                // since each character is in own span
                                this.error('Syntax error');
                            }
                            this.resume();
                        }
                    );
                }
            },
             prompt: 'exec> '
         });
 });
<?php

require('json-rpc.php');

if (function_exists('xdebug_disable')) {
    xdebug_disable();
}

function balance($code) {
    $pa = str_split(preg_replace("/[^()]/", "", $code));
    $i = 0;
    foreach ($pa as $p) {
        if ($p == '(') {
            $i++;
        } else if ($p == ')') {
            $i--;
        }
    }
    return $i;
}

class Service {
    function enter($code) {
        $p = balance($code);
        if ($p < 0) {
            throw new Exception("Parenthesis error");
        } else if ($p > 0) {
            return "__ENTER__";
        } else {
            return "__EXEC__";
        }
    }
}

handle_json_rpc(new Service());

?>

Instead of JSON-RPC you can use normal ajax call and inside interpreter function you can call ajax. The only limitation is that you will have two requests. If you want one you will to put your code instead of "__EXEC__" and return exec value if result will be __ENTER__ you insert \n normaly but if you have different value you just echo it and call original() note this in this case your interpreter can be empty (use $.noop) it will be only used to handle displaying of prompt.

jcubic added a commit to jcubic/jquery.terminal-www that referenced this issue Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants