55use std:: { io, str:: FromStr , thread, time:: Duration } ;
66
77use crate :: {
8- control:: * ,
8+ control:: { clear_line , flush , move_cursor_down , move_cursor_up , Visibility } ,
99 read:: { read_key, Key } ,
10+ styled:: { Color , StyledText } ,
1011} ;
1112
1213/// A Wrapper for empty inputs returning a None
@@ -51,15 +52,20 @@ where
5152 T :: Err : std:: fmt:: Debug ,
5253{
5354 loop {
54- print ! ( "\x1b [31m?\x1b [0m {before} \x1b [90m›\x1b [0m " ) ;
55+ let quest = StyledText :: new ( "?" ) . fg ( Color :: Red ) ;
56+ let caret = StyledText :: new ( "›" ) . fg ( Color :: BrightBlack ) ;
57+ print ! ( "{quest} {before} {caret} " ) ;
5558 flush ( ) ;
5659
5760 let mut cli = String :: new ( ) ;
5861 io:: stdin ( ) . read_line ( & mut cli) . unwrap ( ) ;
5962
6063 match cli. parse ( ) {
6164 Ok ( value) => return value,
62- Err ( _) => println ! ( "\n \x1b [31mX\x1b [0m Invalid Input Type\n " ) ,
65+ Err ( _) => {
66+ let x = StyledText :: new ( "X" ) . fg ( Color :: Red ) ;
67+ println ! ( "\n {x} Invalid Input Type\n " )
68+ }
6369 }
6470 }
6571}
@@ -82,7 +88,9 @@ pub fn select<'a>(before: &'a str, options: &'a [&'a str]) -> usize {
8288 let mut i = 0 ;
8389
8490 // print everything
85- println ! ( "\x1b [31m?\x1b [0m {before} \x1b [90m›\x1b [0m " ) ;
91+ let quest = StyledText :: new ( "?" ) . fg ( Color :: Red ) ;
92+ let caret = StyledText :: new ( "›" ) . fg ( Color :: BrightBlack ) ;
93+ println ! ( "{quest} {before} {caret} " ) ;
8694
8795 populate ( options, None , 0 ) ;
8896
@@ -140,7 +148,9 @@ pub fn multiselect(before: &str, options: &[&str]) -> Vec<bool> {
140148 let mut i = 0 ;
141149
142150 // print everything
143- println ! ( "\x1b [31m?\x1b [0m {before} \x1b [90m›\x1b [0m " ) ;
151+ let quest = StyledText :: new ( "?" ) . fg ( Color :: Red ) ;
152+ let caret = StyledText :: new ( "›" ) . fg ( Color :: BrightBlack ) ;
153+ println ! ( "{quest} {before} {caret} " ) ;
144154
145155 populate ( options, Some ( & matrix) , 0 ) ;
146156
@@ -185,22 +195,21 @@ pub fn multiselect(before: &str, options: &[&str]) -> Vec<bool> {
185195 matrix
186196}
187197
188- /// Populate function for multiselect
198+ /// Populate function for select/ multiselect
189199fn populate ( options : & [ & str ] , matrix : Option < & [ bool ] > , cursor : usize ) {
190200 for ( i, option) in options. iter ( ) . enumerate ( ) {
191201 clear_line ( ) ;
192202 if i == cursor {
193- println ! (
194- "\x1b [36m ›\x1b [0m\x1b [3{}m {}\x1b [0m" ,
195- if matrix. is_some( ) && matrix. unwrap( ) [ i] {
196- "2"
197- } else {
198- "6"
199- } ,
200- option
201- ) ;
203+ let caret = StyledText :: new ( "›" ) . fg ( Color :: Green ) ;
204+ let option = if matrix. is_some ( ) && matrix. unwrap ( ) [ i] {
205+ StyledText :: new ( option) . fg ( Color :: Green )
206+ } else {
207+ StyledText :: new ( option) . fg ( Color :: Cyan )
208+ } ;
209+ println ! ( " {caret} {option}" ) ;
202210 } else if matrix. is_some ( ) && matrix. unwrap ( ) [ i] {
203- println ! ( "\x1b [32m {}\x1b [0m" , option) ;
211+ let option = StyledText :: new ( option) . fg ( Color :: Green ) ;
212+ println ! ( " {}" , option) ;
204213 } else {
205214 println ! ( " {}" , option) ;
206215 }
@@ -211,27 +220,27 @@ fn populate(options: &[&str], matrix: Option<&[bool]>, cursor: usize) {
211220/// Enumeration representing different types of spinners.
212221#[ derive( Debug , Clone ) ]
213222pub enum SpinnerType {
223+ /// Spinner with characters `/` `-` `\` `|`.
214224 Standard ,
225+ /// Spinner with dots `.` `..` `...` `.....`.
215226 Dots ,
227+ /// Spinner with box characters `▌` `▀` `▐` `▄`.
216228 Box ,
229+ /// Spinner with flip characters `_` `_` `_` `-` `\` `'` `´` `-` `_` `_` `_`.
217230 Flip ,
218- Custom ( Vec < & ' static str > ) ,
231+ /// Custom spinner with user-defined frames.
232+ Custom ( & ' static [ & ' static str ] ) ,
219233}
220234
221235impl SpinnerType {
222- /// Converts the spinner type to a vector of frames, gives back the following variants:
223- /// - `SpinnerType::Standard`: Standard spinner with characters / - \ |.
224- /// - `SpinnerType::Dots`: Spinner with dots . .. ... .....
225- /// - `SpinnerType::Box`: Spinner with box characters ▌ ▀ ▐ ▄.
226- /// - `SpinnerType::Flip`: Spinner with flip characters _ _ _ - \ ' ´ - _ _ _.
227- /// - `SpinnerType::Custom(frames)`: Custom spinner with user-defined frames.
228- pub fn to_frames ( & self ) -> Vec < & ' static str > {
236+ /// Returns the frames of the spinner type.
237+ pub fn frames ( & self ) -> & ' static [ & ' static str ] {
229238 match self {
230- SpinnerType :: Standard => vec ! [ "/" , "-" , "\\ " , "|" ] ,
231- SpinnerType :: Dots => vec ! [ "." , ".." , "..." , "...." , "..." , ".." ] ,
232- SpinnerType :: Box => vec ! [ "▌" , "▀" , "▐" , "▄" ] ,
233- SpinnerType :: Flip => vec ! [ "_" , "_" , "_" , "-" , "`" , "`" , "'" , "´" , "-" , "_" , "_" , "_" ] ,
234- SpinnerType :: Custom ( frames) => frames. to_owned ( ) ,
239+ SpinnerType :: Standard => & [ "/" , "-" , "\\ " , "|" ] ,
240+ SpinnerType :: Dots => & [ "." , ".." , "..." , "...." , "..." , ".." ] ,
241+ SpinnerType :: Box => & [ "▌" , "▀" , "▐" , "▄" ] ,
242+ SpinnerType :: Flip => & [ "_" , "_" , "_" , "-" , "`" , "`" , "'" , "´" , "-" , "_" , "_" , "_" ] ,
243+ SpinnerType :: Custom ( frames) => frames,
235244 }
236245 }
237246}
@@ -246,7 +255,7 @@ impl SpinnerType {
246255/// - `time`: A floating-point number representing the duration of the spinner animation in seconds.
247256/// - `spinner_type`: The type of spinner to display.
248257pub fn spinner ( mut time : f64 , spinner_type : SpinnerType ) {
249- let frames = spinner_type. to_frames ( ) ;
258+ let frames = spinner_type. frames ( ) ;
250259 let mut i = 0 ;
251260
252261 while time > 0.0 {
0 commit comments