Skip to content

Allowing richer integration with integrated terminal (co-processes) #13337

Closed

Description

Popular terminal emulators such as iTerm2 and ConEmu allow for user defined interaction with the content being displayed. For instance there are coprocesses and triggers in iTerm2 which allow for very powerful interactions. For instance I have a coprocess that automagically make diagnostics in VS Code when running tsc -w in iTerm2.

I think VS Code would greatly benefit from such rich interaction capabilities between the terminal and the outside-world

Activity

  1. added
    feature-request
    on Oct 7, 2016
  2. added
    terminal
    on Oct 7, 2016
  3. added this to the Backlog milestone on Oct 7, 2016
  4. changed the title [-]Allowing richer integration with integrated terminal[/-] [+]Allowing richer integration with integrated terminal (co-processes)[/+] on Nov 16, 2016
  5. Tyriar commented on Jan 5, 2017

    @Tyriar
    Contributor

    So triggers seem to be mainly used for syntax highlighting based on some Googling. Moving the tasks framework to an extension built on top of the integrated terminal (#15584) could probably be done using some form of triggers that run a callback as it allows matching on a particular line.

    It also doesn't expose the potential security holes that coprocesses do as it doesn't touch stdin.

  6. Tyriar commented on Jan 5, 2017

    @Tyriar
    Contributor

    Johannes Rieken (@jrieken) can you go into a little more detail for your particular co-process? Could this be done with a trigger if you could grab stdout and listen for a line matching a regex?

  7. jrieken commented on Jan 6, 2017

    @jrieken
    ContributorAuthor

    My co-process goes like this:

    #!/usr/local/bin/node
    
    // process.stdin.setEncoding('utf8');
    
    const hook = process.argv[process.argv.length - 1];
    
    const {createConnection} = require('net');
    
    function onError(err) {
      console.error(err);
      process.exit(1);
    }
    
    const socket = createConnection(hook, () => {
      socket.removeListener('error', onError);
      process.stdin.pipe(socket);
    });
    
    socket.once('error', onError);

    It gets started by iTerm and simply pipes stdin to another socket. That socket is read from an extensions and marker are created for whatever comes in (a very simplifies version of the task logic).

    The interesting bit is that stdin change when a new line is send but also when an existing line is modified, like a progress spinner renders etc.

    The trigger bit in this is more that the trigger can be used to start a process. Like so

    1. Have a trigger that regexes for tsc -w and then start a specific co-process
    2. Make the co-process read and process stdin (which mirrors the terminal contents)
  8. Tyriar commented on Jan 6, 2017

    @Tyriar
    Contributor

    I'm concerned about extensions listening to stdin (eg. credentials) without the users knowledge. We could always do something like this not as part of an extension without worrying about that though.

  9. jrieken commented on Jan 6, 2017

    @jrieken
    ContributorAuthor

    It's not stdin of the process that runs. iTerm takes whenever is on the screen and sends it the co-process, by that becoming stdin. That is literally the characters (including control-sequences) that are rendered. From https://www.iterm2.com/documentation-coprocesses.html:

    All output in a terminal window (that is, what you see on the screen) is also input to the coprocess. All output from the coprocess acts like text that the user is typing at the keyboard.

  10. Tyriar commented on Jan 6, 2017

    @Tyriar
    Contributor

    Ah I was thinking the wrong thing for some reason. Is there anything else other than a Terminal.addTrigger(string, callback) and some way of grabbing the terminal's data stream that would be needed for your use case? Here's an example:

    var coprocessNotRunning = false;
    var t = window.createTerminal();
    t.addTrigger(/tsc -w/, line => {
      console.log('tsc -w started: ' + data);
      if (coprocessNotRunning) {
        coprocessNotRunning = true;
        t.on('data', data => coprocess(data));
      }
    });
    
    function coprocess(data) {
        // Do something with the data sent from the pty process
        // Terminate at some point?
        //   t.off('data', coprocess);
    }
  11. Tyriar commented on Jan 6, 2017

    @Tyriar
    Contributor

    I was looking at problemMatcher.ts yesterday for Dirk Bäumer (@dbaeumer)'s use case which is similar (if not the same) to yours which scans for "problems" and registers them as problems in the product. This is what I was leaning towards for the solution for that:

    const tscMatcher = /^([^\s].*)\((\d+|\d+,\d+|\d+,\d+,\d+,\d+)\):\s+(error|warning|info)\s+(TS\d+)\s*:\s*(.*)$/;
    t.addTrigger(tscMatcher, lineMatch => {
    	const file = lineMatch[1];
    	const location = lineMatch[2];
    	const severity = lineMatch[3];
    	const code = lineMatch[4];
    	const message = lineMatch[5];
    	// register as a problem
    });

    It's not critical to turn the trigger on and off here as the tasks framework would launch the process and add the trigger immediately, when the process closes the terminal will no longer accept input.

  12. 3 remaining items

  13. dbaeumer commented on Jan 10, 2017

    @dbaeumer
    Member

    Daniel Imms (@Tyriar) yes, that would be sufficient for me.

  14. KalitaAlexey commented on Jan 16, 2017

    @KalitaAlexey

    How to use .on('data', callback) to separate stdout and stderr?
    I'd like to have that.

  15. Tyriar commented on Jan 16, 2017

    @Tyriar
    Contributor

    Alexey (@KalitaAlexey) right now this is only an internal api, good to know though.

  16. KalitaAlexey commented on Jan 16, 2017

    @KalitaAlexey

    Daniel Imms (@Tyriar),
    Have you tried it already?
    Does it invoke callback for both stdout and stderr data?

  17. Tyriar commented on Jan 16, 2017

    @Tyriar
    Contributor

    It sends whatever is sent to the terminal including escape sequences. This is being changed though to what is printed to the terminal, something like onData(listener: (line: string) => void), see #18453

    I'm not sure we can differentiate between stderr from stdout as the process is not being called directly but being run within a pseudoterminal.

  18. KalitaAlexey commented on Jan 16, 2017

    @KalitaAlexey

    Daniel Imms (@Tyriar),
    What if I would like to have a way to process stdout and stderr separately?
    onStdoutData and onStderrData will be good enough.
    How hard is it to add support for this feature?

  19. Tyriar commented on Jan 16, 2017

    @Tyriar
    Contributor

    I think it's a limitation of the library we use to only have a single fd for stderr and stdout. But anyway it's probably some time out before something like this manifests itself in the terminal api. I would need to investigate more deeply at that point.

  20. BartNetJS commented on Nov 27, 2017

    @BartNetJS

    Daniel Imms (@Tyriar) you mentioned this thread on #18453 (comment)

    So I continue here.

    What I try to do is to get the response from the commands in the terminal window into an extension (to evaluate the result and give suggestions).

    You said that the ondata will not be exposed to extensions.
    Is there another way to grab the command result into an extension.

    Or is it possible to create a custom xterm.js integrated terminal?

  21. Tyriar commented on Dec 22, 2017

    @Tyriar
    Contributor

    Bart (@BartNetJS) no it's not possible to get the data right now and needs this or a similar feature to be implemented.

  22. zikalino commented on Mar 19, 2018

    @zikalino

    Daniel Imms (@Tyriar) so i understand security issues that could occur if extension was able to access Terminal's stdin/stdout.
    But how about changing createTerminal function so it could pass stdin/stdout as a parameter.

    This way extension could create its own process, or SSH connection or whatever, and then create custom terminal for that connection.

  23. Tyriar commented on Apr 5, 2018

    @Tyriar
    Contributor

    This case should be covered by #46192, merging into that

  24. added
    *duplicate
    on Apr 5, 2018
  25. locked and limited conversation to collaborators on May 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

*duplicateapifeature-requestterminal

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions