जावा में कमांड लाइन प्रगति बार


130

मेरे पास जावा प्रोग्राम कमांड लाइन मोड में चल रहा है। मैं एक प्रगति पट्टी प्रदर्शित करना चाहूंगा, जिसमें नौकरी का प्रतिशत दिखाया गया है। उसी प्रकार की प्रगति पट्टी जिसे आप यूनिक्स के तहत wget का उपयोग करते हुए देखेंगे। क्या यह संभव है?


मुझे आपके विशिष्ट प्रश्न का उत्तर नहीं पता है, लेकिन आप इस प्रश्न में बताए गए पुस्तकालयों की जांच करके शुरू कर सकते हैं: stackoverflow.com/questions/435740/…
जोनिक

धन्यवाद। पुस्तकालयों का हवाला दिया कि कमांड-लाइन तर्क पार्सिंग के बारे में अधिक हैं, बजाय कंसोल में प्रदर्शित होने के। लेकिन धन्यवाद वैसे भी।
जी। ओरु

7
यह विषय पर है। ओपी पूछते हैं कि क्या संभव है, पुस्तकालय की सिफारिश के लिए नहीं (कम से कम वर्तमान में संपादित करें)
नील मैकगिगन

मैं इस कोड का प्रयास करता हूं stackoverflow.com/questions/1001290/… और चल रहा है! (केवल टर्मिनल में सही चल रहा है, ग्रहण में उपयोग न करें)
वेंडेल

जवाबों:


176

मैंने पहले भी इस तरह की बात लागू की है। यह जावा के बारे में इतना नहीं है, लेकिन क्या पात्रों को सांत्वना देने के लिए।

कुंजी के बीच \nऔर अंतर है \r\nएक नई लाइन की शुरुआत में जाता है। लेकिन \rसिर्फ गाड़ी वापसी है - यह उसी लाइन की शुरुआत में वापस जाती है।

तो करने वाली बात यह है कि अपनी प्रगति पट्टी को प्रिंट करें, उदाहरण के लिए, स्ट्रिंग को प्रिंट करके

"|========        |\r"

प्रगति पट्टी के अगले टिक पर, एक लंबी पट्टी के साथ एक ही पंक्ति को ओवरराइट करें। (क्योंकि हम \ r का उपयोग कर रहे हैं, हम एक ही पंक्ति पर रहते हैं) उदाहरण के लिए:

"|=========       |\r"

आपको क्या करना है याद रखना है, जब किया जाता है, अगर आप तब प्रिंट करते हैं

"done!\n"

आपके पास अभी भी लाइन पर प्रगति पट्टी से कुछ कचरा हो सकता है। इसलिए जब आप प्रगति पट्टी के साथ हो जाते हैं, तो लाइन से हटाने के लिए पर्याप्त व्हाट्सएप प्रिंट करना सुनिश्चित करें। जैसे कि:

"done             |\n"

उम्मीद है की वो मदद करदे।


क्या यह वास्तव में क्रॉस-प्लेटफॉर्म है? यह शायद विंडोज और लिनक्स पर ठीक व्यवहार करेगा, लेकिन मैक के बारे में क्या? मेरे पास एक नहीं है, इसलिए मैं इसे आज़मा नहीं सकता ...
रादू मुराज़े

2
@RaduMurzea OS X एक * NIX OS है, इसलिए यदि यह लिनक्स पर काम करता है, तो यह OS X पर काम करेगा (यह सब कुछ सच नहीं है, लेकिन यहाँ सच है)।
1927 में bfontaine

3
धन्यवाद! हालांकि, यह चींटी के साथ काम नहीं करता है (दोनों linux और osx पर कोशिश की, ठीक काम करता है जब जावा को सीधे लागू करता है)। किसी को कोई विचार?
user495285

ध्यान दें कि यह कंसोल में निर्मित ग्रहण में काम नहीं करता है (कम से कम मेरे लिए)। लेकिन जैसा कि यह एक विकास-सांत्वना है यह बहुत ज्यादा मायने नहीं रखना चाहिए।
Qw3ry


46

नहीं है https://github.com/ctongfei/progressbar , लाइसेंस: एमआईटी

सिंपल कंसोल प्रोग्रेस बार। प्रगति पट्टी लेखन अब एक और सूत्र पर चलता है।

इष्टतम दृश्य प्रभावों के लिए मेनलो, फिरा मोनो, स्रोत कोड प्रो या एसएफ मोनो की सिफारिश की जाती है।

