Skip to content

Commit

Permalink
Add CBA_fnc_prettyFormat (#1522)
Browse files Browse the repository at this point in the history
* 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
3 people committed Jun 24, 2023
1 parent 1ec3163 commit c472830
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions addons/strings/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CfgFunctions {
PATHTO_FNC(formatElapsedTime);
PATHTO_FNC(formatNumber);
PATHTO_FNC(leftTrim);
PATHTO_FNC(prettyFormat);
PATHTO_FNC(removeWhitespace);
PATHTO_FNC(replace);
PATHTO_FNC(rightTrim);
Expand Down
72 changes: 72 additions & 0 deletions addons/strings/fnc_prettyFormat.sqf
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
6 changes: 6 additions & 0 deletions addons/strings/script_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

#include "\x\cba\addons\main\script_macros.hpp"

#define STRING_REPEAT(string, repeats) (call {\
private _return = [];\
_return resize (repeats);\
_return apply {string} joinString ""\
})

#define UTF8_TABLE [\
["%20"," "],\
["%21","!"],\
Expand Down
2 changes: 1 addition & 1 deletion addons/strings/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["strings"]
#define TESTS ["strings", "prettyFormat"]

SCRIPT(test-strings);

Expand Down
49 changes: 49 additions & 0 deletions addons/strings/test_prettyFormat.sqf
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);

0 comments on commit c472830

Please sign in to comment.