configurable color schemes: greenish, amberish, midnight, monochrome v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 01 Jul 2022 23:38:15 +0200
branchv_0
changeset 40 59f3cb26bfe7
parent 39 f33464965693
child 41 e1339b8e838e
configurable color schemes: greenish, amberish, midnight, monochrome
src/TabularPrefetchingHandler.h
--- a/src/TabularPrefetchingHandler.h	Fri Jul 01 22:56:30 2022 +0200
+++ b/src/TabularPrefetchingHandler.h	Fri Jul 01 23:38:15 2022 +0200
@@ -40,24 +40,30 @@
 
 class TabularPrefetchingHandler : public handlers::RelationalReaderStringHandler {
 private:
-	
+
 	class ColorScheme {
-	};
-	
+	public:
+		const char* ESC_BRIGHT = "\u001b[1m";
+		const char* ESC_RED = "\u001b[31m";
+		const char* ESC_GREEN = "\u001b[32m";
+		const char* ESC_BLUE = "\u001b[34m";
+		const char* ESC_YELLOW = "\u001b[33m";
+		const char* ESC_AMBER = "\u001b[38;5;166m";
+		const char* ESC_CYAN = "\u001b[36m";
+		const char* ESC_RESET = "\u001b[0m";
+		const char* ESC_EMPTY = "";
+
+		const char* header = ESC_BRIGHT;
+		const char* border = ESC_GREEN;
+		const char* value = ESC_CYAN;
+		const char* relation = ESC_RED;
+		const char* replacement = ESC_RED;
+		const char* count = ESC_YELLOW;
+		const char* reset = ESC_RESET;
+	} cs;
+
 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
-	const char* ESC_BRIGHT = "\u001b[1m";
-	const char* ESC_RED = "\u001b[31m";
-	const char* ESC_GREEN = "\u001b[32m";
-	const char* ESC_YELLOW = "\u001b[33m";
-	const char* ESC_CYAN = "\u001b[36m";
-	const char* ESC_RESET = "\u001b[0m";
 
-	const char* ESC_HEADER = ESC_BRIGHT;
-	const char* ESC_BORDER = ESC_GREEN;
-	const char* ESC_VALUE = ESC_CYAN;
-	const char* ESC_RELATION = ESC_RED;
-	const char* ESC_REPLACEMENT = ESC_RED;
-	const char* ESC_COUNT = ESC_YELLOW;
 
 	const char* INDENT = " "; // table indent from the left
 
@@ -75,7 +81,7 @@
 	integer_t columnCount = 0;
 
 	const string_t colorizeReplacement(const string_t &replacement, const char* valueColor) {
-		return convertor.from_bytes(ESC_RESET) + convertor.from_bytes(ESC_REPLACEMENT) + replacement + convertor.from_bytes(ESC_RESET) + convertor.from_bytes(valueColor);
+		return convertor.from_bytes(cs.reset) + convertor.from_bytes(cs.replacement) + replacement + convertor.from_bytes(cs.reset) + convertor.from_bytes(valueColor);
 	}
 
 	/**
@@ -113,7 +119,7 @@
 			}
 		}
 
-		result << convertor.from_bytes(ESC_RESET);
+		result << convertor.from_bytes(cs.reset);
 
 		return result.str();
 	}
@@ -146,7 +152,7 @@
 	}
 
 	void printHorizontalLine(const string_t& left, const string_t& middle, const string_t& right, const string_t& bar) {
-		output << INDENT << ESC_BORDER;
+		output << INDENT << cs.border;
 		output << convertor.to_bytes(left);
 		for (size_t c = 0; c < columnCount; c++) {
 			integer_t width = columnWidths[c];
@@ -154,7 +160,7 @@
 			if (c < (columnCount - 1)) output << convertor.to_bytes(middle);
 		}
 		output << convertor.to_bytes(right);
-		output << ESC_RESET << std::endl;
+		output << cs.reset << std::endl;
 
 	}
 
@@ -185,14 +191,14 @@
 		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
 
 		// Print column headers:
-		output << INDENT << ESC_BORDER << verticalSeparator << ESC_RESET;
+		output << INDENT << cs.border << verticalSeparator << cs.reset;
 		for (size_t i = 0; i < columnCount; i++) {
-			output << " " << convertor.to_bytes(formatValue(columnNames[i], ESC_HEADER));
+			output << " " << convertor.to_bytes(formatValue(columnNames[i], cs.header));
 			for (integer_t p = 0; p < paddings[i]; p++) {
 				output << " ";
 			}
 			if (getConfiguration(writeTypes)) output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")";
-			output << ESC_BORDER << " " << verticalSeparator << ESC_RESET;
+			output << cs.border << " " << verticalSeparator << cs.reset;
 		}
 		output << std::endl;
 		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"├", L"┼", L"┤", L"─");
@@ -205,16 +211,16 @@
 		// Print particular rows:
 		for (size_t i = 0; i < values.size(); i++) {
 			integer_t columnIndex = i % columnCount;
-			if (columnIndex == 0) output << INDENT << ESC_BORDER << verticalSeparator << ESC_RESET;
+			if (columnIndex == 0) output << INDENT << cs.border << verticalSeparator << cs.reset;
 			string_t stringValue = values[i];
 			integer_t padding = columnWidths[columnIndex] - computeWidth(stringValue);
 			boolean_t alignRight = columnTypes[columnIndex] == TypeId::BOOLEAN || columnTypes[columnIndex] == TypeId::INTEGER;
 
 			if (alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
-			output << " " << convertor.to_bytes(formatValue(stringValue, ESC_VALUE));
+			output << " " << convertor.to_bytes(formatValue(stringValue, cs.value));
 			if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
 
-			output << ESC_BORDER << " " << verticalSeparator << ESC_RESET;
+			output << cs.border << " " << verticalSeparator << cs.reset;
 			if (columnIndex == (columnCount - 1)) output << std::endl;
 		}
 		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╰", L"┴", L"╯", L"─");
@@ -226,7 +232,7 @@
 
 		if (getConfiguration(writeRecordCount)) {
 			integer_t recordCount = values.size() / columnCount;
-			output << ESC_COUNT << "Record count: " << ESC_RESET << recordCount << std::endl;
+			output << cs.count << "Record count: " << cs.reset << recordCount << std::endl;
 		}
 
 		values.clear();
@@ -248,7 +254,32 @@
 			}
 		}
 
-		if (getConfiguration(writeRelationName)) output << ESC_RELATION << convertor.to_bytes(name) << ":" << ESC_RESET << endl;
+		if (configuration.colorScheme == Configuration::ColorScheme::Greenish) {
+			cs = ColorScheme();
+		} else if (configuration.colorScheme == Configuration::ColorScheme::Amberish) {
+			cs = ColorScheme();
+			cs.border = cs.ESC_YELLOW;
+			cs.value = cs.ESC_AMBER;
+		} else if (configuration.colorScheme == Configuration::ColorScheme::Monochrome) {
+			cs = ColorScheme();
+			cs.border = cs.ESC_EMPTY;
+			cs.count = cs.ESC_EMPTY;
+			cs.header = cs.ESC_EMPTY;
+			cs.relation = cs.ESC_EMPTY;
+			cs.replacement = cs.ESC_EMPTY;
+			cs.reset = cs.ESC_EMPTY;
+			cs.value = cs.ESC_EMPTY;
+		} else if (configuration.colorScheme == Configuration::ColorScheme::Midnight) {
+			cs = ColorScheme();
+			cs.relation = cs.ESC_CYAN;
+			cs.count = cs.ESC_CYAN;
+			cs.border = cs.ESC_BLUE;
+			cs.value = cs.ESC_EMPTY;
+		} else {
+			throw RelpipeReaderException(L"Unsupported ColorScheme: " + std::to_wstring((int) configuration.colorScheme));
+		}
+
+		if (getConfiguration(writeRelationName)) output << cs.relation << convertor.to_bytes(name) << ":" << cs.reset << endl;
 		columnCount = attributes.size();
 		columnTypes.resize(columnCount);
 		columnTypeCodes.resize(columnCount);