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

org.gradle.internal.logging.events.StyledTextOutputEvent Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2016 the original author or authors.
 *
 * 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.gradle.internal.logging.events;

import org.gradle.api.logging.LogLevel;
import org.gradle.internal.logging.text.StyledTextOutput;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class StyledTextOutputEvent extends RenderableOutputEvent {
    private final List spans;

    public StyledTextOutputEvent(long timestamp, String category, String text) {
        this(timestamp, category, StyledTextOutput.Style.Normal, text);
    }

    public StyledTextOutputEvent(long timestamp, String category, LogLevel logLevel, String text) {
        this(timestamp, category, logLevel, StyledTextOutput.Style.Normal, text);
    }

    public StyledTextOutputEvent(long timestamp, String category, StyledTextOutput.Style style, String text) {
        this(timestamp, category, null, style, text);
    }

    public StyledTextOutputEvent(long timestamp, String category, LogLevel logLevel, StyledTextOutput.Style style, String text) {
        this(timestamp, category, logLevel, Collections.singletonList(new Span(style, text)));
    }

    public StyledTextOutputEvent(long timestamp, String category, List spans) {
        this(timestamp, category, null, spans);
    }

    public StyledTextOutputEvent(long timestamp, String category, LogLevel logLevel, Span... spans) {
        this(timestamp, category, logLevel, Arrays.asList(spans));
    }

    public StyledTextOutputEvent(long timestamp, String category, LogLevel logLevel, List spans) {
        super(timestamp, category, logLevel);
        this.spans = new ArrayList(spans);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append('[').append(getLogLevel()).append("] [");
        builder.append(getCategory()).append("] ");
        for (Span span : spans) {
            builder.append('<');
            builder.append(span.style);
            builder.append(">");
            builder.append(span.text);
            builder.append("");
        }
        return builder.toString();
    }

    public StyledTextOutputEvent withLogLevel(LogLevel logLevel) {
        return new StyledTextOutputEvent(getTimestamp(), getCategory(), logLevel, spans);
    }

    public List getSpans() {
        return spans;
    }

    @Override
    public void render(StyledTextOutput output) {
        for (Span span : spans) {
            output.style(span.style);
            output.text(span.text);
        }
    }

    public static class Span implements Serializable {
        private final String text;
        private final StyledTextOutput.Style style;

        public Span(StyledTextOutput.Style style, String text) {
            this.style = style;
            this.text = text;
        }

        public Span(String text) {
            this.style = StyledTextOutput.Style.Normal;
            this.text = text;
        }

        public StyledTextOutput.Style getStyle() {
            return style;
        }

        public String getText() {
            return text;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy