summaryrefslogtreecommitdiffhomepage
path: root/wfpctl/src/extras/cli/util.cpp
diff options
context:
space:
mode:
authorOdd Stranne <odd@mullvad.net>2018-04-09 21:25:12 +0200
committerOdd Stranne <odd@mullvad.net>2018-04-11 10:26:18 +0200
commit0b5dd6c0dd8ad121c4a5c14c45c9b470dc1b9809 (patch)
treeecf317c10a93c27365b4bb5e8a3c3241bab2e815 /wfpctl/src/extras/cli/util.cpp
parenta5f7cf5592003093caaa658516758fb3c45206d1 (diff)
downloadmullvadvpn-0b5dd6c0dd8ad121c4a5c14c45c9b470dc1b9809.tar.xz
mullvadvpn-0b5dd6c0dd8ad121c4a5c14c45c9b470dc1b9809.zip
Add source files
Diffstat (limited to 'wfpctl/src/extras/cli/util.cpp')
-rw-r--r--wfpctl/src/extras/cli/util.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/wfpctl/src/extras/cli/util.cpp b/wfpctl/src/extras/cli/util.cpp
new file mode 100644
index 0000000000..6181e68639
--- /dev/null
+++ b/wfpctl/src/extras/cli/util.cpp
@@ -0,0 +1,62 @@
+#include "stdafx.h"
+#include "util.h"
+#include "inlineformatter.h"
+#include "libcommon/string.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()));
+ }
+
+ return arg->second;
+}