@@ -3,9 +3,11 @@ use std::io::{self, Write};
3
3
use std:: process;
4
4
use std:: sync:: Arc ;
5
5
6
+ use crossterm:: style:: Color ;
6
7
use rand:: Rng ;
7
8
8
9
use crate :: args:: prelude:: * ;
10
+ use crate :: colour;
9
11
use crate :: config:: { BehaviourOnError , Config , NumberOfQuestions } ;
10
12
use crate :: question:: { Generator , Question } ;
11
13
use crate :: skill:: doomsday_algorithm;
@@ -142,8 +144,15 @@ impl AppImpl {
142
144
}
143
145
144
146
fn handle_question ( & self , question : & Question ) {
145
- println ! ( "\n Q: {}" , question. prompt( ) ) ;
146
- print ! ( "A: " ) ;
147
+ println ! (
148
+ "\n {}{}" ,
149
+ colour:: format_text( "Q: " , self . use_colour( ) , Color :: DarkYellow ) ,
150
+ question. prompt( )
151
+ ) ;
152
+ print ! (
153
+ "{}" ,
154
+ colour:: format_text( "A: " , self . use_colour( ) , Color :: DarkYellow )
155
+ ) ;
147
156
io:: stdout ( ) . flush ( ) . expect ( "IO operation failed (flush)" ) ;
148
157
149
158
self . stats . start_new_question ( ) ;
@@ -190,25 +199,38 @@ impl AppImpl {
190
199
}
191
200
} ;
192
201
193
- println ! ( "{number_of_questions}. Use Ctrl+C to exit." ) ;
202
+ let number_of_questions =
203
+ colour:: format_text ( number_of_questions, self . use_colour ( ) , Color :: DarkYellow ) ;
204
+ let ctrl_c = colour:: format_text ( "Ctrl+C" , self . use_colour ( ) , Color :: Yellow ) ;
205
+
206
+ println ! ( "{number_of_questions}. Use {ctrl_c} to exit." ) ;
194
207
}
195
208
196
209
fn print_stats_in_between ( & self ) {
197
- match self . number_of_questions ( ) {
198
- NumberOfQuestions :: Infinite => println ! (
199
- "Time taken: {}, current accuracy: {} ({})" ,
200
- self . stats. get_last_question_time( ) ,
201
- self . stats. get_current_accuracy( ) ,
202
- self . stats. get_number_of_correct_answers( ) ,
210
+ let text = match self . number_of_questions ( ) {
211
+ NumberOfQuestions :: Infinite => colour:: format_text (
212
+ & format ! (
213
+ "Time taken: {}, current accuracy: {} ({})" ,
214
+ self . stats. get_last_question_time( ) ,
215
+ self . stats. get_current_accuracy( ) ,
216
+ self . stats. get_number_of_correct_answers( )
217
+ ) ,
218
+ self . use_colour ( ) ,
219
+ Color :: Grey ,
203
220
) ,
204
- NumberOfQuestions :: Limited ( _) => println ! (
205
- "Time taken: {}, current accuracy: {} ({}), questions left: {}" ,
206
- self . stats. get_last_question_time( ) ,
207
- self . stats. get_current_accuracy( ) ,
208
- self . stats. get_number_of_correct_answers( ) ,
209
- self . stats. get_number_of_remaining_questions( ) ,
221
+ NumberOfQuestions :: Limited ( _) => colour:: format_text (
222
+ & format ! (
223
+ "Time taken: {}, current accuracy: {} ({}), questions left: {}" ,
224
+ self . stats. get_last_question_time( ) ,
225
+ self . stats. get_current_accuracy( ) ,
226
+ self . stats. get_number_of_correct_answers( ) ,
227
+ self . stats. get_number_of_remaining_questions( ) ,
228
+ ) ,
229
+ self . use_colour ( ) ,
230
+ Color :: Grey ,
210
231
) ,
211
- }
232
+ } ;
233
+ println ! ( "{text}" ) ;
212
234
}
213
235
214
236
fn print_stats_post_game ( & self ) {
@@ -251,9 +273,17 @@ impl AppImpl {
251
273
fn print_answer_feedback ( & self , correct : bool , correct_answer : & str ) {
252
274
let mut feedback = String :: new ( ) ;
253
275
if correct {
254
- feedback. push_str ( & Self :: random_feedback_correct ( ) ) ;
276
+ feedback. push_str ( & colour:: format_text (
277
+ & Self :: random_feedback_correct ( ) ,
278
+ self . use_colour ( ) ,
279
+ Color :: Green ,
280
+ ) ) ;
255
281
} else {
256
- feedback. push_str ( & Self :: random_feedback_incorrect ( ) ) ;
282
+ feedback. push_str ( & colour:: format_text (
283
+ & Self :: random_feedback_incorrect ( ) ,
284
+ self . use_colour ( ) ,
285
+ Color :: Red ,
286
+ ) ) ;
257
287
match self . config . options . behaviour_on_error {
258
288
BehaviourOnError :: ShowCorrect => {
259
289
feedback. push_str ( & format ! ( " Correct answer: {correct_answer}" ) ) ;
@@ -388,4 +418,8 @@ impl AppImpl {
388
418
fn number_of_questions ( & self ) -> NumberOfQuestions {
389
419
self . config . options . number_of_questions
390
420
}
421
+
422
+ fn use_colour ( & self ) -> bool {
423
+ self . config . options . use_colour
424
+ }
391
425
}
0 commit comments