@@ -185,17 +185,34 @@ pub enum SpinnerType {
185185 Custom ( Vec < & ' static str > ) ,
186186}
187187
188+ impl SpinnerType {
189+ /// Converts the spinner type to a vector of frames.
190+ ///
191+ /// # Returns
192+ ///
193+ /// Returns a vector of strings representing frames for the spinner, gives back the following variants:
194+ /// - `SpinnerType::Standard`: Standard spinner with characters / - \ |.
195+ /// - `SpinnerType::Dots`: Spinner with dots . .. ... .....
196+ /// - `SpinnerType::Box`: Spinner with box characters ▌ ▀ ▐ ▄.
197+ /// - `SpinnerType::Flip`: Spinner with flip characters _ _ _ - \ ' ´ - _ _ _.
198+ /// - `SpinnerType::Custom(frames)`: Custom spinner with user-defined frames.
199+ fn to_frames ( & self ) -> Vec < & ' static str > {
200+ match self {
201+ SpinnerType :: Standard => vec ! [ "/" , "-" , "\\ " , "|" ] ,
202+ SpinnerType :: Dots => vec ! [ "." , ".." , "..." , "...." , "..." , ".." ] ,
203+ SpinnerType :: Box => vec ! [ "▌" , "▀" , "▐" , "▄" ] ,
204+ SpinnerType :: Flip => vec ! [ "_" , "_" , "_" , "-" , "`" , "`" , "'" , "´" , "-" , "_" , "_" , "_" ] ,
205+ SpinnerType :: Custom ( frames) => frames. to_owned ( ) ,
206+ }
207+ }
208+ }
209+
188210/// Displays a console-based spinner animation.
189211///
190212/// # Parameters
191213///
192214/// - `time`: A floating-point number representing the duration of the spinner animation in seconds.
193- /// - `spinner_type`: The type of spinner to display, which can be one of the following:
194- /// - `SpinnerType::Standard`: Standard spinner with characters / - \ |.
195- /// - `SpinnerType::Dots`: Spinner with dots . .. ... .....
196- /// - `SpinnerType::Box`: Spinner with box characters ▌ ▀ ▐ ▄.
197- /// - `SpinnerType::Flip`: Spinner with flip characters _ _ _ - \ ' ´ - _ _ _.
198- /// - `SpinnerType::Custom(frame)`: Custom spinner with a user-defined frame.
215+ /// - `spinner_type`: The type of spinner to display.
199216///
200217/// # Example
201218///
@@ -210,24 +227,18 @@ pub enum SpinnerType {
210227/// ```
211228pub fn spinner ( mut time : f64 , spinner_type : SpinnerType ) {
212229 let stdout = Term :: buffered_stdout ( ) ;
230+ let frames = spinner_type. to_frames ( ) ;
213231 let mut i = 0 ;
214232
215233 while time > 0.0 {
216234 stdout. clear_line ( ) . unwrap ( ) ;
217- let frame = match spinner_type {
218- SpinnerType :: Standard => vec ! [ "/" , "-" , "\\ " , "|" ] ,
219- SpinnerType :: Dots => vec ! [ "." , ".." , "..." , "...." , "..." , ".." ] ,
220- SpinnerType :: Box => vec ! [ "▌" , "▀" , "▐" , "▄" ] ,
221- SpinnerType :: Flip => vec ! [ "_" , "_" , "_" , "-" , "`" , "`" , "'" , "´" , "-" , "_" , "_" , "_" ] ,
222- SpinnerType :: Custom ( ref custom_frame) => custom_frame. to_vec ( ) ,
223- } ;
224- stdout. write_line ( frame[ i] ) . unwrap ( ) ;
235+ stdout. write_line ( frames[ i] ) . unwrap ( ) ;
225236 stdout. move_cursor_up ( 1 ) . unwrap ( ) ;
226- stdout. move_cursor_right ( frame [ i] . len ( ) ) . unwrap ( ) ;
237+ stdout. move_cursor_right ( frames [ i] . len ( ) ) . unwrap ( ) ;
227238 stdout. flush ( ) . unwrap ( ) ;
228239 thread:: sleep ( Duration :: from_secs_f64 ( 0.075 ) ) ;
229240 time -= 0.075 ;
230- if i < frame . len ( ) - 1 {
241+ if i < frames . len ( ) - 1 {
231242 i += 1
232243 } else {
233244 i = 0
0 commit comments