aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.hh
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2020-03-24 14:17:10 +0100
committerEelco Dolstra <edolstra@gmail.com>2020-03-24 14:26:23 +0100
commit7a8de57d3e7e53a4f6d8060c7201f3fbbe6cd325 (patch)
treee418ac74ab2a2dd45e4ea4c8ecb7017d8b76e9b8 /src/libutil/util.hh
parent4260a22a55d7afc931b22e8c41282fbd0ed18a77 (diff)
Pretty-print 'nix why-depends' / 'nix-store -q --tree' output
Extracted from 678301072f05b650dc15c5edb4c25f08f0d6cace.
Diffstat (limited to 'src/libutil/util.hh')
-rw-r--r--src/libutil/util.hh34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 8c2cef9e1..acef682cd 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -451,6 +451,13 @@ void ignoreException();
#define ANSI_BLUE "\e[34;1m"
+/* Tree formatting. */
+constexpr char treeConn[] = "├───";
+constexpr char treeLast[] = "└───";
+constexpr char treeLine[] = "│ ";
+constexpr char treeNull[] = " ";
+
+
/* Truncate a string to 'width' printable characters. If 'filterAll'
is true, all ANSI escape sequences are filtered out. Otherwise,
some escape sequences (such as colour setting) are copied but not
@@ -576,4 +583,31 @@ extern PathFilter defaultPathFilter;
AutoCloseFD createUnixDomainSocket(const Path & path, mode_t mode);
+// A Rust/Python-like enumerate() iterator adapter.
+// Borrowed from http://reedbeta.com/blog/python-like-enumerate-in-cpp17.
+template <typename T,
+ typename TIter = decltype(std::begin(std::declval<T>())),
+ typename = decltype(std::end(std::declval<T>()))>
+constexpr auto enumerate(T && iterable)
+{
+ struct iterator
+ {
+ size_t i;
+ TIter iter;
+ bool operator != (const iterator & other) const { return iter != other.iter; }
+ void operator ++ () { ++i; ++iter; }
+ auto operator * () const { return std::tie(i, *iter); }
+ };
+
+ struct iterable_wrapper
+ {
+ T iterable;
+ auto begin() { return iterator{ 0, std::begin(iterable) }; }
+ auto end() { return iterator{ 0, std::end(iterable) }; }
+ };
+
+ return iterable_wrapper{ std::forward<T>(iterable) };
+}
+
+
}