Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
0.3.0
0.3.0 / 2025-11-24
- Improved documentation
- Added "start" option to PrettyPrint for prefix control
- Added custom "label" support for tensor formatting in PrettyPrint
- Added web environment `<pre>` wrapping support

0.2.1 / 2025-11-22
- Added Github Actions
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ composer require apphp/pretty-print

## Usage

Note: When used in web (non-CLI) environments, output is automatically wrapped in `<pre>` to preserve spacing. In CLI, no wrapping is applied.

### Global helper functions

// Print scalars/strings
Print scalars/strings
```php
pprint('Hello', 123, 4.56);
// Hello 123 4.5600
Expand Down Expand Up @@ -47,6 +49,16 @@ pprint($matrix);
// ])
```

Custom label instead of "tensor"
```php
pprint($matrix, label: 'arr');
// arr([
// [ 1, 2, 3, 4, 5],
// [ 6, 7, 8, 9, 10],
// [11, 12, 13, 14, 15]
// ])
```

2D tensor-style formatting with summarization
```php
$matrix = [
Expand Down Expand Up @@ -81,7 +93,7 @@ pprint($tensor3d, headB: 1, tailB: 1, headRows: 1, tailRows: 1, headCols: 1, tai
// ])
```

New line control
Postfix and prefix control
```php
// No newline at the end (like Python's end="")
pprint('Same line', end: '');
Expand All @@ -90,6 +102,11 @@ pprint('Add line');
pprint('Add line', end: "\n");
// Added addedional 2 newlines at the end after printing
pprint('Add 2 lines', end: "\n\n");

// Add a prefix at the start of the printed string
pprint('Tabbed', start: "\t");
// Combine with end to avoid newline
pprint('Prompted', start: '>>> ', end: '');
```

Print and then exit the script
Expand Down Expand Up @@ -120,7 +137,9 @@ $pp('Metrics:', [[0.91, 0.02], [0.03, 0.88]]);

### Options reference

- **start**: string. Prefix printed before the content. Example: `pprint('Hello', ['start' => "\t"])`.
- **end**: string. Line terminator, default to new line. Example: `pprint('no newline', ['end' => '']);`
- **label**: string. Prefix label for 2D/3D formatted arrays, default `tensor`. Example: `pprint($m, ['label' => 'arr'])`.
- **headB / tailB**: ints. Number of head/tail 2D blocks shown for 3D tensors.
- **headRows / tailRows**: ints. Rows shown per 2D slice with ellipsis between.
- **headCols / tailCols**: ints. Columns shown per 2D slice with ellipsis between.
Expand Down
Loading