Added means to edit entries in the text interface (#1)
the build was successful Details

This commit is contained in:
tastytea 2018-10-17 18:30:41 +02:00
parent 79bd6f7d01
commit 6e57c4b6dd
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 211 additions and 109 deletions

View File

@ -22,70 +22,47 @@ 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";
cout << "Type help or h to show this help. Type quit or q to quit the program.\n";
string answer;
cout << question << ": ";
std::getline(cin, answer, '\n');
return answer;
}
int main(int argc, char *argv[])
const bool askblocked()
{
bool keeprunning = true;
cout << "This is whyblocked " << global::version << ".\n";
print_help();
while (keeprunning)
{
string answer = "";
cout << ": ";
cin >> 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')
const string blocked = get_answer("Blocked(b) or silenced(s)");
if (blocked[0] == 'b' || blocked[0] == 'B')
{
string url;
cout << "URL: ";
cin >> url;
return true;
}
else if (blocked[0] == 's' || blocked[0] == 'S')
{
return false;
}
}
}
if (database::add_receipt(user, url))
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 (answer[0] == 'n' || answer[0] == 'N')
else if (receipt_yn[0] == 'n' || receipt_yn[0] == 'N')
{
break;
}
@ -94,23 +71,103 @@ int main(int argc, char *argv[])
continue;
}
}
break;
}
case 'r':
case 'R':
const void add()
{
string user;
cout << "User or instance: ";
cin >> user;
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";
}
break;
}
case 'v':
case 'V':
const void view()
{
result_view result;
if (database::view(result))
@ -129,18 +186,16 @@ int main(int argc, char *argv[])
cout << std::get<2>(line) << '\n';
}
}
break;
}
case 'd':
case 'D':
const void details()
{
cout << "User or instance: ";
cin >> answer;
const string user = get_answer("User or instance");
{
result_details result;
if (database::details(answer, result))
if (database::details(user, result))
{
cout << answer << " is ";
cout << user << " is ";
if (std::get<0>(result) == 1)
{
cout << "blocked, because: ";
@ -161,12 +216,59 @@ int main(int argc, char *argv[])
}
}
}
}
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";
}
int main(int argc, char *argv[])
{
bool keeprunning = true;
cout << "This is whyblocked " << global::version << ".\n";
help();
while (keeprunning)
{
string answer = get_answer("");
switch (answer[0])
{
case 'a':
case 'A':
{
add();
break;
}
case 'e':
case 'E':
{
edit();
break;
}
case 'r':
case 'R':
{
remove();
break;
}
case 'v':
case 'V':
{
view();
break;
}
case 'd':
case 'D':
{
details();
break;
}
case 'h':
case 'H':
{
print_help();
help();
break;
}
case 'q':