<commit_before>/*
 * This file is part of EspruinoHub, a Bluetooth-MQTT bridge for
 * Puck.js/Espruino JavaScript Microcontrollers
 *
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ----------------------------------------------------------------------------
 * Writes the current status to the console
 * ----------------------------------------------------------------------------
 */

var attributes = require('./attributes');

var logHistory = [];

 function getStatusText() {
   var inRange = require("./discovery.js").inRange;
   var status = '';
   //process.stdout.write('\x1B[2J\x1B[0f');
   // ...
   status += logHistory.join("\n")+"\n\n";

   status += (new Date()).toString()+"\n\n";
   // sort by most recent
   var arr = [];
   for (var id in inRange)
     arr.push(inRange[id]);
   //arr.sort(function(a,b) { return a.rssi - b.rssi; });
   arr.sort(function(a,b) { return a.id.localeCompare(b.id); });
   // output
   var amt = 3;
   var maxAmt = process.stdout.getWindowSize()[1];
   for (var i in arr) {
     var p = arr[i];
     if (++amt > maxAmt) { console.log("..."); return; }
     status += p.id+" - "+p.name+" (RSSI "+p.rssi+")\n";
     for (var j in p.data) {
       if (++amt > maxAmt) { console.log("..."); return; }
       var n = attributes.getReadableAttributeName(j);
       var v = p.data[j].payload;
       status += "  "+n+" => "+JSON.stringify(attributes.decodeAttribute(n,v))+"\n";
     }
   }
   return status;
 }

 function dumpStatus() {
   var status = '\033c'; // clear screen
   status += getStatusText();
   console._log(status);
 }

 // -----------------------------------------

exports.init = function() {
  console._log = console.log;
  /** Replace existing console.log with something that'll let us
  report status alongside evrything else */
  console.log = function() {
    //var args = Array.from(arguments);
    var args = Array.prototype.slice.call(arguments);
    if (logHistory.length>10)
      logHistory = logHistory.slice(-5);
    logHistory.push(args.join("\t"));
    dumpStatus();
  };

  setInterval(dumpStatus, 1000);
};
exports.getStatusText = getStatusText;
<commit_msg>Disable status display when running without console, eg. `systemd`<commit_after>/*
 * This file is part of EspruinoHub, a Bluetooth-MQTT bridge for
 * Puck.js/Espruino JavaScript Microcontrollers
 *
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ----------------------------------------------------------------------------
 * Writes the current status to the console
 * ----------------------------------------------------------------------------
 */

var attributes = require('./attributes');

var logHistory = [];

 function getStatusText() {
   var inRange = require("./discovery.js").inRange;
   var status = '';
   //process.stdout.write('\x1B[2J\x1B[0f');
   // ...
   status += logHistory.join("\n")+"\n\n";

   status += (new Date()).toString()+"\n\n";
   // sort by most recent
   var arr = [];
   for (var id in inRange)
     arr.push(inRange[id]);
   //arr.sort(function(a,b) { return a.rssi - b.rssi; });
   arr.sort(function(a,b) { return a.id.localeCompare(b.id); });
   // output
   var amt = 3;
   var maxAmt = process.stdout.getWindowSize()[1];
   for (var i in arr) {
     var p = arr[i];
     if (++amt > maxAmt) { console.log("..."); return; }
     status += p.id+" - "+p.name+" (RSSI "+p.rssi+")\n";
     for (var j in p.data) {
       if (++amt > maxAmt) { console.log("..."); return; }
       var n = attributes.getReadableAttributeName(j);
       var v = p.data[j].payload;
       status += "  "+n+" => "+JSON.stringify(attributes.decodeAttribute(n,v))+"\n";
     }
   }
   return status;
 }

 function dumpStatus() {
   var status = '\033c'; // clear screen
   status += getStatusText();
   console._log(status);
 }

 // -----------------------------------------

exports.init = function() {
  if (undefined === process.stdout.getWindowSize)
    return;
  console._log = console.log;
  /** Replace existing console.log with something that'll let us
  report status alongside evrything else */
  console.log = function() {
    //var args = Array.from(arguments);
    var args = Array.prototype.slice.call(arguments);
    if (logHistory.length>10)
      logHistory = logHistory.slice(-5);
    logHistory.push(args.join("\t"));
    dumpStatus();
  };

  setInterval(dumpStatus, 1000);
};
exports.getStatusText = getStatusText;

{'fraction_non_alphanumeric': 0.23846314470661933, 'fraction_numerical': 0.010022969304656505, 'mean_word_length': 2.17428760768721, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4841273', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1465, 'n_words': 388}