From d05c547bfa34c7da036635bc88e9620908579304 Mon Sep 17 00:00:00 2001 From: denes Date: Thu, 19 Jan 2017 12:31:24 +0100 Subject: [PATCH] moving out some string utility funtions as free functions in the anonym namespace --- graph_browser.cpp | 79 +++++++++++++++++++++++------------------------ graph_browser.hpp | 7 ++--- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/graph_browser.cpp b/graph_browser.cpp index ada3b47..8adffb0 100644 --- a/graph_browser.cpp +++ b/graph_browser.cpp @@ -1,12 +1,35 @@ -#include -#include - -#include - #include #include "graph_browser.hpp" +namespace { + std::string historyToString(const std::deque& d, size_t max_length) + { + if (d.empty()) + return std::string(); + + std::string s(d.back()); + for (auto rit = d.crbegin()+1; rit != d.rend(); ++rit) + if (s.length() + (*rit).length() + 3 < max_length) + s.insert(0, std::string(*rit) + " | "); + + return s; + } + + std::string toCommaSeparatedList(const std::vector& v) + { + std::string s; + for (size_t i = 0; i < v.size()-1; ++i) { + s += v[i]; + s += ","; + } + s += v.back(); + return s; + } + +} + + void GraphBrowser::init() { initscr(); @@ -91,11 +114,11 @@ void GraphBrowser::mainLoop() switch(c) { case KEY_DOWN: menu_driver(menu_, REQ_DOWN_ITEM); - update_neighbours(); + updateNeighbours(); break; case KEY_UP: menu_driver(menu_, REQ_UP_ITEM); - update_neighbours(); + updateNeighbours(); break; case KEY_LEFT: { if (history_.size() == 1) @@ -103,15 +126,15 @@ void GraphBrowser::mainLoop() history_.pop_back(); const std::string prev = history_.back(); - update_current(prev); - update_neighbours(); + updateCurrent(prev); + updateNeighbours(); break; } case KEY_RIGHT: { std::string next = item_name(current_item(menu_)); history_.push_back(next); - update_current(next); - update_neighbours(); + updateCurrent(next); + updateNeighbours(); break; } case KEY_NPAGE: @@ -131,21 +154,21 @@ void GraphBrowser::mainLoop() void GraphBrowser::setCurrentVertex(const std::string& s) { history_.push_back(s); - update_current(s); + updateCurrent(s); } -void GraphBrowser::update_current(const std::string& s) +void GraphBrowser::updateCurrent(const std::string& s) { const std::vector& n = graph_.neighboursOf(s); addItems(n); mvprintw(0, 0, "%s",std::string(TERM_MAX_X,' ').c_str()); - mvprintw(0, 0, historyToString().c_str()); + mvprintw(0, 0, historyToString(history_, TERM_MAX_X).c_str()); refresh(); } -void GraphBrowser::update_neighbours() +void GraphBrowser::updateNeighbours() { const size_t n_width = n_window_width-1; @@ -161,7 +184,7 @@ void GraphBrowser::update_neighbours() mvwprintw(n_win, i+1, 1, std::string(n[i], 0, std::min(n[i].length(), n_width)).c_str()); const std::vector& n_of_n = graph_.neighboursOf(n[i]); - const std::string n_of_n_string = neighboursToString(n_of_n); + const std::string n_of_n_string = toCommaSeparatedList(n_of_n); mvwprintw(n_of_n_win_, i+1, 2, std::string(n_of_n_string, 0, std::min(n_of_n_string.length(), n_of_n_width)).c_str()); } @@ -189,27 +212,3 @@ void GraphBrowser::addItems(const std::vector& stringVector) post_menu(menu_); wrefresh(current_win_); } - -std::string GraphBrowser::historyToString() const -{ - if (history_.empty()) - return std::string(); - - std::string s(history_.back()); - for (auto rit = history_.crbegin()+1; rit != history_.rend(); ++rit) - if (s.length() + (*rit).length() + 3 < TERM_MAX_X) - s.insert(0, std::string(*rit) + " | "); - - return s; -} - -std::string GraphBrowser::neighboursToString(const std::vector& n) const -{ - std::string s; - for (size_t i = 0; i < n.size()-1; ++i) { - s += n[i]; - s += ","; - } - s += n.back(); - return s; -} \ No newline at end of file diff --git a/graph_browser.hpp b/graph_browser.hpp index 0f2b652..fa97188 100644 --- a/graph_browser.hpp +++ b/graph_browser.hpp @@ -29,13 +29,10 @@ public: private: - void update_current(const std::string& s); - void update_neighbours(); + void updateCurrent(const std::string& s); + void updateNeighbours(); void addItems(const std::vector& stringVector); - std::string historyToString() const; - std::string neighboursToString(const std::vector& n) const; - MENU *menu_; WINDOW *current_win_, *n_win, * n_of_n_win_; ITEM **items_;