mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.23k stars 243 forks source link

TestTerminalPosition, TestTerminalRectangle & TestTerminalSize and changes to those classes #601

Open ginkoblongata opened 3 months ago

ginkoblongata commented 3 months ago

Core changed classes:

// the three immutable classes
TerminalSize
TerminalPosition
TerminalRectangle
// tests for them
TestTerminalSize
TestTerminalPosition
TestTerminalRectangle

// the rest of the files changed are due to these, and their changes
// are all of a similar nature to each other
// of these changes, while I've put careful thought into each, I think
// possibly the static fields like OF_0x0 etc could maybe
// just be of0x0() of1x1() methods, mostly to make reading and writing easier

Primary change:

Reason for the different parameters on the methods is to provide ease of use. The methods like withRelativeRows(), withRelativeColumns() are left in, but they are synonymous with plus() or minus().

Also, they now all have the logic which can return the same instance if the resulting TerminalSize or TerminalPosition were to have the same columns & rows.

Added convenience equals(int, int) method:

public class TerminalSize implements Comparable<TerminalPosition> {
     public boolean equals(int columns, int rows) {...
public class TerminalPosition implements Comparable<TerminalPosition> {
     public boolean equals(int column, int row) {...

Possibly:

ginkoblongata commented 3 months ago

Let me know of anything I need to address for this merge request. I think it's pretty good, with the size though I've anticipated a combination of further explanation or changes to do for this merge request would happen. After this merge request then I've got a fix for SplitPanel in the pipeline, and then I'm ready to finally tackle generalized ScrollPanel capability.

avl42 commented 2 months ago

I think, this PR is too much at once.

Try to split it up into separate topics that can be individually checked and judged.

Removing methods is not to be taken lightly (unless it were about private methods.) getDimension() may be useful, if you write code that ought to work in either horizonal or vertical direction - LinearLayout would first come to my mind for this.

PS: I do not have any say here - I'm only just a "contributor". I only write this feedback, because 1.) that was my own thought after seeing your PR, and 2.) Martin hasn't answered on his own, yet.

Edit: I removed a comment about multiply/divide when I saw these methods already existed before and that only an overload was added. That's fine (imho).

avl42 commented 2 months ago

One more comment about the changes in TerminalSize:

1.) making the fields public is a "no go" - it contradicts "immutability".

2.) I like the static method "of", but its current implementation does 4 comparisons in the non-trivial cases, and up to 6 comparisons in the semi-trivial cases (with only one component in {0,1}). I think, it might be better to change it to a nested if-structure roughly like that:

if (rows==0) {
    if(columns==0) { return ...} else if (columns==1) { return ...}
else if (rows==1) {
    if(columns==0) { return ...} else if (columns==1) { return ...}
}

actually, I think that constants for 0x0 and 1x1 would be enough... The use cases for 1x0 and 0x1 are pretty low imho, and having fewer branches in "of" is probably a better investment than 1,0 and 0,1 instances...

In that sense, my suggestion for the initial part of "of()" would be:

if (rows==columns) {
    if (rows==0) { return OF_0x0; } else if (rows==1) { return OF_1x1; }
}