org.shaf.shell.util.ViewUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.shell.util.ViewUtils.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.shaf.shell.util;

import org.shaf.core.util.Progress;

import com.google.common.base.Strings;

/**
 * The set of utility functions for views.
 * 
 * @author Mykola Galushka
 */
public class ViewUtils {

    /**
     * The progress bar length.
     */
    private static final int PROGRESS_BAR_LENGTH = 20;

    /**
     * Returns a progress bar.
     * 
     * @param progress
     *            the progress indicator.
     * @return the progress bar.
     */
    public static final String bar(final Progress progress) {
        int percent = (int) (100 * progress.getValue());
        int len = (int) (PROGRESS_BAR_LENGTH * progress.getValue());

        StringBuilder bar = new StringBuilder();
        bar.append('[');
        bar.append(Strings.repeat("=", len));
        bar.append(Strings.repeat(" ", 20 - len));
        bar.append(Strings.repeat(" ", 3 - String.valueOf(percent).length()));
        bar.append(String.valueOf(percent) + "%");
        bar.append(']');
        bar.append(((Strings.isNullOrEmpty(progress.getMessage())) ? "" : " : " + progress.getMessage()));
        return bar.toString();
    }
}