From 52afc19755d1da27b38cd41b989f3cedcdf30c2f Mon Sep 17 00:00:00 2001 From: Cristian-Adrian Frasineanu Date: Sun, 20 Nov 2016 13:30:56 +0200 Subject: [PATCH] Just a pinch of refactoring for Console and Controller. Nothing big. --- app/Console.cpp | 46 +++++++++++++++++++++------------------------- app/Console.h | 2 +- app/Controller.cpp | 6 +++++- app/View.cpp | 4 +++- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/app/Console.cpp b/app/Console.cpp index 6ed44b1..c08715e 100644 --- a/app/Console.cpp +++ b/app/Console.cpp @@ -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 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 { @@ -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 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 { @@ -110,15 +112,16 @@ void Console::loadViews(const fs::path &viewsFolder) void Console::loadActions() { + // TODO: load actions via config file vector 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; @@ -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() @@ -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() @@ -165,7 +161,7 @@ void Console::renderPreviousView() { clearScreen(); this->currentView = this->previousViews.back(); - this->renderView(); + this->handleView(); this->previousViews.pop_back(); } } @@ -173,7 +169,7 @@ void Console::renderPreviousView() void Console::reloadView() { clearScreen(); - this->renderView(); + this->handleView(); } bool Console::takeActionIfAny() diff --git a/app/Console.h b/app/Console.h index cae0a34..4da6dc9 100644 --- a/app/Console.h +++ b/app/Console.h @@ -45,7 +45,7 @@ class Console { void showPrompt(); unsigned getDelay(); - void renderView(); + void handleView(); void renderNextView(); void renderPreviousView(); void reloadView(); diff --git a/app/Controller.cpp b/app/Controller.cpp index b0f5289..a8f2fee 100644 --- a/app/Controller.cpp +++ b/app/Controller.cpp @@ -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() @@ -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(); } @@ -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) diff --git a/app/View.cpp b/app/View.cpp index 8a9b44d..adfcaf7 100644 --- a/app/View.cpp +++ b/app/View.cpp @@ -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 &availableOptions) @@ -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", @@ -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;