Skip to content

Commit

Permalink
added -w and -a flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Iipal committed May 1, 2021
1 parent 74f4273 commit 9fdd7fe
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
2 changes: 2 additions & 0 deletions includes/Flags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class Flags {
static void parse(int argc, char *argv[]);

static unsigned int max_time;
static unsigned int max_words;
static bool is_monochrome;
static bool is_alphabetic;

private:
Flags();
Expand Down
39 changes: 32 additions & 7 deletions srcs/Flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,48 @@
#include <getopt.h>

#define FLAG_H 'h'
#define FLAG_W 'w'
#define FLAG_T 't'
#define FLAG_M 'm'
#define FLAG_A 'a'

#define FLAGS_OPT "ht:m"
#define FLAGS_OPT "hw:t:ma"

#define FLAGS_USAGE "Usage: ./typos [" FLAGS_OPT "]"

#define FLAG_M_DEFAULT false
#define FLAG_W_DEFAULT 42
#define FLAG_W_MIN 10
#define FLAG_W_MAX 150
#define FLAG_W_DESC \
"Number of maximum words to type.\n" \
" Accepts only positive integer in range: 10 - 150; " \
"Default: 42."

#define FLAG_T_DEFAULT Timer::SECONDS_DEFAULT
#define FLAG_T_MIN Timer::SECONDS_MIN
#define FLAG_T_MAX Timer::SECONDS_MAX

#define FLAG_T_DESC \
"Number of maximum time to type in seconds.\n" \
" Accepts only positive integer in range: 10 - 240; " \
"Default: 60."

#define FLAG_M_DEFAULT false

#define FLAG_A_DEFAULT false

#define FLAG_H_DESC \
FLAGS_USAGE "\n" \
" -h : print this message.\n" \
" -m : Monochrome mode. Disabling all colors.\n" \
" -t [seconds]: " FLAG_T_DESC "\n"
FLAGS_USAGE \
"\n" \
" -h : print this message.\n" \
" -w [number] : " FLAG_W_DESC "\n" \
" -t [seconds]: " FLAG_T_DESC "\n" \
" -m : Monochrome mode. Default: false;\n" \
" -a : Words sorted in alphabetical order. Default: false;\n"

unsigned int Flags::max_time = FLAG_T_DEFAULT;
unsigned int Flags::max_words = FLAG_W_DEFAULT;
bool Flags::is_monochrome = FLAG_M_DEFAULT;
bool Flags::is_alphabetic = FLAG_A_DEFAULT;

Flags::Flags() {}

Expand Down Expand Up @@ -60,6 +76,11 @@ void Flags::parse(int argc, char *argv[]) {
int c;
while (-1 != (c = getopt(argc, argv, FLAGS_OPT))) {
switch (c) {
case FLAG_W:
Flags::max_words = flag_positive_integer_arg_parse(
optarg, FLAG_W, FLAG_W_MIN, FLAG_W_MAX, FLAG_W_DEFAULT);
break;

case FLAG_T:
Flags::max_time = flag_positive_integer_arg_parse(
optarg, FLAG_T, FLAG_T_MIN, FLAG_T_MAX, FLAG_T_DEFAULT);
Expand All @@ -69,6 +90,10 @@ void Flags::parse(int argc, char *argv[]) {
Flags::is_monochrome = true;
break;

case FLAG_A:
Flags::is_alphabetic = true;
break;

case '?':
fprintf(stderr, "%s\n", FLAGS_USAGE);
exit(EXIT_FAILURE);
Expand Down
43 changes: 8 additions & 35 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
#include "typos.hpp"

const std::string __test_strings[] = {
"trapshooters", "welled", "interrelated", "pipped",
"overdyes", "prorogations", "rigidifying", "new",
"ductility", "grazing", "unifaces", "capacitating",
"orby", "kernel", "bumkins", "moonflowers",
"chrysoprases", "explicable", "nasalised", "getaways",
"antimusical", "vivifies", "oomiacks", "kerrias",
"coulisses", "cullender", "playas", "magnate",
"sexologic", "amnic", "androgynies", "ruffly",
"canthal", "unhairers", "isotonic", "aerobats",
"nonmechanistic", "curabilities", "needs", "gemminess",
"reallots", "urinate", "counterplay", "ravine",
"animadverts", "antineutrino", "disrelishing", "endopeptidases",
"confidantes", "cabbalas", "deliciousness", "recomputes",
"laughingly", "sips", "fud", "sufficers",
"synoptical", "superior", "rurban", "microclines",
"billet", "windingly", "squealers", "foilsman",
"gynophobes", "ptosis", "backer", "simioid",
"perusing", "ruing", "neurotoxin", "phosphene",
"reschooled", "lien", "masting", "doily",
"phonemicists", "unutilized", "updry", "unafraid",
"smugnesses", "preinterviews", "purfler", "beglamor",
"affluently", "anamneses", "chaired", "hardheaded",
"graybeards", "drainages", "stratus", "emblemizing",
"bilboes", "trudgeon", "flipflopping", "hands",
"renails", "hookup", "scavenger", "drawing"};
const size_t __test_strings_length =
sizeof(__test_strings) / sizeof(*__test_strings);

static inline int welcome_screen(void) {
const std::string msg = "PRESS ANY KEY TO START";
const int x = Print::get_center_x(msg.length());
Expand Down Expand Up @@ -58,14 +29,16 @@ int main(int argc, char *argv[]) {
int input = 0;
bool is_input_ok = true;
bool stop = welcome_screen() == Typing::KEY_ESC;
if (stop) {
finish(0);
}

Typing test_typing = Typing(__test_strings, __test_strings_length);
if (!stop) {
Timer::init(Flags::max_time, &test_typing);
Typing test_typing(Words::get_words(Flags::max_words), Flags::max_words);

Print::text(test_typing);
Print::text_delimiter();
}
Timer::init(Flags::max_time, &test_typing);

Print::text(test_typing);
Print::text_delimiter();

while (!stop) {
const TypingWord *current_word = test_typing.get_word();
Expand Down

0 comments on commit 9fdd7fe

Please sign in to comment.