Skip to content

Commit 195257f

Browse files
committed
Clean out the output of info
1 parent 3ac72fa commit 195257f

File tree

2 files changed

+82
-21
lines changed

2 files changed

+82
-21
lines changed

lib/commands/info.js

+37-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { parseHyperUrl } from '../urls.js'
55
import { HyperStructInfoTracker } from '../hyper/info-tracker.js'
66

77
const FULL_USAGE = `
8+
If no URLs are specified, will list all hypers currently seeded.
9+
10+
Options:
11+
12+
-l/--long - List the full keys of the hypers.
13+
814
Examples:
915
1016
hyp info
@@ -19,6 +25,14 @@ export default {
1925
simple: '[urls...]',
2026
full: FULL_USAGE
2127
},
28+
options: [
29+
{
30+
name: 'long',
31+
default: false,
32+
abbr: 'l',
33+
boolean: true
34+
}
35+
],
2236
command: async function (args) {
2337
var hyperClient = getClient()
2438
var mirroringClient = getMirroringClient()
@@ -51,20 +65,37 @@ export default {
5165

5266
// periodically update stdout with the status-line
5367
const updateStatusLine = () => {
54-
const networkStatusLine = network && network.announce ? 'but online (announcing)' : 'and not online'
55-
const seedingStatusLine = mirror && mirror.mirroring ? 'Seeding' : `Not seeding ${networkStatusLine}`
56-
statusLines[i] = `${tracker.genStatusLine()} - ${seedingStatusLine}`
68+
if (!network && !mirror) {
69+
statusLines[i] = `${tracker.genStatusIdent(args.long)}...`
70+
} else {
71+
const networkStatusLine = network && network.announce ? 'but online (announcing)' : 'and not online'
72+
const seedingStatusLine = mirror && mirror.mirroring ? 'Seeding' : `Not seeding ${networkStatusLine}`
73+
statusLines[i] = `
74+
${tracker.genStatusIdent(args.long)}:
75+
${tracker.genStatusPeerCount()} |
76+
${tracker.genStatusNetStats()}
77+
- ${seedingStatusLine}
78+
`.split('\n').map(s => s.trim()).join(' ')
79+
}
5780
statusLog.print()
5881
}
5982
updateStatusLine()
6083
setInterval(updateStatusLine, 1e3).unref()
6184

6285
// periodically calculate the size of the hyper structure
6386
const updateState = async () => {
64-
if (tracker.struct) {
65-
;({ mirror, network } = await getStatus(key, tracker.struct.type, tracker.struct.api.discoveryKey))
87+
try {
88+
if (tracker.struct) {
89+
;({ mirror, network } = await getStatus(key, tracker.struct.type, tracker.struct.api.discoveryKey))
90+
}
91+
await tracker.fetchState()
92+
} catch (e) {
93+
if (e.toString().includes('RPC stream destroyed')) {
94+
// ignore
95+
} else {
96+
console.error(e)
97+
}
6698
}
67-
await tracker.fetchState().catch(console.error)
6899
setTimeout(updateState, 1e3).unref()
69100
}
70101
updateState()

lib/hyper/info-tracker.js

+45-15
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,54 @@ export class HyperStructInfoTracker {
2626
}
2727

2828
genStatusLine () {
29-
var netCfg = 'Not swarming'
30-
if (this.netCfg) {
31-
if (this.netCfg.announce) netCfg = `Announced to seed`
32-
else if (this.netCfg.lookup) netCfg = `Watching for announced seeders`
33-
}
29+
var netCfg = this.genStatusNetCfg()
3430
if (!this.struct) {
35-
return `${chalk.bold(short(this.keyStr))}: ${netCfg}`
31+
return `${this.genStatusIdent()}: ${netCfg}`
3632
}
3733

38-
var stats = this.netStats
39-
var dl = bytes(stats.download[stats.download.length - 1], {unitSeparator: ' '})
40-
var ul = bytes(stats.upload[stats.download.length - 1], {unitSeparator: ' '})
4134
return `
42-
${chalk.bold(short(this.keyStr))} (${chalk.bold(this.struct.type)}):
43-
${this.peers.length} connected |
44-
${typeof this.totalBlocks !== 'undefined'
45-
? `${this.totalBlocks ? Math.round(this.downloadedBlocks/this.totalBlocks*100) : 100}% downloaded (${bytes(this.size)} total)`
46-
: 'Calculating...'}
47-
[↓${dl}/s ↑${ul}/s]
35+
${this.genStatusIdent()}:
36+
${this.genStatusPeerCount()} |
37+
${this.genStatusPctDownloaded()}
38+
${this.genStatusNetStats()}
4839
- ${netCfg}
4940
`.split('\n').map(s => s.trim()).join(' ')
5041
}
5142

43+
genStatusIdent (long = false) {
44+
if (!this.struct) return `${chalk.bold(maybeShort(this.keyStr, !long))}`
45+
return `${chalk.bold(maybeShort(this.keyStr, !long))} (${chalk.bold(this.struct.type)})`
46+
}
47+
48+
genStatusPeerCount () {
49+
return `${this.peers.length} connected`
50+
}
51+
52+
genStatusPctDownloaded () {
53+
if (typeof this.totalBlocks === 'undefined') return ''
54+
if (this.totalBlocks) {
55+
return `${this.totalBlocks ? Math.round(this.downloadedBlocks/this.totalBlocks*100) : 100}% downloaded (${bytes(this.size)} total)`
56+
}
57+
return 'Calculating...'
58+
}
59+
60+
genStatusNetStats () {
61+
var stats = this.netStats
62+
if (!stats) return ''
63+
var dl = bytes(stats.download[stats.download.length - 1], {unitSeparator: ' '})
64+
var ul = bytes(stats.upload[stats.download.length - 1], {unitSeparator: ' '})
65+
return `[↓${dl}/s ↑${ul}/s]`
66+
}
67+
68+
genStatusNetCfg () {
69+
var netCfg = 'Not swarming'
70+
if (this.netCfg) {
71+
if (this.netCfg.announce) netCfg = `Announced to seed`
72+
else if (this.netCfg.lookup) netCfg = `Watching for announced seeders`
73+
}
74+
return netCfg
75+
}
76+
5277
async fetchState () {
5378
this.netCfg = await hyper.getClient().network.status(this.discoveryKey)
5479

@@ -104,6 +129,11 @@ function short (key) {
104129
return `${key.slice(0, 6)}..${key.slice(-2)}`
105130
}
106131

132+
function maybeShort (key, yesShort) {
133+
if (yesShort) return short(key)
134+
return key
135+
}
136+
107137
async function countHasCoreBlocks (core, from, to) {
108138
var count = 0
109139
for (let i = from; i < to; i++) {

0 commit comments

Comments
 (0)