const m = require('mithril') const socket = require('../socket') const Log = { oninit: function() { this.connected = socket.connected this.loglines = [] socket.on('newlog', data => { this.loglines.push(this.formatLine(data)) m.redraw() }) socket.on('connect', () => { this.loglines = [] this.loadData() socket.emit('core.listenlogs', {}) m.redraw() }) this.loadData() }, loadData: function() { socket.emit('core.getlastlogs', {}, (res) => { this.loglines = res.map(this.formatLine) m.redraw() }) }, formatLine: function(line) { return m.trust(line.replace(/\\033\[37m/g, '') .replace(/\\033\[33m/g, '') .replace(/\\033\[36m/g, '') .replace(/\\033\[35m/g, '') .replace(/\\033\[31m/g, '') .replace(/\\033\[7m/g, '') .replace(/\\033\[32m/g, '') .replace(/\\033\[27m/g, '') .replace(/\\033\[39m/g, '')) }, view: function() { return [ m('h1.header', 'Log'), m('div#logger', [ this.loglines.map((line, i) => { return m('div', { key: i }, line) }), m('div.padder'), ]), ] } } module.exports = Log