text/Java

나는 인내심이 적고 적어.. (progress bar)

hoonzii 2023. 3. 15. 10:01
반응형

tl;dr

  1. System.out.print(”…. \r”) 사용 시 해당 줄에서 지워지고 다시 써짐
  2. 애니메이션으로 보여지는건 직접 구현하거나, 잘 만들어진 거(예를 들어) 가져다가 쓰세요!

intellij로 자바 실행 시 오래 걸리는 작업의 경우, 콘솔창(intellij)에서 print 찍어 놓은 게 없으면 하염없이 기다려야 한다...

이 경우 얼마나 진행됐는지 알고 싶을 때는 로딩바 (progress bar)라고 불리는 게 필요한데 오늘은 직접 한번 코드로 구현해 보자.

 

일단 오늘의 글을 도와줄 코드

public class mainClass {
    public static void main(String[] args) {

        mainClass mainClass = new mainClass();

        int n = 691; // Task length
        for(int i = 0; i < n; i++) {
            System.out.println("i : "+i+" / total : "+n);
            // heavy Task...!
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

 

보통은 for-loop 시 시작은 0부터 종료는 임의의 N까지 인 경우가 많다.

각 연산 시마다 System.out.println() 함수를 호출 시

ln이 붙어있기에 한 줄씩 결과를 보여준다.

System.out.println("i : "+i+" / total : "+n);

건수가 많지 않을 때 이 방법이 나을 수 있다. sout 숏컷으로 한 번이라도 타자를 덜 칠 수 있으니…

그치만 이러면 console 창이 위아래로 지저분해지니

How can I print to the same line?

 

How can I print to the same line?

I want to print a progress bar like so: [# ] 1% [## ] 10% [########## ] 50% But these should all be printed to the same line in the terminal instead...

stackoverflow.com

프린트 시 같은 줄에서 내용만 바뀌게끔 해당 라인을 살짝 바꿔준다.

System.out.print("i : "+i+" / total : "+n+"\\r");

위와 같이 같은 자리에서 i만 바뀌면서 좀 더 진행상황이 깔끔하게 보이는 걸 확인할 수 있다.

 

그치만 난 좀 더 progress bar 같이 보였으면 좋겠다…! (배터리가 차는 거 같이!!)

함수를 하나 만들어주자.

  1. 입력으로는 현재 step, 총 step이 들어온다.
  2. [##   ]와 같은 애니메이션 효과가 있었으면 좋겠다.
  3. 현재 step에 맞춰 #이 채워지게 보여줬으면 좋겠다.
  4. 애니메이션과 더불어 퍼센트 (ex.10.98%) 도 보이면 좋겠다.
    1. 퍼센트는 소수점 두 자리로 고정!
public String returnProgressStr(int i, int n){
    double progress = (double) i / n * 100.0;
    int mod = (int) (progress/10) % 10;
    StringBuilder result = new StringBuilder();
    for(int j = 0; j < 10; j++) {
        if (j < mod)
            result.append("#");
        else
            result.append(" ");
    }
    return "["+result.toString()+"] "+String.format("%.2f", progress)+" % complete";
}

progress 변수의 경우, 퍼센트를 계산

mod 변수의 경우, 애니메이션에 # 을 채워 넣기 위해 개수를 계산

(10보다 아래면 0개, 10이면 1개… 같이)

메인 코드도 고쳐주자.

public static void main(String[] args) {

    mainClass mainClass = new mainClass();

    int n = 691; // Task length
    for(int i = 0; i < n; i++) {
//            System.out.print("i : "+i+" / total : "+n+"\\r");
        System.out.print(mainClass.returnProgressStr(i,n)+"\\r");
        // heavy Task...!
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

실행 결과를 살펴보자.

 

원하는 대로 돌아가는 걸 확인했다.

내가 생각한 건 보통 누군가가 구현했으니, 이런 모듈도 분명 있을 터…

 

 

Command line progress bar in Java

I have a Java program running in command line mode. I would like to display a progress bar, showing the percentage of job done. The same kind of progress bar you would see using wget under unix. Is...

stackoverflow.com

답변으로 해당 링크 (MIT lisence)인 모듈이 있으니 참고해 보시길…

 

 

반응형