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

Add CBA_fnc_prettyFormat #1522

Merged
merged 24 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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/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);