Output - Sam36502/SimpleTUI GitHub Wiki

String Alignment

Left Align

If you want to format a string to have all the text moved to the left of a fixed size string and padded with spaces, then the leftAlign(string, length) method is what you need. This will pad the right side of any string you give it until the desired length is reached. It won't trim the string. If the length is shorter than the string, it will simply return the string without any changes.

Example:

Output.leftAlign("Hello!", 10);
// Returns "Hello!    "

Output.leftAlign("Hi", 10);
// Returns "Hi        "

Output.leftAlign("Hello!", 5);
// Returns "Hello!"

Right Align

If you want to format a string to have all the text moved to the right of a fixed size string and padded with spaces, then the rightAlign(string, length) method is what you need. This will pad the left side of any string you give it until the desired length is reached. It won't trim the string. If the length is shorter than the string, it will simply return the string without any changes.

Example:

Output.rightAlign("Hello!", 10);
// Returns "    Hello!"

Output.rightAlign("Hi", 10);
// Returns "        Hi"

Output.rightAlign("Hello!", 5);
// Returns "Hello!"

Centre Align

If you want to format a string to have all the text moved to the centre of a fixed size string and padded with spaces, then the centerAlign(string, length) method is what you need. This will pad the left and right side of any string you give it until the desired length is reached. It won't trim the string. If the length is shorter than the string, it will simply return the string without any changes. If the desired length is uneven, it will pad the right side one more than the left.

Example:

Output.rightAlign("Hello!", 9);
// Returns " Hello!  "

Output.rightAlign("Hi", 9);
// Returns "   Hi    "

Output.rightAlign("Hello!", 5);
// Returns "Hello!"

Clearing The Screen

To clear the screen use Output.clearScreen(). At the moment, to keep this library as portable as possible, this clear function is not really a clear function. It currently simply prints out one hundred carriage-returns.

I have planned to add a proper portable clear-screen function by Version 2.2.

Basic Bitmap Images

If you have an integer matrix and you wish to translate it to a 2D ASCII image, you can use the bitmap(array) method.

If not supplied with any arguments apart from a matrix of integers, the function will output every 1 as a # and everything else as a whitespace.

Example:

int[][] mat = {{1, 0, 0, 1}, {2, 3, 4, 5}, {1, 0, 0, 1}, {99, 1, 1, 999}};
Output.bitmap(mat);
// Returns this String:
"#  #
     
 #  #
  ## 
"

If you wish to use a different character for the pixel, you can specify this with a character following the array parameter:

Output.bitmap(mat, '*'); // Same array as above
// Returns this String:
"*  *
     
 *  *
  ** 
"

This can also be done with a matrix of boolean values.

boolean[][] mat = {{true, false, false, true},
                   {false, false, false, false},
                   {true, false, false, true},
                   {false, true, true, false}};
Output.bitmap(mat, '*'); // Also uses '#' if no char is given
// Returns this String:
"*  *
     
 *  *
  ** 
"

If you want to add a bit of complexity to your images, you can provide a whole charset to work with. Then each integer can be assigned to a character to output.

Example:

char[] charset = {'A', 'B', 'C', 'D'};
int[][] mat = {{1, 2, 3}, {3, 0, 2}, {2, 3, 1}};

Output.bitmap(mat, charset);
// Returns this String:
"ABC
 C B
 BCA
"

Printing Matrices

If you have a matrix you just want to print the contents of, then matrixPrint(matrix) is what you need. This will take integers, doubles, and any other Object, just as long as they override or have a toString()method. When printing out the matrix, they will simply print the value of each cell, padded with whitespaces to keep them all in neat columns. The columns are kept apart from one another with a single whitespace.

Example:

int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Output.printMatrix(mat);
// Returns this String:
"1 2 3
 4 5 6
 7 8 9
"

double[][] mat = {{1.1, 1.2, 1.3}, {3.14, 1.41, 4.20}, {3.1, 3.2, 3.3}};
Output.printMatrix(mat);
// Returns this String:
" 1.1  1.2  1.3
 3.14 1.41 4.20
  3.1  3.2  3.3
"

String[][] mat = {{"Hello", "World!"}, {"What's", "Hanging?"}};
Output.printMatrix(mat);
// Returns this String:
"  Hello  World!
  What's Hanging
"