@@ -24,6 +24,11 @@ import { hasOwnProperty } from './util/object';
2424import { ItemRef } from './item/ItemRef' ;
2525import { ScookShelf } from './shelf/ScookShelf' ;
2626import { DigiShelf } from './shelf/DigiShelf' ;
27+ import * as cliProgress from 'cli-progress' ;
28+ import { DownloadOptions , DownloadProgress } from './item/download-options' ;
29+ import { Book } from './item/Book' ;
30+ import { ItemGroup } from './item/ItemGroup' ;
31+ import { Item } from './item/Item' ;
2732
2833const { version } = JSON . parse (
2934 readFileSync ( join ( __dirname , '../package.json' ) ) . toString ( )
@@ -177,16 +182,73 @@ const cmd = command({
177182 }
178183
179184 console . log ( `Downloading "${ itemRef . title } "...` ) ;
185+
186+ const multiBar = new cliProgress . MultiBar (
187+ {
188+ format :
189+ ' {bar} | {value}/{total} | {percentage}% | ETA: {eta}s | {title}' ,
190+ } ,
191+ cliProgress . Presets . shades_classic
192+ ) ;
193+ const bars = new Map < Item , cliProgress . Bar > ( ) ;
194+ const barsUpdater = setInterval (
195+ ( ) => bars . forEach ( ( bar ) => bar . updateETA ( ) ) ,
196+ 1000
197+ ) ;
198+
199+ const options : DownloadOptions = {
200+ ...args ,
201+ format : args . format as PaperFormat | undefined ,
202+ onStart ( progress ) {
203+ let bar : cliProgress . SingleBar | null = null ;
204+ if ( progress . item instanceof Book ) {
205+ bar = multiBar . create (
206+ ( progress as DownloadProgress < Book > ) . pageCount ,
207+ 0 ,
208+ {
209+ title : `${ progress . item . constructor . name } : ${ progress . item . title } ` ,
210+ }
211+ ) ;
212+ } else if ( progress . item instanceof ItemGroup ) {
213+ bar = multiBar . create (
214+ ( progress as DownloadProgress < ItemGroup > ) . items . length ,
215+ 0 ,
216+ { title : `Group: ${ progress . item . title } ` }
217+ ) ;
218+ }
219+ bar && bars . set ( progress . item , bar ) ;
220+ } ,
221+ onProgress ( progress ) {
222+ const bar = bars . get ( progress . item ) ! ;
223+ if ( progress . item instanceof Book ) {
224+ bar . update (
225+ ( progress as DownloadProgress < Book > ) . downloadedPages
226+ ) ;
227+ } else if ( progress . item instanceof ItemGroup ) {
228+ bar . update (
229+ ( progress as DownloadProgress < ItemGroup > ) . downloadedItems
230+ . length
231+ ) ;
232+ }
233+ } ,
234+ } ;
235+
236+ let err : unknown = null ;
180237 try {
181- await item . download ( args . outDir , {
182- ...args ,
183- format : args . format as PaperFormat ,
184- } ) ;
238+ await item . download ( args . outDir , options ) ;
185239 } catch ( e ) {
186- console . error ( e ) ;
240+ err = e ;
241+ }
242+
243+ clearInterval ( barsUpdater ) ;
244+ multiBar . stop ( ) ;
245+
246+ if ( err ) {
247+ console . error ( err ) ;
187248 console . error ( `Failed to download "${ itemRef . title } !"` ) ;
188249 continue ;
189250 }
251+
190252 console . log ( `Successfully downloaded "${ itemRef . title } "!` ) ;
191253 }
192254 }
0 commit comments