कंसोल या एंडले मोनो फोंट के लिए, ProgressBarStyle.ASCIIनीचे (देखें) का उपयोग करें क्योंकि इन फोंट में बॉक्स-ड्राइंग ग्लिफ़ को ठीक से संरेखित नहीं किया गया है।

Maven:

<dependency>
  <groupId>me.tongfei</groupId>
  <artifactId>progressbar</artifactId>
  <version>0.5.5</version>
</dependency>

उपयोग:

ProgressBar pb = new ProgressBar("Test", 100); // name, initial max
 // Use ProgressBar("Test", 100, ProgressBarStyle.ASCII) if you want ASCII output style
pb.start(); // the progress bar starts timing
// Or you could combine these two lines like this:
//   ProgressBar pb = new ProgressBar("Test", 100).start();
some loop {
  ...
  pb.step(); // step by 1
  pb.stepBy(n); // step by n
  ...
  pb.stepTo(n); // step directly to n
  ...
  pb.maxHint(n);
  // reset the max of this progress bar as n. This may be useful when the program
  // gets new information about the current progress.
  // Can set n to be less than zero: this means that this progress bar would become
  // indefinite: the max would be unknown.
  ...
  pb.setExtraMessage("Reading..."); // Set extra message to display at the end of the bar
}
pb.stop() // stops the progress bar

1
यह साफ-सुथरा दिखता है, और यह मुझे अपने सीएलआई पर जोड़ी गई दो सौ स्ट्रिंग-बफर लाइनों को बदलने की अनुमति देगा। लेकिन क्या होगा अगर मुझे एक एप्लिकेशन मिला है जो "टिक" ( step()कॉल के बीच मिनट ) के बीच मिनटों तक चलता है ? क्या आपकी लाइब्रेरी अतुल्यकालिक रूप से चलती है, इस प्रकार घड़ी को अपडेट करने की अनुमति है? यदि मेरा पुस्तकालय दिनों के लिए चलता है तो क्या होगा? क्या यह /rरणनीति का उपयोग करता है ? क्या इसका परिणाम बहु-सौ-मेगाबाइट आउटपुट में होगा?
ग्रोस्तव

23

मुझे सही तरीके से काम करने के लिए निम्न कोड मिला। यह आउटपुट बफ़र को बाइट्स लिखता है। शायद यह है कि एक लेखक का उपयोग करने वाले तरीके , लक्ष्य की मूल रेखा को समाप्त करने के लिए मेल खाने की System.out.println()घटनाओं की जगह लेते हैं (यदि ठीक से कॉन्फ़िगर नहीं किया गया है)।\r\n

public class Main{
    public static void main(String[] arg) throws Exception {
        String anim= "|/-\\";
        for (int x =0 ; x < 100 ; x++) {
            String data = "\r" + anim.charAt(x % anim.length()) + " " + x;
            System.out.write(data.getBytes());
            Thread.sleep(100);
        }
    }
}

7

मैंने डाउनलोड फ़ाइल की जाँच करने के लिए नंगे प्रतिशत प्रगति की है।

मैं अपनी फ़ाइल डाउनलोड में समय-समय पर कुल फ़ाइल-आकार की जांच करने के लिए विधि को कॉल करता हूं और शेष और उस में प्रस्तुत करता हूं %

इसका उपयोग अन्य कार्य उद्देश्य के लिए भी किया जा सकता है।

परीक्षण और आउटपुट उदाहरण

progressPercentage(0, 1000);
[----------] 0%

progressPercentage(10, 100);
[*---------] 10%

progressPercentage(500000, 1000000);
[*****-----] 50%

progressPercentage(90, 100);
[*********-] 90%

progressPercentage(1000, 1000);
[**********] 100%

लूप के साथ परीक्षण करें

for (int i = 0; i <= 200; i = i + 20) {
    progressPercentage(i, 200);
    try {
        Thread.sleep(500);
    } catch (Exception e) {
    }
}

विधि को आसानी से संशोधित किया जा सकता है:

public static void progressPercentage(int remain, int total) {
    if (remain > total) {
        throw new IllegalArgumentException();
    }
    int maxBareSize = 10; // 10unit for 100%
    int remainProcent = ((100 * remain) / total) / maxBareSize;
    char defaultChar = '-';
    String icon = "*";
    String bare = new String(new char[maxBareSize]).replace('\0', defaultChar) + "]";
    StringBuilder bareDone = new StringBuilder();
    bareDone.append("[");
    for (int i = 0; i < remainProcent; i++) {
        bareDone.append(icon);
    }
    String bareRemain = bare.substring(remainProcent, bare.length());
    System.out.print("\r" + bareDone + bareRemain + " " + remainProcent * 10 + "%");
    if (remain == total) {
        System.out.print("\n");
    }
}

