krr
krr

Reputation: 69

Click button and send command to execute on xterm.js terminal

I want to click a button and send a linux command such as "ls" to be executed on xterm.js terminal. Is there a way to do this?

Pty pseudo-terminal written in Go is running remotely and I'm using xterm.js as the front end component.

Upvotes: 0

Views: 2594

Answers (2)

Mike R
Mike R

Reputation: 879

I did this same thing with Python sending a command from the app to the embedded xterm.js terminal and one thing i recommend is setting it up so you can send the raw command without special escaping needed.

I found this javscript "heredoc" function which allowed me to send the raw complex multiline oneliners to xterm.js without issue. leaving this here in case anyone else finds it useful.

    @QtCore.pyqtSlot(QtCore.QModelIndex)
    def run_command_via_js_double_click(self, signal):
        # send a command to the server
        script = """
        var cmd = (function() {/*%s*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
            wssh.send(cmd);
        """ % cmd_replaced
        self.currentWidget().page().runJavaScript(script)

Can see an example of it in action below.

enter image description here

Upvotes: 1

krr
krr

Reputation: 69

Something like this should take care of the case mentioned above

In HTML

<button (click)="runCmd('ls')>ls</button> 

In app.component.ts

Assuming there is a websocket server that is listening to the end point mentioned here...

public runCmd(command){
  var url = "ws://<ip>:<port>/pty"
  var terminal = new Terminal()
  terminal.open(document.getElementById("terminalDiv");
  var socket = new WebSocket(url);
  var attachAddon = new AttachAddon(socket);
  terminal.loadAddon(attachAddon);

  //socket.send() will send the data passed to it. 
  socket.send(command);
}

Upvotes: 3

Related Questions