Skip to content

Commit

Permalink
wip: departures part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 7, 2018
1 parent 7922392 commit f2c8e60
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
17 changes: 9 additions & 8 deletions commands/departures.js → commands/departures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions commands/departures/render.js
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/render-time-left.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit f2c8e60

Please sign in to comment.