5

C # उदाहरण लेकिन मैं मान रहा हूं कि यह System.out.printजावा के लिए समान है । अगर मैं गलत हूं तो मुझे सुधारने के लिए स्वतंत्र महसूस करें।

मूल रूप से, आप \rअपने संदेश की शुरुआत के लिए बच चरित्र लिखना चाहते हैं जो कर्सर को अगली पंक्ति में जाने के बिना लाइन (लाइन फीड) की शुरुआत में वापस आ जाएगा।

    static string DisplayBar(int i)
    {
        StringBuilder sb = new StringBuilder();

        int x = i / 2;
        sb.Append("|");
        for (int k = 0; k < 50; k++)
            sb.AppendFormat("{0}", ((x <= k) ? " " : "="));
        sb.Append("|");

        return sb.ToString();
    }

    static void Main(string[] args)
    {
        for (int i = 0; i <= 100; i++)
        {
            System.Threading.Thread.Sleep(200);
            Console.Write("\r{0} {1}% Done", DisplayBar(i), i);
        }

        Console.ReadLine();

    }

अच्छा उदाहरण, निश्चित रूप से सहायक होगा। धन्यवाद।
जी। ओरु १२'०

सुंदर! धन्यवाद। एक आकर्षण की तरह काम करता है। पढ़ने में अासान। लवली
खलोफेलो मालोमा

4

थोड़ा परिष्कृत और अद्यतन @ maytham-s की विधि। अब यह प्रगति पट्टी के एक मनमाने आकार का समर्थन कर रहा है:

    public static void progressPercentage(int done, int total) {
        int size = 5;
        String iconLeftBoundary = "[";
        String iconDone = "=";
        String iconRemain = ".";
        String iconRightBoundary = "]";

        if (done > total) {
            throw new IllegalArgumentException();
        }
        int donePercents = (100 * done) / total;
        int doneLength = size * donePercents / 100;

        StringBuilder bar = new StringBuilder(iconLeftBoundary);
        for (int i = 0; i < size; i++) {
            if (i < doneLength) {
                bar.append(iconDone);
            } else {
                bar.append(iconRemain);
            }
        }
        bar.append(iconRightBoundary);

        System.out.print("\r" + bar + " " + donePercents + "%");

        if (done == total) {
            System.out.print("\n");
        }
    }

3

यहाँ ऊपर का एक संशोधित संस्करण है:

