Watch
1
0
Fork
You've already forked cli-flag-collection
0
mirror of synced 2026-11-04 20:07:18 +00:00

Unicode is hopefully handled; flag width is not perfectly variable

This commit is contained in:
MysteryBirb 2026-09-14 13:28:05 +02:00
commit 20624c4564

View file

@ -1,4 +1,3 @@
#include <clocale>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -12,10 +11,23 @@
int flagWidth; int flagWidth;
int flagHeight; int flagHeight;
std::string flagChar(" "); std::string flagStr(" ");
void drawFlag(const unsigned char flagArray[][3], unsigned char flagStripes) { void drawFlag(const unsigned char flagArray[][3], unsigned char flagStripes) {
short i, y, j; short i, y, j, uni = 0;
short divLen = flagStr.size();
// Hopefully will handle all unicode things in a string
for (int k : flagStr) {
if (k < 0 && uni != 2) {
uni++;
}
if (uni == 2) {
divLen -= 1;
uni = 0;
}
}
//std::println("Divided length: {0} ({1} / {2})", (flagWidth / divLen), flagWidth, divLen);
for (i = 0; i < flagStripes; i++) { for (i = 0; i < flagStripes; i++) {
if (flagHeight / flagStripes <= 0) { if (flagHeight / flagStripes <= 0) {
@ -23,20 +35,19 @@ void drawFlag(const unsigned char flagArray[][3], unsigned char flagStripes) {
break; break;
} }
for (y = 0; y < flagHeight / flagStripes; y++) { for (y = 0; y < flagHeight / flagStripes; y++) {
if (flagChar != " ") { if (flagStr != " ") {
flagChar.shrink_to_fit(); flagStr.shrink_to_fit();
std::print("\x1b[38;2;{0};{1};{2}m", flagArray[i][0], flagArray[i][1], flagArray[i][2]); std::print("\x1b[38;2;{0};{1};{2}m", flagArray[i][0], flagArray[i][1], flagArray[i][2]);
} }
else { else {
std::print("\x1b[48;2;{0};{1};{2}m", flagArray[i][0], flagArray[i][1], flagArray[i][2]); std::print("\x1b[48;2;{0};{1};{2}m", flagArray[i][0], flagArray[i][1], flagArray[i][2]);
} }
for (j = 0; j < flagWidth; j++) { for (j = 0; j < flagWidth / divLen; j++) {
std::print("{0}", flagChar.at(0)); // Imma leave this how it is before i deal with bs letters like 'ä' std::print("{0}", flagStr);
} }
} }
std::println("\x1b[0m"); std::println("\x1b[0m");
} }
//std::println("Divided length: {0}\nflagWidth: {1}", (flagWidth / flagChar.size()), flagWidth);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -56,7 +67,7 @@ int main(int argc, char **argv) {
{"list", no_argument, nullptr, LIST}, {"list", no_argument, nullptr, LIST},
{"flag", required_argument, nullptr, 'f'}, {"flag", required_argument, nullptr, 'f'},
{"height", required_argument, nullptr, 'h'}, {"height", required_argument, nullptr, 'h'},
{"character", required_argument, nullptr, 'c'}, {"string", required_argument, nullptr, 's'},
{nullptr, 0, nullptr, 0}, {nullptr, 0, nullptr, 0},
}; };
@ -79,7 +90,7 @@ int main(int argc, char **argv) {
short allFlagsSize = sizeof(allFlags) / sizeof(allFlags[0]); short allFlagsSize = sizeof(allFlags) / sizeof(allFlags[0]);
int opt; int opt;
while ((opt = getopt_long(argc, argv, "h:c:f:", long_options, nullptr)) != -1) { while ((opt = getopt_long(argc, argv, "h:s:f:", long_options, nullptr)) != -1) {
switch (opt) { switch (opt) {
case '0': case '0':
std::print("{}", opt); std::print("{}", opt);
@ -93,13 +104,13 @@ int main(int argc, char **argv) {
flagHeight = atoi(argv[optind - 1]); flagHeight = atoi(argv[optind - 1]);
} }
break; break;
case 'c': case 's':
if (argc == 3 || argc == 2) { if (argc == 3 || argc == 2) {
std::cerr << "You can't run '" << argv[1] << "' alone!\n"; std::cerr << "You can't run '" << argv[1] << "' alone!\n";
return 1; return 1;
} }
else { else {
flagChar.insert(0, argv[optind - 1]); // I will keep this at index 0 so it can't even break it for now flagStr = argv[optind - 1]; // I will keep this at index 0 so it can't even break it for now
} }
break; break;
case 'f': case 'f':
@ -149,8 +160,8 @@ int main(int argc, char **argv) {
<< "All options:\n" << "All options:\n"
<< "\t-f, --flag [FLAG]\t\tPrints the chosen flag\n" << "\t-f, --flag [FLAG]\t\tPrints the chosen flag\n"
<< "\t-h, --height [Integer]\t\tPrints the flag with the chosen positive integer\n" << "\t-h, --height [Integer]\t\tPrints the flag with the chosen positive integer\n"
<< "\t-c, --character \'[Char]\'\tPrint the chosen flag with the given character (Only supports ASCII for now)\n" << "\t-s, --string \'[String]\'\t\tPrint the chosen flag with the given string (Flag width is not yet perfectly variable)\n"
<< "\t--list\t\t\tList all available flags\n" << "\t--list\t\t\t\tList all available flags\n"
<< "\t--help\t\t\t\tPrints out the help screen\n"; << "\t--help\t\t\t\tPrints out the help screen\n";
return 0; return 0;
case '?': case '?':