Skip to content

Commit c472830

Browse files
commy27erraDystopian
authored
Add CBA_fnc_prettyFormat (#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 <[email protected]> * Added space between command and argument Co-authored-by: Dystopian <[email protected]> * 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 <[email protected]> * Removed ";" from returned values Co-authored-by: commy2 <[email protected]> * Fix linter Co-authored-by: commy2 <[email protected]> * Added parameter for line break char * Documentation for line break param * Default indentation 4 spaces * Renamed _tabs to _indents --------- Co-authored-by: 7erra <[email protected]> Co-authored-by: Dystopian <[email protected]>
1 parent 1ec3163 commit c472830

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

addons/strings/CfgFunctions.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class CfgFunctions {
99
PATHTO_FNC(formatElapsedTime);
1010
PATHTO_FNC(formatNumber);
1111
PATHTO_FNC(leftTrim);
12+
PATHTO_FNC(prettyFormat);
1213
PATHTO_FNC(removeWhitespace);
1314
PATHTO_FNC(replace);
1415
PATHTO_FNC(rightTrim);

addons/strings/fnc_prettyFormat.sqf

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "script_component.hpp"
2+
/* ----------------------------------------------------------------------------
3+
Function: CBA_fnc_prettyFormat
4+
5+
Description:
6+
Makes an array easy to read.
7+
8+
Parameters:
9+
_array - Array to format <ARRAY>
10+
_indents - Indentation string (optional, default: " ") <STRING>
11+
_lineBreak - Seperator string (optional, default: endl) <STRING>
12+
_depth - Initial indentation count (optional, default: 0) <NUMBER>
13+
14+
Returns:
15+
Formatted string <STRING>
16+
17+
Examples:
18+
(begin example)
19+
[[0, 1, ["22", 33, []], 4]] call CBA_fnc_prettyFormat;
20+
//[
21+
// 0,
22+
// 1,
23+
// [
24+
// "22",
25+
// 33,
26+
// []
27+
// ],
28+
// 4
29+
//]
30+
31+
[[0, 1, ["22", 33, []], 4], ">---"] call CBA_fnc_prettyFormat;
32+
//[
33+
//>---0,
34+
//>---1,
35+
//>---[
36+
//>--->---"22",
37+
//>--->---33,
38+
//>--->---[]
39+
//>---],
40+
//>---4
41+
//]
42+
43+
[[0, 1, ["22", 33, []], 4], ">---", "\n"] call CBA_fnc_prettyFormat;
44+
//[\n>---0,\n>---1,\n>---[\n>--->---"22",\n>--->---33,\n>--->---[]\n>---],\n>---4\n]
45+
(end)
46+
47+
Author:
48+
Terra, Dystopian, commy2
49+
50+
---------------------------------------------------------------------------- */
51+
params [
52+
["_array", [], [[]]],
53+
["_indent", " ", [""]],
54+
["_lineBreak", endl, [""]],
55+
["_depth", 0, [0]]
56+
];
57+
58+
private _indents = STRING_REPEAT(_indent, _depth);
59+
60+
if (_array isEqualTo []) exitWith {
61+
_indents + "[]" // return
62+
};
63+
64+
private _lines = _array apply {
65+
if (_x isEqualType []) then {
66+
[_x, _indent, _lineBreak, _depth + 1] call CBA_fnc_prettyFormat
67+
} else {
68+
_indents + _indent + str _x
69+
};
70+
};
71+
72+
_indents + "[" + _lineBreak + (_lines joinString ("," + _lineBreak)) + _lineBreak + _indents + "]" // return

addons/strings/script_component.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

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

14+
#define STRING_REPEAT(string, repeats) (call {\
15+
private _return = [];\
16+
_return resize (repeats);\
17+
_return apply {string} joinString ""\
18+
})
19+
1420
#define UTF8_TABLE [\
1521
["%20"," "],\
1622
["%21","!"],\

addons/strings/test.sqf

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define DEBUG_MODE_FULL
66
#include "script_component.hpp"
77

8-
#define TESTS ["strings"]
8+
#define TESTS ["strings", "prettyFormat"]
99

1010
SCRIPT(test-strings);
1111

addons/strings/test_prettyFormat.sqf

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// ----------------------------------------------------------------------------
2+
#define DEBUG_MODE_FULL
3+
#include "script_component.hpp"
4+
5+
SCRIPT(test_prettyFormat);
6+
7+
// ----------------------------------------------------------------------------
8+
9+
LOG("Testing CBA_fnc_prettyFormat");
10+
11+
private _fn = "CBA_fnc_prettyFormat";
12+
TEST_DEFINED("CBA_fnc_prettyFormat","");
13+
14+
private _val = [] call CBA_fnc_prettyFormat;
15+
private _exp = "[]";
16+
TEST_OP(_val,isEqualTo,_exp,_fn);
17+
18+
_val = [[], "xY", nil, 2] call CBA_fnc_prettyFormat;
19+
_exp = "xYxY[]";
20+
TEST_OP(_val,isEqualTo,_exp,_fn);
21+
22+
_val = [[0, 1, ["22", 33, []], 4]] call CBA_fnc_prettyFormat;
23+
_exp = [
24+
"[",
25+
" 0,",
26+
" 1,",
27+
" [",
28+
" ""22"",",
29+
" 33,",
30+
" []",
31+
" ],",
32+
" 4",
33+
"]"
34+
] joinString endl;
35+
TEST_OP(_val,isEqualTo,_exp,_fn);
36+
37+
_val = [[0, 1, ["22", 33, []], 4], ">---", "\n"] call CBA_fnc_prettyFormat;
38+
_exp = "[\n>---0,\n>---1,\n>---[\n>--->---""22"",\n>--->---33,\n>--->---[]\n>---],\n>---4\n]";
39+
TEST_OP(_val,isEqualTo,_exp,_fn);
40+
41+
_val = [[[[]]], """", endl, 1] call CBA_fnc_prettyFormat;
42+
_exp = [
43+
"""[",
44+
"""""[",
45+
"""""""[]",
46+
"""""]",
47+
"""]"
48+
] joinString endl;
49+
TEST_OP(_val,isEqualTo,_exp,_fn);

0 commit comments

Comments
 (0)