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

Little variable length fix

This commit is contained in:
MysteryBirb 2026-10-05 11:13:18 +02:00
commit 6c1322c63d
4 changed files with 170 additions and 161 deletions

View file

@ -1,8 +1,8 @@
CC=g++ CC=g++
SRC=cli-flag-collection.cpp SRC=cli-flag-collection.cpp
dynamic-dev: debug-dev:
${CC} ${SRC} -Wall -march=native -std=c++23 -o cfc-cli-dynamic ${CC} ${SRC} -Wall -march=native -DDEBUG -std=c++23 -o cfc-cli-debug
static: static:
${CC} ${SRC} -Wall -Werror -march=native -O3 -flto -fomit-frame-pointer -static -std=c++23 -o cfc-cli-static ${CC} ${SRC} -Wall -Werror -march=native -O3 -flto -fomit-frame-pointer -DNDEBUG -static -std=c++23 -o cfc-cli-static
release: release:
${CC} ${SRC} -Wall -Werror -march=native -O3 -flto -fomit-frame-pointer -std=c++23 -o cfc-cli-release ${CC} ${SRC} -Wall -Werror -march=native -O3 -flto -fomit-frame-pointer -DNDEBUG -std=c++23 -o cfc-cli-release

View file

@ -24,26 +24,26 @@ For now my program is not in any repo so you have to build it from source.
Firstly you pull this repo with: Firstly you pull this repo with:
``` ```bash
git clone https://codeberg.org/MysteryBirb/cli-flag-collection.git git clone https://codeberg.org/MysteryBirb/cli-flag-collection.git
``` ```
then cd into it: then cd into it:
``` ```bash
cd cli-flag-collection cd cli-flag-collection
``` ```
To compile the dynamic version without any optimizations you just run `make`. To compile the dynamic version without any optimizations you just run `make`.
For the release and release static build you can run: For the release and release static build you can run:
``` ```bash
make static make static
``` ```
or or (recommended)
``` ```bash
make release make release
``` ```

BIN
cfc-cli-debug Executable file

Binary file not shown.

View file

