Skip to content

Instantly share code, notes, and snippets.

@vkargov
Last active August 7, 2017 22:15
Show Gist options
  • Save vkargov/f14ac96fc57e972f8f3d4acd8836e17b to your computer and use it in GitHub Desktop.
Save vkargov/f14ac96fc57e972f8f3d4acd8836e17b to your computer and use it in GitHub Desktop.
fold_labels.cs
// 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