Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format specifier for output of arrays via "%#..." by using known type specifiers when printing #5246

Open
just-like-that opened this issue Aug 24, 2022 · 1 comment

Comments

@just-like-that
Copy link

just-like-that commented Aug 24, 2022

Describe the project you are working on

A vector drawing program with primitive drawing routines.

Describe the problem or limitation you are having in your project

For logging and debugging reasons I frequently put out arrays' contents.

Especially float type and vector type tend to have long decimal parts which are usually of less interest to me.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add format specifier for arrays by using already existing type specifiers for int, float and vector.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

On output of an array of floats write for instance:
print("%[%10.3f]" % [array])
like in this example but for all values of type float:

print("%10.3f" % 10000.5555)
# Output: " 10000.556"

On output of an array of vectors write for instance:
print("%[%.2v]" % [array])

On output of an array of ints write for instance:
print("%[%010d]" % [array])
like in this example but for all values of type int:

print("%010d" % 12345)
# output: "0000012345"

If you have different types in one array use this syntax to support each separately:
print("%[%010d %10.3f %.2v]" % [array])

So the outer "%[]" acts as a clip for the other format specifiers and qualifies for arrays.
Different format specifier are separated by a space. Each format specifier is written as before.

The rest works like today with the already existing syntax.

If this enhancement will not be used often, can it be worked around with a few lines of script?

I think it will be used often. As output of arrays during development is quite common and easier than looking into them in debug.

Is there a reason why this should be core and not an add-on in the asset library?

Format specifier are core already.

GDScript format strings

@just-like-that just-like-that changed the title Format specifier for output of arrays by using known type specifiers when printing Format specifier for output of arrays via "%[...]" by using known type specifiers when printing Aug 24, 2022
@just-like-that
Copy link
Author

just-like-that commented Sep 7, 2023

Update to my original proposal.

As the hash character looks like an array already and it's currently NOT used as format specifier I recommend to use it as array specifier instead of the brackets (see above).

So the already existing format specifiers are used in conjunction with # (the hash character) to format all items of this type in the array given.

It looks like: %#<existing_format_specifier>

E.g. A.)
array = [1.23456, 9.87654]

print("%#10.3f" % [array])

outputs: "[1.235, 9.877]"

E.g. B.)
array = [123_456, 987_654]

print("%#010d" % [array])

outputs: "[0000123456, 000987654]"

E.g. C.)
array = [1.23456, 9.87654]

print("%#.2f" % [array])

outputs: "[1.23, 9.88]"

E.g. D.)
array = [Vector2(1.23456, 9.87654)]

print("%#.2v" % [array])

outputs: "(1.23, 9.88)"

@just-like-that just-like-that changed the title Format specifier for output of arrays via "%[...]" by using known type specifiers when printing Format specifier for output of arrays via "%#..." by using known type specifiers when printing Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants