muttley
Version:
Monitor Unit Test Tool
125 lines • 5.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const os_1 = __importDefault(require("os"));
const fs_1 = __importDefault(require("fs"));
// for colours see https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
var FgColour;
(function (FgColour) {
FgColour[FgColour["red"] = 31] = "red";
FgColour[FgColour["green"] = 32] = "green";
FgColour[FgColour["yellow"] = 33] = "yellow";
FgColour[FgColour["blue"] = 34] = "blue";
FgColour[FgColour["magenta"] = 35] = "magenta";
FgColour[FgColour["cyan"] = 36] = "cyan";
FgColour[FgColour["white"] = 37] = "white";
})(FgColour = exports.FgColour || (exports.FgColour = {}));
const defaultColumns = 80, defaultRows = 24;
const columns = process.stdout.columns || defaultColumns;
function write(...args) {
process.stdout.write(args.join(''));
}
exports.write = write;
function writeline(...args) {
write(...args, os_1.default.EOL);
}
exports.writeline = writeline;
function renderTable(table) {
const rowHeadings = table.columns.map((column) => column.name.padEnd(column.width)).join(' ');
writeline(rowHeadings);
table.rows.forEach((row, index) => {
if (index < (process.stdout.rows || defaultRows) - 6) {
const color = table.rowColour(row);
write(`\x1b[${color}m`);
table.columns.forEach((column) => {
write(column
.func(row)
.toString()
.padEnd(column.width)
.substr(0, column.width), ' ');
});
writeline('\x1b[0m');
}
});
}
exports.renderTable = renderTable;
let lastTotal = 0, lastIdle = 0, lastSys = 0, lastUser = 0;
function renderHeader(testCount, failing, running, fileCount) {
write('\x1b[2J'); //clear
write('\x1b[0;0H'); // top left
const oneHundred = 100;
const decimalPlaces = 2;
const cpuList = os_1.default.cpus();
const sys = cpuList.map(cpu => cpu.times.sys).reduce((x, y) => x + y);
const user = cpuList.map(cpu => cpu.times.user).reduce((x, y) => x + y);
const idle = cpuList.map(cpu => cpu.times.idle).reduce((x, y) => x + y);
const total = sys + user + idle;
const totalDelta = total - lastTotal;
const idleDelta = ((oneHundred * (idle - lastIdle)) / totalDelta).toFixed(decimalPlaces);
const sysDelta = ((oneHundred * (sys - lastSys)) / totalDelta).toFixed(decimalPlaces);
const userDelta = ((oneHundred * (user - lastUser)) / totalDelta).toFixed(decimalPlaces);
lastIdle = idle;
lastSys = sys;
lastUser = user;
lastTotal = total;
const freeMem = ((oneHundred * os_1.default.freemem()) / os_1.default.totalmem()).toFixed(decimalPlaces);
// time
const timeWidth = 8;
const isoOffsetToTime = 11;
const msPerMinute = 60000;
write(`\x1b[0;${columns - timeWidth}H`);
const tzoffset = new Date().getTimezoneOffset() * msPerMinute; //offset in milliseconds
write(new Date(Date.now() - tzoffset).toISOString().substr(isoOffsetToTime, timeWidth));
// test summary
write('\x1b[0;0H'); // top left again
writeline(`Tests ${testCount}, Failing ${failing}, Running ${running}, Files monitored ${fileCount}`);
// system
write(`CPU Usage ${userDelta}% user, ${sysDelta}% sys, ${idleDelta}% idle, ${freeMem}% mem free`);
}
exports.renderHeader = renderHeader;
function renderFileWindow(filepath, height, line) {
const content = fs_1.default.readFileSync(filepath);
const halfHeight = height / 2;
const lines = content.toString().split(os_1.default.EOL);
const [start, end, firstRow] = lines.length > height ? [line - halfHeight, line + halfHeight, line - halfHeight + 1] : [0, lines.length, 1];
const window = lines.slice(start, end);
let rownum = firstRow;
window.forEach((sourceline) => {
const prefix = rownum === line ? '\x1b[35m' : '';
const rowNumColumnWidth = 6;
writeline(prefix + (rownum++).toString().padEnd(rowNumColumnWidth), sourceline, '\x1b[0m');
});
}
exports.renderFileWindow = renderFileWindow;
// export function renderPacman(): void {
// Oikake (追いかけ)
// see https://en.wikipedia.org/wiki/ANSI_escape_code
// const blue = '\x1b[94m';
// const white = '\x1b[97m';
// for (let i = 0; i < 40; i++) {
// process.stdout.write(blue + String.fromCodePoint(0x2551) + white + String.fromCodePoint(0x2022) + white);
// if (i < move) console.log();
// else if (move === i) console.log(String.fromCodePoint(0x1f354));
// else console.log(String.fromCodePoint(0x2022));
// }
// move = move > 30 ? 0 : move + 1;
// process.stdout.write(
// [
// ,
// '\x1b[31m',
// // String.fromCodePoint(0x2560),
// // String.fromCodePoint(0x2550),
// // String.fromCodePoint(0x2550),
// String.fromCodePoint(0x2557),
// String.fromCodePoint(0x2022), // dot
// String.fromCodePoint(0x1f354), // burger
// String.fromCodePoint(0x1f3a7), // phones
// String.fromCodePoint(0x1f47b), // ghost https://en.wikipedia.org/wiki/Ghosts_(Pac-Man)
// String.fromCodePoint(0x1f3ae), // gamepad
// '\x1b[0m',
// ].join(''),
// );
// }
//# sourceMappingURL=render.js.map