Added means to edit entries in the text interface (#1)

develop
tastytea 2018-10-17 18:30:41 +02:00
parent 79bd6f7d01
commit 6e57c4b6dd
No known key found for this signature in database
GPG Key ID: CFC39497F1B26E07
1 changed files with 211 additions and 109 deletions

View File

@ -22,9 +22,205 @@ using std::cout;
using std::cerr; using std::cerr;
using std::cin; 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<string> 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<string, int, string> &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"; 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; bool keeprunning = true;
cout << "This is whyblocked " << global::version << ".\n"; cout << "This is whyblocked " << global::version << ".\n";
print_help(); help();
while (keeprunning) while (keeprunning)
{ {
string answer = ""; string answer = get_answer("");
cout << ": ";
cin >> answer;
switch (answer[0]) switch (answer[0])
{ {
case 'a': case 'a':
case 'A': case 'A':
{ {
string user, reason; add();
int blocked = -1; break;
cout << "User or instance: "; }
cin >> user; case 'e':
while (blocked == -1) case 'E':
{ {
cout << "Blocked(b) or silenced(s): "; edit();
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;
}
}
break; break;
} }
case 'r': case 'r':
case 'R': case 'R':
{ {
string user; remove();
cout << "User or instance: ";
cin >> user;
if (database::remove(user))
{
cout << user << " removed.\n";
}
break; break;
} }
case 'v': case 'v':
case 'V': case 'V':
{ {
result_view result; view();
if (database::view(result))
{
for (const std::tuple<string, int, string> &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';
}
}
break; break;
} }
case 'd': case 'd':
case 'D': case 'D':
{ {
cout << "User or instance: "; details();
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';
}
}
}
}
break; break;
} }
case 'h': case 'h':
case 'H': case 'H':
{ {
print_help(); help();
break; break;
} }
case 'q': case 'q':