Search code examples
c#pretty-print

Minimod Pretty Print - How to customize line breaks?


I'm using Minimod.PrettyPrint. For multidimensional lists and arrays, the PrettyPrint() function inserts line breaks between elements automatically if the elements are over a certain length.

My question is - How do I control when the line breaks happen between array elements? Specifically, I'd like all array elements to have a line break between them. For example, I would like the function

// myArray is of type int[][] 
myArray.PrettyPrint(/*insert customization here to make the line break*/);

to output

[
  [<elements of myArray[0]>],
  [<elements of myArray[1]>],
  [<elements of myArray[2]>]
]

By default, PrettyPrint() will print this as

 [[<elements of myArray[0]>],[<elements of myArray[1]>],[<elements of myArray[2]>]]

if the length the string written by PrettyPrint() is below a certain length.

There is an overload of PrettyPrint() that takes an object to customize the output, but I can't find examples of how to use it.

Edit: I've tried

PrettyPrintMinimod.Settings settings = new PrettyPrintMinimod.Settings();
settings.PreferMultiline(true);
myArray.PrettyPrint(settings);

and it doesn't seem to do anything. If I pass settings.PreferMultiline(false) to an array with many elements, I still get line breaks, and if I pass settings.PreferMultiline(true) I don't get line breaks.


Solution

  • It works OK for me:

    var test = new int[][] { new int[] {1,2,3}, new int[] {4,5,6} };
    
    var settings = new PrettyPrintMinimod.Settings();
    settings.PreferMultiline(true);
    Debug.WriteLine(test.PrettyPrint(settings));
    

    It produces output like this:

    [
      [
        1,
        2,
        3
      ],
      [
        4,
        5,
        6
      ]
    ]
    

    If that's not really what you're after, there's always the nuclear option - write your own formatter:

    settings.RegisterFormatterFor(typeof(int[][]), o => {
        var array2d = (int[][])o;
        return "[\r\n" + string.Join(",\r\n", 
            array2d.Select(array1d => "\t[" + string.Join(", ", 
                array1d.Select(s => s.ToString())) + "]")) + "\r\n]";
    });
    
    Debug.WriteLine(test.PrettyPrint(settings));
    

    Which produces output like this:

    [
        [1, 2, 3],
        [4, 5, 6]
    ]