commit 09f76348c7262c0af259ea58f2cf14ab97473c20
parent 539e3e7517fc4138a64aeaa3caf90cc8818cd677
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Fri, 26 Aug 2022 21:54:11 +0100
elastic-tabstops: Add ET converter
Diffstat:
1 file changed, 106 insertions(+), 0 deletions(-)
diff --git a/src/elastic-tabstops.cpp b/src/elastic-tabstops.cpp
@@ -0,0 +1,106 @@
+#include <iostream>
+#include <vector>
+#include <cassert>
+#include <sstream>
+#include <memory>
+
+#if 1
+#define DIM "\e[2m"
+#define RESET "\e[m"
+#define INDENT DIM " >> " RESET
+#define PADDING DIM " ยป" RESET
+#else
+#define INDENT " "
+#define PADDING " "
+#endif
+
+using namespace std;
+
+struct column
+{
+ size_t width;
+ bool is_indent;
+};
+
+struct cell
+{
+ string str;
+ shared_ptr<column> column_ref;
+};
+
+struct line
+{
+ vector<cell> cells;
+ string tail;
+};
+
+vector<line> backlog;
+
+void flush_backlog()
+{
+ for (auto& line : backlog)
+ {
+ for (auto& c : line.cells)
+ {
+ if (c.column_ref->is_indent)
+ cout << INDENT;
+ else
+ {
+ c.str.resize(c.column_ref->width, ' ');
+ cout << c.str << PADDING;
+ }
+ }
+ cout << line.tail << endl;
+ }
+ backlog.clear();
+}
+
+int main()
+{
+ string buf;
+ vector<size_t> chunk_stack;
+ vector<shared_ptr<column>> columns;
+
+ while (getline(cin, buf))
+ {
+ struct line l;
+ istringstream in2(move(buf));
+
+ while (getline(in2, buf, '\t'))
+ l.cells.push_back({move(buf), {}});
+
+ if (l.cells.size())
+ {
+ l.tail = move(l.cells.back().str);
+ l.cells.pop_back();
+ }
+
+ columns.resize(l.cells.size());
+
+ bool indenting = true;
+ for (size_t i = 0; i < columns.size(); ++i)
+ {
+ auto& p = columns[i];
+ auto width = l.cells[i].str.size();
+
+ if (width)
+ indenting = false;
+
+ if (p)
+ {
+ p->width = max(p->width, width);
+ p->is_indent &= indenting;
+ } else
+ p = make_shared<column>(column{width, indenting});
+
+ l.cells[i].column_ref = p;
+ }
+
+ backlog.push_back(move(l));
+ auto& C = backlog.back().cells;
+
+ if (C.empty())
+ flush_backlog();
+ }
+ flush_backlog();
+}