private static boolean loading = true;
private static synchronized void loading(String msg) throws IOException, InterruptedException {
    System.out.println(msg);
    Thread th = new Thread() {
        @Override
        public void run() {
            try {
                System.out.write("\r|".getBytes());
                while(loading) {
                    System.out.write("-".getBytes());
                    Thread.sleep(500);
                }
                System.out.write("| Done \r\n".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    th.start();
}

... और मुख्य में:

loading("Calculating ...");

2

यह जावा कुर्सेस लाइब्रेरी के साथ संभव होगा। यही मैंने पाया है। मैंने खुद इसका इस्तेमाल नहीं किया है और मुझे नहीं पता कि यह क्रॉस-प्लेटफॉर्म है या नहीं।


मेरे द्वारा उपयोग किए जाने वाले आसान उपयोग के लिए शाप थोड़ा अधिक हो सकता है, लेकिन यह सुनिश्चित है कि यह एक ट्रैक है। धन्यवाद।
जी। ओरु

2

जब मैं दौड़ की स्थिति को रोकने के लिए एक उपकरण में देरी करने के लिए "बाउंसिंग" प्रगति पट्टी का उपयोग करता हूं।

private void delay(long milliseconds) {
    String bar = "[--------------------]";
    String icon = "%";

    long startTime = new Date().getTime();
    boolean bouncePositive = true;
    int barPosition = 0;

    while((new Date().getTime() - startTime) < milliseconds) {
        if(barPosition < bar.length() && barPosition > 0) {
            String b1 = bar.substring(0, barPosition);
            String b2 = bar.substring(barPosition);
            System.out.print("\r Delaying: " + b1 + icon + b2);
            if(bouncePositive) barPosition++;
            else barPosition--;
        } if(barPosition == bar.length()) {
            barPosition--;
            bouncePositive = false;
        } if(barPosition == 0) {
            barPosition++;
            bouncePositive = true;
        }

        try { Thread.sleep(100); }
        catch (Exception e) {}
    }
    System.out.print("\n");
}

2

मैंने हाल ही में एक ही समस्या का सामना किया है, आप मेरे कोड की जांच कर सकते हैं: मैंने इसे 5% पर एक # सेट किया है, जिसे आप बाद में संशोधित कर सकते हैं।

public static void main (String[] args) throws java.lang.Exception
{
    int i = 0;
    while(i < 21) {
        System.out.print("[");
        for (int j=0;j<i;j++) {
            System.out.print("#");
        }

        for (int j=0;j<20-i;j++) {
            System.out.print(" ");
        }

        System.out.print("] "+  i*5 + "%");
        if(i<20) {
            System.out.print("\r");
            Thread.sleep(300);
        }
        i++;
    }
    System.out.println();
}

1
public static void main(String[] argv) throws Exception{


    System.out.write("\r".getBytes());
    int percentage =10;
    while(percentage <= 100) {
        String temp =generateStars(percentage);
        System.out.write(temp.getBytes());
        System.out.print("\b\b\b");
        percentage = percentage+10;
        Thread.sleep(500);
    }
}

    public static String generateStars(int percentage)
    {
        int startsNum = percentage / 4;
        StringBuilder builder = new StringBuilder();
        while(startsNum >= 0)
        {
        builder.append("*");
        startsNum--;
        }
        builder.append(percentage+"%");
        return builder.toString();
    }

1

मैंने इवा कैंपबेल के कोड को जावा में संपादित किया और पर्केंट्स में स्वरूपित प्रगति को जोड़ा।

public static String progressBar(int currentValue, int maxValue) {
    int progressBarLength = 33; //
    if (progressBarLength < 9 || progressBarLength % 2 == 0) {
        throw new ArithmeticException("formattedPercent.length() = 9! + even number of chars (one for each side)");
    }
    int currentProgressBarIndex = (int) Math.ceil(((double) progressBarLength / maxValue) * currentValue);
    String formattedPercent = String.format(" %5.1f %% ", (100 * currentProgressBarIndex) / (double) progressBarLength);
    int percentStartIndex = ((progressBarLength - formattedPercent.length()) / 2);

    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (int progressBarIndex = 0; progressBarIndex < progressBarLength; progressBarIndex++) {
        if (progressBarIndex <= percentStartIndex - 1
        ||  progressBarIndex >= percentStartIndex + formattedPercent.length()) {
            sb.append(currentProgressBarIndex <= progressBarIndex ? " " : "=");
        } else if (progressBarIndex == percentStartIndex) {
            sb.append(formattedPercent);
        }
    }
    sb.append("]");
    return sb.toString();
}

int max = 22;
System.out.println("Generating report...");
for (int i = 0; i <= max; i++) {
   Thread.sleep(100);
   System.out.print(String.format("\r%s", progressBar(i, max)));
}
System.out.println("\nSuccessfully saved 32128 bytes");

और आउटपुट:

Generating report...

[========      24.2 %             ]

[============  45.5 %             ]

[============  78.8 % =====       ]

[============  87.9 % ========    ]

[============ 100.0 % ============]

Successfully saved 32128 bytes

0
public class ProgressBar
{
    private int max;

    public ProgressBar(int max0) {
        max = max0;
        update(0);
    }

    public void update(int perc) {
        String toPrint = "|";
        for(int i = 0; i < max; i++) {
            if(i <= (perc + 1))
                toPrint += "=";
            else
                toPrint += " ";
        }

        if(perc >= max)
            Console.print("\r");
        else
            Console.print(toPrint + "|\r");
    }
}

0
public class Main {

    public static void main(String[] args) throws Exception {

        System.out.println("Loading : ");
            int count =1;
            for(int j=1;j<150;j++){

                System.out.print("\r");
                if(count==1){
                    System.out.print("/");
                    count++;
                }
                else if(count==2){
                    System.out.print("|");
                    count++;
                }
                else if(count==3){
                    System.out.print("-");
                    count++;
                }
                else if(count==4){
                    System.out.print("\\");
                    count++;
                }
                else if(count==5){
                    System.out.print("|");
                    count++;
                }
                else
                    count = 1;
            Thread.sleep(200);
        }

        }

    }

0
static String progressBar(int progressBarSize, long currentPosition, long startPositoin, long finishPosition) {
    String bar = "";
    int nPositions = progressBarSize;
    char pb = '░';
    char stat = '█';
    for (int p = 0; p < nPositions; p++) {
        bar += pb;
    }
    int ststus = (int) (100 * (currentPosition - startPositoin) / (finishPosition - startPositoin));
    int move = (nPositions * ststus) / 100;
    return "[" + bar.substring(0, move).replace(pb, stat) + ststus + "%" + bar.substring(move, bar.length()) + "]";
}

यहाँ छवि विवरण दर्ज करें

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.