javastringkotlinindentationconsole-output

Properly indent Java/Kotlin console output


I am outputting a lot of information to the console that is gathered across multiple nested calls. I would like to be able to print the information on the screen in a readable manner but the I can't seem to get the indentation right, without hardcoding the number the number of \ts.

Basically, how can I get my code to indent based on the indentation level of the previous line. If the previous line is indented with \t and I do "\n\t", after that, I would like the new line to indent with respect to the previous line. Meaning I expected to be like

String str = "original line (no indention)"
+ "\n"
+ "\t originally indented line"
+ "\n"
+ "\t the second indented line"

The output is

original line (no indention)
    originally indented line
    the second indented line

but I would like it to be

original line (no indention)
    originally indented line
         the second indented line

Please keep in mind that in my actual code each level of indention is a result of aggregation from a different file so it's hard to just know to indent twice on the second line. I need it to be able to indent simply based on the indentation of previous line so I don't have to hard code the levels of indentation.


Solution

  • Ended up, replacing every new line \n with \n\t during each iteration and that seemed to do the trick. Crazy how it was such a simple solution that I over looked.