~ev/wiredove-chat

e65ea6105410d64443965599a8409e97036a05f7 — Ev Bogue 1 year, 10 days ago 518b98d
add human time for message renders
2 files changed, 45 insertions(+), 1 deletions(-)

M client.js
A misc.js
M client.js => client.js +2 -1
@@ 1,5 1,6 @@
import { readLines } from "https://deno.land/std@0.97.0/io/mod.ts"
import { keys, box, unbox, publish, open } from './util.js'
import { human } from './misc.js'

const server = Deno.args[0] || "ws://localhost:8080"
const ws = new WebSocket(server)


@@ 44,7 45,7 @@ keys('wiredove').then(keypair => {
          log.push(message)
          console.clear()
          log.forEach(mssg => {
            console.log(mssg.author.substring(0, 10) + ' ' + mssg.text + ' ' + mssg.timestamp)
            console.log(mssg.author.substring(0, 10) + ' ' + mssg.text + ' - ' + human(new Date(mssg.timestamp)))
          })
        })
        Deno.stdout.write(new TextEncoder().encode(prompty))

A misc.js => misc.js +43 -0
@@ 0,0 1,43 @@
// human-time by Dave Eddy https://github.com/bahamas10/human

export function human (seconds) {
  if (seconds instanceof Date)
    seconds = Math.round((Date.now() - seconds) / 1000);
  var suffix = seconds < 0 ? 'from now' : 'ago';
  seconds = Math.abs(seconds);

  var times = [
    seconds / 60 / 60 / 24 / 365, // years
    seconds / 60 / 60 / 24 / 30,  // months
    seconds / 60 / 60 / 24 / 7,   // weeks
    seconds / 60 / 60 / 24,       // days
    seconds / 60 / 60,            // hours
    seconds / 60,                 // minutes
    seconds                       // seconds
  ];
  var names = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'];

  for (var i = 0; i < names.length; i++) {
    var time = Math.floor(times[i]);
    var name = names[i];
    if (time > 1)
      name += 's';

    if (time >= 1)
      return time + ' ' + name + ' ' + suffix;
  }
  return 'now'
  //return '0 seconds ' + suffix;
}

// hscrpt by Dominic Tarr https://github.com/dominictarr/hscrpt/blob/master/LICENSE
function h (tag, attrs, content) {
  if(Array.isArray(attrs)) content = attrs, attrs = {}
  var el = document.createElement(tag)
  for(var k in attrs) el[k] = attrs[k]
  if(content) content.forEach(function (e) {
    if(e) el.appendChild('string' == typeof e ? document.createTextNode(e) : e)
  })
  return el
}