001/*
002 * Copyright 2025 devteam@scivicslab.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing,
011 * software distributed under the License is distributed on an
012 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied.  See the License for the
014 * specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.scivicslab.actoriac;
019
020import com.github.ricksbrown.cowsay.Cowsay;
021import com.scivicslab.pojoactor.core.accumulator.StreamingAccumulator;
022
023/**
024 * StreamingAccumulator with cowsay display support for actor-IaC.
025 *
026 * <p>This accumulator extends {@link StreamingAccumulator} to add cowsay
027 * ASCII art visualization for workflow step transitions. The cowfile (character)
028 * can be customized to show different ASCII art animals.</p>
029 *
030 * <h2>Example Usage</h2>
031 * <pre>{@code
032 * IaCStreamingAccumulator acc = new IaCStreamingAccumulator();
033 * acc.setCowfile("tux");  // Use Linux penguin
034 * acc.cowsay("my-workflow", "- states: [\"0\", \"1\"]\n  actions: ...");
035 * }</pre>
036 *
037 * <h2>Available Cowfiles</h2>
038 * <p>44 cowfiles are available including: tux, dragon, stegosaurus, kitty,
039 * bunny, turtle, elephant, ghostbusters, vader, and many more.</p>
040 *
041 * @author devteam@scivicslab.com
042 * @since 2.12.0
043 */
044public class IaCStreamingAccumulator extends StreamingAccumulator {
045
046    /**
047     * Cowfile name for cowsay output.
048     * When null, uses the default cow.
049     */
050    private String cowfile = null;
051
052    /**
053     * Constructs an IaCStreamingAccumulator with default settings.
054     */
055    public IaCStreamingAccumulator() {
056        super();
057    }
058
059    /**
060     * Sets the cowfile name for cowsay output.
061     *
062     * <p>Available cowfiles include: tux, dragon, stegosaurus, kitty, bunny,
063     * turtle, elephant, ghostbusters, vader, and many more (44 total).</p>
064     *
065     * @param cowfile the cowfile name (e.g., "tux", "dragon"), or null for default cow
066     */
067    public void setCowfile(String cowfile) {
068        this.cowfile = cowfile;
069    }
070
071    /**
072     * Gets the cowfile name for cowsay output.
073     *
074     * @return the cowfile name, or null if using default cow
075     */
076    public String getCowfile() {
077        return cowfile;
078    }
079
080    /**
081     * Renders a workflow step as cowsay ASCII art and returns the result.
082     *
083     * <p>This method formats the workflow name and step YAML into a cowsay
084     * message with the configured cowfile character. The rendered ASCII art
085     * is returned as a string instead of being printed directly, allowing
086     * the caller to send it to the output multiplexer.</p>
087     *
088     * @param workflowName the name of the workflow
089     * @param stepYaml the YAML representation of the step (first 10 lines recommended)
090     * @return the rendered cowsay ASCII art string
091     */
092    public String renderCowsay(String workflowName, String stepYaml) {
093        String displayText = "[" + workflowName + "]\n" + stepYaml;
094        String[] cowsayArgs;
095        if (cowfile != null && !cowfile.isBlank()) {
096            cowsayArgs = new String[] { "-f", cowfile, displayText };
097        } else {
098            cowsayArgs = new String[] { displayText };
099        }
100        return Cowsay.say(cowsayArgs);
101    }
102
103    /**
104     * Lists all available cowfile names.
105     *
106     * @return array of available cowfile names
107     */
108    public static String[] listCowfiles() {
109        String[] listArgs = { "-l" };
110        String cowList = Cowsay.say(listArgs);
111        return cowList.trim().split("\\s+");
112    }
113}