refactor(cli): Вынес логику форматирования таблиц в utility-класс (close #52)
- Перенес метод formatWithTable из ListCommand в новый класс Formatter - Обновил ListCommand и SearchCommand, чтобы они использовали Formatter.asTable - Изменение улучшает переиспользование кода и разделение ответственности
This commit is contained in:
parent
8254f5292b
commit
a7eaa693bd
@ -1,12 +1,11 @@
|
|||||||
package ru.kamask.pet.todo.cli;
|
package ru.kamask.pet.todo.cli;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import ru.kamask.pet.todo.model.Identifiable;
|
import ru.kamask.pet.todo.model.Identifiable;
|
||||||
import ru.kamask.pet.todo.model.SimpleTask;
|
|
||||||
import ru.kamask.pet.todo.service.EntityService;
|
import ru.kamask.pet.todo.service.EntityService;
|
||||||
import ru.kamask.pet.todo.service.TaskService;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
|
import ru.kamask.pet.todo.util.Formatter;
|
||||||
|
|
||||||
public class ListCommand implements Command {
|
public class ListCommand implements Command {
|
||||||
@Override
|
@Override
|
||||||
@ -18,31 +17,14 @@ public class ListCommand implements Command {
|
|||||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||||
if (args.length > 0)
|
if (args.length > 0)
|
||||||
return Optional.of(Command.errorMessage);
|
return Optional.of(Command.errorMessage);
|
||||||
TaskService taskService = (TaskService) service;
|
|
||||||
List<SimpleTask> allTasks = taskService.getAll();
|
|
||||||
var res = formatWithTable(allTasks, "Список задач пуст.");
|
|
||||||
|
|
||||||
return Optional.of(res);
|
TaskService taskService = (TaskService) service;
|
||||||
|
|
||||||
|
return Optional.of(Formatter.asTable(taskService.getAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String usage() {
|
public String usage() {
|
||||||
return String.format(templateUsage, name(), "Список всех задач.");
|
return String.format(templateUsage, name(), "Список всех задач.");
|
||||||
}
|
}
|
||||||
|
|
||||||
String formatWithTable(List<SimpleTask> tasks, String msgIfEmpty) {
|
|
||||||
String template = "%-2s | %-30s | %s\n";
|
|
||||||
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
|
||||||
res.append("-".repeat(50) + "\n");
|
|
||||||
|
|
||||||
if (tasks.size() == 0)
|
|
||||||
return res.append("\n" + msgIfEmpty).toString();
|
|
||||||
|
|
||||||
for (SimpleTask task : tasks) {
|
|
||||||
SimpleTask.Data data = task.data();
|
|
||||||
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import java.util.Optional;
|
|||||||
import ru.kamask.pet.todo.model.Identifiable;
|
import ru.kamask.pet.todo.model.Identifiable;
|
||||||
import ru.kamask.pet.todo.service.EntityService;
|
import ru.kamask.pet.todo.service.EntityService;
|
||||||
import ru.kamask.pet.todo.service.TaskService;
|
import ru.kamask.pet.todo.service.TaskService;
|
||||||
|
import ru.kamask.pet.todo.util.Formatter;
|
||||||
|
|
||||||
public class SearchCommand implements Command {
|
public class SearchCommand implements Command {
|
||||||
@Override
|
@Override
|
||||||
@ -27,8 +28,7 @@ public class SearchCommand implements Command {
|
|||||||
TaskService taskService = (TaskService) service;
|
TaskService taskService = (TaskService) service;
|
||||||
|
|
||||||
var matchTask = taskService.search(args[0]);
|
var matchTask = taskService.search(args[0]);
|
||||||
var res = new ListCommand().formatWithTable(matchTask, "Не найдено задач, соответствующих запросу.");
|
|
||||||
|
|
||||||
return Optional.of(res);
|
return Optional.of(Formatter.asTable(matchTask, "Не найдено задач, соответствующих запросу."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
todo/src/main/java/ru/kamask/pet/todo/util/Formatter.java
Normal file
27
todo/src/main/java/ru/kamask/pet/todo/util/Formatter.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package ru.kamask.pet.todo.util;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import ru.kamask.pet.todo.model.SimpleTask;
|
||||||
|
|
||||||
|
public class Formatter {
|
||||||
|
public static String asTable(List<SimpleTask> tasks){
|
||||||
|
return asTable(tasks, "Список задач пуст.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String asTable(List<SimpleTask> tasks, String msgIfEmpty){
|
||||||
|
String template = "%-2s | %-30s | %s\n";
|
||||||
|
var res = new StringBuilder(String.format(template, "ID", "Название задачи", "Статус"));
|
||||||
|
res.append("-".repeat(50) + "\n");
|
||||||
|
|
||||||
|
if (tasks.size() == 0)
|
||||||
|
return res.append("\n" + msgIfEmpty).toString();
|
||||||
|
|
||||||
|
for (SimpleTask task : tasks) {
|
||||||
|
SimpleTask.Data data = task.data();
|
||||||
|
res.append(String.format(template, data.id(), data.title(), data.done() ? "выполнено" : "не выполнено"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user