diff --git a/w9/ConsoleView.java b/w9/ConsoleView.java new file mode 100644 index 0000000..1b3f296 --- /dev/null +++ b/w9/ConsoleView.java @@ -0,0 +1,73 @@ +package com.example.datacollect.view; + +import com.example.datacollect.model.Article; +import java.util.List; +import java.util.Scanner; + +public class ConsoleView { + private static final String ANSI_RESET = "\u001B[0m"; + private static final String ANSI_GREEN = "\u001B[32m"; + private static final String ANSI_RED = "\u001B[31m"; + private static final String ANSI_BLUE = "\u001B[34m"; + private static final String ANSI_WHITE = "\u001B[37m"; + private static final String ANSI_BG_BLACK = "\u001B[40m"; + + private static final boolean DARK_THEME = true; + + private final Scanner scanner = new Scanner(System.in); + + public String readLine() { + if (DARK_THEME) { + System.out.print(ANSI_BG_BLACK + ANSI_WHITE + "> " + ANSI_RESET); + } else { + System.out.print("> "); + } + return scanner.nextLine(); + } + + public void printSuccess(String msg) { + if (DARK_THEME) { + System.out.println(ANSI_BG_BLACK + ANSI_GREEN + msg + ANSI_RESET); + } else { + System.out.println(ANSI_GREEN + msg + ANSI_RESET); + } + } + + public void printError(String msg) { + if (DARK_THEME) { + System.out.println(ANSI_BG_BLACK + ANSI_RED + msg + ANSI_RESET); + } else { + System.out.println(ANSI_RED + msg + ANSI_RESET); + } + } + + public void printInfo(String msg) { + if (DARK_THEME) { + System.out.println(ANSI_BG_BLACK + ANSI_BLUE + msg + ANSI_RESET); + } else { + System.out.println(ANSI_BLUE + msg + ANSI_RESET); + } + } + + public void display(List
articles) { + if (articles.isEmpty()) { + printInfo("暂无文章,请先执行 crawl。"); + return; + } + for (int i = 0; i < articles.size(); i++) { + Article a = articles.get(i); + String output = (i + 1) + ". " + a.getTitle() + " | " + a.getUrl(); + if (a.getAuthor() != null && !a.getAuthor().isEmpty()) { + output += " | Author: " + a.getAuthor(); + } + if (a.getPublishDate() != null && !a.getPublishDate().isEmpty()) { + output += " | Date: " + a.getPublishDate(); + } + if (DARK_THEME) { + System.out.println(ANSI_BG_BLACK + ANSI_WHITE + output + ANSI_RESET); + } else { + System.out.println(output); + } + } + } +}