Skip to content

Commit

Permalink
Sleep helper function and check if the view can be opened in the render.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 18, 2016
1 parent abdf64e commit fd4bbdf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
19 changes: 10 additions & 9 deletions app/Bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Windows.h>
#include <conio.h>

#include "Helpers.h"
#include "ClassContainer.h"

using namespace std;
Expand All @@ -21,30 +22,30 @@ void main()
console.setLastInput(_getch());
cout << console.getLastInput()
<< endl;
//console.renderNextView();
}
catch (const invalid_argument &e)
{
cout << endl
<< e.what()
<< endl;

Sleep(console.getDelay());

// Clear the keyboard buffer during the sleep cycle (if there are any keys pressed),
// so as long as the keyboard is hit during that period, the input won't be taken into consideration.
while (_kbhit())
{
_getch();
}
sleepAndClearBuffer(console.getDelay());

console.reloadView();
}
catch (const system_error &e)
{
cout << e.code()
<< " "
<< e.what()
<< endl;
}
} while (console.getLastInput() != 'q');
}
catch (const invalid_argument &e)
{
cout << e.what()
<< endl;
}

}
6 changes: 5 additions & 1 deletion app/ClassContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ unsigned User::getUuid()

//---View---
//----------
map<string, map<char, string>> View::ViewsOptions = {{ "", {{'\0', ""}} }};
map<string, map<char, string>> View::ViewsOptions = { { "", {{'\0', ""}} } };

View::View(string viewName, map<char, string> availableOptions, bool hasInterpolation)
{
Expand Down Expand Up @@ -175,6 +175,10 @@ void Console::renderView(View &view)
<< endl;
buffer.clear();
}
else
{
throw system_error(error_code(500, system_category()), "The view stream couldn't be opened.");
}

viewFile.close();
}
Expand Down
20 changes: 17 additions & 3 deletions app/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <iostream>
#include <vector>
#include <map>
#include <Windows.h>
#include <conio.h>

#include "Helpers.h"

Expand All @@ -9,17 +11,29 @@ bool isInCharStringMap(const map<char, string> &haystack, char needle)
return haystack.find(needle) != haystack.end();
}

bool isInCharVector(vector<char> haystack, char needle)
bool isInCharVector(const vector<char> &haystack, char needle)
{
return find(haystack.begin(), haystack.end(), needle) != haystack.end();
}

bool isInIntVector(vector<int> haystack, int needle)
bool isInIntVector(const vector<int> &haystack, int needle)
{
return find(haystack.begin(), haystack.end(), needle) != haystack.end();
}

bool isInStringVector(vector<string> haystack, string needle)
bool isInStringVector(const vector<string> &haystack, string needle)
{
return find(haystack.begin(), haystack.end(), needle) != haystack.end();
}

void sleepAndClearBuffer(unsigned delay)
{
Sleep(delay);

// Clear the keyboard buffer during the sleep cycle (if there are any keys pressed),
// so as long as the keyboard is hit during that period, the input won't be taken into consideration.
while (_kbhit())
{
_getch();
}
}
4 changes: 3 additions & 1 deletion app/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <iostream>
#include <vector>
#include <map>

using namespace std;

bool isInCharStringMap(const map<char, string> &, char);
bool isInCharVector(const vector<char> &, char);
bool isInIntVector(const vector<int> &, int);
bool isInStringVector(const vector<string> &, string);
bool isInStringVector(const vector<string> &, string);
void sleepAndClearBuffer(unsigned delay);

0 comments on commit fd4bbdf

Please sign in to comment.