1
1
/*
2
- * Copyright (c) 2002-2022 , the original author(s).
2
+ * Copyright (c) 2002-2023 , the original author(s).
3
3
*
4
4
* This software is distributable under the BSD license. See the terms of the
5
5
* BSD license in the documentation provided with this software.
@@ -337,14 +337,15 @@ private void internalPrintln(Map<String, Object> options, Object object) {
337
337
String style = (String ) options .getOrDefault (Printer .STYLE , "" );
338
338
options .put (Printer .STYLE , valueHighlighter (style ));
339
339
int width = (int ) options .get (Printer .WIDTH );
340
+ int maxrows = (int ) options .get (Printer .MAXROWS );
340
341
if (!style .isEmpty () && object instanceof String ) {
341
- highlightAndPrint (width , (SyntaxHighlighter ) options .get (Printer .STYLE ), (String ) object , true );
342
+ highlightAndPrint (width , (SyntaxHighlighter ) options .get (Printer .STYLE ), (String ) object , true , maxrows );
342
343
} else if (style .equalsIgnoreCase ("JSON" )) {
343
344
if (engine == null ) {
344
345
throw new IllegalArgumentException ("JSON style not supported!" );
345
346
}
346
347
String json = engine .toJson (object );
347
- highlightAndPrint (width , (SyntaxHighlighter ) options .get (Printer .STYLE ), json , true );
348
+ highlightAndPrint (width , (SyntaxHighlighter ) options .get (Printer .STYLE ), json , true , maxrows );
348
349
} else if (options .containsKey (Printer .SKIP_DEFAULT_OPTIONS )) {
349
350
highlightAndPrint (options , object );
350
351
} else if (object instanceof Exception ) {
@@ -354,7 +355,7 @@ private void internalPrintln(Map<String, Object> options, Object object) {
354
355
} else if (object instanceof String || object instanceof Number ) {
355
356
String str = object .toString ();
356
357
SyntaxHighlighter highlighter = (SyntaxHighlighter ) options .getOrDefault (Printer .VALUE_STYLE , null );
357
- highlightAndPrint (width , highlighter , str , doValueHighlight (options , str ));
358
+ highlightAndPrint (width , highlighter , str , doValueHighlight (options , str ), maxrows );
358
359
} else {
359
360
highlightAndPrint (options , object );
360
361
}
@@ -464,14 +465,20 @@ private boolean doValueHighlight(Map<String, Object> options, String value) {
464
465
}
465
466
}
466
467
467
- private void highlightAndPrint (int width , SyntaxHighlighter highlighter , String object , boolean doValueHighlight ) {
468
- for (String s : object .split ("\\ r?\\ n" )) {
468
+ private void highlightAndPrint (
469
+ int width , SyntaxHighlighter highlighter , String object , boolean doValueHighlight , int maxrows ) {
470
+ String [] rows = object .split ("\\ r?\\ n" , maxrows );
471
+ int lastRowIdx = rows .length == maxrows ? rows .length - 1 : rows .length ;
472
+ for (int i = 0 ; i < lastRowIdx ; i ++) {
469
473
AttributedStringBuilder asb = new AttributedStringBuilder ();
470
- List <AttributedString > sas = asb .append (s ).columnSplitLength (width );
474
+ List <AttributedString > sas = asb .append (rows [ i ] ).columnSplitLength (width );
471
475
for (AttributedString as : sas ) {
472
476
highlight (width , highlighter , as .toString (), doValueHighlight ).println (terminal ());
473
477
}
474
478
}
479
+ if (rows .length == maxrows ) {
480
+ throw new TruncatedOutputException ("Truncated output: " + maxrows );
481
+ }
475
482
}
476
483
477
484
private Map <String , Object > keysToString (Map <Object , Object > map ) {
@@ -749,6 +756,7 @@ private boolean isNumber(String str) {
749
756
@ SuppressWarnings ("unchecked" )
750
757
private void highlightAndPrint (Map <String , Object > options , Object obj ) {
751
758
int width = (int ) options .get (Printer .WIDTH );
759
+ int maxrows = (int ) options .get (Printer .MAXROWS );
752
760
totLines = 0 ;
753
761
String message = null ;
754
762
RuntimeException runtimeException = null ;
@@ -758,10 +766,9 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
758
766
highlightMap (options , keysToString ((Map <Object , Object >) obj ), width );
759
767
} else if (collectionObject (obj )) {
760
768
List <Object > collection = objectToList (obj );
761
- if (collection .size () > (int ) options .get (Printer .MAXROWS )) {
762
- message = "Truncated output: " + options .get (Printer .MAXROWS ) + "/" + collection .size ();
763
- collection =
764
- collection .subList (collection .size () - (int ) options .get (Printer .MAXROWS ), collection .size ());
769
+ if (collection .size () > maxrows ) {
770
+ message = "Truncated output: " + maxrows + "/" + collection .size ();
771
+ collection = collection .subList (collection .size () - maxrows , collection .size ());
765
772
}
766
773
if (!collection .isEmpty ()) {
767
774
if (collection .size () == 1 && !options .containsKey (Printer .ONE_ROW_TABLE )) {
@@ -771,7 +778,8 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
771
778
} else if (canConvert (elem ) && !options .containsKey (Printer .TO_STRING )) {
772
779
highlightMap (options , objectToMap (options , elem ), width );
773
780
} else if (elem instanceof String && options .get (Printer .STYLE ) != null ) {
774
- highlightAndPrint (width , (SyntaxHighlighter ) options .get (Printer .STYLE ), (String ) elem , true );
781
+ highlightAndPrint (
782
+ width , (SyntaxHighlighter ) options .get (Printer .STYLE ), (String ) elem , true , maxrows );
775
783
} else {
776
784
highlightValue (options , null , objectToString (options , obj ))
777
785
.println (terminal ());
0 commit comments