diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-12-24 13:54:43 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-12-24 13:54:43 +0100 |
| commit | a0666be3ab58ed83ad6d622cfe2b8293c40dffbb (patch) | |
| tree | e2688587db986a3bc4b040dce1041dac3b352a80 /lib | |
| parent | c80b2f4575cfc3d74ec8e0b112943cbd081ffcd7 (diff) | |
Update code and dpendencies
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/renderers/256_colors.js | 10 | ||||
| -rw-r--r-- | lib/renderers/ansi.js | 6 | ||||
| -rw-r--r-- | lib/renderers/fake_color.js | 2 | ||||
| -rw-r--r-- | lib/renderers/true_color.js | 2 | ||||
| -rw-r--r-- | lib/screens/circle.js | 36 | ||||
| -rw-r--r-- | lib/screens/gradients.js | 26 | ||||
| -rw-r--r-- | lib/screens/mirrors.js | 44 | ||||
| -rw-r--r-- | lib/screens/random.js | 26 | ||||
| -rw-r--r-- | lib/screens/sprinkles.js | 28 | ||||
| -rw-r--r-- | lib/tomato_sauce.js | 174 | ||||
| -rw-r--r-- | lib/util.js | 62 |
11 files changed, 208 insertions, 208 deletions
diff --git a/lib/renderers/256_colors.js b/lib/renderers/256_colors.js index f6e57d3..8fc67b2 100644 --- a/lib/renderers/256_colors.js +++ b/lib/renderers/256_colors.js @@ -9,11 +9,11 @@ */ module.exports = function (red, blue, green) { - const redValue = Math.round(red * 5 / 255); - const blueValue = Math.round(blue * 5 / 255); - const greenValue = Math.round(green * 5 / 255); + const redValue = Math.round(red * 5 / 255); + const blueValue = Math.round(blue * 5 / 255); + const greenValue = Math.round(green * 5 / 255); - const colorNumber = 16 + 36 * redValue + 6 * greenValue + blueValue; + const colorNumber = 16 + 36 * redValue + 6 * greenValue + blueValue; - return `\x1B[48;5;${colorNumber}m`; + return `\x1B[48;5;${colorNumber}m`; }; diff --git a/lib/renderers/ansi.js b/lib/renderers/ansi.js index b3a461d..b15be7c 100644 --- a/lib/renderers/ansi.js +++ b/lib/renderers/ansi.js @@ -9,8 +9,8 @@ */ module.exports = function (red, blue, green) { - const colorOffset = Math.round((red + blue + green) * 7 / (255 * 3)); - const colorNumber = 40 + colorOffset; + const colorOffset = Math.round((red + blue + green) * 7 / (255 * 3)); + const colorNumber = 40 + colorOffset; - return `\x1B[${colorNumber}m`; + return `\x1B[${colorNumber}m`; }; diff --git a/lib/renderers/fake_color.js b/lib/renderers/fake_color.js index b03309c..d0482dc 100644 --- a/lib/renderers/fake_color.js +++ b/lib/renderers/fake_color.js @@ -8,5 +8,5 @@ */ module.exports = function (red, blue, green) { - return `\x1B[28;2;${Math.round(red)};${Math.round(green)};${Math.round(blue)}m`; + return `\x1B[28;2;${Math.round(red)};${Math.round(green)};${Math.round(blue)}m`; }; diff --git a/lib/renderers/true_color.js b/lib/renderers/true_color.js index 811d186..ae7d11f 100644 --- a/lib/renderers/true_color.js +++ b/lib/renderers/true_color.js @@ -15,5 +15,5 @@ */ module.exports = function (red, blue, green) { - return `\x1B[48;2;${Math.round(red)};${Math.round(green)};${Math.round(blue)}m`; + return `\x1B[48;2;${Math.round(red)};${Math.round(green)};${Math.round(blue)}m`; }; diff --git a/lib/screens/circle.js b/lib/screens/circle.js index 720e250..d09cb07 100644 --- a/lib/screens/circle.js +++ b/lib/screens/circle.js @@ -8,29 +8,29 @@ */ module.exports = function (modulation, width, height, renderer) { - const response = []; + let response = ''; - const circles = width > height ? height : width; + const circles = width > height ? height : width; - for (let i = 0; i < circles; ++i) { - const centerX = Math.round(width / 2) + 1; - const centerY = Math.round(height / 2) + 1; + for (let i = 0; i < circles; ++i) { + const centerX = Math.round(width / 2) + 1; + const centerY = Math.round(height / 2) + 1; - const red = Math.floor(Math.random() * 255); - const blue = Math.floor(Math.random() * 255); - const green = Math.floor(Math.random() * 255); + const red = Math.floor(Math.random() * 255); + const blue = Math.floor(Math.random() * 255); + const green = Math.floor(Math.random() * 255); - for (let j = 0; j < 180; ++j) { - const angle = 2 * j * (Math.PI / 180); - const x = Math.round(centerX + Math.sin(angle) * i); - const y = Math.round(centerY + Math.cos(angle) * i); + for (let j = 0; j < 180; ++j) { + const angle = 2 * j * (Math.PI / 180); + const x = Math.round(centerX + Math.sin(angle) * i); + const y = Math.round(centerY + Math.cos(angle) * i); - if (x <= width && x > 0 && y <= height && y > 0) { - const position = `\x1B[${y};${x}H`; // Move cursor to y,x (CSI y;x H) - response += `${position}${renderer(red, blue, green)} `; - } + if (x <= width && x > 0 && y <= height && y > 0) { + const position = `\x1B[${y};${x}H`; // Move cursor to y,x (CSI y;x H) + response += `${position}${renderer(red, blue, green)} `; + } + } } - } - return response; + return response; }; diff --git a/lib/screens/gradients.js b/lib/screens/gradients.js index 2d4a2f0..9694fc6 100644 --- a/lib/screens/gradients.js +++ b/lib/screens/gradients.js @@ -8,22 +8,22 @@ */ module.exports = function (modulation, width, height, renderer) { - let response = ''; + let response = ''; - for (let i = 0; i < height; ++i) { - for (let j = 0; j < width; ++j) { - const red = ((modulation + i) * 255 / height) % 255; - const blue = ((modulation + j) * 255 / width) % 255; - const green = ((modulation + i * j) * 255 / (width * height)) % 255; + for (let i = 0; i < height; ++i) { + for (let j = 0; j < width; ++j) { + const red = ((modulation + i) * 255 / height) % 255; + const blue = ((modulation + j) * 255 / width) % 255; + const green = ((modulation + i * j) * 255 / (width * height)) % 255; - response = response + renderer(red, blue, green); - response = response + ' '; - } + response = response + renderer(red, blue, green); + response = response + ' '; + } - if (i < height - 1) { - response = response + '\n'; + if (i < height - 1) { + response = response + '\n'; + } } - } - return response; + return response; }; diff --git a/lib/screens/mirrors.js b/lib/screens/mirrors.js index e842217..4f29c7d 100644 --- a/lib/screens/mirrors.js +++ b/lib/screens/mirrors.js @@ -8,33 +8,33 @@ */ module.exports = function (modulation, width, height, renderer) { - const response = []; + const response = []; - const scale = 2 + Math.round(Math.random() * 4); - const scaledHeight = Math.floor(height / scale); - const scaledWidth = Math.floor(width / scale); + const scale = 2 + Math.round(Math.random() * 4); + const scaledHeight = Math.floor(height / scale); + const scaledWidth = Math.floor(width / scale); - for (let i = 0; i < scaledHeight; ++i) { - const row = []; - for (let j = 0; j < scaledWidth; ++j) { - const red = ((modulation + i) * 255 / height) % 255; - const blue = ((modulation + j) * 255 / width) % 255; - const green = ((modulation + i * j) * 255 / (width * height)) % 255; + for (let i = 0; i < scaledHeight; ++i) { + const row = []; + for (let j = 0; j < scaledWidth; ++j) { + const red = ((modulation + i) * 255 / height) % 255; + const blue = ((modulation + j) * 255 / width) % 255; + const green = ((modulation + i * j) * 255 / (width * height)) % 255; - const cell = [renderer(red, blue, green), ' ']; - row.push(cell.join('')); - } + const cell = [renderer(red, blue, green), ' ']; + row.push(cell.join('')); + } - let rowText = ''; - for (let j = 0; j < scale; ++j) { - rowText += row.reverse().join(''); - } + let rowText = ''; + for (let j = 0; j < scale; ++j) { + rowText += row.reverse().join(''); + } - for (let j = 1; j < scale; ++j) { - response[j * i] = rowText; - response[(height - 1 - (j * i))] = rowText; + for (let j = 1; j < scale; ++j) { + response[j * i] = rowText; + response[(height - 1 - (j * i))] = rowText; + } } - } - return response.join('\n'); + return response.join('\n'); }; diff --git a/lib/screens/random.js b/lib/screens/random.js index 0cacea7..d5de257 100644 --- a/lib/screens/random.js +++ b/lib/screens/random.js @@ -8,22 +8,22 @@ */ module.exports = function (modulation, width, height, renderer) { - let response = ''; + let response = ''; - for (let i = 0; i < height; ++i) { - for (let j = 0; j < width; ++j) { - const red = Math.floor(Math.random() * 255); - const blue = Math.floor(Math.random() * 255); - const green = Math.floor(Math.random() * 255); + for (let i = 0; i < height; ++i) { + for (let j = 0; j < width; ++j) { + const red = Math.floor(Math.random() * 255); + const blue = Math.floor(Math.random() * 255); + const green = Math.floor(Math.random() * 255); - response = response + renderer(red, blue, green); - response = response + ' '; - } + response = response + renderer(red, blue, green); + response = response + ' '; + } - if (i < height - 1) { - response = response + '\n'; + if (i < height - 1) { + response = response + '\n'; + } } - } - return response; + return response; }; diff --git a/lib/screens/sprinkles.js b/lib/screens/sprinkles.js index f12200e..9ba1e16 100644 --- a/lib/screens/sprinkles.js +++ b/lib/screens/sprinkles.js @@ -9,24 +9,24 @@ */ module.exports = function (modulation, width, height, renderer) { - let response = ''; + let response = ''; - const maxSprinkleCount = (width * height) / 2; - const minSprinkleCount = (width * height) / 8; - const sprinkleCount = Math.round(Math.random() * (maxSprinkleCount - minSprinkleCount)) + minSprinkleCount; + const maxSprinkleCount = (width * height) / 2; + const minSprinkleCount = (width * height) / 8; + const sprinkleCount = Math.round(Math.random() * (maxSprinkleCount - minSprinkleCount)) + minSprinkleCount; - const red = Math.floor(Math.random() * 255); - const blue = Math.floor(Math.random() * 255); - const green = Math.floor(Math.random() * 255); + const red = Math.floor(Math.random() * 255); + const blue = Math.floor(Math.random() * 255); + const green = Math.floor(Math.random() * 255); - for (let i = 0; i < sprinkleCount; ++i) { - const x = Math.round(Math.random() * (width - 1)) + 1; - const y = Math.round(Math.random() * (height - 1)) + 1; + for (let i = 0; i < sprinkleCount; ++i) { + const x = Math.round(Math.random() * (width - 1)) + 1; + const y = Math.round(Math.random() * (height - 1)) + 1; - const position = `\x1B[${y};${x}H`; // Move cursor to y,x (CSI y;x H) + const position = `\x1B[${y};${x}H`; // Move cursor to y,x (CSI y;x H) - response += `${position}${renderer(red, blue, green)} `; - } + response += `${position}${renderer(red, blue, green)} `; + } - return response; + return response; }; diff --git a/lib/tomato_sauce.js b/lib/tomato_sauce.js index f3017ea..e71a207 100644 --- a/lib/tomato_sauce.js +++ b/lib/tomato_sauce.js @@ -6,10 +6,10 @@ const EventEmitter = require('events'); const Util = require('./util'); const internals = { - // Interpret as Command Sequences. - kEscape: new Buffer([0xFF, 0xF4, 0XFF, 0xFD, 0x06]), // IAC IP IAC DO TIMING_MARK - kNAWSRequest: new Buffer([0xFF, 0xFD, 0X1F]), // IAC DO NAWS - kNAWSResponse: new Buffer([0xFF, 0xFB, 0X1F, 0xFF, 0xFA, 0X1F]) // IAC WILL NAWS IAC SB NAWS + // Interpret as Command Sequences. + kEscape: Buffer.from([0xFF, 0xF4, 0XFF, 0xFD, 0x06]), // IAC IP IAC DO TIMING_MARK + kNAWSRequest: Buffer.from([0xFF, 0xFD, 0X1F]), // IAC DO NAWS + kNAWSResponse: Buffer.from([0xFF, 0xFB, 0X1F, 0xFF, 0xFA, 0X1F]) // IAC WILL NAWS IAC SB NAWS }; /** @@ -63,127 +63,127 @@ const internals = { */ const TomatoSauce = class TomatoSauce extends EventEmitter { - constructor(config = {}) { + constructor(config = {}) { - super(); + super(); - this.screens = []; - this.renderers = []; + this.screens = []; + this.renderers = []; - // Defaults. - this.port = 9999; - this.frequency = 333; - this.modulation = 5; + // Defaults. + this.port = 9999; + this.frequency = 333; + this.modulation = 5; - Object.assign(this, config); - } + Object.assign(this, config); + } - /** + /** * Main entry point, initializes the server and binds events for connections * * @function run * @instance * @memberof TomatoSauce */ - run() { + run() { - this._startServer(); - this._bindEvents(); - } + this._startServer(); + this._bindEvents(); + } - // Creates a socket, server based on the configuration. Emits the - // listening event when ready. + // Creates a socket, server based on the configuration. Emits the + // listening event when ready. - _startServer() { + _startServer() { - const server = Net.createServer(); - this.server = server; - server.listen(this.port, () => { + const server = Net.createServer(); + this.server = server; + server.listen(this.port, () => { - this.emit('listening', { - data: { - server - } - }); - }); - } + this.emit('listening', { + data: { + server + } + }); + }); + } - // Binds the connection and error events + // Binds the connection and error events - _bindEvents() { + _bindEvents() { - // Send the error event all the way up. - this.server.on('error', (error) => { + // Send the error event all the way up. + this.server.on('error', (error) => { - this.emit('error', { - data: { - error - } - }); - }); + this.emit('error', { + data: { + error + } + }); + }); - // Send the error event all the way up. - this.server.on('connection', (socket) => { + // Send the error event all the way up. + this.server.on('connection', (socket) => { - this._renderScreen(socket); - }); - } + this._renderScreen(socket); + }); + } - // Obtains viewport size, and initializes a random screen with a random - // renderer, setting the required interval to draw. + // Obtains viewport size, and initializes a random screen with a random + // renderer, setting the required interval to draw. - _renderScreen(socket) { + _renderScreen(socket) { - const connectionData = { - width: null, - height: null, - modulation: 0, - screen: this._getScreen(), - renderer: this._getRenderer(), - socket - }; - const interval = null; + const connectionData = { + width: null, + height: null, + modulation: 0, + screen: this._getScreen(), + renderer: this._getRenderer(), + socket + }; + let interval = null; - socket.write(internals.kNAWSRequest); + socket.write(internals.kNAWSRequest); - socket.on('data', (data) => { + socket.on('data', (data) => { - if (data.slice(0, 6).compare(internals.kNAWSResponse) === 0) { - connectionData.width = Util.parse16BitBuffer(data.slice(6, 8)); - connectionData.height = Util.parse16BitBuffer(data.slice(8, 10)); + if (data.slice(0, 6).compare(internals.kNAWSResponse) === 0) { + connectionData.width = Util.parse16BitBuffer(data.slice(6, 8)); + connectionData.height = Util.parse16BitBuffer(data.slice(8, 10)); - socket.write('\x1B[2J'); // Clear the Screen (CSI 2 J) - interval = setInterval(this._writeMessage.bind(this, connectionData), this.frequency); - } + socket.write('\x1B[2J'); // Clear the Screen (CSI 2 J) + interval = setInterval(this._writeMessage.bind(this, connectionData), this.frequency); + } - if (data.compare(internals.kEscape) === 0) { - socket.write('\n'); - clearInterval(interval); - socket.end(); - } - }); - } + if (data.compare(internals.kEscape) === 0) { + socket.write('\n'); + clearInterval(interval); + socket.end(); + } + }); + } - // Resets the cursor, gets a frame and sends it to the socket. + // Resets the cursor, gets a frame and sends it to the socket. - _writeMessage(connectionData) { + _writeMessage(connectionData) { - const payload = connectionData.screen(connectionData.modulation, connectionData.width, connectionData.height, connectionData.renderer); - const message = `\x1B[1;1H${payload}`; // reset cursor position before payload + const payload = connectionData.screen(connectionData.modulation, connectionData.width, connectionData.height, connectionData.renderer); + const message = `\x1B[1;1H${payload}`; // reset cursor position before payload - connectionData.modulation = (connectionData.modulation + this.modulation) % 256; - connectionData.socket.write(message); - } + connectionData.modulation = (connectionData.modulation + this.modulation) % 256; + connectionData.socket.write(message); + } - _getScreen() { + _getScreen() { - return Util.pickRandom(this.screens); - } + return Util.pickRandom(this.screens); + } - _getRenderer() { + _getRenderer() { - return Util.pickRandom(this.renderers); - } + return Util.pickRandom(this.renderers); + } }; module.exports = TomatoSauce; diff --git a/lib/util.js b/lib/util.js index 65f14ae..4c8c3c1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -11,7 +11,7 @@ const Path = require('path'); */ const Util = { - /** + /** * Parses a 16 bit number buffer * * @function parse16BitBuffer @@ -19,12 +19,12 @@ const Util = { * @param {Array<String>} buffer the buffer to parse * @return {Number} the parsed value */ - parse16BitBuffer(buffer) { + parse16BitBuffer(buffer) { - return buffer[0] * 256 + buffer[1]; - }, + return buffer[0] * 256 + buffer[1]; + }, - /** + /** * Picks a random element from an array * * @function pickRandom @@ -32,12 +32,12 @@ const Util = { * @param {Array} array the array to use * @return {Any} the picked element */ - pickRandom(array) { + pickRandom(array) { - return array[Math.floor(Math.random() * array.length)]; - }, + return array[Math.floor(Math.random() * array.length)]; + }, - /** + /** * For a gi ven path, requires all of the files and returns an array * with the results. If the directory contains any non-requireable * files, it will fail. @@ -47,36 +47,36 @@ const Util = { * @param {String} path the path where the files are located * @return {Array} the array of all the loaded modules */ - loadFiles(path) { + loadFiles(path) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { - Fs.readdir(path, (err, files) => { + Fs.readdir(path, (err, files) => { - if (err) { - return reject(err); - } + if (err) { + return reject(err); + } - const loadedFiles = []; + const loadedFiles = []; - for (const file of files) { - const filePath = Path.join(path, file); - let loadedFile; + for (const file of files) { + const filePath = Path.join(path, file); + let loadedFile; - try { - loadedFile = require(filePath); - } - catch (err) { - return reject(err); - } + try { + loadedFile = require(filePath); + } + catch (err) { + return reject(err); + } - loadedFiles.push(loadedFile); - } + loadedFiles.push(loadedFile); + } - resolve(loadedFiles); - }); - }); - } + resolve(loadedFiles); + }); + }); + } }; module.exports = Util; |