diff --git a/matrix/format.ts b/matrix/format.ts new file mode 100644 index 0000000..746d4d0 --- /dev/null +++ b/matrix/format.ts @@ -0,0 +1,128 @@ +import { thousandsSeparator, toHHMMSS, timeSince } from '@squeebot/core/lib/common'; +import { HTMLFormatter } from '@squeebot/core/lib/types'; + +export class MatrixFormatter extends HTMLFormatter { + colors: { [key: string]: string } = { + black: '#000000', + darkblue: '#00007f', + green: '#009300', + red: '#ff0000', + brown: '#7f0000', + purple: '#9c009c', + gold: '#fc7f00', + yellow: '#ffff00', + limegreen: '#00fc00', + cyan: '#00ffff', + lightblue: '#0000fc', + blue: '#009393', + pink: '#ff00ff', + darkgray: '#7f7f7f', + gray: '#d2d2d2', + white: '#ffffff' + }; + + public color(color: string, msg: string): string { + return `${msg}`; + } + + compose(objs: any): any { + const str = []; + + for (const i in objs) { + const elem = objs[i]; + + const elemType = elem[0]; + let elemValue = elem[1]; + const elemParams = elem[2]; + + if (!elemValue) { + continue; + } + + let valueColor = null; + + // Special types + if (elemParams && elemParams.type) { + switch (elemParams.type) { + case 'time': + elemValue = new Date(elemValue).toString(); + break; + case 'metric': + elemValue = thousandsSeparator(elemValue); + break; + case 'timesince': + elemValue = timeSince(elemValue); + break; + case 'duration': + elemValue = toHHMMSS(elemValue); + break; + case 'description': + valueColor = 'blue'; + elemValue = `"${elemValue}"`; + break; + } + } + + // Currently, disregard image fields. + if (elemType === 'image') { + continue; + } + + // Bold value + if (elemType === 'bold' || elemType === 'b' || elemType === 'strong') { + elemValue = this.format('bold', elemValue); + } + + // Italic value + if (elemType === 'italic' || elemType === 'i' || elemType === 'em' || elemType === 'emphasis') { + elemValue = this.format('italic', elemValue); + } + + // Underlined value + if (elemType === 'underline' || elemType === 'ul') { + elemValue = this.format('underline', elemValue); + } + + // Colorize the value + if (elemParams?.color) { + valueColor = elemParams.color; + } + + // Add the label, if present + if (elemParams?.label) { + // Set label color to default + let labelColor = 'green'; + if (elemParams.color) { + labelColor = elemParams.color; + } + + if (!valueColor) { + valueColor = 'blue'; + } + + // Handle array label + // Prefer the label over the icon version. + let label = elemParams.label; + if (typeof label === 'object') { + label = elemParams.label[elemParams.label.length - 1]; + } else { + label = label; + } + + // Add colon + label = `${label}:`; + + if (valueColor && valueColor === labelColor) { + str.push(this.color(valueColor, label + ' ' + elemValue)); + } else { + str.push(this.color(labelColor, label) + ' ' + (valueColor ? this.color(valueColor, elemValue) : elemValue)); + } + } else { + str.push(valueColor ? this.color(valueColor, elemValue) : elemValue); + } + } + + // May return an object, but your protocol must support it. + return str.join(' '); + } +} diff --git a/matrix/plugin.json b/matrix/plugin.json index c599c38..6740711 100644 --- a/matrix/plugin.json +++ b/matrix/plugin.json @@ -3,7 +3,7 @@ "name": "matrix", "description": "Matrix.org Service for Squeebot 3", "tags": ["service", "matrix"], - "version": "1.0.2", + "version": "1.0.3", "dependencies": ["control?"], "npmDependencies": ["matrix-bot-sdk@0.5.19"] } diff --git a/matrix/plugin.ts b/matrix/plugin.ts index 5a5fd19..dd40905 100644 --- a/matrix/plugin.ts +++ b/matrix/plugin.ts @@ -20,12 +20,13 @@ import { logger } from '@squeebot/core/lib/core'; import { EMessageType, Formatter, - HTMLFormatter, IMessage, IMessageTarget, Protocol } from '@squeebot/core/lib/types'; +import { MatrixFormatter } from './format'; + class MatrixMessageAdapter implements IMessage { public time: Date = new Date(); public resolved = false; @@ -70,7 +71,7 @@ class MatrixMessageAdapter implements IMessage { } class MatrixProtocol extends Protocol { - public format: Formatter = new HTMLFormatter(); + public format: Formatter = new MatrixFormatter(); public type = 'MatrixProtocol'; public nameCache: Map = new Map(); diff --git a/squeebot.repo.json b/squeebot.repo.json index 5218952..298627f 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -3,7 +3,7 @@ "plugins": [ { "name": "matrix", - "version": "1.0.2" + "version": "1.0.3" } ], "typescript": true