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 10 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
71 changes: 71 additions & 0 deletions addons/arrays/fnc_prettyFormat.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_prettyPrint
commy2 marked this conversation as resolved.
Show resolved Hide resolved

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_prettyPrint;
// _return ==>
// "[
// 0,
// 1,
// [
// ""22"",
// 33,
// []
// ],
// 4
// ]"
_return = [ [0, 1, ["22", 33, []], 4], ">---" ] call CBA_fnc_prettyPrint;
// _return ==>
// [
// >---0,
// >---1,
// >---[
// >--->---""22"",
// >--->---33,
// >--->---[]
// >---],
// >---4
// ]
(end)

Author:
Terra
---------------------------------------------------------------------------- */
SCRIPT(prettyPrint);
params [["_array", [], [[]]], ["_tab", toString [9], [""]], ["_depth", 0, [0]]];
private _tabs = [];
_tabs resize _depth;
_tabs = _tabs apply {_tab} joinString "";
7erra marked this conversation as resolved.
Show resolved Hide resolved
if (_array isEqualTo []) exitWith {
_tabs + "[]"
};
private _lines = [_tabs + "["];
private _lastIndex = count _array - 1;
{
private _line = "";
commy2 marked this conversation as resolved.
Show resolved Hide resolved
if (_x isEqualType []) then {
_line = _line + ([_x, _tab, _depth + 1] call CBA_fnc_prettyPrint);
} else {
_line = _tab + _tabs + _line + str _x;
commy2 marked this conversation as resolved.
Show resolved Hide resolved
};
if (_forEachIndex != _lastIndex) then {
_line = _line + ",";
};
_lines pushBack _line;
} forEach _array;

_lines pushBack (_tabs + "]");
_lines joinString endl