question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catch result from `exec` command to allow formatting.

See original GitHub issue

Is there a way to catch the ouput of an exec command?

Is there a way to get the output from an exec command without writing it to the terminal right away? I would like to modify the output of some commands that read files by appending the keepWords option through using the echo command to break lines correctly (this would resolve the issue of pages being indexed by bots incorrectly due to added div elements on weird places in the source).

For example, this.echo('a long text here', {keepWords: true}) will do exactly what I want. But in my case I store text files which are readable with a cat command. For example: this.exec('cat filename.info', true);, or this.echo('[[cat filename.info]]', {keepWords: true});, but in both cases words are not kept together.

I’ve tried several things to apply echo options on an exec command.

  • Manipulate the promise of this.exec('command with arguments')
  • Execute content inside echo this.echo('[[command with arguments]]', {keepWords: true});
  • Trying some things inside the onBeforeCommand and onAfterCommand.

Please let me know if I need to supply more information. Finally, thank you for writing and maintaining this project.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jcubiccommented, Nov 14, 2019

You can add options to command like:

cat -w filename.info

and in cat command you can parse the command, you will need to disable arity if you use object as argument.

$('body').terminal({
   cat: function(...args) {
       var opt = $.terminal.parse_options(args, {boolean: ['w']})
       // the function return object {_: [], w: true})
       var echo_opt = {};
       if (opt.w) {
           echo_opt.keepWords = true;
       }
       if (opt._.length === 1) {
          var fname = opt._[0]; // or use var [fname] = opt._;
          fetch(fname).then(r => r.text()).then(text => {
             this.echo(text, echo_opt);
          });
        }
   }
}, {checkArity: false});

if you use function to get args array like this:

$('body').terminal(function(command) {
   var cmd = $.terminal.parse_command(command);
   var args = cmd.args;
   if (cmd.name == 'cat') {
      // ... code from previous snippet
   }
});
0reactions
jcubiccommented, Dec 9, 2019

renderHandler was added in version 2.9.0. With this new API you just need to return object from server {echo: 'string', options: {keepWords: true}} and in render handler you check if value is plainObject and if it have echo property if so you echo the string and use options:

$('...').terminal('server.php', {
    renderHandler: function(value) {
        if ($.isPlainObject(value) && value.echo) {
            this.echo(value.echo, value.options);
            return false;
        }
    }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you get the output of a system command in Go?
Use exec. Run by specifying Pipe as the stdout (and stderr if you want). It will return cmd, which contains an os. File...
Read more >
Using Format commands to change output view - PowerShell
PowerShell has a extensible formatting system that allows you to present output in lists, tables, or custom layouts.
Read more >
Advanced command execution in Go with os/exec
Running a command and showing output​​ Writer interface so we can set them to any type that implements Write() method, like os. File...
Read more >
Document.execCommand() - Web APIs | MDN
Removes all formatting from the current selection. selectAll. Selects all of the content of the editable region. strikeThrough. Toggles ...
Read more >
4 Formatting Query Results - Oracle Help Center
Formatting Columns. Through the SQL*Plus COLUMN command, you can change the column headings and reformat the column data in your query results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found