Skip to content

Commit a701298

Browse files
feat: add --no-color flag
1 parent 79e5931 commit a701298

File tree

3 files changed

+208
-3
lines changed

3 files changed

+208
-3
lines changed

Cargo.lock

+184-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "practicestuff"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
description = "Simple CLI trainer for improving calculation and memorisation skills"
55
authors = ["Bartłomiej Jaszczak"]
66
repository = "https://gitlab.com/bartekjaszczak/practicestuff"
@@ -14,6 +14,7 @@ edition = "2021"
1414
ctrlc = "3.4"
1515
rand = "0.8.5"
1616
chrono = "0.4.38"
17+
crossterm = "0.28.1"
1718

1819
[dev-dependencies]
1920
assert_cmd = "2.0.16"

src/config.rs

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ARG_ID_VERSION: &str = "version";
1414
const ARG_ID_NUMBER_OF_QUESTIONS: &str = "num_of_questions";
1515
const ARG_ID_DISABLE_LIVE_STATISTICS: &str = "disable_live_stats";
1616
const ARG_ID_BEHAVIOUR_ON_ERROR: &str = "behaviour_on_err";
17+
const ARG_ID_NO_COLOUR: &str = "no_colour";
1718

1819
const BEHAVIOUR_ON_ERROR_CONTINUE: &str = "continue";
1920
const BEHAVIOUR_ON_ERROR_SHOW_CORRECT: &str = "showcorrect";
@@ -127,15 +128,19 @@ impl BehaviourOnError {
127128
}
128129
}
129130

131+
#[allow(clippy::struct_excessive_bools)]
130132
#[derive(Debug, Clone)]
131133
pub struct GeneralOptions {
132134
pub arg_definitions: Vec<Arg>,
135+
133136
pub show_help: bool,
134137
pub show_version: bool,
135138

136139
pub number_of_questions: NumberOfQuestions,
137140
pub disable_live_statistics: bool,
138141
pub behaviour_on_error: BehaviourOnError,
142+
143+
pub use_colour: bool,
139144
}
140145

141146
impl GeneralOptions {
@@ -169,13 +174,17 @@ impl GeneralOptions {
169174
);
170175
let behaviour_on_error = BehaviourOnError::from_string(&behaviour_on_error);
171176

177+
let use_colour =
178+
!bool::set_value_from_arg_or_default(ARG_ID_NO_COLOUR, &parsed_args, &arg_definitions);
179+
172180
Ok(Self {
173181
arg_definitions,
174182
show_help,
175183
show_version,
176184
number_of_questions,
177185
disable_live_statistics,
178186
behaviour_on_error,
187+
use_colour
179188
})
180189
}
181190

@@ -244,6 +253,14 @@ impl GeneralOptions {
244253
.stop_parsing(false)
245254
.default_value(ArgValue::Str("showcorrect".to_string()))
246255
.build(),
256+
Arg::builder()
257+
.id(ARG_ID_NO_COLOUR)
258+
.long_name("no-color")
259+
.description(vec!["Disable coloured output.".to_string()])
260+
.kind(ArgKind::Flag)
261+
.stop_parsing(false)
262+
.default_value(ArgValue::Bool(false))
263+
.build(),
247264
]
248265
}
249266
}
@@ -317,6 +334,7 @@ mod tests {
317334
BehaviourOnError::ShowCorrect
318335
);
319336
assert!(config.skill.is_some());
337+
assert!(config.options.use_colour);
320338
}
321339

322340
#[test]
@@ -326,6 +344,7 @@ mod tests {
326344
"--number-of-questions=10".to_string(),
327345
"--disable-live-statistics".to_string(),
328346
"--behavior-on-error=repeat".to_string(),
347+
"--no-color".to_string(),
329348
"powers".to_string(),
330349
];
331350
let config = Config::build(&args).expect("should build successfully");
@@ -338,12 +357,14 @@ mod tests {
338357
assert!(config.options.disable_live_statistics);
339358
assert_eq!(config.options.behaviour_on_error, BehaviourOnError::Repeat);
340359
assert!(config.skill.is_some());
360+
assert!(!config.options.use_colour);
341361

342362
// Different set of args
343363
let args = [
344364
"command".to_string(),
345365
"-n".to_string(),
346366
"0".to_string(),
367+
"--no-color".to_string(),
347368
"-d".to_string(),
348369
"-b".to_string(),
349370
"continue".to_string(),
@@ -362,6 +383,7 @@ mod tests {
362383
BehaviourOnError::NextQuestion
363384
);
364385
assert!(config.skill.is_some());
386+
assert!(!config.options.use_colour);
365387
}
366388

367389
#[test]

0 commit comments

Comments
 (0)