Skip to content

Commit

Permalink
Just a pinch of refactoring for Console and Controller. Nothing big.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 20, 2016
1 parent 5b762fb commit 52afc19
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
46 changes: 21 additions & 25 deletions app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ string Console::viewsFolder = "..\\views";
Console::Console()
{
this->mode = new char[strlen("live") + 1];
this->lastInput = '\0';
strcpy(this->mode, "live");
this->exit = false;

map<char, string> availableOptions = View::getViewsOptions().find(Console::initialView)->second;
this->previousViews = {};

this->currentView = View(Console::initialView, availableOptions);
this->delay = 2000;

this->loadActions();
this->loadViews(Console::viewsFolder);

if (find(this->loadedViews.begin(), this->loadedViews.end(), this->currentView.getViewName()) != this->loadedViews.end())
{
this->renderView();
this->handleView();
}
else
{
Expand All @@ -36,20 +37,21 @@ Console::Console(char *mode)
{
this->mode = new char[strlen("debug") + 1];
strcpy(this->mode, "debug");
this->lastInput = '\0';
this->exit = false;

string viewName = "debug.view";
map<char, string> availableOptions = View::getViewsOptions().find(Console::initialView)->second;
this->previousViews = {};

this->currentView = View(viewName, availableOptions);
this->delay = 2000;

this->loadActions();
this->loadViews(Console::viewsFolder);

if (find(this->loadedViews.begin(), this->loadedViews.end(), this->currentView.getViewName()) != this->loadedViews.end())
{
this->renderView();
this->handleView();
}
else
{
Expand Down Expand Up @@ -110,15 +112,16 @@ void Console::loadViews(const fs::path &viewsFolder)

void Console::loadActions()
{
// TODO: load actions via config file
vector<char> actions = { 'q', 'b', 'n', 'c' };

this->actions = actions;
}

void Console::renderView()
void Console::handleView()
{
string content;
string path = Console::viewsFolder;
string content,
path = Console::viewsFolder;
path.append("\\").append(this->currentView.getViewName());

stringstream buffer;
Expand All @@ -128,21 +131,15 @@ void Console::renderView()
{
buffer << viewFile.rdbuf();
this->currentView.setRawFormat(buffer.str());

if (Controller::hasInput(buffer.str()))
{
this->theController = Controller(this->currentView.getViewName(), buffer.str(), View::getViewExtension());
}
else
{
cout << buffer.str()
<< endl;
}

buffer.clear();

viewFile.close();
this->theController = Controller(this->currentView.getViewName(), buffer.str(), View::getViewExtension());
}
else
{
cout << buffer.str()
<< endl;
}
buffer.clear();
viewFile.close();
}

void Console::renderNextView()
Expand All @@ -152,11 +149,10 @@ void Console::renderNextView()

// Cache the current view
this->previousViews.push_back(this->currentView);

this->currentView = View(nextView, nextOptions);

clearScreen();
this->renderView();
this->handleView();
}

void Console::renderPreviousView()
Expand All @@ -165,15 +161,15 @@ void Console::renderPreviousView()
{
clearScreen();
this->currentView = this->previousViews.back();
this->renderView();
this->handleView();
this->previousViews.pop_back();
}
}

void Console::reloadView()
{
clearScreen();
this->renderView();
this->handleView();
}

bool Console::takeActionIfAny()
Expand Down
2 changes: 1 addition & 1 deletion app/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Console {
void showPrompt();
unsigned getDelay();

void renderView();
void handleView();
void renderNextView();
void renderPreviousView();
void reloadView();
Expand Down
6 changes: 5 additions & 1 deletion app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ void Controller::prepareView()
{
cout << this->viewChunk.c_str()
<< endl;

// Show for now
// TODO use the input/output format to parse the chunk
}

void Controller::prepareViewInput()
Expand Down Expand Up @@ -42,7 +45,7 @@ Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
this->controllerName = new char[strlen(controllerName.c_str()) + 1];
strcpy(this->controllerName, controllerName.c_str());
this->viewChunk = viewChunk;
this->controllerAttributions = controllerAttributions;
this->controllerAttributions = {};

this->prepareView();
}
Expand All @@ -57,6 +60,7 @@ char *Controller::getControllerName()
return this->controllerName;
}

// One Controller at a time, you expected more?
void Controller::operator=(const Controller &controller)
{
if (controller.controllerName != NULL)
Expand Down
4 changes: 3 additions & 1 deletion app/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ View::View()
{
this->viewName = new char[strlen("NO_VIEW") + 1];
strcpy(this->viewName, "NO_VIEW");
this->availableOptions = {};
}

View::View(string &viewName, map<char, string> &availableOptions)
Expand Down Expand Up @@ -44,6 +45,7 @@ void View::setRawFormat(string &rawFormat)

void View::loadViewsOptions()
{
// TODO: load ViewsOptions via config file
string homeView = "home.view",
loginView = "login.view",
signupView = "signup.view",
Expand Down Expand Up @@ -77,7 +79,7 @@ string View::getViewExtension()
return View::ViewExtenstion;
}

// Prevent multiple assignments of views, i.e. a view can transition only once and cannot split into multiple views.
// The transition can be made only between two views, synchronously.
void View::operator=(const View &view)
{
delete[] this->viewName;
Expand Down

0 comments on commit 52afc19

Please sign in to comment.