src/TabularPrefetchingHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 02 Jul 2022 19:45:18 +0200
branchv_0
changeset 42 ca216de56ef0
parent 41 e1339b8e838e
permissions -rw-r--r--
allow setting some options through ENV variables (not only CLI arguments)

/**
 * Relational pipes
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>

#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>
#include <relpipe/reader/RelpipeReaderException.h>

#include "Configuration.h"

namespace relpipe {
namespace out {
namespace tabular {

using namespace relpipe::reader;

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* INDENT = " "; // table indent from the left

	std::ostream& output;
	Configuration& configuration;
	RelationConfiguration* currentRelationConfiguration;

#define getConfiguration(option) (currentRelationConfiguration ? currentRelationConfiguration->option : configuration.option)

	std::vector<TypeId> columnTypes;
	std::vector<string_t> columnTypeCodes;
	std::vector<string_t> columnNames;
	std::vector<integer_t> columnWidths;
	std::vector<string_t> values; // all values are saved here and processed at the end of the relation
	integer_t columnCount = 0;

	const string_t colorizeReplacement(const string_t &replacement, const char* valueColor) {
		return convertor.from_bytes(cs.reset) + convertor.from_bytes(cs.replacement) + replacement + convertor.from_bytes(cs.reset) + convertor.from_bytes(valueColor);
	}

	/**
	 * Sanitizes whitespace that could broke table layout.
	 * 
	 * TODO: sanitize also escape sequences and emoji (resp. properly measure their width)
	 * 
	 * @param value original value
	 * @param color value foreground color
	 * @return value with replaced whitespaces
	 */
	const string_t formatValue(const string_t &value, const char* color) {
		std::wstringstream result;

		result << convertor.from_bytes(color);

		for (auto & ch : value) {
			// see computeWidth below
			switch (ch) {
				case L'\n': result << colorizeReplacement(L"↲", color);
					break;
				case L'\r': result << colorizeReplacement(L"⏎", color);
					break;
				case L'\t': result << colorizeReplacement(L"↹", color);
					break;
				case L' ': result << colorizeReplacement(L"⎵", color);
					break;
				case L'\a': result << colorizeReplacement(L"*", color); // 🔔 ␇
					break;
				case L'\b': result << colorizeReplacement(L"←", color); // ␈
					break;
				case L'\e': result << colorizeReplacement(L"ESC", color); // ␛
					break;
				default: result << ch;
			}
		}

		result << convertor.from_bytes(cs.reset);

		return result.str();
	}

	/**
	 * @param stringValue
	 * @return the width that would the string occupy on the display (particular characters might be wider than 1 column)
	 */
	integer_t computeWidth(const string_t& stringValue) {
		integer_t width = 0;
		for (wchar_t ch : stringValue) {
			// see formatValue() above
			switch (ch) {
				case L'\n':
				case L'\r':
				case L'\t':
				case L' ':
				case L'\a':
				case L'\b':
					width += 1;
					break;
				case L'\e':
					width += 3;
					break;
				default:
					width += std::max(0, wcwidth(ch));
			}
		}
		return width;
	}

	void printHorizontalLine(const string_t& left, const string_t& middle, const string_t& right, const string_t& bar) {
		output << INDENT << cs.border;
		output << convertor.to_bytes(left);
		for (size_t c = 0; c < columnCount; c++) {
			integer_t width = columnWidths[c];
			for (integer_t w = 0; w < (width + 2); w++) output << convertor.to_bytes(bar); // 2 = left and right padding of the value
			if (c < (columnCount - 1)) output << convertor.to_bytes(middle);
		}
		output << convertor.to_bytes(right);
		output << cs.reset << std::endl;

	}

	void printCachedData() {
		// Compute column widths and paddings:
		vector<integer_t> paddings(columnCount);
		for (size_t i = 0; i < columnCount; i++) {
			string_t typeCode = columnTypeCodes[i];
			string_t columnName = columnNames[i];
			integer_t minWidth = columnName.size() + (getConfiguration(writeTypes) ? typeCode.size() + 3 : 0); // 3 = " ()" in "columnName (typeCode)"
			columnWidths[i] = max(columnWidths[i], minWidth);
			paddings[i] = columnWidths[i] - minWidth;
		}

		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╭", L"┬", L"╮", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"┌", L"┬", L"┐", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╔", L"╦", L"╗", L"═");
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));

		std::string verticalSeparator;
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) verticalSeparator = "│";
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) verticalSeparator = "│";
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) verticalSeparator = "║";
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) verticalSeparator = " ";
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) verticalSeparator = "|";
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));

		// Print column headers:
		output << INDENT << cs.border << verticalSeparator << cs.reset;
		for (size_t i = 0; i < columnCount; i++) {
			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 << cs.border << " " << verticalSeparator << cs.reset;
		}
		output << std::endl;
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"├", L"┼", L"┤", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"├", L"┼", L"┤", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╠", L"╬", L"╣", L"═");
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));

		// Print particular rows:
		for (size_t i = 0; i < values.size(); i++) {
			integer_t columnIndex = i % columnCount;
			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, cs.value));
			if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " ";

			output << cs.border << " " << verticalSeparator << cs.reset;
			if (columnIndex == (columnCount - 1)) output << std::endl;
		}
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╰", L"┴", L"╯", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"└", L"┴", L"┘", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╚", L"╩", L"╝", L"═");
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));

		if (getConfiguration(writeRecordCount)) {
			integer_t recordCount = values.size() / columnCount;
			output << cs.count << "Record count: " << cs.reset << recordCount << std::endl;
		}

		values.clear();
	}

public:

	TabularPrefetchingHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
	}

	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
		if (columnCount) printCachedData();

		currentRelationConfiguration = nullptr;
		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
			if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) {
				currentRelationConfiguration = &configuration.relationConfigurations[i];
				break; // it there are multiple matches, only the first configuration is used
			}
		}

		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::BlackAndWhite) {
			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);
		columnNames.resize(columnCount);
		columnWidths.resize(columnCount, 0);
		fill(columnWidths.begin(), columnWidths.end(), 0);
		for (int i = 0; i < attributes.size(); i++) {
			columnNames[i] = attributes[i].getAttributeName();
			columnTypes[i] = attributes[i].getTypeId();
			columnTypeCodes[i] = attributes[i].getTypeName();
		}
	}

	void attribute(const string_t& value) override {
		integer_t i = values.size() % columnCount;
		values.push_back(value);
		columnWidths[i] = max(columnWidths[i], computeWidth(value));
	}

	void endOfPipe() {
		if (columnCount) printCachedData();
	}

};

#undef getConfiguration

}
}
}