package com.lanyuanxiaoyao.service.command.utils;

import cn.hutool.core.util.StrUtil;
import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import me.tongfei.progressbar.ConsoleProgressBarConsumer;
import me.tongfei.progressbar.DelegatingProgressBarConsumer;
import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 命令行工具类
 *
 * @author ZhangJiacheng
 * @date 2022-04-27
 */
public class CommandLineUtils {
    private static final Logger logger = LoggerFactory.getLogger(CommandLineUtils.class);

    @SafeVarargs
    public static String generateResultLines(Supplier<String>... suppliers) {
        return Arrays.stream(suppliers)
                .map(Supplier::get)
                .filter(StrUtil::isNotBlank)
                .collect(Collectors.joining("\n"));
    }

    public static ProgressBarBuilder progressbarBuilder(String name, Integer max) {
        return progressbarBuilder(name, max.longValue());
    }

    public static ProgressBarBuilder progressbarBuilder(String name, Long max) {
        return new ProgressBarBuilder()
                .setConsumer(new ConsoleProgressBarConsumer(System.out))
                .setInitialMax(max)
                .setStyle(ProgressBarStyle.ASCII)
                .setTaskName(name);
    }
}
