From 6e57c4b6dddbb68e9b8f4d6ce4ddbbe4a15bacc7 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 17 Oct 2018 18:30:41 +0200 Subject: [PATCH] Added means to edit entries in the text interface (#1) --- src/interface_text.cpp | 320 +++++++++++++++++++++++++++-------------- 1 file changed, 211 insertions(+), 109 deletions(-) diff --git a/src/interface_text.cpp b/src/interface_text.cpp index 6fe102a..c738cf2 100644 --- a/src/interface_text.cpp +++ b/src/interface_text.cpp @@ -22,9 +22,205 @@ using std::cout; using std::cerr; using std::cin; -const void print_help() +const string get_answer(const string &question) { - cout << "Type add, remove, view or details. Or just the first letter.\n"; + string answer; + + cout << question << ": "; + std::getline(cin, answer, '\n'); + + return answer; +} + +const bool askblocked() +{ + while (true) + { + const string blocked = get_answer("Blocked(b) or silenced(s)"); + if (blocked[0] == 'b' || blocked[0] == 'B') + { + return true; + } + else if (blocked[0] == 's' || blocked[0] == 'S') + { + return false; + } + } +} + +const void askrecipes(const string &user) +{ + while (true) + { + const string receipt_yn = get_answer("Add receipt? [y/n]"); + if (receipt_yn[0] == 'y' || receipt_yn[0] == 'Y') + { + const string receipt = get_answer("Receipt"); + + if (database::add_receipt(user, receipt)) + { + cout << "Receipt added.\n"; + } + } + else if (receipt_yn[0] == 'n' || receipt_yn[0] == 'N') + { + break; + } + else + { + continue; + } + } +} + +const void add() +{ + const string user = get_answer("User or instance"); + int blocked; + + if (askblocked()) + { + blocked = 1; + } + else + { + blocked = 0; + } + const string reason = get_answer("Reason"); + + if (database::add_block(user, blocked, reason)) + { + cout << user << " added.\n"; + } + + askrecipes(user); +} + +const void edit() +{ + result_details olddata; + const string olduser = get_answer("User or instance"); + if (database::details(olduser, olddata)) + { + cout << "A blank line keeps the former value.\n"; + string newuser = get_answer("Change user or instance to"); + if (newuser.empty()) + { + newuser = olduser; + } + + int blocked; + if (askblocked()) + { + blocked = 1; + } + else + { + blocked = 0; + } + + cout << "Old reason was: " << std::get<1>(olddata) << '\n'; + string newreason = get_answer("Change reason to"); + if (newreason.empty()) + { + newreason = std::get<1>(olddata); + } + + std::vector newreceipts; + for (const string &oldreceipt : std::get<2>(olddata)) + { + cout << "Old receipt was: " << oldreceipt << '\n'; + string newreceipt = get_answer("Change receipt to"); + if (newreceipt.empty()) + { + newreceipt = oldreceipt; + } + newreceipts.push_back(newreceipt); + } + + if (database::remove(olduser)) + { + database::add_block(newuser, blocked, newreason); + if (!newreceipts.empty()) + { + for (const string &newreceipt : newreceipts) + { + database::add_receipt(newuser, newreceipt); + } + } + askrecipes(newuser); + } + else + { + cerr << "Could not remove " << olduser << ".\n"; + } + } +} + +const void remove() +{ + const string user = get_answer("User or instance"); + + if (database::remove(user)) + { + cout << user << " removed.\n"; + } +} + +const void view() +{ + result_view result; + if (database::view(result)) + { + for (const std::tuple &line : result) + { + if (std::get<1>(line) == 1) + { + cout << " Blocked: "; + } + else + { + cout << "Silenced: "; + } + cout << std::get<0>(line) << " because: "; + cout << std::get<2>(line) << '\n'; + } + } +} + +const void details() +{ + const string user = get_answer("User or instance"); + { + result_details result; + if (database::details(user, result)) + { + cout << user << " is "; + if (std::get<0>(result) == 1) + { + cout << "blocked, because: "; + } + else if (std::get<0>(result) == 0) + { + cout << "silenced, because: "; + } + cout << std::get<1>(result) << '\n'; + + if (!std::get<2>(result).empty()) + { + cout << "Receipts:\n"; + for (const string &url : std::get<2>(result)) + { + cout << " " << url << '\n'; + } + } + } + } +} + +const void help() +{ + cout << "Type add, edit, remove, view or details. Or just the first letter.\n"; cout << "Type help or h to show this help. Type quit or q to quit the program.\n"; } @@ -33,140 +229,46 @@ int main(int argc, char *argv[]) bool keeprunning = true; cout << "This is whyblocked " << global::version << ".\n"; - print_help(); + help(); while (keeprunning) { - string answer = ""; - cout << ": "; - cin >> answer; + string answer = get_answer(""); switch (answer[0]) { case 'a': case 'A': { - string user, reason; - int blocked = -1; - cout << "User or instance: "; - cin >> user; - while (blocked == -1) - { - cout << "Blocked(b) or silenced(s): "; - cin >> answer; - if (answer[0] == 'b' || answer[0] == 'B') - { - blocked = 1; - } - else if (answer[0] == 's' || answer[0] == 'S') - { - blocked = 0; - } - } - cout << "Reason: "; - cin.ignore(); - std::getline(cin, reason, '\n'); - - if (database::add_block(user, blocked, reason)) - { - cout << user << " added.\n"; - } - - while (true) - { - cout << "Add receipt? [y/n] "; - cin >> answer; - if (answer[0] == 'y' || answer[0] == 'Y') - { - string url; - cout << "URL: "; - cin >> url; - - if (database::add_receipt(user, url)) - { - cout << "Receipt added.\n"; - } - } - else if (answer[0] == 'n' || answer[0] == 'N') - { - break; - } - else - { - continue; - } - } + add(); + break; + } + case 'e': + case 'E': + { + edit(); break; } case 'r': case 'R': { - string user; - cout << "User or instance: "; - cin >> user; - - if (database::remove(user)) - { - cout << user << " removed.\n"; - } + remove(); break; } case 'v': case 'V': { - result_view result; - if (database::view(result)) - { - for (const std::tuple &line : result) - { - if (std::get<1>(line) == 1) - { - cout << " Blocked: "; - } - else - { - cout << "Silenced: "; - } - cout << std::get<0>(line) << " because: "; - cout << std::get<2>(line) << '\n'; - } - } + view(); break; } case 'd': case 'D': { - cout << "User or instance: "; - cin >> answer; - { - result_details result; - if (database::details(answer, result)) - { - cout << answer << " is "; - if (std::get<0>(result) == 1) - { - cout << "blocked, because: "; - } - else if (std::get<0>(result) == 0) - { - cout << "silenced, because: "; - } - cout << std::get<1>(result) << '\n'; - - if (!std::get<2>(result).empty()) - { - cout << "Receipts:\n"; - for (const string &url : std::get<2>(result)) - { - cout << " " << url << '\n'; - } - } - } - } + details(); break; } case 'h': case 'H': { - print_help(); + help(); break; } case 'q':