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 20 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(prettyFormat);
PATHTO_FNC(reject);
PATHTO_FNC(select);
PATHTO_FNC(selectBest);
Expand Down
65 changes: 65 additions & 0 deletions addons/arrays/fnc_prettyFormat.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_prettyFormat

Description:
Makes an array easier to read.

Parameters:
_array - The array to format <ARRAY>
_tab - The tab char to use (optional, default: tab) <STRING>
_depth - Starting indent (optional, default: 0) <NUMBER>

Returns:
Formatted string <STRING>

Examples:
(begin example)
_return = [ [0, 1, ["22", 33, []], 4] ] call CBA_fnc_prettyFormat;
// _return ==>
// "[
// 0,
// 1,
// [
// ""22"",
// 33,
// []
// ],
// 4
// ]"
_return = [ [0, 1, ["22", 33, []], 4], ">---" ] call CBA_fnc_prettyFormat;
// _return ==>
// [
// >---0,
// >---1,
// >---[
// >--->---""22"",
// >--->---33,
// >--->---[]
// >---],
// >---4
// ]
(end)

Author:
Terra, Dystopian, commy2
---------------------------------------------------------------------------- */
SCRIPT(prettyFormat);
params [["_array", [], [[]]], ["_tab", toString [9], [""]], ["_lineBreak", endl, [""]], ["_depth", 0, [0]]];
commy2 marked this conversation as resolved.
Show resolved Hide resolved
private _tabs = [];
_tabs resize _depth;
_tabs = (_tabs apply {_tab}) joinString "";

if (_array isEqualTo []) exitWith {
_tabs + "[]"
};

private _lines = _array apply {
if (_x isEqualType []) then {
[_x, _tab, _lineBreak, _depth + 1] call CBA_fnc_prettyFormat
} else {
_tab + _tabs + str _x
};
};

_tabs + "[" + _lineBreak + (_lines joinString ("," + _lineBreak)) + _lineBreak + _tabs + "]"