1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/command/text/TableFormatter.java

121 lines
4.0 KiB
Java
Raw Normal View History

/*
* This file is part of ComputerCraft - http://www.computercraft.info
2019-01-01 01:10:18 +00:00
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.command.text;
2019-04-03 22:27:10 +00:00
import net.minecraft.text.StringTextComponent;
import net.minecraft.text.TextComponent;
import net.minecraft.text.TextFormat;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Nullable;
import static dan200.computercraft.shared.command.text.ChatHelpers.coloured;
import static dan200.computercraft.shared.command.text.ChatHelpers.translate;
public interface TableFormatter
{
2019-04-03 22:27:10 +00:00
TextComponent SEPARATOR = coloured( "| ", TextFormat.GRAY );
TextComponent HEADER = coloured( "=", TextFormat.GRAY );
/**
* Get additional padding for the component
*
* @param component The component to pad
* @param width The desired width for the component
* @return The padding for this component, or {@code null} if none is needed.
*/
@Nullable
2019-04-03 22:27:10 +00:00
TextComponent getPadding( TextComponent component, int width );
/**
* Get the minimum padding between each column
*
* @return The minimum padding.
*/
int getColumnPadding();
2019-04-03 22:27:10 +00:00
int getWidth( TextComponent component );
2019-04-03 22:27:10 +00:00
void writeLine( int id, TextComponent component );
default int display( TableBuilder table )
{
if( table.getColumns() <= 0 ) return 0;
int rowId = table.getId();
int columns = table.getColumns();
int[] maxWidths = new int[columns];
2019-04-03 22:27:10 +00:00
TextComponent[] headers = table.getHeaders();
if( headers != null )
{
for( int i = 0; i < columns; i++ ) maxWidths[i] = getWidth( headers[i] );
}
2019-04-03 22:27:10 +00:00
for( TextComponent[] row : table.getRows() )
{
for( int i = 0; i < row.length; i++ )
{
int width = getWidth( row[i] );
if( width > maxWidths[i] ) maxWidths[i] = width;
}
}
// Add a small amount of padding after each column
{
int padding = getColumnPadding();
for( int i = 0; i < maxWidths.length - 1; i++ ) maxWidths[i] += padding;
}
// And compute the total width
int totalWidth = (columns - 1) * getWidth( SEPARATOR );
for( int x : maxWidths ) totalWidth += x;
if( headers != null )
{
2019-04-03 22:27:10 +00:00
StringTextComponent line = new StringTextComponent( "" );
for( int i = 0; i < columns - 1; i++ )
{
2019-04-03 22:27:10 +00:00
line.append( headers[i] );
TextComponent padding = getPadding( headers[i], maxWidths[i] );
if( padding != null ) line.append( padding );
line.append( SEPARATOR );
}
2019-04-03 22:27:10 +00:00
line.append( headers[columns - 1] );
writeLine( rowId++, line );
// Write a separator line. We round the width up rather than down to make
// it a tad prettier.
int rowCharWidth = getWidth( HEADER );
int rowWidth = totalWidth / rowCharWidth + (totalWidth % rowCharWidth == 0 ? 0 : 1);
2019-04-03 22:27:10 +00:00
writeLine( rowId++, coloured( StringUtils.repeat( HEADER.getText(), rowWidth ), TextFormat.GRAY ) );
}
2019-04-03 22:27:10 +00:00
for( TextComponent[] row : table.getRows() )
{
2019-04-03 22:27:10 +00:00
StringTextComponent line = new StringTextComponent( "" );
for( int i = 0; i < columns - 1; i++ )
{
2019-04-03 22:27:10 +00:00
line.append( row[i] );
TextComponent padding = getPadding( row[i], maxWidths[i] );
if( padding != null ) line.append( padding );
line.append( SEPARATOR );
}
2019-04-03 22:27:10 +00:00
line.append( row[columns - 1] );
writeLine( rowId++, line );
}
if( table.getAdditional() > 0 )
{
2019-04-03 22:27:10 +00:00
writeLine( rowId++, coloured( translate( "commands.computercraft.generic.additional_rows", table.getAdditional() ), TextFormat.AQUA ) );
}
return rowId - table.getId();
}
}