Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Created August 1, 2017 22:19
Show Gist options
  • Save mafintosh/460904e61a09269d619f720233e57e48 to your computer and use it in GitHub Desktop.
Save mafintosh/460904e61a09269d619f720233e57e48 to your computer and use it in GitHub Desktop.
var neat = require('neat-log')
var output = require('neat-log/output')
var progress = require('progress-string')
var crypto = require('crypto')
var hypercore = require('hypercore')
var hyperdiscovery = require('hyperdiscovery')
var feed = hypercore('data', process.argv[2], {valueEncoding: 'json'})
function debug (state) {
if (feed.writable) feed.append({time: Date.now(), state: state})
}
var log = neat(function (state) {
debug(state)
return output`
Mining BitCoin ... (${state.hash})
[${progress(state)(state.progress)}]
Coins mined: ${state.mined}
`
})
feed.on('ready', function () {
console.log('feed is ' + feed.key.toString('hex'))
hyperdiscovery(feed, {live: true})
log.use(function (state, bus) {
state.progress = 0
state.mined = 0
if (!feed.writable) {
feed.createReadStream({live: true, tail: true}).on('data', function (data) {
var keys = Object.keys(data.state)
keys.forEach(function (k) {
state[k] = data.state[k]
})
bus.render()
})
return
}
setInterval(function () {
state.width = 50
state.total = 200
state.progress++
if (state.progress > 200) {
state.progress = 0
state.mined++
}
state.hash = crypto.randomBytes(16).toString('hex')
bus.render()
}, 250)
})
})
{
"name": "neat-log-debug",
"version": "1.0.0",
"description": "",
"main": "test.js",
"dependencies": {
"hypercore": "^6.6.6",
"hyperdiscovery": "^6.0.4",
"neat-log": "^1.1.0",
"progress-string": "^1.2.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment