Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Pretty print
billy2
Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,167 PRO
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.
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.
</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.
5
Comments
-
Legit! I will use this for sure.
RENDERCAD
rendercad.ai - Photorealistic product rendering.
▚▞▚▞▚▞▚▞▚
________________________________________________________________________0 -
Thanks Billy! I was also going to do this but didn't get around to it.Senior Director, Technical Services, EMEA0
-
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.
0 -
Super helpful! All this time I’ve just been struggling through the block of text 😂0
-
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!0 -
Very nice! To make it slightly more concise, without stringification: https://cad.onshape.com/documents/eb712303e88373103a085b5c/w/fedd55f887653eb812cf1613/e/5b9dfee9d3b70a36176bd8c51
-
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.)2





