Skip to content

Commit

Permalink
Text wraps
Browse files Browse the repository at this point in the history
  • Loading branch information
EmperorPenguin18 committed Nov 24, 2021
1 parent cd9f764 commit 6bace9e
Showing 1 changed file with 107 additions and 35 deletions.
142 changes: 107 additions & 35 deletions scrycli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,89 @@
#include <dlfcn.h>
#include <iostream>
#include <libscry.h>
#include <sys/ioctl.h>
#include <regex>
#define SPACE 35
#define GAP 10

using namespace std;

struct cardstruct {
string name;
string mana_cost;
string type_line;
string oracle_text;
string power;
string toughness;
string name;
string mana_cost;
string type_line;
string oracle_text;
string power;
string toughness;
};

string linesize(string line, int size) {
string output = "";
while (line.length() > size) {
int num = line.find(' ', size);
if (num < 0) break;
output += line.substr(0, num+1) + "\n";
line = line.substr(num+1, line.length()-num-1);
}
output += line;
return output;
}

string format(cardstruct card) {
string output = card.name + "\t" +
card.mana_cost + "\n";
output += card.type_line + "\n";
output += card.oracle_text + "\n";
string tab(GAP, ' ');
regex dash("");
string output = linesize(card.name + tab +
card.mana_cost, SPACE) + "\n";
output += linesize(card.type_line, SPACE) + "\n";
output += linesize(card.oracle_text, SPACE) + "\n";
if (card.power.size() > 0)
output += "\t\t" + card.power + "/" +
output += tab + tab + card.power + "/" +
card.toughness + "\n";
output = regex_replace(output, dash, "-");
return output;
}

vector<string> explode(const string& str, const char& ch) {
string next;
vector<string> result;
for (string::const_iterator it = str.begin(); it != str.end(); it++) {
if (*it == ch) {
if (!next.empty()) {
result.push_back(next);
next.clear();
}
} else next += *it;
}
if (!next.empty()) result.push_back(next);
return result;
}

size_t maxsize(vector<vector<string>> input) {
size_t output = 0;
for (int i = 0; i < input.size(); i++) {
if (input[i].size() > output) output = input[i].size();
}
return output;
}

string combine(vector<string> strs) {
vector<vector<string>> exploded;
for (int i = 0; i < strs.size(); i++) {
exploded.push_back(explode(strs[i], '\n'));
}
string output = "";
for (int i = 0; i < maxsize(exploded); i++) {
for (int j = 0; j < exploded.size(); j++) {
if (exploded[j].size() == i) exploded[j].push_back("");
int num = SPACE+GAP-exploded[j][i].length();
string * whitespace;
if (num > 0) whitespace = new string(num, ' ');
else whitespace = new string("");
output.append(exploded[j][i] + *whitespace);
delete(whitespace);
}
output += "\n";
}
return output;
}

Expand All @@ -40,31 +103,40 @@ int main(int argc, char **argv)
Scry* scry = (Scry*)create();
List * list = scry->cards_search_cache(argv[1]);
vector<Card *> cards = list->allcards();
for (int i = 0; i < cards.size(); i++) {
if (cards[i]->dual_sided()) {
vector<Card *> splitcard = scry->split(cards[i]);
cardstruct card;
for (int j = 0; j < splitcard.size(); j++) {
card.name = splitcard[j]->name();
card.mana_cost = splitcard[j]->mana_cost();
card.type_line = splitcard[j]->type_line();
card.oracle_text = splitcard[j]->oracle_text();
card.power = splitcard[j]->power();
card.toughness = splitcard[j]->toughness();
if (j != splitcard.size()-1)
cout << format(card) << "//\n";
else cout << format(card) << endl;
}
} else {
cardstruct card;
card.name = cards[i]->name();
card.mana_cost = cards[i]->mana_cost();
card.type_line = cards[i]->type_line();
card.oracle_text = cards[i]->oracle_text();
card.power = cards[i]->power();
card.toughness = cards[i]->toughness();
cout << format(card) << endl;
struct winsize w;
for (int i = 0; i < cards.size(); i+=(w.ws_col/(SPACE+GAP)) ) {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
vector<string> cardstr;
for (int j = 0; j < w.ws_col/(SPACE+GAP); j++) {
if (i+j == cards.size()) break;
if (cards[i+j]->dual_sided()) {
vector<Card *> splitcard = scry->split(cards[i+j]);
cardstruct card;
string output = "";
for (int k = 0; k < splitcard.size(); k++) {
card.name = splitcard[k]->name();
card.mana_cost = splitcard[k]->mana_cost();
card.type_line = splitcard[k]->type_line();
card.oracle_text = splitcard[k]->oracle_text();
card.power = splitcard[k]->power();
card.toughness = splitcard[k]->toughness();
if (k != splitcard.size()-1)
output += format(card) + "//\n";
else output += format(card) + "\n";
}
cardstr.push_back(output);
} else {
cardstruct card;
card.name = cards[i+j]->name();
card.mana_cost = cards[i+j]->mana_cost();
card.type_line = cards[i+j]->type_line();
card.oracle_text = cards[i+j]->oracle_text();
card.power = cards[i+j]->power();
card.toughness = cards[i+j]->toughness();
cardstr.push_back(format(card) + "\n");
}
}
cout << combine(cardstr) << endl << endl;
}
destroy( scry );
}
Expand Down

0 comments on commit 6bace9e

Please sign in to comment.