All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redhat.ceylon.common.StatusPrinter Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package com.redhat.ceylon.common;

public class StatusPrinter {
    private int lineWidth;
    private int remaining;
    private final static char abbreviationChar = '…';
    private final static String abbreviationString = "…";
    private StringBuilder currentLine;
    private String capturedLine;

    public StatusPrinter(){
        this(Integer.getInteger("com.redhat.ceylon.common.tool.terminal.width", 80));
    }
    
    public StatusPrinter(int i) {
        this.lineWidth = i;
        this.remaining = i;
        this.currentLine = new StringBuilder(lineWidth);
    }

    public void clearLine(){
        if(currentLine.length() == 0)
            return;
        StringBuilder sb = new StringBuilder(lineWidth+2);
        sb.append('\r');
        for(int i=0;i= max){
            if(codePoints != max)
                return limit(str, max);
            else
                return str;
        }else{
            return limit(str, remaining);
        }
    }

    public String partRight(String str, int sizeLimit){
        if(remaining == 0)
            return "";
        int codePoints = str.codePointCount(0, str.length());
        int max = Math.min(codePoints, sizeLimit);
        if(remaining >= max){
            if(codePoints != max)
                return leftPad(limit(str, max), remaining - max);
            else
                return leftPad(str, remaining - codePoints);
        }else{
            return limit(str, remaining);
        }
    }

    public String part(String str){
        if(remaining == 0)
            return "";
        int codePoints = str.codePointCount(0, str.length());
        if(remaining >= codePoints){
            return str;
        }else{
            return limit(str, remaining);
        }
    }

    public String partRight(String str){
        if(remaining == 0)
            return "";
        int codePoints = str.codePointCount(0, str.length());
        if(remaining >= codePoints){
            return leftPad(str, remaining-codePoints);
        }else{
            return limit(str, remaining);
        }
    }

    private String leftPad(String str, int count) {
        if(count == 0)
            return str;
        // normal length works here because we dont change its code point length, every
        // space is 1 code point
        StringBuilder b = new StringBuilder(str.length()+count);
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy