MultiplayerMinesweeper/scripts/log.mjs

126 lines
2.7 KiB
JavaScript

import * as os from "os"
var mode = 0
var logs = []
var rightView = [0,1,2,3,4,5,6]
var info = [[],[]]
export function setMode(type) {
mode = type
}
export function updateLog() {
if (mode) return
let templogs = logs;
console.clear()
const columns = process.stdout.columns;
const rows = process.stdout.rows;
const vertSplit = Math.round(rows/4)
for (var i = 0; i < vertSplit; i++) {
process.stdout.cursorTo(Math.round(columns/2),i);
process.stdout.clearLine();
process.stdout.write(`|`)
}
for (var i = 0; i < columns; i++) {
process.stdout.cursorTo(i+1, vertSplit)
process.stdout.write(`=`)
}
process.stdout.cursorTo(0,0)
process.stdout.write("Players:")
const games = info[0]
var pointer = 0
for (i = 0; i < games.length; i++) {
process.stdout.cursorTo(0,pointer+1)
process.stdout.write(`\x1b[46mGameName: \x1b[1m${games[i].name}\x1b[0m`)
const players = games[i].players;
for (var j = 0; j < players.length; j++) {
process.stdout.cursorTo(0,pointer+2)
process.stdout.write(`--Name: \x1b[1m${players[j].name}\x1b[0m -\x1b[1m ID:${players[j].id}\x1b[0m`)
pointer++;
}
pointer++;
}
if (templogs.length > Math.round(3*rows/4)-3) {
templogs.splice(0, logs.length-Math.round(3*rows/4)+2);
}
for (var i = 0; i < templogs.length; i++ ) {
process.stdout.cursorTo(0,vertSplit+1+i)
process.stdout.write(`${templogs[i]}${os.EOL}` )
}
}
export function log(string, color) {
if (mode) return console.log(string)
if (string == undefined) return;
var prefix = '';
switch (color) {
case "reset":
prefix = "\x1b[0m"
break;
case "bright":
prefix = "\x1b[1m"
break;
case "dim":
prefix = "\x1b[2m"
break;
case "underscore":
prefix = "\x1b[4m"
break;
case "reverse":
prefix = "\x1b[7m"
break;
case "FgBlack":
prefix = "\x1b[30m"
break;
case "FgRed":
prefix = "\x1b[31m"
break;
case "FgGreen":
prefix = "\x1b[32m"
break;
case "FgYellow":
prefix = "\x1b[33m"
break;
case "FgBlue":
prefix = "\x1b[34m"
break;
case "FgMagenta":
prefix = "\x1b[35m"
break;
case "FgCyan":
prefix = "\x1b[36m"
break;
case "FgWhite":
prefix = "\x1b[37m"
break;
}
// BgBlack = "\x1b[40m"
// BgRed = "\x1b[41m"
// BgGreen = "\x1b[42m"
// BgYellow = "\x1b[43m"
// BgBlue = "\x1b[44m"
// BgMagenta = "\x1b[45m"
// BgCyan = "\x1b[46m"
// BgWhite = "\x1b[47m"
const newString = prefix + string + "\x1b[0m"
logs.push(newString)
updateLog();
}
export function setInfo (players){
if (mode) return false
if (players != undefined) {
info[0] = players;
updateLog()
}
}