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

New function: prettyFormat #1519

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CfgFunctions {
PATHTO_FNC(inject);
PATHTO_FNC(insert);
PATHTO_FNC(join);
PATHTO_FNC(prettyPrint);
PATHTO_FNC(reject);
PATHTO_FNC(select);
PATHTO_FNC(selectBest);
Expand Down
67 changes: 67 additions & 0 deletions addons/arrays/fnc_prettyPrint.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_prettyPrint

Description:
Makes an array easier to read.

Parameters:
_this - Array <ARRAY>

Returns:
Formatted string <STRING>

Examples:
(begin example)
_return = [0, 1, ["22", 33, []], 4] call CBA_fnc_prettyPrint;
// _return ==>
// "[
// 0,
// 1,
// [
// ""22"",
// 33,
// []
// ],
// 4
// ]"
commy2 marked this conversation as resolved.
Show resolved Hide resolved
(end)

Author:
Terra
---------------------------------------------------------------------------- */
SCRIPT(prettyPrint);

if (count _this == 0) exitWith {
"[]"
};
private _lines = ["["];
7erra marked this conversation as resolved.
Show resolved Hide resolved
// Recursive function calls have _depth already defined
commy2 marked this conversation as resolved.
Show resolved Hide resolved
private _depth = if (isNil "_depth") then {
0
} else {
_depth + 1
};

{
private _line = "";
for "_i" from 1 to (_depth + 1) do {
_line = " " + _line;
};
_line = if (_x isEqualType []) then {
_line + (_x call CBA_fnc_prettyPrint);
} else {
_line + str _x;
};
if (_forEachIndex != count _this - 1) then {
_line = _line + ",";
};
_lines pushBack _line;
} forEach _this;

private _closingBracket = "";
for "_i" from 1 to _depth do {
_closingBracket = " " + _closingBracket;
};
_lines pushBack (_closingBracket + "]");
_lines joinString endl