Skip to content

Commit

Permalink
Added Ctrl+R shortcut for reset all and re-roll words
Browse files Browse the repository at this point in the history
  • Loading branch information
Iipal committed May 2, 2021
1 parent 9479fca commit 501a1e2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 26 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,24 @@ Typing speed test in terminal written in C++ using ncurses.
- Any other key will start the `typos`.

- While typing:
- <kbd>Tab</kbd>: Fully resetting current typing test(Already typed stats, words, and even timer will be reset). And you will start the test from the beginning.
- <kbd>Ctrl</kbd>+<kbd>C</kbd>, <kbd>Ctrl</kbd>+<kbd>D</kbd>, and <kbd>Esc</kbd>: Now closing the `typos`.
- <kbd>Ctrl</kbd>+<kbd>Backspace</kbd>: Removing current word, or goes to start of the previous.
- <kbd>Backspace</kbd>: Removing current character.
- <kbd>←</kbd>: Moves to the previously typed character.
- <kbd>→</kbd>: Moves to the next character. Works only for already typed characters.
- <kbd>Backspace</kbd>: Removing current character.
- <kbd>Ctrl</kbd>+<kbd>Backspace</kbd>: Removing current word, or goes to start of the previous.
- <kbd>Tab</kbd>: Fully resetting current typing test(Already typed stats, words, and even timer will be reset). And you will start the test from the beginning.
- <kbd>Ctrl</kbd>+<kbd>R</kbd>: Reset everything just like <kbd>Tab</kbd> do, but also re-roll words.
- <kbd>Ctrl</kbd>+<kbd>C</kbd>, <kbd>Ctrl</kbd>+<kbd>D</kbd>, and <kbd>Esc</kbd>: Now closing the `typos`.
- Only printable characters will be prompt to the test (see `man 3 isprint`).
- Any other input will be ignored.

- Stats Screen:
- <kbd>Ctrl</kbd>+<kbd>C</kbd>, <kbd>Ctrl</kbd>+<kbd>D</kbd>, and <kbd>Esc</kbd>: Now closing the `typos`.
- <kbd>Ctrl</kbd>+<kbd>S</kbd>: Saves your typing test result to `./typos.log` file. By appending new test data to the end of the file and with the current local date at the top of each result.
- <kbd>Tab</kbd>: Restart the test.
- <kbd>Ctrl</kbd>+<kbd>R</kbd>: Restart the test and re-roll the words.

> !! IMPORTANT !!; Because of reasons of handling the "Escape Sequences" the <kbd>Esc</kbd>-key by itself has a delay in 1 sec. My advice is to use <kbd>Ctrl</kbd>+<kbd>C</kbd>, and <kbd>Ctrl</kbd>+<kbd>D</kbd> keybindgds instead of <kbd>Esc</kbd> whatever it's possible. The <kbd>Esc</kbd> is not removed because for some peoples the <kbd>Esc</kbd>-key may be a more comfortable way to close the program.
> !! IMPORTANT !!; My advice is to use <kbd>Ctrl</kbd>+<kbd>C</kbd>, and <kbd>Ctrl</kbd>+<kbd>D</kbd> keybindings instead of <kbd>Esc</kbd> whatever it's possible. Because of reasons of handling the "Escape Sequences" the <kbd>Esc</kbd>-key by itself has a delay in 1 sec.
>> The <kbd>Esc</kbd> is not removed because for some peoples the <kbd>Esc</kbd>-key may be a more comfortable way to close the program.
***

Expand Down
11 changes: 10 additions & 1 deletion includes/Typing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ class Typing : public TypingStats {
bool validate_input(int input, TypingWord *word);

void reset(void);
void new_words(void);

TypingWord **get_words(void) const;

TypingWord *get_word(void) const;
TypingWord *get_word(size_t pos) const;
TypingWord *get_prev_word(void) const;
TypingWord *get_next_word(void) const;
TypingWord *get_prev_word(void) const;

TypingChar get_char_at(void) const;
TypingChar get_char_at(size_t ch_pos) const;
TypingChar get_char_at(size_t ch_pos, size_t w_pos) const;

size_t get_length(void) const;
size_t get_current_word_pos(void) const;
Expand All @@ -34,4 +39,8 @@ class Typing : public TypingStats {
TypingWord **words;
size_t length;
size_t current_word_pos;

void _new_words(const std::vector<std::string> strings,
size_t strings_length);
void _delete_words(void);
};
1 change: 1 addition & 0 deletions includes/TypingKeys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct TypingKeys {
static const unsigned char KEY_CTRL_BACKSPACE = 0x8;
static const unsigned char KEY_TAB = 0x9;
static const unsigned char KEY_NEW_LINE = 0x0A;
static const unsigned char KEY_CTRL_R = 0x12;
static const unsigned char KEY_CTRL_S = 0x13;
static const unsigned char KEY_ESC = 0x1B;

Expand Down
6 changes: 3 additions & 3 deletions srcs/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ void Timer::break_the_words(void) {
break;
}

case TypingKeys::KEY_CTRL_R:
Timer::_typing->new_words();
case TypingKeys::KEY_TAB: {
clean_lines();
Timer::_typing->reset();
Timer::_typing->reset_stats();

box(stdscr, 0, 0);
Timer::init(Flags::max_time);
Print::render_all(*Timer::_typing);

Timer::init(Flags::max_time);
stop = true;
break;
}
Expand Down
27 changes: 24 additions & 3 deletions srcs/Typing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

Typing::Typing(const std::vector<std::string> strings, size_t strings_length)
: TypingStats(), length(strings_length), current_word_pos(0) {
this->_new_words(strings, strings_length);
}

void Typing::_new_words(const std::vector<std::string> strings,
size_t strings_length) {
try {
this->words = new TypingWord *[strings_length + 1];

Expand All @@ -27,13 +31,16 @@ Typing::Typing(const std::vector<std::string> strings, size_t strings_length)
exit(EXIT_FAILURE);
}
}
Typing::~Typing() {

Typing::~Typing() { this->_delete_words(); }
void Typing::_delete_words(void) {
if (this->words) {
for (size_t i = 0; this->words[i]; ++i) {
delete this->words[i];
}
delete[] this->words;
}
this->words = NULL;
}

void Typing::iterate(void) {
Expand Down Expand Up @@ -190,7 +197,7 @@ bool Typing::validate_input(int input, TypingWord *const word) {
void Typing::reset(void) {
Print::clean_input();
this->current_word_pos = 0;
for (size_t i = 0; this->length > i; ++i) {
for (size_t i = 0; this->words[i]; ++i) {
TypingWord *word = this->words[i];
const size_t word_length = word->get_length();

Expand All @@ -202,6 +209,11 @@ void Typing::reset(void) {
}
}

void Typing::new_words(void) {
this->_delete_words();
this->_new_words(Words::get_words(Flags::max_words), Flags::max_words);
}

TypingWord **Typing::get_words(void) const { return this->words; }

TypingWord *Typing::get_word(void) const {
Expand All @@ -220,7 +232,6 @@ TypingWord *Typing::get_prev_word(void) const {

return out;
}

TypingWord *Typing::get_next_word(void) const {
TypingWord *out = NULL;

Expand All @@ -231,6 +242,16 @@ TypingWord *Typing::get_next_word(void) const {
return out;
}

TypingChar Typing::get_char_at(void) const {
return this->get_word()->get_char_at();
};
TypingChar Typing::get_char_at(size_t ch_pos) const {
return this->get_char_at(ch_pos, this->current_word_pos);
}
TypingChar Typing::get_char_at(size_t ch_pos, size_t w_pos) const {
return this->get_word(w_pos)->get_char_at(ch_pos);
}

size_t Typing::get_length(void) const { return this->length; }
size_t Typing::get_current_word_pos(void) const {
return this->current_word_pos;
Expand Down
6 changes: 3 additions & 3 deletions srcs/TypingKeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ chtype TypingKeys::get_input(void) {

bool TypingKeys::is_valid_input_key(chtype input) { return _valid_keys[input]; }

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc99-designator"

const bool TypingKeys::_valid_keys[KEY_MAX] = {
Expand All @@ -28,8 +27,10 @@ const bool TypingKeys::_valid_keys[KEY_MAX] = {
[TypingKeys::KEY_CTRL_BACKSPACE] = true,
[TypingKeys::KEY_TAB] = true,
[TypingKeys::KEY_NEW_LINE] = true,
[TypingKeys::KEY_CTRL_R] = true,
[TypingKeys::KEY_CTRL_S] = true,
[TypingKeys::KEY_ESC] = true,

[TypingKeys::KEY_SPACE] = true,
[TypingKeys::KEY_EXC_MARK] = true,
[TypingKeys::KEY_QUOTE] = true,
Expand Down Expand Up @@ -125,9 +126,8 @@ const bool TypingKeys::_valid_keys[KEY_MAX] = {
[TypingKeys::KEY_VERTICAL_SLASH] = true,
[TypingKeys::KEY_CRL_BRACKET_CLOSE] = true,
[TypingKeys::KEY_TILDE] = true,

[TypingKeys::KEY_ARROW_LEFT] = true,
[TypingKeys::KEY_ARROW_RIGHT] = true,
[TypingKeys::KEY_DEL] = true,
};

#pragma clang diagnostic pop
23 changes: 12 additions & 11 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ int main(int argc, char *argv[]) {
Print::render_all(test_typing);

while (!stop) {
const TypingWord *current_word = test_typing.get_word();
const TypingChar current_char = current_word->get_char_at();
const chtype current_ch = current_char;

Print::current_char(current_char);
Print::current_char(test_typing.get_char_at());
Print::input_word(test_typing);

input = TypingKeys::get_input();
Expand All @@ -62,11 +58,13 @@ int main(int argc, char *argv[]) {
break;

case TypingKeys::KEY_DEL:
Print::clear_current_char(current_char);
Print::clear_current_char(test_typing.get_char_at());
test_typing.backspace();
Print::clear_current_char(test_typing.get_word()->get_char_at());
Print::clear_current_char(test_typing.get_char_at());
break;

case TypingKeys::KEY_CTRL_R:
test_typing.new_words();
case TypingKeys::KEY_TAB:
test_typing.reset();
test_typing.reset_stats();
Expand All @@ -75,12 +73,12 @@ int main(int argc, char *argv[]) {
break;

case TypingKeys::KEY_ARROW_LEFT:
Print::current_char(current_char, 0);
Print::current_char(test_typing.get_char_at(), 0);
test_typing.move_to_prev_ch();
break;

case TypingKeys::KEY_ARROW_RIGHT:
Print::current_char(current_char, 0);
Print::current_char(test_typing.get_char_at(), 0);
test_typing.move_to_next_ch();
break;

Expand All @@ -93,8 +91,11 @@ int main(int argc, char *argv[]) {
default:
is_input_ok = test_typing.validate_input(input);

Print::current_char(current_word->get_char_at(), 0);
if (current_ch || (!current_ch && is_input_ok)) {
const TypingChar ch = test_typing.get_char_at();
Print::current_char(ch, 0);

const char _ch = ch;
if (_ch || (!_ch && is_input_ok)) {
test_typing.iterate();
}
}
Expand Down

0 comments on commit 501a1e2

Please sign in to comment.