summaryrefslogtreecommitdiffhomepage
path: root/windows/winfw/src/extras/cli/util.cpp
blob: 67ae42e448a7dfde5ca20b0820e794f8aa948634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "stdafx.h"
#include "util.h"
#include "inlineformatter.h"
#include <libcommon/string.h>
#include <libcommon/error.h>
#include <string>

void PrettyPrintProperties(MessageSink messageSink, PrettyPrintOptions options, const PropertyList &properties)
{
	size_t longestName = 0;

	for (auto &property : properties)
	{
		longestName = max(longestName, property.name.size());
	}

	//
	// Format is:
	// indent, name, [separator], tab, [tab, ...]
	//

	const auto separator = std::wstring(options.useSeparator ? L":" : L"");

	auto insert = options.indent + longestName + separator.size();

	if (0 == (insert % 8))
	{
		insert += 8;
	}
	else
	{
		insert = ((insert / 8) + 1) * 8;
	}

	std::wstring indenter(options.indent, L' ');
	InlineFormatter f;

	for (auto &property : properties)
	{
		auto at = options.indent + property.name.size() + separator.size();
		auto distance = insert - at;
		auto tabs = (0 == (distance % 8) ? distance / 8 : (distance / 8) + 1);

		messageSink((f << indenter << property.name << separator
			<< std::wstring(tabs, L'\t') << property.value).str());
	}
}

std::wstring GetArgumentValue(const common::string::KeyValuePairs &arguments, const std::wstring &key)
{
	auto arg = arguments.find(key);

	if (arguments.end() == arg)
	{
		std::wstringstream ss;

		ss << L"Missing argument: '" << key << L"'";

		throw std::runtime_error(common::string::ToAnsi(ss.str()).c_str());
	}

	return arg->second;
}