Last active
August 7, 2017 22:15
-
-
Save vkargov/f14ac96fc57e972f8f3d4acd8836e17b to your computer and use it in GitHub Desktop.
fold_labels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cat thoughts.dot | mono fold_labels.exe 40 | unflatten | dot -Tpdf > thoughts.pdf && open thoughts.pdf | |
// unflatten makes the graph less... flat | |
using System; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
class Fold | |
{ | |
static void FoldTillQuotes (StringBuilder s, int i, int maxlen) | |
{ | |
int last_space = -1; | |
int last_wrap = i; | |
do { | |
if (s[i] == ' ') | |
last_space = i; | |
if (i - last_wrap >= maxlen && last_space != -1) { | |
s[last_space] = '\n'; | |
last_wrap = i; | |
} | |
} while (s[i++] != '"'); | |
} | |
static void Main (string[] args) | |
{ | |
string line; | |
var regex = new Regex("label ?= ?\""); | |
int maxlen; | |
if (args.Length == 0) | |
maxlen = 40; | |
else | |
maxlen = int.Parse (args[0]); | |
while ((line = Console.ReadLine()) != null) | |
{ | |
var sbline = new StringBuilder(line); | |
var matches = regex.Matches (line); | |
if (matches.Count > 0) { | |
foreach (Match match in matches) | |
FoldTillQuotes (sbline, match.Index+match.Length, maxlen); | |
} | |
Console.WriteLine (sbline.ToString()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment