• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Multiline text output inside exec block is displayed as 1 line

Scheduled Pinned Locked Moved Development
4 Posts 2 Posters 424 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    autonomus
    last edited by autonomus Aug 21, 2020, 11:26 AM Aug 21, 2020, 11:19 AM

    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

    S 1 Reply Last reply Aug 21, 2020, 12:15 PM Reply Quote 0
    • S Offline
      sdetweil @autonomus
      last edited by sdetweil Aug 21, 2020, 12:16 PM Aug 21, 2020, 12:15 PM

      @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

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      A 1 Reply Last reply Aug 21, 2020, 11:10 PM Reply Quote 0
      • A Offline
        autonomus @sdetweil
        last edited by autonomus Aug 21, 2020, 11:11 PM Aug 21, 2020, 11:10 PM

        @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.

        S 1 Reply Last reply Aug 21, 2020, 11:11 PM Reply Quote 0
        • S Offline
          sdetweil @autonomus
          last edited by Aug 21, 2020, 11:11 PM

          @autonomus cool. glad u got it working

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Sam, technical setup by Karsten.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy