JSFiddle - React, Tailwind, and code Playground

by Farzad YZ

JavaScript

const os = require("os");
// install this custom module!!
const babar = require("babar");

function calculateFreeMemPercentage() {
  return Number(((os.freemem * 100) / os.totalmem).toFixed(2));
}

function printChart(points) {
  console.log(
    babar(points, {
      caption: "Memory usage chart",
      color: "magenta",
      width: 40,
      height: 40,
      yFractions: 0.1,
      xFractions: 0.1
    })
  );
}

// Run
(function main() {
  const points = [];
  let i = 0;

  setInterval(() => {
    // Gets a 2d Array with x axis values as counter and y axis values as memory usage percentage
    points.push([i++, calculateFreeMemPercentage()]);
    points.length > 1 &&
      (function fragment() {
        console.clear();
        printChart(points);
      })();
  }, 1000);
})();