print.js 1 KB
function printMemory(state) {
  let addr = 0;
  console.log(`----------------`);
  for (const block of state) {
    if (block.id == -1) {
      console.log(`|     free     |`);
    } else {
      const pid = `${block.id}`.padStart(6, " ");
      console.log(`| pid: ${pid}  |`);
    }
    const size = `${block.size}`.padStart(5, " ");
    const from = `${addr}`.padStart(5, " ");
    addr += block.size;
    const to = `${addr}`.padStart(5, " ");
    console.log(`|              |`);
    console.log(`| size: ${size}K |`);
    console.log(`| from: ${from}K |`);
    console.log(`| to:   ${to}K |`);
    console.log(`----------------`);
  }
}

function printStatus(state) {
  const freeBlocks = state.filter(block => block.id == -1);
  const count = freeBlocks.length;
  const totalFree = freeBlocks.reduce((z, block) => z + block.size, 0);
  console.log(`${totalFree}K free`);
  console.log(`${count} block(s)`);
  console.log(`Average: ${Math.round(totalFree / count)}\n`);
}

module.exports = { printMemory, printStatus };