You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
2.3 KiB

package com.example.datacollect.view;
import com.example.datacollect.model.Article;
import com.example.datacollect.model.DataService;
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 final Scanner scanner = new Scanner(System.in);
public String readLine() {
System.out.print("> ");
return scanner.nextLine();
}
public void printSuccess(String msg) {
System.out.println(ANSI_GREEN + msg + ANSI_RESET);
}
public void printError(String msg) {
System.out.println(ANSI_RED + msg + ANSI_RESET);
}
public void printInfo(String msg) {
System.out.println(ANSI_BLUE + msg + ANSI_RESET);
}
public void display(List<Article> articles,int totalSize) {
ConsoleView view=new ConsoleView();
if (articles.isEmpty()) {
view.printInfo("暂无文章,请先执行 crawl。");
return;
}
// 计算分页信息
String paginationInfo = "";
if (totalSize > articles.size()) {
paginationInfo = " (显示前 " + articles.size() + " 条,共 " + totalSize + " 条)";
}
view.printInfo("文章列表" + paginationInfo);
System.out.println("--------------------------------------------------");
for (int i = 0; i < articles.size(); i++) {
Article a = articles.get(i);
System.out.println((i + 1) + ". " + a.getTitle() + " | " + a.getUrl());
}
}
// 在 ConsoleView 类中添加
public void displayHistory(List<String> history) {
if (history.isEmpty()) {
printInfo("暂无命令历史。");
return;
}
printInfo("命令历史 (共 " + history.size() + " 条):");
System.out.println("--------------------------------------------------");
// 倒序显示,最新的在上面
for (int i = history.size() - 1; i >= 0; i--) {
System.out.println((i + 1) + ". " + history.get(i));
}
System.out.println("--------------------------------------------------");
}
}