Home
Software
Utilities
About
Reader (night2.cpp)
https://centaur.pw/software/night/night2.cpp
[
Direct Link
]
/* Night (night2) * 7/20/2025 * Version 2.2 * Jon S. <jon@centaur.pw> * https://www.centaur.pw/ * * Copyright (C) Centaur, 2025. All Rights Reserved. * * night2.cpp */ #include "night2.h" /* Function definitions */ int lines(string str) { int o = 1; for (unsigned int i = 0 ; i < str.length(); i++) { if (str[i] == 10) o++; } return o; } string* str2array(string str) { static string arraystr[MAX_LINES]; stringstream ss(str); int i = 0; for (string line; getline(ss, line); i++) { arraystr[i] = line; } return arraystr; } string box(string str) { int c = lines(str); if (c > MAX_LINES) return str; if (c == 1) { string r = ""; r += BOX_TOP_LEFT_CORNER; for (unsigned int i = 0; i < strip_night(str).length() + 2; i++) r += BOX_HORIZONTAL_LINE; r += BOX_TOP_RIGHT_CORNER; r += "\n"; r += BOX_VERTICAL_LINE; r += ' ' + night2(str) + ' ' + BOX_VERTICAL_LINE; r += "\n"; r += BOX_BOTTOM_LEFT_CORNER; for (unsigned int i = 0; i < strip_night(str).length() + 2; i++) r += BOX_HORIZONTAL_LINE; r += BOX_BOTTOM_RIGHT_CORNER; return r; } else { string* x; x = str2array(str); int numlines = lines(str); unsigned int longest = 0; for (int i = 0; i < numlines; i++) { if (strlen(strip_night(x[i]).c_str()) > longest) longest = strlen(strip_night(x[i]).c_str()); } string r = ""; r += BOX_TOP_LEFT_CORNER; for (unsigned int i = 0; i < longest + 2; i++) r += BOX_HORIZONTAL_LINE; r += BOX_TOP_RIGHT_CORNER; r += "\n"; for (int i = 0; i < numlines; i++) { string j = ""; j += BOX_VERTICAL_LINE; j += ' ' + night2(x[i]) + (strlen(strip_night(x[i]).c_str()) == longest ? "" : string(longest - strlen(strip_night(x[i]).c_str()), ' ')) + ' ' + BOX_VERTICAL_LINE; r += j; r += "\n"; } r += BOX_BOTTOM_LEFT_CORNER; for (unsigned int i = 0; i < longest + 2; i++) r += BOX_HORIZONTAL_LINE; r += BOX_BOTTOM_RIGHT_CORNER; r += "\n"; return r; } } string readtag(string str, string begin_str, string end_str) { string a = str; a = replace(a, begin_str, ""); a = replace(a, end_str, ""); return a; } string center(string str) { try { int control_chars = 0; for (unsigned int i = 0; i < str.length(); i++) if (str[i] == '\e') control_chars++; string output = ""; int v = console_size.ws_col - str.length(); output.append(control_chars * 2, ' '); output.append(v / 2, ' '); output += str; return output; } catch (...) { return str; } } string replace(string str, const string f, const string r) { if (f.empty()) return str; string a = str; size_t b = 0; while ((b = a.find(f, b)) != string::npos) { a.replace(b, f.length(), r); b += r.length(); } return a; } string strip_ansi(string str) { string b = str; b = replace(b, BLACK, ""); b = replace(b, RED, ""); b = replace(b, GREEN, ""); b = replace(b, YELLOW, ""); b = replace(b, BLUE, ""); b = replace(b, PURPLE, ""); b = replace(b, CYAN, ""); b = replace(b, WHITE, ""); b = replace(b, BGBLACK, ""); b = replace(b, BGRED, ""); b = replace(b, BGGREEN, ""); b = replace(b, BGYELLOW, ""); b = replace(b, BGBLUE, ""); b = replace(b, BGPURPLE, ""); b = replace(b, BGCYAN, ""); b = replace(b, BGWHITE, ""); b = replace(b, BOLD, ""); b = replace(b, FAINT, ""); b = replace(b, ITALIC, ""); b = replace(b, LINE, ""); b = replace(b, STRIKE, ""); b = replace(b, CLEAR, ""); b = replace(b, CURSOR, ""); b = replace(b, REVERSE, ""); b = replace(b, START, ""); b = replace(b, RESTORE, ""); b = replace(b, UP, ""); b = replace(b, DOWN, ""); b = replace(b, LEFT, ""); b = replace(b, RIGHT, ""); b = replace(b, NONE, ""); b = replace(b, RESET, ""); return b; } string strip_night(string str) { string b = str; b = replace(b, Code_Black, ""); b = replace(b, Code_Red, ""); b = replace(b, Code_Green, ""); b = replace(b, Code_Yellow, ""); b = replace(b, Code_Blue, ""); b = replace(b, Code_Purple, ""); b = replace(b, Code_Cyan, ""); b = replace(b, Code_White, ""); b = replace(b, Code_BGBlack, ""); b = replace(b, Code_BGRed, ""); b = replace(b, Code_BGGreen, ""); b = replace(b, Code_BGYellow, ""); b = replace(b, Code_BGBlue, ""); b = replace(b, Code_BGPurple, ""); b = replace(b, Code_BGCyan, ""); b = replace(b, Code_BGWhite, ""); b = replace(b, Code_Bold, ""); b = replace(b, Code_Faint, ""); b = replace(b, Code_Italic, ""); b = replace(b, Code_Line, ""); b = replace(b, Code_Strike, ""); b = replace(b, Code_Clear, ""); b = replace(b, Code_Cursor, ""); b = replace(b, Code_Reverse, ""); b = replace(b, Code_Reset, ""); b = replace(b, Code_BeginCenter, ""); b = replace(b, Code_EndCenter, ""); b = replace(b, Code_BeginBox, ""); b = replace(b, Code_EndBox, ""); b = replace(b, Code_Start, ""); b = replace(b, Code_Restore, ""); b = replace(b, Code_Up, ""); b = replace(b, Code_Down, ""); b = replace(b, Code_Left, ""); b = replace(b, Code_Right, ""); b = replace(b, Code_NewLine, ""); b = replace(b, Code_None, ""); return b; } string night2(const string str) { string bc = Code_BeginCenter; string ec = Code_EndCenter; string bb = Code_BeginBox; string eb = Code_EndBox; string b = str; b = replace(b, Code_None, NONE); b = replace(b, Code_NewLine, NEWLINE); if (b.length() >= bb.length() && b.find(bb) != string::npos) return box(readtag(b, bb, eb)); b = replace(b, Code_Black, BLACK); b = replace(b, Code_Red, RED); b = replace(b, Code_Green, GREEN); b = replace(b, Code_Yellow, YELLOW); b = replace(b, Code_Blue, BLUE); b = replace(b, Code_Purple, PURPLE); b = replace(b, Code_Cyan, CYAN); b = replace(b, Code_White, WHITE); b = replace(b, Code_BGBlack, BGBLACK); b = replace(b, Code_BGRed, BGRED); b = replace(b, Code_BGGreen, BGGREEN); b = replace(b, Code_BGYellow, BGYELLOW); b = replace(b, Code_BGBlue, BGBLUE); b = replace(b, Code_BGPurple, BGPURPLE); b = replace(b, Code_BGCyan, BGCYAN); b = replace(b, Code_BGWhite, BGWHITE); b = replace(b, Code_Bold, BOLD); b = replace(b, Code_Faint, FAINT); b = replace(b, Code_Italic, ITALIC); b = replace(b, Code_Line, LINE); b = replace(b, Code_Strike, STRIKE); b = replace(b, Code_Clear, CLEAR); b = replace(b, Code_Cursor, CURSOR); b = replace(b, Code_Reverse, REVERSE); b = replace(b, Code_Reset, RESET); b = replace(b, Code_Start, START); b = replace(b, Code_Restore, RESTORE); b = replace(b, Code_Up, UP); b = replace(b, Code_Down, DOWN); b = replace(b, Code_Left, LEFT); b = replace(b, Code_Right, RIGHT); #if (defined(SUPPORT_OLD_CODES) && SUPPORT_OLD_CODES == 1) b = replace(b, Night1_Code_Black, BLACK); b = replace(b, Night1_Code_Red, RED); b = replace(b, Night1_Code_Green, GREEN); b = replace(b, Night1_Code_Yellow, YELLOW); b = replace(b, Night1_Code_Blue, BLUE); b = replace(b, Night1_Code_Purple, PURPLE); b = replace(b, Night1_Code_Cyan, CYAN); b = replace(b, Night1_Code_White, WHITE); b = replace(b, Night1_Code_Bold, BOLD); b = replace(b, Night1_Code_Line, LINE); b = replace(b, Night1_Code_Clear, CLEAR); b = replace(b, Night1_Code_Cursor, CURSOR); b = replace(b, Night1_Code_Reverse, REVERSE); b = replace(b, Night1_Code_Reset, RESET); #endif /* SUPPORT_OLD_CODES */ if (b.length() >= bc.length() && b.find(bc) != string::npos) return center(readtag(b, bc, ec)); else return b; //return b; } void night2file(char file[]) { ifstream ifile(file); if (!ifile) { cout << file << ": " << strerror(errno) << endl; return; } else { for (string line; getline(ifile, line);) { if (line[0] == '#') continue; cout << night2(line) << endl; } ifile.close(); } return; } /* Main function */ int main(int argc, char **argv) { if (argc < 2) { for (string line; getline(cin, line);) { cout << night2(line) << endl; } return 0; } else if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { cout << "Centaur Night version " << NIGHT_VERSION << endl << endl; cout << "Usage: " << argv[0] << " <file> ..." << endl; cout << " " << argv[0] << " -s | --string <string> ..." << endl; cout << " " << argv[0] << " \"`cat file`\" | \"$(cat file)\"" << endl; cout << " " << "cat file | " << argv[0] << endl; cout << " " << argv[0] << " -t | --test" << endl; cout << " " << argv[0] << " -h | --help" << endl << endl; cout << PROGRAM_HELP << endl; return 0; } else if (argc > 1 && (strcmp(argv[1], "--test") == 0 || strcmp(argv[1], "-t") == 0)) { cout << night2(TEST_PATTERN) << endl; cout << night2(":center:Centered Text:.center:") << endl; return 0; } else if (argc >= 2 && (strcmp(argv[1], "--string") == 0 || strcmp(argv[1], "-s") == 0)) { string input = ""; for (int i = 2; i < argc; i++) { input = input + argv[i]; if (i + 1 != argc) input = input + " "; } cout << night2(input) << endl; return 0; } else if (argc > 1) { for (int i = 1; i < argc; i++) { night2file(argv[i]); } return 0; } }