带占用指标的命令行进度条

时间:2015-06-17 08:21:51

标签: java command-line terminal

我有一个程序处理一个庞大而冗长的工作。我事先知道任务的大小,所以我显示了一个进度条。 然而,单个子任务可能需要很长时间,所以最后进度条也可以长时间处于空闲状态,使其看起来像程序已经停滞。

对于那里的住院病人用户我虽然可能会包含一个指示器,它只是不断旋转一个简单的ASCII动画,让他们放心,仍有一些计算在进行。 我将问题与进度条更新结合起来有一个问题 - 如果我使用回车符,它会显示我的进度条应该在哪里,我宁愿把它放在PB上,如下所示:

Busy /
0     25    50    75    100%
|-----|-----|-----|-----|
[*****************  

代码:

public class Test {

    public static void main(String[] args) {

        try {

            int barLength = 100;
            int jobLength = 501;
            double stepSize = (double) barLength / (double) jobLength;

            String message = "Busy";
            RotatingProgressBar progressBar = new RotatingProgressBar(message);
            progressBar.start();

            System.out
                    .println("0                        25                       50                       75                       100%");
            System.out
                    .println("|------------------------|------------------------|------------------------|------------------------|");

            for (int i = 0; i <= jobLength; i++) {

                Thread.sleep(100);

                double progress = (stepSize * i) / barLength;
                updateProgress(progress, barLength);

            }

            progressBar.setShowProgress(false);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }// END: main

    static void updateProgress(double progressPercentage, int barLength) {

        System.out.print("\r[");
        int i = 0;
        for (; i < (int) (progressPercentage * (barLength - 1)); i++) {
            System.out.print("*");
        }

        for (; i < barLength - 1; i++) {
            System.out.print(" ");
        }

        System.out.print("]");
    }// END: updateProgress

    static class RotatingProgressBar extends Thread {

        private final String anim = "|/-\\";
        private boolean showProgress = true;
        private String message;

        public RotatingProgressBar(String message) {
            this.message = message;
        }

        public void run() {

            int i = 0;

            while (showProgress) {

                System.out.print("\r");
                System.out.print(message + anim.charAt(i++ % anim.length()));

                try {

                    Thread.sleep(10);

                } catch (Exception e) {
                    // do nothing
                }// END: try-catch

            }// END: while

        }// END: run

        public void setShowProgress(boolean showProgress) {
            this.showProgress = showProgress;
        }

//      public void setCarriageColumn(int column) {
//          this.column = column;
//      }
    }// END: class

}// END: class

2 个答案:

答案 0 :(得分:1)

您可以使用此方法删除已经打印到System.out的字符:

 public void deleteChar(int times) {
    for (int i = 0; i < times; i++) {
        System.out.print((char) 8);
    }
}

但是这个(以及其他解决方法)并不适用于所有类型的终端/控制台。这适用于Windows cmd,powershell和大多数Unix终端,但它在Eclipse或Netbeans中会失败。

答案 1 :(得分:0)

我想我解决了这个问题,灵感来自@Stefan Lindenberg和@nafas。我将两个方法合并为一个,忙指针连续旋转作为进度条的最后一个字符,我更新了进度条:

class ProgressBar extends Thread {

    private static final String anim = "|/-\\";
    private boolean showProgress;
    double progressPercentage;
    private final int barLength;

    public ProgressBar(int barLength) {
        this.barLength = barLength;
        this.showProgress = true;
        this.progressPercentage = 0;
    }

    public void run() {

        int i = 0;

        while (showProgress) {

            String progress = "\r";
            int column = (int) (progressPercentage * barLength);
            for (int j = 0; j <= column; j++) {
                progress += ("*");
            }

            System.out.print(progress + anim.charAt(i++ % anim.length()));

            try {

                Thread.sleep(10);

            } catch (Exception e) {
                // do nothing
            }// END: try-catch

        }// END: while

    }// END: run

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public void setProgressPercentage(double progressPercentage) {
        this.progressPercentage = progressPercentage;
    }
}// END: class

然后使用它:

        for (int i = 0; i <= jobLength; i++) {

            Thread.sleep(100);

            double progress = (stepSize * i) / barLength;
            progressBar.setProgressPercentage(progress);

        }

看起来不错。