MagicMirror² v2.12.0 is available! For more information about this release, check out this topic.

Multiline text output inside exec block is displayed as 1 line



  • Hello,

    I have a very simple module that executes a linux “cat” command on a file every N seconds.
    It works as expected except the line breaks in the file are ignored. All lines are displayed as 1 single line.

    This is how I call the cat command inside node_helper.js:
    var NodeHelper = require(‘node_helper’);
    const exec = require(‘child_process’).exec;

    module.exports = NodeHelper.create({

    readfile: function () {
    exec("cat /path/to/file/out.txt", (error, stdout, stderr) => {
    	if (error) {
    		console.error(`exec error: ${error}`);
    		return;
    	}
        	this.sendSocketNotification('UPDATE', stdout);
    });
    },
    
    socketNotificationReceived: function(notification, payload) {
    	if (notification === 'TICK') {
    	this.readfile();
    }
    },
    

    });

    Inside the module class, this is how I handle the update coming from socketNotificationReceived:

    	socketNotificationReceived: function(notification, payload) {
    	    if (notification === 'UPDATE') {
    	this.config.text = payload;
    	this.updateDom()
        }
    	},
    

    The reason I took this approach is I have absolutely no JS experience whatsoever, so I’m preparing the text file using a bash script and then use my MMM to display the result. (I moved the processing outside JS domain)

    Is it possible to use “cat” and display multiline?
    Or should I use a completely different approach e.g. reading the file inside JS line by line and displaying the result. If this is the correct approach then please point me to some examples.

    Thanks in advance



  • @autonomus when cat returns, u have a big blob of text, with a line feed ‘\n’ between each ‘line’

    web doesn’t understand ‘\n’

    so you have to change those to ‘< br>’
    line break. note there is a space between < and br. this forum hides the content if that space is not provided



  • @sdetweil Thanks for the help!
    You made me realise that I need to return HTML syntax. This works as expected:
    line 1 text < br>
    line 2 text < br>

    For reference, I was building a table for my data.
    Instead of multi lines with tabs, I switched to HTML tables.



  • @autonomus cool. glad u got it working


Log in to reply