More work on list item and display logic

v2-dev
William278 2 years ago
parent 7d19e316ac
commit 47243552dd

@ -5,14 +5,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="1adcdbad-26db-47ae-b8e5-83088ba9f2ff" name="Changes" comment="Add license header">
<change afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/ListItem.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/SortOption.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/StringListItem.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/ListOptions.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/ListOptions.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/PaginatedList.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/PaginatedList.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/PaginationException.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/PaginationException.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/StringListItem.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/net/william278/paginedown/StringListItem.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/net/william278/paginedown/PaginatedListTests.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/net/william278/paginedown/PaginatedListTests.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -41,13 +36,7 @@
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="PagineDown" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
</path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="PagineDown" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="other" type="c8890929:TasksNode$1" />
<item name="Run Configurations" type="7b0102dc:RunConfigurationsNode" />
</path>
</expand>
<select />
@ -65,6 +54,11 @@
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY">
<map>
<entry key="$PROJECT_DIR$" value="master" />
</map>
</option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">

@ -31,7 +31,7 @@ import java.util.List;
public class ListOptions<I extends ListItem> {
@NotNull
protected String headerFormat = "[%topic% (%first_item_on_page_index%-%last_item_on_page_index% of %total_items%):](%color%)";
protected String headerFormat = "%order_control_button% [%topic% (%first_item_on_page_index%-%last_item_on_page_index% of %total_items%):](%color%) (%filter_buttons%)";
@NotNull
protected String footerFormat = "%previous_page_button%Page [%current_page%](%color%)/[%total_pages%](%color%)%next_page_button% %page_jumpers%";
@NotNull
@ -48,6 +48,11 @@ public class ListOptions<I extends ListItem> {
protected String pageJumperCurrentPageFormat = "[%current_page%](%color%)";
@NotNull
protected String pageJumperPageFormat = "[%target_page_index%](show_text=&7Jump to page %target_page_index% run_command=/%command% %target_page_index%)";
protected String ascendingSortButtonFormat = "[▲](white show_text=&7Sort items ascendingly run_command=/%command% %target_sort% %current_page%)";
protected String descendingSortButtonFormat = "[▼](white show_text=&7Sort items descendingly run_command=/%command% %target_sort% %current_page%)";
protected String filterSeparator = "/";
protected String filterButtonFormat = "[%filter_name%](show_text=&7Filter by %filter_name% run_command=/%command% %target_filter% %current_page%)";
protected String filterButtonActiveFormat = "[%filter_name%](%color% show_text=&7Filter by %filter_name% run_command=/%command% %target_filter% %current_page%)";
@NotNull
protected String topic = "List";
@NotNull
@ -62,11 +67,15 @@ public class ListOptions<I extends ListItem> {
protected int itemsPerPage = 10;
protected int pageJumperStartButtons = 3;
protected int pageJumperEndButtons = 3;
protected List<SortOption<I>> availableSortOptions = new ArrayList<>();
@SuppressWarnings("unchecked")
protected List<SortOption<I>> availableSortOptions = new ArrayList<>() {{
add((SortOption<I>) SortOption.NAME);
}};
private ListOptions() {
}
@SuppressWarnings("rawtypes")
@NotNull
public static Builder builder() {
return new Builder<>();

@ -176,7 +176,7 @@ public class PaginatedList<I extends ListItem> {
final StringJoiner menuJoiner = new StringJoiner("\n");
if (!options.headerFormat.isBlank()) {
menuJoiner.add(formatPageString(options.headerFormat, page));
menuJoiner.add(formatPageString(options.headerFormat, page, ascending, option));
if (options.spaceAfterHeader) {
menuJoiner.add("");
}
@ -194,7 +194,7 @@ public class PaginatedList<I extends ListItem> {
if (options.spaceBeforeFooter) {
menuJoiner.add("");
}
menuJoiner.add(formatPageString(options.footerFormat, page));
menuJoiner.add(formatPageString(options.footerFormat, page, ascending, option));
}
return menuJoiner.toString();
}
@ -241,7 +241,7 @@ public class PaginatedList<I extends ListItem> {
* @return The formatted page string
*/
@NotNull
private String formatPageString(@NotNull String format, int page) {
private String formatPageString(@NotNull String format, int page, boolean ascending, @NotNull SortOption<I> option) {
final StringBuilder convertedFormat = new StringBuilder();
StringBuilder currentPlaceholder = new StringBuilder();
boolean readingPlaceholder = false;
@ -249,7 +249,7 @@ public class PaginatedList<I extends ListItem> {
if (c == '%') {
if (readingPlaceholder) {
switch (currentPlaceholder.toString().toLowerCase()) {
case "topic" -> convertedFormat.append(formatPageString(options.topic, page));
case "topic" -> convertedFormat.append(formatPageString(options.topic, page, ascending, option));
case "color" ->
convertedFormat.append(String.format("#%02x%02x%02x", options.themeColor.getRed(), options.themeColor.getGreen(), options.themeColor.getBlue()));
case "first_item_on_page_index" ->
@ -261,12 +261,12 @@ public class PaginatedList<I extends ListItem> {
case "total_pages" -> convertedFormat.append(getTotalPages());
case "previous_page_button" -> {
if (page > 1) {
convertedFormat.append(formatPageString(options.previousButtonFormat, page));
convertedFormat.append(formatPageString(options.previousButtonFormat, page, ascending, option));
}
}
case "next_page_button" -> {
if (page < getTotalPages()) {
convertedFormat.append(formatPageString(options.nextButtonFormat, page));
convertedFormat.append(formatPageString(options.nextButtonFormat, page, ascending, option));
}
}
case "next_page_index" -> convertedFormat.append(page + 1);
@ -274,11 +274,11 @@ public class PaginatedList<I extends ListItem> {
case "command" -> convertedFormat.append(options.command);
case "page_jumpers" -> {
if (getTotalPages() > 2) {
convertedFormat.append(formatPageString(options.pageJumpersFormat, page));
convertedFormat.append(formatPageString(options.pageJumpersFormat, page, ascending, option));
}
}
case "page_jump_buttons" -> {
convertedFormat.append(getPageJumperButtons(page));
convertedFormat.append(getPageJumperButtons(page, ascending, option));
}
}
} else {
@ -303,13 +303,15 @@ public class PaginatedList<I extends ListItem> {
* @return The formatted page jumper
*/
@NotNull
private String formatPageJumper(final int page) {
return formatPageString(options.pageJumperPageFormat.replaceAll("%target_page_index%",
Integer.toString(page)), page);
private String formatPageJumper(final int page, final boolean ascending, @NotNull SortOption<I> option) {
return formatPageString(
options.pageJumperPageFormat.replaceAll("%target_page_index%", Integer.toString(page)),
page, ascending, option
);
}
@NotNull
protected String getPageJumperButtons(final int page) {
protected String getPageJumperButtons(final int page, final boolean ascending, @NotNull SortOption<I> option) {
final StringJoiner pageGroups = new StringJoiner(options.pageJumperGroupSeparator);
StringJoiner pages = new StringJoiner(options.pageJumperPageSeparator);
int lastPage = 1;
@ -320,9 +322,9 @@ public class PaginatedList<I extends ListItem> {
pages = new StringJoiner(options.pageJumperPageSeparator);
}
if (page == i) {
pages.add(formatPageString(options.pageJumperCurrentPageFormat, i));
pages.add(formatPageString(options.pageJumperCurrentPageFormat, i, ascending, option));
} else {
pages.add(formatPageString(formatPageJumper(i), i));
pages.add(formatPageString(formatPageJumper(i, ascending, option), i, ascending, option));
}
lastPage = i;
}

@ -2,11 +2,11 @@ package net.william278.paginedown;
import org.jetbrains.annotations.NotNull;
public class StringListItem implements ListItem {
public final class StringListItem implements ListItem {
private final String item;
public StringListItem(String item) {
public StringListItem(@NotNull String item) {
this.item = item;
}

@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("ALL")
public class PaginatedListTests {
// Generates a test list of StringListItems
@ -63,10 +64,10 @@ public class PaginatedListTests {
.setPageJumperCurrentPageFormat("%current_page%")
.build()
);
Assertions.assertEquals("1|2…19|20", longList.getPageJumperButtons(2));
Assertions.assertEquals("1|2…6…19|20", longList.getPageJumperButtons(6));
Assertions.assertEquals("1|2…19|20", longList.getPageJumperButtons(20));
Assertions.assertEquals("1|2…19|20", longList.getPageJumperButtons(1));
Assertions.assertEquals("1|2…19|20", longList.getPageJumperButtons(2, true, (SortOption<StringListItem>) SortOption.NAME));
Assertions.assertEquals("1|2…6…19|20", longList.getPageJumperButtons(6, true, (SortOption<StringListItem>) SortOption.NAME));
Assertions.assertEquals("1|2…19|20", longList.getPageJumperButtons(20, true, (SortOption<StringListItem>) SortOption.NAME));
Assertions.assertEquals("1|2…19|20", longList.getPageJumperButtons(1, true, (SortOption<StringListItem>) SortOption.NAME));
}
@Test
@ -81,9 +82,9 @@ public class PaginatedListTests {
.setPageJumperCurrentPageFormat("%current_page%")
.build()
);
Assertions.assertEquals("1|2…4|5", mediumList.getPageJumperButtons(1));
Assertions.assertEquals("1|2|3|4|5", mediumList.getPageJumperButtons(3));
Assertions.assertEquals("1|2…4|5", mediumList.getPageJumperButtons(5));
Assertions.assertEquals("1|2…4|5", mediumList.getPageJumperButtons(1, true, (SortOption<StringListItem>) SortOption.NAME));
Assertions.assertEquals("1|2|3|4|5", mediumList.getPageJumperButtons(3, true, (SortOption<StringListItem>) SortOption.NAME));
Assertions.assertEquals("1|2…4|5", mediumList.getPageJumperButtons(5, true, (SortOption<StringListItem>) SortOption.NAME));
}
@Test
@ -98,6 +99,6 @@ public class PaginatedListTests {
.setPageJumperCurrentPageFormat("%current_page%")
.build()
);
Assertions.assertEquals("1|2", shortList.getPageJumperButtons(2));
Assertions.assertEquals("1|2", shortList.getPageJumperButtons(2, true, (SortOption<StringListItem>) SortOption.NAME));
}
}

Loading…
Cancel
Save