-
Notifications
You must be signed in to change notification settings - Fork 5
/
ui.js
151 lines (131 loc) · 3.97 KB
/
ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var pretty = require('prettier-bytes')
var progress = require('progress-string')
var cliTruncate = require('cli-truncate')
var output = require('neat-log/output')
module.exports = {
main: mainView,
progress: progressView
}
function mainView (state) {
return output(`
${state.title}
${archiveUI(state)}
${networkUI(state)}
`)
}
function progressView (state) {
if (state.downloading) return downloadUI(state)
else if (state.importer) {
return output(`
${importUI(state)}
${fileImport(state)}
`)
}
return ''
}
function archiveUI (state) {
if (!state.archive || !state.stats) return ''
var archive = state.archive
var stats = state.stats.get()
var size = stats.byteLength || 0
var files = stats.files
return output(`
${state.downloading ? 'Downloading' : 'Syncing'} Archive: ${files} files (${pretty(size)})
`)
}
function networkUI (state) {
// state.exiting = last render before download exit
if (!state.network || state.downloadExit) return ''
if (!state.network.connected || !state.archive.content) {
if (state.writable) return '\nNo Connections'// '\nWaiting for Connections...'
return '\nConnecting...'
}
return output(`
${state.archive.content.peers.length} peers ${speed()}
`)
function speed () {
var display = ''
var upSpeed = state.uploadSpeed || 0
var downSpeed = state.downloadSpeed || 0
display += `Uploading ${pretty(upSpeed)}/s `
display += `Downloading ${pretty(downSpeed)}/s`
if (display.length) display = '| ' + display
return display
}
}
function downloadUI (state) {
if (state.nsync) {
return output(`
Archive up to date.
${state.opts.live ? 'Waiting for changes...' : ''}
`)
}
var stats = state.stats.get()
if (!stats.downloaded) {
return '' // no metadata yet
}
if (!state.downloadBar) {
makeBar()
state.archive.on('update', makeBar)
}
return output(`
${state.downloadBar(stats.downloaded)}
`)
function makeBar () {
var total = stats.length
state.downloadBar = progress({
total: total,
style: function (a, b) {
return `[${a}${b}] ${(100 * stats.downloaded / total).toFixed(2)}%`
}
})
}
}
function importUI (state) {
if (state.count) {// Initial import done
if (state.importer.putDone.files >= state.count.files) {
if (state.importer.pending.length) return 'Importing updated files.'
if (!state.opts.watch) return 'All files imported.'
return 'Watching for file changes.'
}
} else {
if (!state.importer.count.files) return `Checking for file updates ...`
var indexSpeed = state.importer.indexSpeed ? `(${pretty(state.importer.indexSpeed)}/s)` : ''
return output(`
Imported ${state.importer.putDone.files} of ${state.importer.count.files} files ${indexSpeed}
(Calculating total import count...)
`)
}
var total = state.count.bytes
var totalBar = progress({
total: total,
style: function (a, b) {
return `[${a}${b}] ${(100 * state.importer.putDone.bytes / total).toFixed(0)}%`
}
})
return output(`
Importing ${state.count.files} files to Archive (${pretty(state.importer.indexSpeed)}/s)
${totalBar(state.importer.putDone.bytes)}
`)
}
function fileImport (state) {
if (!state.fileImport) return ''
if (state.fileImport.type === 'del') return `\nDEL: ${state.fileImport.src.name}`
var total = state.fileImport.src.stat.size
// var bar = progress({
// total: total,
// width: 35,
// style: function (a, b) {
// return `[${a}${b}] ${pretty(state.fileImport.progress)} / ${pretty(total)}`
// }
// })
var name = state.fileImport.dst.name.substr(1) // del / at start
var size
// >500 mb show progress to
if (total < 5e8) size = `(${pretty(total)})`
else size = `(${pretty(state.fileImport.progress)} / ${pretty(total)})`
return output(`
ADD: ${cliTruncate(name, process.stdout.columns - 7 - size.length, {position: 'start'})} ${size}
`)
// ${bar(state.fileImport.progress)}
}