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

io.bdeploy.common.cli.data.DataFormat Maven / Gradle / Ivy

Go to download

Public API including dependencies, ready to be used for integrations and plugins.

There is a newer version: 7.3.6
Show newest version
package io.bdeploy.common.cli.data;

import java.io.IOException;
import java.io.PrintStream;

import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.Log;

/**
 * The different data output formats.
 */
public enum DataFormat {

    /** Output will be given in a human readable format. */
    TEXT,

    /** Output will be given in CSV. */
    CSV,

    /** Output will be given in JSON. */
    JSON;

    private static final int TERMINAL_SIZE = getTerminalSize();

    public DataTable createTable(PrintStream target) {
        switch (this) {
            case TEXT:
                return new DataTableText(target).setMaxTableLengthHint(TERMINAL_SIZE);
            case CSV:
                return new DataTableCsv(target);
            case JSON:
                return new DataTableJson(target);
        }
        throw new IllegalStateException("Unsupported DataFormat: " + this.name());
    }

    public DataResult createResult(PrintStream target) {
        switch (this) {
            case TEXT:
                return new DataResultText(target);
            case CSV:
                return new DataResultCsv(target);
            case JSON:
                return new DataResultJson(target);
        }
        throw new IllegalStateException("Unsupported DataFormat: " + this.name());
    }

    private static int getTerminalSize() {
        try (Terminal terminal = TerminalBuilder.terminal()) {
            return terminal.getWidth();
        } catch (IOException e) {
            Log.warn("Failed to get terminal width.", e);
            return -1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy