Skip to content

Commit 0a2004d

Browse files
committed
feat: bar removal
1 parent 7c3d28a commit 0a2004d

5 files changed

Lines changed: 137 additions & 57 deletions

File tree

‎src/BarFunctions.ts‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎src/BarManager.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { MultiBar, SingleBar, Presets } from 'cli-progress'
2+
3+
import { PlexifyOptions } from './Options/PlexifyOptions'
4+
5+
export class BarManager {
6+
private readonly bars: Map<string, SingleBar> = new Map()
7+
8+
private multibar: MultiBar
9+
10+
constructor(
11+
private readonly options: PlexifyOptions,
12+
private readonly format: string = '[{bar} {percentage}%] ETA: {eta_formatted} - {message}',
13+
) {
14+
this.initialize()
15+
}
16+
17+
initialize() {
18+
if (this.options.disableBars) {
19+
return
20+
}
21+
22+
this.multibar = new MultiBar(
23+
{
24+
format: this.format,
25+
stopOnComplete: true,
26+
},
27+
Presets.shades_classic,
28+
)
29+
}
30+
31+
createBar(name: string) {
32+
if (this.options.disableBars) {
33+
return
34+
}
35+
36+
const bar = this.multibar.create(100, 0, {})
37+
this.bars.set(name, bar)
38+
}
39+
40+
incrementBar(name: string, value: number = 1) {
41+
if (this.options.disableBars) {
42+
return
43+
}
44+
45+
const bar = this.bars.get(name)
46+
47+
if (bar) {
48+
bar.increment(value)
49+
}
50+
}
51+
52+
removeBar(name: string) {
53+
if (this.options.disableBars) {
54+
return
55+
}
56+
57+
const bar = this.bars.get(name)
58+
59+
if (bar) {
60+
this.multibar.remove(bar)
61+
}
62+
63+
this.bars.delete(name)
64+
}
65+
66+
startBar(name: string, total: number, payload: any = {}) {
67+
if (this.options.disableBars) {
68+
return
69+
}
70+
71+
const bar = this.bars.get(name)
72+
73+
if (bar) {
74+
bar.start(total, 0, payload)
75+
}
76+
}
77+
78+
stopBar(name: string) {
79+
if (this.options.disableBars) {
80+
return
81+
}
82+
83+
const bar = this.bars.get(name)
84+
85+
if (bar) {
86+
bar.stop()
87+
}
88+
}
89+
90+
updateBar(name: string, value: number, payload: any = {}) {
91+
if (this.options.disableBars) {
92+
return
93+
}
94+
95+
const bar = this.bars.get(name)
96+
97+
if (bar) {
98+
bar.update(value, payload)
99+
}
100+
}
101+
}

‎src/Commands/ConvertCommand.ts‎

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import os from 'os'
22

33
import { fs } from '@nofrills/fs'
44
import { Throttle } from '@nnode/common'
5-
import { MultiBar, Presets } from 'cli-progress'
65
import { CommandModule, Arguments, CommandBuilder } from 'yargs'
76

87
import { Logger } from '../Logger'
8+
import { BarManager } from '../BarManager'
99
import { MediaScanner } from '../MediaScanner'
1010
import { MediaConverter } from '../MediaConverter'
1111
import { StreamProgress } from '../StreamProgress'
1212
import { ConvertOptions } from '../Options/ConvertOptions'
13-
import { createChildBar } from '../BarFunctions'
1413

1514
export class ConvertCommand implements CommandModule<{}, ConvertOptions> {
1615
aliases = ['convert']
@@ -49,39 +48,31 @@ export class ConvertCommand implements CommandModule<{}, ConvertOptions> {
4948
}
5049

5150
async handler(args: Arguments<ConvertOptions>) {
52-
const bars = new MultiBar(
53-
{
54-
format: '[{bar} {percentage}%] ETA: {eta_formatted} - {message}',
55-
stopOnComplete: true,
56-
},
57-
Presets.shades_classic,
58-
)
51+
const bars = new BarManager(args)
5952

60-
const scanbar = createChildBar(bars)
53+
const scanbar = 'scanner'
6154
const scanner = new MediaScanner(Logger)
6255

63-
if (args.disableBars === false) {
64-
bars.remove(scanbar)
65-
}
56+
bars.createBar(scanbar)
6657

6758
scanner.on('progress', () => {
6859
if (args.disableBars === false) {
69-
scanbar.increment(1)
60+
bars.incrementBar(scanbar)
7061
}
7162
})
7263

7364
scanner.on('start', (total: number) => {
7465
if (args.disableBars === false) {
75-
scanbar.start(total, 0, { message: 'scanning' })
66+
bars.startBar(scanbar, total, { message: 'scanning' })
7667
}
7768
})
7869

7970
scanner.on('stop', () => {
8071
if (args.disableBars === false) {
81-
scanbar.stop()
72+
bars.stopBar(scanbar)
8273
}
8374

84-
bars.remove(scanbar)
75+
bars.removeBar(scanbar)
8576
})
8677

8778
const scanned = await scanner.scan(args.path, args.minutes, args.reverse, (filename: string) => {
@@ -92,28 +83,27 @@ export class ConvertCommand implements CommandModule<{}, ConvertOptions> {
9283
return true
9384
})
9485

95-
let current = 0
96-
86+
const filebar = 'filebar'
9787
const payload = { message: args.path }
98-
const filebar = bars.create(scanned.length, current, payload)
9988

100-
filebar.start(scanned.length, current, payload)
89+
bars.createBar(filebar)
90+
bars.startBar(filebar, scanned.length, payload)
10191

10292
await Throttle(
10393
scanned.map((file) => async () => {
104-
const conversion = bars.create(100, 0, { message: fs.basename(file.filename) })
94+
const id = fs.basename(file.filename)
10595
const converter = new MediaConverter(Logger)
10696

97+
bars.createBar(id)
98+
10799
const handleProgress = (progress: StreamProgress) => {
108-
conversion.update(progress.percent, { message: fs.basename(file.filename) })
109-
filebar.update(current)
100+
bars.updateBar(id, progress.percent, { message: fs.basename(file.filename) })
110101
}
111102

112-
const handleStart = () => conversion.start(100, 0, { message: file.filename })
103+
const handleStart = () => bars.startBar(id, 100, { message: file.filename })
113104

114105
const handleStop = () => {
115-
current++
116-
bars.remove(conversion)
106+
bars.removeBar(id)
117107
}
118108

119109
converter.on('start', handleStart)
@@ -130,11 +120,11 @@ export class ConvertCommand implements CommandModule<{}, ConvertOptions> {
130120
converter.off('start', handleStart)
131121
}
132122

133-
filebar.increment(1)
123+
bars.incrementBar(filebar, 1)
134124
}),
135125
args.processors,
136126
)
137127

138-
filebar.stop()
128+
bars.stopBar(filebar)
139129
}
140130
}

‎src/Commands/ScanCommand.ts‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { fs } from '@nofrills/fs'
2-
import { MultiBar, Presets } from 'cli-progress'
32
import { CommandModule, Arguments, CommandBuilder } from 'yargs'
43

54
import { Logger } from '../Logger'
5+
import { BarManager } from '../BarManager'
66
import { MediaScanner } from '../MediaScanner'
77
import { ScanOptions } from '../Options/ScanOptions'
8-
import { createChildBar } from '../BarFunctions'
98

109
export class ScanCommand implements CommandModule<{}, ScanOptions> {
1110
aliases = ['scan']
@@ -29,39 +28,30 @@ export class ScanCommand implements CommandModule<{}, ScanOptions> {
2928
}
3029

3130
async handler(args: Arguments<ScanOptions>) {
32-
const bars = new MultiBar(
33-
{
34-
format: '[{bar} {percentage}%] ETA: {eta_formatted} - {message}',
35-
stopOnComplete: true,
36-
},
37-
Presets.shades_classic,
38-
)
39-
40-
const scanbar = createChildBar(bars)
31+
const bars = new BarManager(args)
32+
const scanbar = 'scanbar'
4133
const scanner = new MediaScanner(Logger)
4234

43-
if (args.disableBars === false) {
44-
bars.remove(scanbar)
45-
}
35+
bars.createBar(scanbar)
4636

4737
scanner.on('progress', () => {
4838
if (args.disableBars === false) {
49-
scanbar.increment(1)
39+
bars.incrementBar(scanbar, 1)
5040
}
5141
})
5242

5343
scanner.on('start', (total: number) => {
5444
if (args.disableBars === false) {
55-
scanbar.start(total, 0, { message: 'scanning' })
45+
bars.startBar(scanbar, total, { message: 'scanning' })
5646
}
5747
})
5848

5949
scanner.on('stop', () => {
6050
if (args.disableBars === false) {
61-
scanbar.stop()
51+
bars.stopBar(scanbar)
6252
}
6353

64-
bars.remove(scanbar)
54+
bars.removeBar(scanbar)
6555
})
6656

6757
const results = await scanner.scan(args.path, args.minutes, args.reverse, (filename) => {

‎src/index.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@ import yargs from 'yargs'
33
import { ConvertCommand } from './Commands/ConvertCommand'
44
import { InfoCommand } from './Commands/InfoCommand'
55
import { ScanCommand } from './Commands/ScanCommand'
6+
import { Logger } from './Logger'
67

7-
yargs.scriptName('plexify').command(new ConvertCommand()).command(new InfoCommand()).command(new ScanCommand()).parse()
8+
const args = yargs
9+
.scriptName('plexify')
10+
.command(new ConvertCommand())
11+
.command(new InfoCommand())
12+
.command(new ScanCommand())
13+
.parse()
14+
15+
Logger.trace(args)

0 commit comments

Comments
 (0)