11use clap:: ArgMatches ;
22use isahc:: { HttpClient , ReadResponseExt , Request } ;
3-
3+ use std :: io :: prelude :: * ;
44use serde_json:: { Map , Value } ;
55
66use crate :: api:: APIError ;
77use crate :: api:: API ;
88
9- static RST : & ' static str = "\x1B [0m" ;
10- static CYAN : & ' static str = "\x1B [36m" ;
11- static WHITE : & ' static str = "\x1B [37m" ;
12- static RED : & ' static str = "\x1B [31m" ;
13-
149pub fn list ( matches : & ArgMatches < ' _ > , api : & mut API ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
1510 let token = api. get_token ( ) ?;
1611
12+ let mut term = term:: stdout ( ) . unwrap ( ) ;
13+
1714 let client = HttpClient :: new ( ) ?;
1815 let mut url = "https://drink.csh.rit.edu/drinks" . to_string ( ) ;
1916 if let Some ( machine) = matches. value_of ( "machine" ) {
@@ -51,8 +48,9 @@ pub fn list(matches: &ArgMatches<'_>, api: &mut API) -> Result<(), Box<dyn std::
5148 None => return Err ( Box :: new ( APIError :: BadFormat ) ) ,
5249 } ;
5350 let subject_line = format ! ( "{} ({})" , display_name, name) ;
54- println ! ( "{}{}{}" , & CYAN , & subject_line, & RST ) ;
55- println ! ( "{}{}{}" , & CYAN , "=" . repeat( subject_line. len( ) ) , & RST ) ;
51+ term. fg ( term:: color:: CYAN ) . unwrap ( ) ;
52+ writeln ! ( term, "{}" , subject_line) . unwrap ( ) ;
53+ writeln ! ( term, "{}" , "=" . repeat( subject_line. len( ) ) ) . unwrap ( ) ;
5654 let slots: & Vec < Value > = match machine[ "slots" ] . as_array ( ) {
5755 Some ( slots) => slots,
5856 None => return Err ( Box :: new ( APIError :: BadFormat ) ) ,
@@ -72,10 +70,13 @@ pub fn list(matches: &ArgMatches<'_>, api: &mut API) -> Result<(), Box<dyn std::
7270 let slot_number = slot[ "number" ] . as_u64 ( ) . unwrap ( ) ;
7371 let name = item[ "name" ] . as_str ( ) . unwrap ( ) ;
7472 if slot[ "empty" ] . as_bool ( ) . unwrap ( ) {
75- print ! ( "{}{}. {} ({} Credits) [EMPTY]{}" , & RED , slot_number, name, price, & RST )
73+ term. fg ( term:: color:: RED ) . unwrap ( ) ;
74+ write ! ( term, "{}. {} ({} Credits) [EMPTY]" , slot_number, name, price) . unwrap ( ) ;
7675 } else {
77- print ! ( "{}. {} ({} Credits)" , slot_number, name, price)
76+ term. fg ( term:: color:: WHITE ) . unwrap ( ) ;
77+ write ! ( term, "{}. {} ({} Credits)" , slot_number, name, price) . unwrap ( ) ;
7878 }
79+ term. reset ( ) . unwrap ( ) ;
7980 println ! ( "" ) ;
8081 }
8182 println ! ( "" ) ;
0 commit comments