11use crate :: build_order:: { BuildOrder , BuildOrderError } ;
22use crate :: build_parser:: fetch_build_order;
33use crate :: index_manager:: { LOWEST_INDEX , get_st_highest_index} ;
4+ use console:: style;
5+ use indicatif:: { MultiProgress , ProgressBar , ProgressStyle } ;
6+ use std:: thread;
7+ use std:: time:: Duration ;
48
59pub fn fetch_latest ( count : u32 ) -> Vec < BuildOrder > {
10+ let spinner_style = ProgressStyle :: with_template (
11+ "[{percent:.bold.dim}%] {elapsed:.dim} {spinner} {bar}\t {wide_msg}" ,
12+ )
13+ . unwrap ( )
14+ . tick_chars ( "⡇⣆⣤⣰⢸⠹⠛⠏ " ) ;
15+ let pb = ProgressBar :: new ( count as u64 ) ;
16+ pb. set_style ( spinner_style) ;
17+ pb. enable_steady_tick ( Duration :: from_millis ( 100 ) ) ;
18+
619 let highest_index = get_st_highest_index ( ) ;
720 let mut end_index = if count > highest_index - LOWEST_INDEX {
821 LOWEST_INDEX
@@ -21,7 +34,7 @@ pub fn fetch_latest(count: u32) -> Vec<BuildOrder> {
2134 }
2235 Err ( e) => {
2336 if e. eq ( & BuildOrderError :: Cloaked ) {
24- eprintln ! ( "Build order {} is cloaked, skipping." , current_id) ;
37+ pb . set_message ( format ! ( "Build order {} is cloaked, skipping." , current_id) ) ;
2538 end_index = if end_index > LOWEST_INDEX {
2639 end_index - 1
2740 } else {
@@ -35,10 +48,23 @@ pub fn fetch_latest(count: u32) -> Vec<BuildOrder> {
3548 }
3649 }
3750 }
51+ pb. finish_with_message ( format ! (
52+ "{} {} build orders fetched." ,
53+ style( "✔" ) . green( ) ,
54+ build_orders. len( )
55+ ) ) ;
3856 build_orders
3957}
4058
4159pub fn fetch_segment ( start : u32 , end : u32 ) -> Vec < BuildOrder > {
60+ let spinner_style = ProgressStyle :: with_template (
61+ "[{percent:.bold.dim}%] {elapsed:.dim} {spinner} {bar}\t {wide_msg}" ,
62+ )
63+ . unwrap ( )
64+ . tick_chars ( "⡇⣆⣤⣰⢸⠹⠛⠏ " ) ;
65+ let pb = ProgressBar :: new ( ( end - start + 1 ) as u64 ) ;
66+ pb. set_style ( spinner_style) ;
67+
4268 let highest_index = get_st_highest_index ( ) ;
4369 let start = if start < LOWEST_INDEX {
4470 LOWEST_INDEX
@@ -59,12 +85,52 @@ pub fn fetch_segment(start: u32, end: u32) -> Vec<BuildOrder> {
5985 return Vec :: new ( ) ;
6086 }
6187
88+ let m = MultiProgress :: new ( ) ;
89+
6290 let mut build_orders = Vec :: new ( ) ;
63- for id in ( start..=end) . rev ( ) {
64- match fetch_build_order ( id) {
65- Ok ( build_order) => build_orders. push ( build_order) ,
66- Err ( e) => eprintln ! ( "Error fetching build order {}: {}" , id, e) ,
91+ let mut handles = Vec :: new ( ) ;
92+ let count = ( end - start + 1 ) as usize ;
93+
94+ for id in start..=end {
95+ let pb_clone = m. add ( ProgressBar :: new ( 1 ) ) ;
96+ let handle = thread:: spawn ( move || {
97+ let spinner_style =
98+ ProgressStyle :: with_template ( "[{prefix:.bold.dim}] {spinner} \t {wide_msg}" )
99+ . unwrap ( )
100+ . tick_chars ( "⡇⣆⣤⣰⢸⠹⠛⠏ " ) ;
101+ pb_clone. set_message ( format ! ( "Fetching build order {}" , id) ) ;
102+ pb_clone. set_style ( spinner_style. clone ( ) ) ;
103+ pb_clone. enable_steady_tick ( Duration :: from_millis ( 100 ) ) ;
104+ pb_clone. set_prefix ( format ! ( "{}/{}" , id - start + 1 , count) ) ;
105+ match fetch_build_order ( id) {
106+ Ok ( build_order) => {
107+ pb_clone. inc ( 1 ) ;
108+ Some ( build_order)
109+ }
110+ Err ( e) => {
111+ if e. eq ( & BuildOrderError :: Cloaked ) {
112+ pb_clone. set_message ( format ! ( "Build order {} is cloaked, skipping." , id) ) ;
113+ } else {
114+ eprintln ! ( "Error fetching build order {}: {}" , id, e) ;
115+ }
116+ None
117+ }
118+ }
119+ } ) ;
120+ handles. push ( handle) ;
121+ }
122+
123+ for handle in handles {
124+ if let Ok ( Some ( build_order) ) = handle. join ( ) {
125+ build_orders. push ( build_order) ;
67126 }
68127 }
128+
129+ pb. finish_with_message ( format ! (
130+ "{} {} build orders fetched." ,
131+ style( "✔" ) . green( ) ,
132+ build_orders. len( )
133+ ) ) ;
134+
69135 build_orders
70136}
0 commit comments