Permalink
Browse files

update logger and use multi-dimensional array

  • Loading branch information...
joehand committed Nov 4, 2016
1 parent e032bd7 commit a3921bc3b22232d6dc0f85589e18e4300f9348b3
Showing with 27 additions and 20 deletions.
  1. +13 −10 commands/download.js
  2. +13 −9 commands/share.js
  3. +1 −1 package.json
View
@@ -5,21 +5,24 @@ var speedometer = require('speedometer')
var ui = require('../lib/ui')
module.exports = function (dat, args) {
var log = logger(args)
var lines = [[], []]
var log = logger(lines, {debug: args.debug, quiet: args.quiet})
var messages = lines[0] = []
var progress = lines[1] = []
var downloadTxt = 'Downloading '
var finished = false
dat.stats.rateUp = speedometer()
dat.stats.rateDown = speedometer()
log.status('Starting Dat...\n', 0)
log.status('Connecting...', 1)
progress.push('Starting Dat...\n')
progress.push('Connecting...')
dat.on('error', onerror)
dat.open(function () {
log.message('Downloading in ' + dat.dir + '\n')
messages.push('Downloading in ' + dat.dir + '\n')
dat.download(function (err) {
if (err) onerror(err)
})
@@ -32,7 +35,7 @@ module.exports = function (dat, args) {
})
dat.once('key', function (key) {
log.message(ui.keyMsg(key))
messages.push(ui.keyMsg(key))
if (args.quiet) console.log(ui.keyMsg(key))
})
@@ -50,7 +53,7 @@ module.exports = function (dat, args) {
finished = false
dat.stats.rateDown = speedometer()
updateStats()
log.status('', -1) // remove download finished message
progress[2] = '' // remove download finished message
})
dat.on('file-downloaded', updateStats)
@@ -59,16 +62,16 @@ module.exports = function (dat, args) {
dat.stats.rateDown = 0
updateStats()
if (args.exit) {
log.status('', 1)
progress[1] = '' // clear swarm info before exiting
process.exit(0)
}
log.status('\nDownload Finished. You may exit the process with Ctrl-C.', -1)
progress[2] = '\nDownload Finished. You may exit the process with Ctrl-C.'
})
dat.on('swarm-update', printSwarm)
function printSwarm () {
log.status(ui.swarmMsg(dat), 1)
progress[1] = ui.swarmMsg(dat)
}
function updateStats () {
@@ -79,7 +82,7 @@ module.exports = function (dat, args) {
}
msg += ' ' + downloadTxt + chalk.bold(stats.filesTotal) + ' items'
msg += chalk.dim(' (' + prettyBytes(stats.bytesTotal) + ')')
log.status(msg + '\n', 0)
progress[0] = msg + '\n'
}
}
View
@@ -6,22 +6,26 @@ var speedometer = require('speedometer')
var ui = require('../lib/ui')
module.exports = function (dat, args) {
var log = logger(args)
var lines = [[], []]
var log = logger(lines, {debug: args.debug, quiet: args.quiet})
var messages = lines[0] = []
var progress = lines[1] = []
var addText = 'Adding '
var finalized = false
dat.stats.rateUp = speedometer()
log.status('Starting Dat...\n', 0)
if (args.snapshot) log.status('Creating Link...', 1)
else log.status('Connecting...', 1)
progress.push('Starting Dat...\n')
if (args.snapshot) progress.push('Creating Link...')
else progress.push('Connecting...')
dat.on('error', onerror)
dat.open(function () {
dat.archive.open(function () {
if (dat.archive.key && !dat.archive.owner) return download(dat, args)
log.message('Sharing ' + dat.dir + '\n')
messages.push('Sharing ' + dat.dir + '\n')
dat.share(function (err) {
if (err) onerror(err)
})
@@ -43,11 +47,11 @@ module.exports = function (dat, args) {
var msg = 'Calculating Size: '
msg += stats.filesTotal + ' items '
msg += chalk.dim('(' + prettyBytes(stats.bytesTotal) + ')')
log.status(msg + '\n', 0)
progress[0] = msg + '\n'
})
dat.once('key', function (key) {
log.message(ui.keyMsg(key))
messages.push(ui.keyMsg(key))
if (args.quiet) console.log(ui.keyMsg(key))
})
@@ -72,7 +76,7 @@ module.exports = function (dat, args) {
dat.on('swarm-update', printSwarm)
function printSwarm () {
log.status(ui.swarmMsg(dat), 1)
progress[1] = ui.swarmMsg(dat)
}
function updateStats () {
@@ -85,7 +89,7 @@ module.exports = function (dat, args) {
if (finalized) msg += chalk.bold(files) + ' '
msg += 'items'
msg += chalk.dim(' (' + prettyBytes(bytesTotal) + ')')
log.status(msg + '\n', 0)
progress[0] = msg + '\n'
}
}
View
@@ -34,7 +34,7 @@
"pretty-bytes": "^3.0.0",
"pump": "^1.0.1",
"speedometer": "^1.0.0",
"status-logger": "^2.0.1"
"status-logger": "^3.0.0"
},
"bugs": {
"url": "https://github.com/datproject/dat/issues"

0 comments on commit a3921bc

Please sign in to comment.