1
1
use std:: process:: exit;
2
+ use termion:: color;
3
+
2
4
mod split;
3
5
use inquire:: { InquireError , Select , Text } ;
4
6
use split:: * ;
@@ -21,7 +23,7 @@ fn main() {
21
23
22
24
match selected_prompt {
23
25
Ok ( choice) => handle_choice ( choice, & mut all_users, & mut all_transactions) ,
24
- Err ( _) => println ! ( "There was an error, please try again" ) ,
26
+ Err ( _) => print_error_in_red ( "There was an error, please try again" ) ,
25
27
}
26
28
println ! ( ) ;
27
29
}
@@ -34,32 +36,32 @@ fn handle_choice(choice: &str, all_users: &mut Vec<User>, all_transactions: &mut
34
36
"3. Split Bill" => split_bill ( all_transactions) ,
35
37
"4. View all Transactions" => view_all_expenses ( all_transactions) ,
36
38
"5. Exit" => handle_exit ( ) ,
37
- _ => println ! ( "Invalid choice!" ) ,
39
+ _ => print_error_in_red ( "Invalid choice!" ) ,
38
40
}
39
41
}
40
42
41
43
fn create_group ( all_users : & mut Vec < User > ) {
42
44
let no_of_people = Text :: new ( "Enter the number of people:" ) . prompt ( ) ;
43
45
match no_of_people {
44
- Ok ( no_of_people) => {
45
- let mut no_of_people = no_of_people. parse :: < u8 > ( ) . unwrap ( ) ;
46
- if no_of_people > 0 {
47
- while no_of_people > 0 {
46
+ Ok ( no_of_people) => match no_of_people. parse :: < u32 > ( ) . as_mut ( ) {
47
+ Ok ( no_of_people) => {
48
+ while * no_of_people > 0 {
48
49
let username = Text :: new ( "Enter the user name:" ) . prompt ( ) ;
49
50
match username {
50
51
Ok ( username) => {
51
52
let user = User :: create_user ( & username) ;
52
53
all_users. push ( user) ;
53
54
}
54
- Err ( _) => println ! ( "Error in creating user" ) ,
55
+ Err ( _) => print_error_in_red ( "Error in creating user" ) ,
55
56
}
56
- no_of_people -= 1 ;
57
+ * no_of_people -= 1 ;
57
58
}
59
+ println ! ( "------------------Group Created----------------------" ) ;
58
60
}
59
- }
60
- Err ( _) => println ! ( "Error occurred while taking number of people" ) ,
61
+ Err ( _) => print_error_in_red ( "Please Select valid group number" ) ,
62
+ } ,
63
+ Err ( _) => print_error_in_red ( "Error occurred while taking number of people" ) ,
61
64
}
62
- println ! ( "------------------Group Created----------------------" ) ;
63
65
}
64
66
65
67
fn add_expense ( all_users : & mut Vec < User > , all_transactions : & mut Transactions ) {
@@ -86,22 +88,25 @@ fn add_expense(all_users: &mut Vec<User>, all_transactions: &mut Transactions) {
86
88
let amount = Text :: new ( "Enter the amount of expense:" ) . prompt ( ) ;
87
89
match amount {
88
90
Ok ( amount) => {
89
- let amount = amount. parse :: < u64 > ( ) . unwrap ( ) ;
90
- let from = User :: create_user ( selected_user) ;
91
- let to = User :: create_user ( giver_name) ;
92
-
93
- let tx = Transaction :: new ( from, to, amount) ;
94
- all_transactions. add ( tx) ;
91
+ let amount = amount. parse :: < u64 > ( ) ;
92
+ match amount {
93
+ Ok ( amount) => {
94
+ let from = User :: create_user ( selected_user) ;
95
+ let to = User :: create_user ( giver_name) ;
96
+ let tx = Transaction :: new ( from, to, amount) ;
97
+ all_transactions. add ( tx)
98
+ }
99
+ Err ( _) => print_error_in_red ( "Amount is not valid" ) ,
100
+ } ;
95
101
}
96
- Err ( _) => println ! ( "Error in entering amount" ) ,
102
+ Err ( _) => print_error_in_red ( "Error in entering amount" ) ,
97
103
}
98
104
}
99
- Err ( _) => println ! ( "Error in selecting giver" ) ,
105
+ Err ( _) => print_error_in_red ( "Error in selecting giver" ) ,
100
106
}
101
107
}
102
- Err ( _) => println ! ( "Error in selecting payer" ) ,
108
+ Err ( _) => print_error_in_red ( "Error in selecting payer" ) ,
103
109
}
104
- println ! ( "Added Expense..." ) ;
105
110
}
106
111
107
112
fn split_bill ( all_transactions : & mut Transactions ) {
@@ -110,11 +115,21 @@ fn split_bill(all_transactions: &mut Transactions) {
110
115
let selected_tx = all_transactions. split_bill ( ) ;
111
116
selected_tx. display ( ) ;
112
117
}
118
+
113
119
fn view_all_expenses ( all_transactions : & mut Transactions ) {
114
120
all_transactions. display ( ) ;
115
121
}
116
122
117
123
fn handle_exit ( ) {
118
- println ! ( "Exiting CLI..." ) ;
124
+ print_error_in_red ( "Exiting CLI..." ) ;
119
125
exit ( 0 ) ;
120
126
}
127
+
128
+ fn print_error_in_red ( msg : & str ) {
129
+ println ! (
130
+ "{}{}{}" ,
131
+ color:: Fg ( color:: Red ) ,
132
+ msg,
133
+ color:: Fg ( color:: Reset )
134
+ ) ;
135
+ }
0 commit comments