@ -1,33 +1,42 @@
#include <cstdlib> #include <cstdlib>
#include <iostream>
#include <string>
#include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <sys/ioctl.h> #include <iostream>
#include <print> #include <print>
#include <pthread.h>
#include <string>
#include <sys/ioctl.h>
#include <unistd.h>
#ifdef DEBUG
#define DEBUG_PRINT(x) std::print x
#else
#define DEBUG_PRINT(x)
#endif
#define HELP 1000 #define HELP 1000
#define LIST 1001 #define LIST 1001
int flagWidth; int flagWidth = 0;
int flagHeight; int flagHeight = 0;
std::string flagStr(" "); 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, uni = 0; short i, y, j, uni = 0;
short rest = 0;
short divLen = flagStr.size(); short divLen = flagStr.size();
// Hopefully will handle all unicode things in a string // Hopefully will handle all unicode things in a string
for (int k : flagStr) { for (int k : flagStr) {
if (k < 0 && uni != 2) { if (k < 0 && uni != 2) {
uni++; uni += 1;
} }
if (uni == 2) { if (uni == 2) {
divLen -= 1; divLen -= 1;
uni = 0; uni = 0;
} }
} }
//std::println("Divided length: {0} ({1} / {2})", (flagWidth / divLen), flagWidth, divLen); rest = (flagWidth % divLen);
DEBUG_PRINT(("Pre: {0}, After {1}\n", flagWidth, (flagWidth % divLen)));
for (i = 0; i < flagStripes; i++) { for (i = 0; i < flagStripes; i++) {
if (flagHeight / flagStripes <= 0) { if (flagHeight / flagStripes <= 0) {
@ -42,9 +51,12 @@ void drawFlag(const unsigned char flagArray[][3], unsigned char flagStripes) {
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 / divLen; j++) { for (j = 0; j < (flagWidth / divLen); j++) {
std::print("{0}", flagStr); std::print("{0}", flagStr);
} }
for (short l = 0; l < rest; l++) {
std::print("{0}", flagStr.at(l));
}
} }
std::println("\x1b[0m"); std::println("\x1b[0m");
} }
@ -57,10 +69,7 @@ int main(int argc, char **argv) {
* - add more flags for inclusivity * - add more flags for inclusivity
*/ */
//std::setlocale(LC_CTYPE, "de_DE.utf8"); static struct option long_options[]{
//std::locale::global(std::locale("de_DE.utf8"));
static struct option long_options[] {
{"help", no_argument, nullptr, HELP}, {"help", no_argument, nullptr, HELP},
{"list", no_argument, nullptr, LIST}, {"list", no_argument, nullptr, LIST},
{"flag", required_argument, nullptr, 'f'}, {"flag", required_argument, nullptr, 'f'},
@ -80,10 +89,11 @@ int main(int argc, char **argv) {
const unsigned char lesbianFlag[5][3] = {{212, 44, 0}, {253, 152, 85}, {255, 255, 255}, {209, 97, 162}, {162, 1, 97}}; const unsigned char lesbianFlag[5][3] = {{212, 44, 0}, {253, 152, 85}, {255, 255, 255}, {209, 97, 162}, {162, 1, 97}};
const unsigned char dutchFlag[3][3] = {{220, 16, 46}, {255, 255, 255}, {0, 61, 165}}; const unsigned char dutchFlag[3][3] = {{220, 16, 46}, {255, 255, 255}, {0, 61, 165}};
const unsigned char biFlag[3][3] = {{214, 2, 112}, {155, 79, 150}, {0, 56, 168}}; const unsigned char biFlag[3][3] = {{214, 2, 112}, {155, 79, 150}, {0, 56, 168}};
const unsigned char nonBinaryFlag[4][3] = {{252, 244, 52}, {255, 255, 255}, {156, 89, 209}, {44, 44, 44}};
unsigned char flagStripes; unsigned char flagStripes;
// Array of all available flags // Array of all available flags
const std::string allFlags[] = {"pride", "trans", "lesbian", "dutch", "bi"}; const std::string allFlags[] = {"pride", "trans", "lesbian", "dutch", "bi", "non-binary"};
short allFlagsSize = sizeof(allFlags) / sizeof(allFlags[0]); short allFlagsSize = sizeof(allFlags) / sizeof(allFlags[0]);
int opt; int opt;
@ -108,7 +118,7 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
else { else {
flagStr = argv[optind - 1]; // I will keep this at index 0 so it can't even break it for now flagStr = argv[optind - 1];
} }
break; break;
case 'f': case 'f':
@ -123,24 +133,22 @@ int main(int argc, char **argv) {
if (strArgv.compare(allFlags[0]) == 0) { if (strArgv.compare(allFlags[0]) == 0) {
flagStripes = 6; flagStripes = 6;
drawFlag(prideFlag, flagStripes); drawFlag(prideFlag, flagStripes);
} } else if (strArgv.compare(allFlags[1]) == 0) {
else if (strArgv.compare(allFlags[1]) == 0) {
flagStripes = 5; flagStripes = 5;
drawFlag(transFlag, flagStripes); drawFlag(transFlag, flagStripes);
} } else if (strArgv.compare(allFlags[2]) == 0) {
else if (strArgv.compare(allFlags[2]) == 0) {
flagStripes = 5; flagStripes = 5;
drawFlag(lesbianFlag, flagStripes); drawFlag(lesbianFlag, flagStripes);
} } else if (strArgv.compare(allFlags[3]) == 0) {
else if (strArgv.compare(allFlags[3]) == 0) {
flagStripes = 3; flagStripes = 3;
drawFlag(dutchFlag, flagStripes); drawFlag(dutchFlag, flagStripes);
} } else if (strArgv.compare(allFlags[4]) == 0) {
else if (strArgv.compare(allFlags[4]) == 0) {
flagStripes = 3; flagStripes = 3;
drawFlag(biFlag, flagStripes); drawFlag(biFlag, flagStripes);
} } else if (strArgv.compare(allFlags[5]) == 0) {
else { flagStripes = 4;
drawFlag(nonBinaryFlag, flagStripes);
} else {
std::cerr << argv[0] << ": invalid option '" << strArgv << "'\n"; std::cerr << argv[0] << ": invalid option '" << strArgv << "'\n";
std::cerr << "Usage: " << argv[0] << " [OPTION] [VALUE] -f [FLAG]\n"; std::cerr << "Usage: " << argv[0] << " [OPTION] [VALUE] -f [FLAG]\n";
return 1; return 1;
@ -154,7 +162,8 @@ int main(int argc, char **argv) {
} }
return 0; return 0;
case HELP: case HELP:
std::cout << "Usage: " << argv[0] << " [OPTION] [ARGUMENT] -f [FLAG]\n" std::cout
<< "Usage: " << argv[0] << " [OPTION] [ARGUMENT] -f [FLAG]\n"
<< "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"