diff --git a/cli-old.js b/cli-old.js deleted file mode 100755 index 6126dbb..0000000 --- a/cli-old.js +++ /dev/null @@ -1,202 +0,0 @@ -#!/usr/bin/env node - -var minimist = require('minimist') -var logger = require('status-logger') -var pretty = require('prettier-bytes') -var speed = require('speedometer') -var progressBar = require('progress-string') -var dat = require('./') - -process.title = 'dat-next' - -var argv = minimist(process.argv.slice(2), { - alias: {sleep: 's', quiet: 'q', watch: 'w'} -}) -var src = argv._[0] || process.cwd() -var dest = argv._[1] - -var output = [ - ['', ''], // Key/Msg + Peer Count - ['', ''], // Total Import/Download Progress - ['', ''] // File Import Progress -] -var log = logger(output, { quiet: argv.quiet }) -var indexSpeed = speed() -var downloadSpeed = speed() -var hasContent -var imported = 0 -var downloaded = 0 -var total = 0 -var fileImported = 0 -var bar -var totalBar -var watchTimeout - -dat(src, dest, argv, function (archive, swarm, progress) { - output[0][0] = 'Here we go!' - setInterval(function () { - networkUI() - log.print() - if (archive.downloaded) { - log.clear() - console.log('Done! Bye bye.') - process.exit(0) - } - }, 200) - log.print() - - archive.once('content', function () { - hasContent = true - imported = archive.content.byteLength - if (!archive.metadata.writable) downloadUI() - }) - swarm.once('connection', function () { - output[0].push('') // add space for peers - }) - - if (!archive.metadata.writable) { - output[0][0] = 'Connecting...' - return - } - - output[0][0] = `dat://${archive.key.toString('hex')}` - - progress.once('count', function (count) { - total = count.bytes - if (!argv.watch) { - totalBar = progressBar({ - total: total, - style: function (a, b) { - return `[${a}${b}] ${pretty(imported)} / ${pretty(total)}` - } - }) - output[1][1] = totalBar(imported) - output[1].push('') // Import Speed - output[1].push('') // Spacer - } - updateImportTotal() - }) - - progress.on('put', function (src, dst) { - // Show progress for files only - if (src.stat.isDirectory()) return - clearTimeout(watchTimeout) - - var name = (dst.name === '/') ? src.name : dst.name // use prettier names if available - output[2][0] = `ADD: ${name}` - fileImported = 0 - - // Avoid flashing progress bar of small files - if (src.stat.size > Math.pow(10, 7)) { - bar = progressBar({ - total: src.stat.size, - style: function (a, b) { - return `[${a}${b}] ${pretty(fileImported)} / ${pretty(src.stat.size)}` - } - }) - output[2][1] = bar(fileImported) - } - }) - - progress.on('put-data', function (chunk, src, dst) { - imported += chunk.length - fileImported += chunk.length - - if (bar) { - output[2][1] = bar(fileImported) - if (!totalBar) output[2][2] = `${pretty(indexSpeed(chunk.length))}/s` - } - updateImportTotal(chunk.length) - }) - - progress.on('put-end', function (src, dst) { - // Remove put file progress - if (bar) { - output[2][1] = '' - if (!totalBar) output[2][2] = '' - } - fileImported = 0 - bar = null - updateImportTotal() - - if (argv.watch) { - watchTimeout = setTimeout(function () { - if (argv.watch) output[2] = [''] // clear output for idle watching - }, 1200) - } - }) - - progress.on('del', function (dst) { - output[2][0] = `DEL: ${dst.name}` - clearTimeout(watchTimeout) - - if (argv.watch) { - watchTimeout = setTimeout(function () { - if (argv.watch) output[2] = [''] // clear output for idle watching - }, 1200) - } - }) - - progress.on('end', function (src, dst) { - // Only fires if argv.watch === false - totalBar = null - output[1] = [output[1][0]] // Clear total bar + import speed - output[2] = [`\nImport complete`] - setTimeout(function () { - output.pop() - }, 5000) - }) - - function updateImportTotal (size) { - size = size || 0 - var verb = !argv.watch - ? imported === total - ? 'Sharing' - : 'Importing to' - : 'Syncing' - - output[1][0] = `${verb} Archive: ${archive.metadata.length - 1} files (${pretty(archive.content.byteLength)})` - if (totalBar) { - output[1][1] = totalBar(imported) - output[1][2] = `${pretty(indexSpeed(size))}/s` - } - } - - function downloadUI () { - var bar = downloadBar() - archive.content.ready(function () { - total = archive.content.length - output[0][0] = `Downloading ${pretty(archive.content.byteLength)}` - output[1][0] = bar(downloaded) - for (var i = 0; i < archive.content.length; i++) { - if (archive.content.has(i)) downloaded++ - } - }) - - archive.content.on('download', function (index, data) { - if (archive.content.length !== total) { - output[0][0] = `Downloading ${pretty(archive.content.byteLength)}` - total = archive.content.length - bar = downloadBar() - } - downloaded++ - var per = (downloaded / total * 100).toFixed(2) - if (bar) output[1][0] = bar(downloaded) + ' ' + per + '%' - output[1][1] = pretty(downloadSpeed(data.length)) + '/s' - }) - - function downloadBar () { - return progressBar({ - total: total, - style: function (a, b) { - return `[${a}${b}]` - } - }) - } - } - - function networkUI () { - if (!swarm.connected || !hasContent) return - output[0][1] = `${archive.content.peers.length} peers` - } -}) diff --git a/cli.js b/cli.js index 90515e5..5760a36 100755 --- a/cli.js +++ b/cli.js @@ -13,23 +13,29 @@ var dat = require('./') process.title = 'dat-next' var argv = minimist(process.argv.slice(2), { - alias: {temp: 't', quiet: 'q', watch: 'w', sleep: 's'} + alias: {temp: 't', help: 'h', watch: 'w', sleep: 's'} }) + +if (argv.help) return usage() + var src = argv._[0] || process.cwd() var dest = argv._[1] var indexSpeed = speed() +var logspeed = 200 -var neat = neatLog([mainView, progressView], {logspeed: 200}) // todo: opts.debug +var neat = neatLog([mainView, progressView], {logspeed: logspeed}) neat.use(runDat) neat.use(trackNetwork) neat.use(trackProgress) function runDat (state, bus) { + state.title = 'Starting Dat program...' bus.emit('render') dat(src, dest, argv, function (err, dat) { if (err) { + bus.clear() console.error('ERROR:', err) process.exit(1) } @@ -50,7 +56,7 @@ function runDat (state, bus) { }) if (dat.writable) state.title = `dat://${dat.key.toString('hex')}` - else state.title = 'Dat Download' + else state.title = 'Dat!' bus.emit('archive') bus.emit('render') @@ -65,8 +71,28 @@ function trackProgress (state, bus) { function trackDownload () { state.downloading = true - state.archive.content.on('sync', function () { + state.modified = false + + state.archive.content.on('clear', function () { + state.modified = true + }) + + state.archive.content.on('download', function (index, data) { + state.modified = true + }) + + state.archive.on('sync', function () { state.nsync = true + if (state.modified && !argv.live) { + state.downloadExit = true + bus.render() + process.exit() + } + bus.emit('render') + }) + + state.archive.on('update', function () { + state.nsync = false bus.emit('render') }) } @@ -77,7 +103,7 @@ function trackProgress (state, bus) { var counting = setInterval(function () { // Update file count while we are going (for big dirs) bus.emit('render') - }, 200) + }, logspeed) state.importing = true state.import = { @@ -138,7 +164,7 @@ function trackNetwork (state, bus) { state.uploadSpeed = speed.uploadSpeed state.downloadSpeed = speed.downloadSpeed bus.emit('render') - }, 500) + }, logspeed) }) } @@ -167,7 +193,8 @@ function archiveUI (state) { } function networkUI (state) { - if (!state.network) return '' + // state.exiting = last render before download exit + if (!state.network || state.downloadExit) return '' if (!state.network.connected || !state.archive.content) { if (state.writable) return '\nWaiting for Connections...' return '\nConnecting...' @@ -181,7 +208,8 @@ function networkUI (state) { function speed () { var output = '' if (state.uploadSpeed) output += `Uploading ${pretty(state.uploadSpeed)}/s` - if (state.downloadSpeed) output += `Downloading ${pretty(state.downloadSpeed)}/s` + // !state.nsync hack so speed doesn't display when done + if (!state.nsync && state.downloadSpeed) output += `Downloading ${pretty(state.downloadSpeed)}/s` return output } } @@ -191,9 +219,12 @@ function downloadUI (state) { return output` Archive up to date with latest. - Exit if you'd like. + ${argv.live ? 'Waiting for updates ...' : ''} ` } + if (!state.stats.get().blocksTotal) { + return '' // no metadata yet + } if (!state.downloaded) { var feed = state.archive.content state.downloaded = 0 @@ -204,7 +235,17 @@ function downloadUI (state) { state.downloaded += 1 }) } + if (!state.downloadBar) { + makeBar() + state.archive.metadata.update(makeBar) + } + return output` + + ${state.downloadBar(state.downloaded)} + ` + + function makeBar () { var total = state.stats.get().blocksTotal state.downloadBar = progress({ total: total, @@ -213,10 +254,6 @@ function downloadUI (state) { } }) } - return output` - - ${state.downloadBar(state.downloaded)} - ` } function importUI (state) { @@ -241,12 +278,13 @@ function importUI (state) { ` function fileImport () { - if (!state.fileImport) return `` + if (!state.fileImport) return '' if (state.fileImport.type === 'del') return `\nDEL: ${state.fileImport.src.name}` if (!state.fileImport.bar) { var total = state.fileImport.src.stat.size state.fileImport.bar = progress({ total: total, + width: 35, style: function (a, b) { return `[${a}${b}] ${pretty(state.fileImport.progress)} / ${pretty(total)}` } @@ -260,3 +298,10 @@ function importUI (state) { ` } } + +function usage () { + console.error('dat-next!') + console.error(' dat-next