src/TabularPrefetchingHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 17 Nov 2019 17:51:13 +0100
branchv_0
changeset 26 32824e833b89
parent 24 992dde455b04
child 27 f03e1f9cfcb6
permissions -rw-r--r--
compute proper width of strings (characters might be wider than 1 column on display) Before: $ echo hello ๐Ÿญ | relpipe-in-csv wide string | relpipe-out-tabular wide: โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚ string (string) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ hello ๐Ÿญ โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ Record count: 1 After: $ echo hello ๐Ÿญ | relpipe-in-csv wide string | relpipe-out-tabular wide: โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚ string (string) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ hello ๐Ÿญ โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ Record count: 1

/**
 * 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>

namespace relpipe {
namespace out {
namespace tabular {

using namespace relpipe::reader;

class TabularPrefetchingHandler : public handlers::RelationalReaderStringHandler {
private:
	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_REPLACEMENT = ESC_RED;

	const char* INDENT = " "; // table indent from the left

	std::ostream &output;

	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(ESC_RESET) + convertor.from_bytes(ESC_REPLACEMENT) + replacement + convertor.from_bytes(ESC_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) {
			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;
				default: result << ch;
			}
		}

		result << convertor.from_bytes(ESC_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) 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 = L"โ”€";
		// TODO: support also ASCII nostalgia:
		// border = border.replaceAll("โ”€", "-");
		// border = border.replaceAll("โ”‚", "|");
		// border = border.replaceAll("[โ•ญโ”ฌโ•ฎโ”œโ”ผโ”คโ•ฐโ”ดโ•ฏ]", "+");

		output << INDENT << ESC_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 << ESC_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() + typeCode.size() + 3; // 3 = " ()" in "columnName (typeCode)"
			columnWidths[i] = max(columnWidths[i], minWidth);
			paddings[i] = columnWidths[i] - minWidth;
		}

		printHorizontalLine(L"โ•ญ", L"โ”ฌ", L"โ•ฎ");

		// Print column headers:
		output << INDENT << ESC_BORDER << "โ”‚" << ESC_RESET;
		for (size_t i = 0; i < columnCount; i++) {
			output << " " << convertor.to_bytes(formatValue(columnNames[i], ESC_HEADER));
			for (integer_t p = 0; p < paddings[i]; p++) {
				output << " ";
			}
			output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")";
			output << ESC_BORDER << " โ”‚" << ESC_RESET;
		}
		output << std::endl;
		printHorizontalLine(L"โ”œ", L"โ”ผ", L"โ”ค");

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

			output << ESC_BORDER << " โ”‚" << ESC_RESET;
			if (columnIndex == (columnCount - 1)) output << std::endl;
		}
		printHorizontalLine(L"โ•ฐ", L"โ”ด", L"โ•ฏ");
		integer_t recordCount = values.size() / columnCount;
		output << ESC_YELLOW << "Record count: " << ESC_RESET << recordCount << std::endl;

		values.clear();
	}

public:

	TabularPrefetchingHandler(std::ostream& output) : output(output) {
	}

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

		output << ESC_RED << convertor.to_bytes(name) << ":" << ESC_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();
	}

};

}
}
}