Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding #12

Open
krasa opened this issue Jan 29, 2022 · 2 comments
Open

Padding #12

krasa opened this issue Jan 29, 2022 · 2 comments

Comments

@krasa
Copy link

krasa commented Jan 29, 2022

Hello,
I am trying to implement krasa/StringManipulation#169

However, there is an issue with padding:

SimpleTable simpleTable = SimpleTable.of()
		.nextRow()
		.nextCell()
		.addLine("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
		.addLine("Duis risus. Nullam rhoncus aliquam metus.")
		.applyToCell(LEFT_PAD.withWidth(5))
		.applyToCell(RIGHT_PAD.withWidth(5))
		.applyToCell(TOP_PAD.withHeight(5))
		.applyToCell(BOTTOM_PAD.withHeight(5))
		;
GridTable g = simpleTable.toGrid();
g = Border.DOUBLE_LINE.apply(g);
System.out.println(Util.asString(g));

produces:

╔═════════════════════════════════════════════════════════╗
║                                                         ║
║                                                         ║
║                                                         ║
║Lorem ipsum dolor sit amet, consectetuer adipiscing elit.║
║Duis risus. Nullam rhoncus aliquam metus.                ║
╚═════════════════════════════════════════════════════════╝

I would expect:

╔═══════════════════════════════════════════════════════════════════╗
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║     Lorem ipsum dolor sit amet, consectetuer adipiscing elit.     ║
║     Duis risus. Nullam rhoncus aliquam metus.                     ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
╚═══════════════════════════════════════════════════════════════════╝
@iNamik
Copy link
Owner

iNamik commented Jan 30, 2022

Greetings and thank you for your interest in my project!

So, in the current context, PAD actually relates to "pad to size", (i.e the since of padding a string to ensure a minimum length), and not "add extra padding" (ie. add extra padding regardless of current length).

I must admit its a bit confusing since this project is table/cell based and in that context, PAD generally refers the 2nd concept above.

I'm considering renaming the *Pad functions to *Expand (to give symmetry to the *Truncate functions), and re-implementing the *Pad functions to be more aligned with Table/Cell context ...

In the meantime, here's how to accomplish what you're looking for:

import com.inamik.text.tables.Cell;
import com.inamik.text.tables.GridTable;
import com.inamik.text.tables.SimpleTable;
import com.inamik.text.tables.grid.Border;
import com.inamik.text.tables.grid.Util;

import java.util.Collection;

import static com.inamik.text.tables.Cell.Functions.BOTTOM_PAD;
import static com.inamik.text.tables.Cell.Functions.LEFT_PAD;
import static com.inamik.text.tables.Cell.Functions.RIGHT_PAD;
import static com.inamik.text.tables.Cell.Functions.TOP_PAD;

class Scratch {
    public static void main(String[] args) {
        // Manually build the cell with content
        //
        Collection<String> cell = Cell.of();
        cell = Cell.append(cell, "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.");
        cell = Cell.append(cell, "Duis risus. Nullam rhoncus aliquam metus.");

        // Determine width and height of actual content
        //
        int cellWidth  = getCellWidth (cell);
        int cellHeight = getCellHeight(cell);

        // Always apply TOP/BOTTOM padding *before* LEFT/RIGHT padding
        //
        SimpleTable simpleTable = SimpleTable.of()
                .nextRow()
                .nextCell(cell)
                // Expand to current height + 5, adding new lines to TOP
                //
                .applyToCell(TOP_PAD   .withHeight(cellHeight + 5    ))
                // Expand to new-current-height + 5, adding new lines to the BOTTOM
                //
                .applyToCell(BOTTOM_PAD.withHeight(cellHeight + 5 + 5))
                // Expand to current width + 5, adding new lines to the LEFT
                //
                .applyToCell(LEFT_PAD  .withWidth (cellWidth  + 5    ))
                // Expand to new-current-width + 5, adding new lines to the RIGHT
                //
                .applyToCell(RIGHT_PAD .withWidth (cellWidth  + 5 + 5))
                ;
        GridTable g = simpleTable.toGrid();
        g = Border.DOUBLE_LINE.apply(g);
        System.out.println(Util.asString(g));    }

    static int getCellWidth(Collection<String> cell) {
        int width = 0;
        for (String line: cell) {
            width = Math.max(width, line.length());
        }
        return width;
    }

    static int getCellHeight(Collection<String> cell) {
        return cell.size();
    }
}

Should generate the following output:

╔═══════════════════════════════════════════════════════════════════╗
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║     Lorem ipsum dolor sit amet, consectetuer adipiscing elit.     ║
║                     Duis risus. Nullam rhoncus aliquam metus.     ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
║                                                                   ║
╚═══════════════════════════════════════════════════════════════════╝

The keys here are :

  • Knowing the height and width of your content so that you can expand from there
  • Applying TOP/BOTTOM padding first
  • Including the first padding length (TOP / LEFT) in the computation for the 2nd padding lengths (BOTTOM / RIGHT)

Give that a try and let me know what you think.

Thanks again for your interest in my project !

-iNamik

@krasa
Copy link
Author

krasa commented Jan 30, 2022

Thanks very much!

I had to change it to :

.applyToCell(RIGHT_PAD.withWidth(cellWidth + 5))
.applyToCell(LEFT_PAD.withWidth(cellWidth + 5 + 5));

so that it does not align right, now it seems to work fine.

I'm considering renaming the *Pad functions to *Expand (to give symmetry to the *Truncate functions), and re-implementing the *Pad functions to be more aligned with Table/Cell context ...

Sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants