Pretty print

billy2
billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,206 PRO
edited November 2023 in FeatureScript
I'm trying to write a simple feature script and got caught up on pretty printing maps. I wrote a simple function that parses the returned data and formats it into a readable structure. Below is a listing of the pretty print function.

</code>//pretty print</pre><pre class="CodeBlock"><code>
export function p(s)
{
    var str = splitIntoCharacters(toString(s));
    var i = 0; //indent
    var c; //cnt
    var t = "  "; //tab

    // println(size(str));

    for (c = 0; c < size(str); c += 1)
    {

        if (str[c] == "{")
        {
            print(str[c]);
            print("\n");
            i += 1;
            for (var a = 0; a < i; a += 1)
                print(t);
        }

        if (str[c] == "[")
        {
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
            print(str[c]);
            print("\n");
            i += 1;
            for (var a = 0; a < i; a += 1)
                print(t);
        }

        if (str[c] == "}")
        {
            i -= 1;
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
            print(str[c]);
        }

        if (str[c] == "]")
        {
            i -= 1;
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
            print(str[c]);
        }

        if (str[c] == ",")
        {
            print(str[c]);
            print("\n");
            for (var a = 0; a < i; a += 1)
                print(t);
        }

        if (str[c] != "{" && str[c] != "}" && str[c] != "[" && str[c] != "]" && str[c] != "," && str[c] != " ")
            print(str[c]);
    }
    println();
}

Below is displaying 2 simple maps. The 1st is a standard string based representation of the map. The 2nd is the same map except it's pretty printed showing the structure of the map.


Below is a more complex map showing both the simple & the pretty version of the map.


The problem comes when you're trying to access something inside the map. Without pretty print, it's hard to create an accessor to a value contained inside the map.

JSON has the same issues when working with an API. Javascript has many string -> object and object -> string operators and it's easy to create pretty print so you can understand an objects structure.

There may be better ways to understand the structure from a response in the FS console, if there is a better pattern, please let me know.

Comments

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,967 PRO
    edited November 2023

    Legit! I will use this for sure.


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • NeilCooke
    NeilCooke Moderator, Onshape Employees Posts: 6,126 image
    Thanks Billy! I was also going to do this but didn't get around to it.
    Senior Director, Technical Services, EMEA
  • billy2
    billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,206 PRO
    Thanks guys, I've already found it helpful with understanding what OS is returning. I tend to look for the [] brackets knowing that these are lists and then trying to figure out if I can use the list. I'm already collecting a list of faces and able to dance through them trying to determine if I can work on them.


  • xTimRice
    xTimRice Member Posts: 61 PRO
    Super helpful! All this time I’ve just been struggling through the block of text 😂
    Handcrafted furniture in Boston
    https://thesmoothcut.com/
  • eric_pesty
    eric_pesty Member, pcbaevp Posts: 2,941 PRO
    That was one of my (many!) major pain points during my brief foray into API stuff!
    This would have helped and will keep it in mind if I find myself messing around with the API for sure!
  • _anton
    _anton Member, Onshape Employees Posts: 555 image
  • billy2
    billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,206 PRO
    @_anton I like it, can we make it standard vs. a function call?


  • _anton
    _anton Member, Onshape Employees Posts: 555 image
    edited November 2023
    Our internal process involves oodles of code review and testing, so, to prioritize any Onshape change, the formal way is to file an improvement request. :)

    (If this makes it into the FS standard library, it will probably end up as a function in string.fs, same as println.)