forked from CBATeam/CBA_A3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CBA_fnc_prettyFormat (CBATeam#1522)
* New function prettyPrint * Use spaces instead of tabs * Fixed spaces, parameters for tab char and indent * Removed trailing whitespace * Use isEqualTo instead of count Co-authored-by: Dystopian <sddex@ya.ru> * Added space between command and argument Co-authored-by: Dystopian <sddex@ya.ru> * Avoid count every loop * Correct indentation for empty arrays * Moved variable assignment inside of the if statement * Renamed prettyPrint to prettyFormat * Changed prettyPrint to prettyFormat in files * Assign variable from if statement * Join all elements explicitly at end of script * Added Dystopian and commy2 as authors * Fixed usage of forEach instead of apply * Return [] for empty arrays * Removed unnecessary brackets Co-authored-by: Dystopian <sddex@ya.ru> * Removed ";" from returned values Co-authored-by: commy2 <commy-2@gmx.de> * Fix linter Co-authored-by: commy2 <commy-2@gmx.de> * Added parameter for line break char * Documentation for line break param * Default indentation 4 spaces * Renamed _tabs to _indents --------- Co-authored-by: 7erra <38866692+7erra@users.noreply.github.com> Co-authored-by: Dystopian <sddex@ya.ru>
- Loading branch information
1 parent
1ec3163
commit c472830
Showing
5 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "script_component.hpp" | ||
/* ---------------------------------------------------------------------------- | ||
Function: CBA_fnc_prettyFormat | ||
Description: | ||
Makes an array easy to read. | ||
Parameters: | ||
_array - Array to format <ARRAY> | ||
_indents - Indentation string (optional, default: " ") <STRING> | ||
_lineBreak - Seperator string (optional, default: endl) <STRING> | ||
_depth - Initial indentation count (optional, default: 0) <NUMBER> | ||
Returns: | ||
Formatted string <STRING> | ||
Examples: | ||
(begin example) | ||
[[0, 1, ["22", 33, []], 4]] call CBA_fnc_prettyFormat; | ||
//[ | ||
// 0, | ||
// 1, | ||
// [ | ||
// "22", | ||
// 33, | ||
// [] | ||
// ], | ||
// 4 | ||
//] | ||
[[0, 1, ["22", 33, []], 4], ">---"] call CBA_fnc_prettyFormat; | ||
//[ | ||
//>---0, | ||
//>---1, | ||
//>---[ | ||
//>--->---"22", | ||
//>--->---33, | ||
//>--->---[] | ||
//>---], | ||
//>---4 | ||
//] | ||
[[0, 1, ["22", 33, []], 4], ">---", "\n"] call CBA_fnc_prettyFormat; | ||
//[\n>---0,\n>---1,\n>---[\n>--->---"22",\n>--->---33,\n>--->---[]\n>---],\n>---4\n] | ||
(end) | ||
Author: | ||
Terra, Dystopian, commy2 | ||
---------------------------------------------------------------------------- */ | ||
params [ | ||
["_array", [], [[]]], | ||
["_indent", " ", [""]], | ||
["_lineBreak", endl, [""]], | ||
["_depth", 0, [0]] | ||
]; | ||
|
||
private _indents = STRING_REPEAT(_indent, _depth); | ||
|
||
if (_array isEqualTo []) exitWith { | ||
_indents + "[]" // return | ||
}; | ||
|
||
private _lines = _array apply { | ||
if (_x isEqualType []) then { | ||
[_x, _indent, _lineBreak, _depth + 1] call CBA_fnc_prettyFormat | ||
} else { | ||
_indents + _indent + str _x | ||
}; | ||
}; | ||
|
||
_indents + "[" + _lineBreak + (_lines joinString ("," + _lineBreak)) + _lineBreak + _indents + "]" // return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// ---------------------------------------------------------------------------- | ||
#define DEBUG_MODE_FULL | ||
#include "script_component.hpp" | ||
|
||
SCRIPT(test_prettyFormat); | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
LOG("Testing CBA_fnc_prettyFormat"); | ||
|
||
private _fn = "CBA_fnc_prettyFormat"; | ||
TEST_DEFINED("CBA_fnc_prettyFormat",""); | ||
|
||
private _val = [] call CBA_fnc_prettyFormat; | ||
private _exp = "[]"; | ||
TEST_OP(_val,isEqualTo,_exp,_fn); | ||
|
||
_val = [[], "xY", nil, 2] call CBA_fnc_prettyFormat; | ||
_exp = "xYxY[]"; | ||
TEST_OP(_val,isEqualTo,_exp,_fn); | ||
|
||
_val = [[0, 1, ["22", 33, []], 4]] call CBA_fnc_prettyFormat; | ||
_exp = [ | ||
"[", | ||
" 0,", | ||
" 1,", | ||
" [", | ||
" ""22"",", | ||
" 33,", | ||
" []", | ||
" ],", | ||
" 4", | ||
"]" | ||
] joinString endl; | ||
TEST_OP(_val,isEqualTo,_exp,_fn); | ||
|
||
_val = [[0, 1, ["22", 33, []], 4], ">---", "\n"] call CBA_fnc_prettyFormat; | ||
_exp = "[\n>---0,\n>---1,\n>---[\n>--->---""22"",\n>--->---33,\n>--->---[]\n>---],\n>---4\n]"; | ||
TEST_OP(_val,isEqualTo,_exp,_fn); | ||
|
||
_val = [[[[]]], """", endl, 1] call CBA_fnc_prettyFormat; | ||
_exp = [ | ||
"""[", | ||
"""""[", | ||
"""""""[]", | ||
"""""]", | ||
"""]" | ||
] joinString endl; | ||
TEST_OP(_val,isEqualTo,_exp,_fn); |