From f2c8e60d56e75e120c8b43f357a18fe601fd033f Mon Sep 17 00:00:00 2001 From: Jannis R Date: Wed, 7 Mar 2018 20:23:53 +0100 Subject: [PATCH] wip: departures part 2 --- .../{departures.js => departures/index.js} | 17 ++++---- commands/departures/render.js | 43 +++++++++++++++++++ lib/render-time-left.js | 11 +++++ package.json | 3 ++ 4 files changed, 66 insertions(+), 8 deletions(-) rename commands/{departures.js => departures/index.js} (87%) create mode 100644 commands/departures/render.js create mode 100644 lib/render-time-left.js diff --git a/commands/departures.js b/commands/departures/index.js similarity index 87% rename from commands/departures.js rename to commands/departures/index.js index 6f73974..422dbe8 100644 --- a/commands/departures.js +++ b/commands/departures/index.js @@ -3,10 +3,12 @@ const searchStations = require('vbb-stations-autocomplete') const getStations = require('vbb-stations') const parseTime = require('parse-messy-time') +const linesAt = require('vbb-lines-at') const hafas = require('vbb-hafas') -const commandKeys = require('../lib/commands-keyboard') -const whenKeys = require('../lib/when-keyboard') +const commandKeys = require('../../lib/commands-keyboard') +const whenKeys = require('../../lib/when-keyboard') +const renderDeps = require('./render') const promptWhen = `\ *When?* @@ -41,12 +43,7 @@ const parseWhen = async (when, ctx) => { return when } -const renderDeps = (deps, header) => { - // todo -} - const printDeps = async (allDeps, ctx) => { - await ctx.replyWithMarkdown(`todo`, commandKeys) // todo for (let i = 0; i < allDeps.length; i += 10) { const deps = allDeps.slice(i, i + 10) await ctx.replyWithMarkdown(renderDeps(deps), commandKeys) @@ -88,7 +85,11 @@ const departures = async (ctx, next) => { ctx.putData('where', null) ]) - // fetch & render + let lines = linesAt[where.id] || [] + lines = lines.map(l => '`' + l.name + '`').join(', ') + await ctx.replyWithMarkdown('*' + where.name + '*\n' + lines) + + // fetch & render deps await ctx.replyWithChatAction('typing') const deps = await hafas.departures(where.id, when) await printDeps(deps, ctx) diff --git a/commands/departures/render.js b/commands/departures/render.js new file mode 100644 index 0000000..ba3c393 --- /dev/null +++ b/commands/departures/render.js @@ -0,0 +1,43 @@ +'use strict' + +const stringWidth = require('string-width') + +const renderTimeLeft = require('../../lib/render-time-left') + +const SPACING = ' ' + +const renderDeps = (deps) => { + const rows = [] + const widths = [] + const maxWidth = [0, 0, 0] + + for (let dep of deps) { + // todo: show delay + const line = dep.line.name + const time = renderTimeLeft(new Date(dep.when) - Date.now()) + const dir = dep.direction + + rows.push([line, time, dir]) + const width = [stringWidth(line), stringWidth(time), stringWidth(dir)] + widths.push(width) + maxWidth[0] = Math.max(width[0], maxWidth[0]) + maxWidth[1] = Math.max(width[1], maxWidth[1]) + maxWidth[2] = Math.max(width[2], maxWidth[2]) + } + + // render table + let str = '```' + for (let i = 0; i < rows.length; i++) { + const row = rows[i] + str += '\n' + for (let j = 0; j < 3; j++) { + str += row[j] + SPACING + if (j !== 2) str += ' '.repeat(maxWidth[j] - widths[i][j]) + } + } + str += '\n```' + + return str +} + +module.exports = renderDeps diff --git a/lib/render-time-left.js b/lib/render-time-left.js new file mode 100644 index 0000000..dcbb49f --- /dev/null +++ b/lib/render-time-left.js @@ -0,0 +1,11 @@ +'use strict' + +const ms = require('ms') + +const renderTimeLeft = (t) => { + t = Math.round(t / 1000) + if (t === 0) return 0 + return (t < 0 ? '-' : '') + ms(Math.abs(t * 1000)) +} + +module.exports = renderTimeLeft diff --git a/package.json b/package.json index 4b17a73..82e08c7 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,12 @@ "hifo": "^1.0.0", "js-string-escape": "^1.0.1", "level": "^3.0.0", + "ms": "^2.1.1", "parse-messy-time": "^2.1.0", + "string-width": "^2.1.1", "telegraf": "^3.19.0", "vbb-hafas": "^4.1.1", + "vbb-lines-at": "^3.11.0", "vbb-stations": "^6.2.1", "vbb-stations-autocomplete": "^3.2.0" }