aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2019-07-07 15:58:07 +0100
committerLuck <git@lucko.me>2019-07-20 23:54:33 +0300
commite9fa0afb91c70de41c4b3ef5b0537b21e2991da4 (patch)
tree30036f2c0f6d0f92ef183816907fc35fb6e6f347 /spark-common/src/main/java/me/lucko/spark/common
parent3b4f13ef58f6ced285876900e1ba5c898dfac858 (diff)
downloadspark-e9fa0afb91c70de41c4b3ef5b0537b21e2991da4.tar.gz
spark-e9fa0afb91c70de41c4b3ef5b0537b21e2991da4.tar.bz2
spark-e9fa0afb91c70de41c4b3ef5b0537b21e2991da4.zip
Implement pagination in /spark activity
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java8
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java109
2 files changed, 76 insertions, 41 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java
index d31d1cf..88a597d 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java
@@ -67,11 +67,15 @@ public class CommandResponseHandler {
}
public void replyPrefixed(Component message) {
- reply(PREFIX.append(message));
+ reply(applyPrefix(message));
}
public void broadcastPrefixed(Component message) {
- broadcast(PREFIX.append(message));
+ broadcast(applyPrefix(message));
+ }
+
+ public static Component applyPrefix(Component message) {
+ return PREFIX.append(message);
}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java
index edb8537..9c81f28 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/ActivityLogModule.java
@@ -20,68 +20,99 @@
package me.lucko.spark.common.command.modules;
-import me.lucko.spark.common.ActivityLog;
+import me.lucko.spark.common.ActivityLog.Activity;
import me.lucko.spark.common.command.Command;
import me.lucko.spark.common.command.CommandModule;
+import me.lucko.spark.common.command.tabcomplete.TabCompleter;
+import net.kyori.text.Component;
import net.kyori.text.TextComponent;
import net.kyori.text.event.ClickEvent;
+import net.kyori.text.feature.pagination.Pagination;
+import net.kyori.text.feature.pagination.Pagination.Renderer;
+import net.kyori.text.feature.pagination.Pagination.Renderer.RowRenderer;
import net.kyori.text.format.TextColor;
import net.kyori.text.format.TextDecoration;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
-public class ActivityLogModule implements CommandModule {
+import static me.lucko.spark.common.command.CommandResponseHandler.*;
+
+public class ActivityLogModule implements CommandModule, RowRenderer<Activity> {
+
+ private final Pagination.Builder pagination = Pagination.builder()
+ .renderer(new Renderer() {
+ @Override
+ public Component renderEmpty() {
+ return applyPrefix(TextComponent.of("There are no entries present in the log."));
+ }
+
+ @Override
+ public Component renderUnknownPage(int page, int pages) {
+ return applyPrefix(TextComponent.of("Unknown page selected. " + pages + " total pages."));
+ }
+ })
+ .resultsPerPage(4);
+
+ @Override
+ public Collection<Component> renderRow(Activity activity, int index) {
+ List<Component> reply = new ArrayList<>(5);
+ reply.add(TextComponent.builder("")
+ .append(TextComponent.builder(">").color(TextColor.DARK_GRAY).decoration(TextDecoration.BOLD, true).build())
+ .append(TextComponent.space())
+ .append(TextComponent.of("#" + (index + 1), TextColor.WHITE))
+ .append(TextComponent.of(" - ", TextColor.DARK_GRAY))
+ .append(TextComponent.of(activity.getType(), TextColor.YELLOW))
+ .append(TextComponent.of(" - ", TextColor.DARK_GRAY))
+ .append(TextComponent.of(formatDateDiff(activity.getTime()), TextColor.GRAY))
+ .build()
+ );
+ reply.add(TextComponent.builder(" ")
+ .append(TextComponent.of("Created by: ", TextColor.GRAY))
+ .append(TextComponent.of(activity.getUser().getName(), TextColor.WHITE))
+ .build()
+ );
+
+ TextComponent.Builder valueComponent = TextComponent.builder(activity.getDataValue(), TextColor.WHITE);
+ if (activity.getDataType().equals("url")) {
+ valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue()));
+ }
+
+ reply.add(TextComponent.builder(" ")
+ .append(TextComponent.of(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", TextColor.GRAY))
+ .append(valueComponent)
+ .build()
+ );
+ reply.add(TextComponent.space());
+ return reply;
+ }
@Override
public void registerCommands(Consumer<Command> consumer) {
consumer.accept(Command.builder()
.aliases("activity", "activitylog", "log")
+ .argumentUsage("page", "page no")
.executor((platform, sender, resp, arguments) -> {
- List<ActivityLog.Activity> log = platform.getActivityLog().getLog();
- log.removeIf(ActivityLog.Activity::shouldExpire);
+ List<Activity> log = platform.getActivityLog().getLog();
+ log.removeIf(Activity::shouldExpire);
if (log.isEmpty()) {
resp.replyPrefixed(TextComponent.of("There are no entries present in the log."));
return;
}
- resp.replyPrefixed(TextComponent.of("Showing recent spark activity...", TextColor.GOLD));
-
- int count = 0;
- for (ActivityLog.Activity activity : log) {
- count++;
-
- resp.replyPrefixed(TextComponent.builder("")
- .append(TextComponent.builder(">").color(TextColor.DARK_GRAY).decoration(TextDecoration.BOLD, true).build())
- .append(TextComponent.space())
- .append(TextComponent.of("#" + count, TextColor.WHITE))
- .append(TextComponent.of(" - ", TextColor.DARK_GRAY))
- .append(TextComponent.of(activity.getType(), TextColor.YELLOW))
- .append(TextComponent.of(" - ", TextColor.DARK_GRAY))
- .append(TextComponent.of(formatDateDiff(activity.getTime()), TextColor.GRAY))
- .build()
- );
- resp.replyPrefixed(TextComponent.builder(" ")
- .append(TextComponent.of("Created by: ", TextColor.GRAY))
- .append(TextComponent.of(activity.getUser().getName(), TextColor.WHITE))
- .build()
- );
-
- TextComponent.Builder valueComponent = TextComponent.builder(activity.getDataValue(), TextColor.WHITE);
- if (activity.getDataType().equals("url")) {
- valueComponent.clickEvent(ClickEvent.openUrl(activity.getDataValue()));
- }
-
- resp.replyPrefixed(TextComponent.builder(" ")
- .append(TextComponent.of(Character.toUpperCase(activity.getDataType().charAt(0)) + activity.getDataType().substring(1) + ": ", TextColor.GRAY))
- .append(valueComponent)
- .build()
- );
- resp.reply(TextComponent.space());
- }
+ int page = Math.max(1, arguments.intFlag("page"));
+
+ Pagination<Activity> activityPagination = this.pagination.build(
+ TextComponent.of("Recent spark activity", TextColor.GOLD),
+ this,
+ value -> "/" + platform.getPlugin().getLabel() + " activity --page " + value
+ );
+ activityPagination.render(log, page).forEach(resp::reply);
})
- .tabCompleter(Command.TabCompleter.empty())
+ .tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--page"))
.build()
);
}