src/TabularPrefetchingHandler.h
author Františ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
permissions -rw-r--r--
configurable color schemes: greenish, amberish, midnight, monochrome
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     1
/**
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     2
 * Relational pipes
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     4
 *
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
24
992dde455b04 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
9
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     8
 *
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    12
 * GNU General Public License for more details.
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    13
 *
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    14
 * You should have received a copy of the GNU General Public License
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    16
 */
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <string>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sstream>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <locale>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <codecvt>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <regex>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/typedefs.h>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
6
fc1e746e26a5 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    31
#include <relpipe/reader/RelpipeReaderException.h>
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    33
#include "Configuration.h"
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    34
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace tabular {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::reader;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
20
b9625491fbf5 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
    41
class TabularPrefetchingHandler : public handlers::RelationalReaderStringHandler {
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    43
39
f33464965693 prepare for configurable color schemes
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    44
	class ColorScheme {
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    45
	public:
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    46
		const char* ESC_BRIGHT = "\u001b[1m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    47
		const char* ESC_RED = "\u001b[31m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    48
		const char* ESC_GREEN = "\u001b[32m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    49
		const char* ESC_BLUE = "\u001b[34m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    50
		const char* ESC_YELLOW = "\u001b[33m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    51
		const char* ESC_AMBER = "\u001b[38;5;166m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    52
		const char* ESC_CYAN = "\u001b[36m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    53
		const char* ESC_RESET = "\u001b[0m";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    54
		const char* ESC_EMPTY = "";
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    55
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    56
		const char* header = ESC_BRIGHT;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    57
		const char* border = ESC_GREEN;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    58
		const char* value = ESC_CYAN;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    59
		const char* relation = ESC_RED;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    60
		const char* replacement = ESC_RED;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    61
		const char* count = ESC_YELLOW;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    62
		const char* reset = ESC_RESET;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    63
	} cs;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    64
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	const char* INDENT = " "; // table indent from the left
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    70
	std::ostream& output;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    71
	Configuration& configuration;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    72
	RelationConfiguration* currentRelationConfiguration;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    73
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    74
#define getConfiguration(option) (currentRelationConfiguration ? currentRelationConfiguration->option : configuration.option)
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	std::vector<TypeId> columnTypes;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	std::vector<string_t> columnTypeCodes;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	std::vector<string_t> columnNames;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	std::vector<integer_t> columnWidths;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	std::vector<string_t> values; // all values are saved here and processed at the end of the relation
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	integer_t columnCount = 0;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	const string_t colorizeReplacement(const string_t &replacement, const char* valueColor) {
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    84
		return convertor.from_bytes(cs.reset) + convertor.from_bytes(cs.replacement) + replacement + convertor.from_bytes(cs.reset) + convertor.from_bytes(valueColor);
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	/**
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	 * Sanitizes whitespace that could broke table layout.
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	 * 
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	 * TODO: sanitize also escape sequences and emoji (resp. properly measure their width)
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	 * 
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	 * @param value original value
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	 * @param color value foreground color
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	 * @return value with replaced whitespaces
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	 */
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	const string_t formatValue(const string_t &value, const char* color) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		std::wstringstream result;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		result << convertor.from_bytes(color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		for (auto & ch : value) {
27
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   102
			// see computeWidth below
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
			switch (ch) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
				case L'\n': result << colorizeReplacement(L"↲", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
				case L'\r': result << colorizeReplacement(L"⏎", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
				case L'\t': result << colorizeReplacement(L"↹", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
				case L' ': result << colorizeReplacement(L"⎵", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
					break;
36
3b75b9f38abc replacement symbols for more whitespace characters: bell, backspace
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   112
				case L'\a': result << colorizeReplacement(L"*", color); // 🔔 ␇
3b75b9f38abc replacement symbols for more whitespace characters: bell, backspace
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   113
					break;
3b75b9f38abc replacement symbols for more whitespace characters: bell, backspace
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   114
				case L'\b': result << colorizeReplacement(L"←", color); // ␈
3b75b9f38abc replacement symbols for more whitespace characters: bell, backspace
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   115
					break;
37
5dcff3c35462 replacement symbols for more whitespace characters: escape
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   116
				case L'\e': result << colorizeReplacement(L"ESC", color); // ␛
5dcff3c35462 replacement symbols for more whitespace characters: escape
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   117
					break;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
				default: result << ch;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
			}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   122
		result << convertor.from_bytes(cs.reset);
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
		return result.str();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
26
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   127
	/**
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   128
	 * @param stringValue
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   129
	 * @return the width that would the string occupy on the display (particular characters might be wider than 1 column)
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   130
	 */
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   131
	integer_t computeWidth(const string_t& stringValue) {
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   132
		integer_t width = 0;
27
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   133
		for (wchar_t ch : stringValue) {
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   134
			// see formatValue() above
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   135
			switch (ch) {
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   136
				case L'\n':
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   137
				case L'\r':
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   138
				case L'\t':
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   139
				case L' ':
36
3b75b9f38abc replacement symbols for more whitespace characters: bell, backspace
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   140
				case L'\a':
3b75b9f38abc replacement symbols for more whitespace characters: bell, backspace
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   141
				case L'\b':
27
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   142
					width += 1;
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   143
					break;
37
5dcff3c35462 replacement symbols for more whitespace characters: escape
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   144
				case L'\e':
5dcff3c35462 replacement symbols for more whitespace characters: escape
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   145
					width += 3;
5dcff3c35462 replacement symbols for more whitespace characters: escape
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   146
					break;
27
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   147
				default:
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   148
					width += std::max(0, wcwidth(ch));
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   149
			}
f03e1f9cfcb6 compute correct width for whitespace characters that will be replaced
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
   150
		}
26
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   151
		return width;
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   152
	}
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   153
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   154
	void printHorizontalLine(const string_t& left, const string_t& middle, const string_t& right, const string_t& bar) {
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   155
		output << INDENT << cs.border;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
		output << convertor.to_bytes(left);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
		for (size_t c = 0; c < columnCount; c++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
			integer_t width = columnWidths[c];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
			for (integer_t w = 0; w < (width + 2); w++) output << convertor.to_bytes(bar); // 2 = left and right padding of the value
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
			if (c < (columnCount - 1)) output << convertor.to_bytes(middle);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
		output << convertor.to_bytes(right);
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   163
		output << cs.reset << std::endl;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	void printCachedData() {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
		// Compute column widths and paddings:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
		vector<integer_t> paddings(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
		for (size_t i = 0; i < columnCount; i++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
			string_t typeCode = columnTypeCodes[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
			string_t columnName = columnNames[i];
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   173
			integer_t minWidth = columnName.size() + (getConfiguration(writeTypes) ? typeCode.size() + 3 : 0); // 3 = " ()" in "columnName (typeCode)"
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
			columnWidths[i] = max(columnWidths[i], minWidth);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
			paddings[i] = columnWidths[i] - minWidth;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   178
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╭", L"┬", L"╮", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   179
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"┌", L"┬", L"┐", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   180
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╔", L"╦", L"╗", L"═");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   181
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   182
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   183
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   184
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   185
		std::string verticalSeparator;
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   186
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) verticalSeparator = "│";
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   187
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) verticalSeparator = "│";
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   188
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) verticalSeparator = "║";
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   189
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) verticalSeparator = " ";
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   190
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) verticalSeparator = "|";
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   191
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
		// Print column headers:
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   194
		output << INDENT << cs.border << verticalSeparator << cs.reset;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
		for (size_t i = 0; i < columnCount; i++) {
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   196
			output << " " << convertor.to_bytes(formatValue(columnNames[i], cs.header));
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
			for (integer_t p = 0; p < paddings[i]; p++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
				output << " ";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
			}
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   200
			if (getConfiguration(writeTypes)) output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")";
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   201
			output << cs.border << " " << verticalSeparator << cs.reset;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
		output << std::endl;
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   204
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"├", L"┼", L"┤", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   205
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"├", L"┼", L"┤", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   206
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╠", L"╬", L"╣", L"═");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   207
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   208
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   209
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   210
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   211
		// Print particular rows:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
		for (size_t i = 0; i < values.size(); i++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   213
			integer_t columnIndex = i % columnCount;
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   214
			if (columnIndex == 0) output << INDENT << cs.border << verticalSeparator << cs.reset;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   215
			string_t stringValue = values[i];
26
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   216
			integer_t padding = columnWidths[columnIndex] - computeWidth(stringValue);
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
			boolean_t alignRight = columnTypes[columnIndex] == TypeId::BOOLEAN || columnTypes[columnIndex] == TypeId::INTEGER;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   219
			if (alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   220
			output << " " << convertor.to_bytes(formatValue(stringValue, cs.value));
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   221
			if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   223
			output << cs.border << " " << verticalSeparator << cs.reset;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   224
			if (columnIndex == (columnCount - 1)) output << std::endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   225
		}
38
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   226
		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╰", L"┴", L"╯", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   227
		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"└", L"┴", L"┘", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   228
		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╚", L"╩", L"╝", L"═");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   229
		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   230
		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
2cc2d3f658f4 configurable table style: rounded, sharp, sharp-double, horizontal-only, ascii
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   231
		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   232
31
c22577615ce4 rename: --print-types, --print-relation-name, --print-record-count to --write-types, --write-relation-name, --write-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   233
		if (getConfiguration(writeRecordCount)) {
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   234
			integer_t recordCount = values.size() / columnCount;
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   235
			output << cs.count << "Record count: " << cs.reset << recordCount << std::endl;
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   236
		}
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   237
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   238
		values.clear();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   241
public:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   242
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   243
	TabularPrefetchingHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
6
fc1e746e26a5 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   246
	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   247
		if (columnCount) printCachedData();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   248
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   249
		currentRelationConfiguration = nullptr;
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   250
		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   251
			if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) {
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   252
				currentRelationConfiguration = &configuration.relationConfigurations[i];
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   253
				break; // it there are multiple matches, only the first configuration is used
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   254
			}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   255
		}
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   256
40
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   257
		if (configuration.colorScheme == Configuration::ColorScheme::Greenish) {
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   258
			cs = ColorScheme();
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   259
		} else if (configuration.colorScheme == Configuration::ColorScheme::Amberish) {
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   260
			cs = ColorScheme();
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   261
			cs.border = cs.ESC_YELLOW;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   262
			cs.value = cs.ESC_AMBER;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   263
		} else if (configuration.colorScheme == Configuration::ColorScheme::Monochrome) {
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   264
			cs = ColorScheme();
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   265
			cs.border = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   266
			cs.count = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   267
			cs.header = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   268
			cs.relation = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   269
			cs.replacement = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   270
			cs.reset = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   271
			cs.value = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   272
		} else if (configuration.colorScheme == Configuration::ColorScheme::Midnight) {
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   273
			cs = ColorScheme();
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   274
			cs.relation = cs.ESC_CYAN;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   275
			cs.count = cs.ESC_CYAN;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   276
			cs.border = cs.ESC_BLUE;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   277
			cs.value = cs.ESC_EMPTY;
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   278
		} else {
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   279
			throw RelpipeReaderException(L"Unsupported ColorScheme: " + std::to_wstring((int) configuration.colorScheme));
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   280
		}
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   281
59f3cb26bfe7 configurable color schemes: greenish, amberish, midnight, monochrome
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   282
		if (getConfiguration(writeRelationName)) output << cs.relation << convertor.to_bytes(name) << ":" << cs.reset << endl;
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   283
		columnCount = attributes.size();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   284
		columnTypes.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   285
		columnTypeCodes.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   286
		columnNames.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   287
		columnWidths.resize(columnCount, 0);
13
3435efcaa8ca fix table width: reset widths to 0
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   288
		fill(columnWidths.begin(), columnWidths.end(), 0);
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   289
		for (int i = 0; i < attributes.size(); i++) {
7
2057919937fa AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   290
			columnNames[i] = attributes[i].getAttributeName();
2057919937fa AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   291
			columnTypes[i] = attributes[i].getTypeId();
2057919937fa AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   292
			columnTypeCodes[i] = attributes[i].getTypeName();
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   293
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   294
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   295
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   296
	void attribute(const string_t& value) override {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   297
		integer_t i = values.size() % columnCount;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   298
		values.push_back(value);
26
32824e833b89 compute proper width of strings (characters might be wider than 1 column on display)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   299
		columnWidths[i] = max(columnWidths[i], computeWidth(value));
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   300
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   301
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
	void endOfPipe() {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   303
		if (columnCount) printCachedData();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   304
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   306
};
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   307
30
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   308
#undef getConfiguration
3c6374467a82 change CLI interface: options: --relation --print-types --print-relation-name --print-record-count
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   309
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   310
}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   311
}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   312
}