github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/tree_node.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package component
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/kubevela/workflow/api/v1alpha1"
    23  
    24  	"github.com/oam-dev/kubevela/pkg/velaql/providers/query/types"
    25  	"github.com/oam-dev/kubevela/references/cli/top/config"
    26  )
    27  
    28  const (
    29  	component           = "🧩"
    30  	workflow            = "👟"
    31  	policy              = "📜"
    32  	trait               = "🔧"
    33  	app                 = "🎯"
    34  	other               = "🚓"
    35  	workflowStepSucceed = "✅"
    36  )
    37  
    38  // TopologyTreeNodeFormatter is the formatter for the topology tree node
    39  type TopologyTreeNodeFormatter struct {
    40  	style *config.ThemeConfig
    41  }
    42  
    43  // NewTopologyTreeNodeFormatter create a new topology tree node formatter
    44  func NewTopologyTreeNodeFormatter(style *config.ThemeConfig) *TopologyTreeNodeFormatter {
    45  	return &TopologyTreeNodeFormatter{
    46  		style: style,
    47  	}
    48  }
    49  
    50  const colorFmt = "%s [%s::b]%s[::]"
    51  
    52  // EmojiFormat format the name with the emoji
    53  func (t TopologyTreeNodeFormatter) EmojiFormat(name string, kind string) string {
    54  	switch kind {
    55  	case "app":
    56  		return fmt.Sprintf(colorFmt, app, t.style.Topology.App.String(), name)
    57  	case "workflow":
    58  		return fmt.Sprintf(colorFmt, workflow, t.style.Topology.Workflow.String(), name)
    59  	case "component":
    60  		return fmt.Sprintf(colorFmt, component, t.style.Topology.Component.String(), name)
    61  	case "policy":
    62  		return fmt.Sprintf(colorFmt, policy, t.style.Topology.Policy.String(), name)
    63  	case "trait":
    64  		return fmt.Sprintf(colorFmt, trait, t.style.Topology.Trait.String(), name)
    65  	default:
    66  		return fmt.Sprintf(colorFmt, other, t.style.Topology.Kind.String(), name)
    67  	}
    68  }
    69  
    70  const workflowStepFmt = "[::b]%s %s[::]"
    71  
    72  // WorkflowStepFormat format the workflow step text with the emoji
    73  func WorkflowStepFormat(name string, status v1alpha1.WorkflowStepPhase) string {
    74  	switch status {
    75  	case v1alpha1.WorkflowStepPhaseSucceeded:
    76  		return fmt.Sprintf(workflowStepFmt, name, workflowStepSucceed)
    77  	default:
    78  		return name
    79  	}
    80  }
    81  
    82  const statusColorFmt = "[%s::b]%s[::]"
    83  
    84  // ColorizeStatus colorize the status text
    85  func (t TopologyTreeNodeFormatter) ColorizeStatus(status types.HealthStatusCode) string {
    86  	switch status {
    87  	case types.HealthStatusHealthy:
    88  		return fmt.Sprintf(statusColorFmt, t.style.Status.Healthy.String(), status)
    89  	case types.HealthStatusUnHealthy:
    90  		return fmt.Sprintf(statusColorFmt, t.style.Status.UnHealthy.String(), status)
    91  	case types.HealthStatusProgressing:
    92  		return fmt.Sprintf(statusColorFmt, t.style.Status.Waiting.String(), status)
    93  	default:
    94  		return fmt.Sprintf(statusColorFmt, t.style.Status.Unknown.String(), status)
    95  	}
    96  }
    97  
    98  const kindColorFmt = "[%s::b]%s[::]"
    99  
   100  // ColorizeKind colorize the kind text
   101  func (t TopologyTreeNodeFormatter) ColorizeKind(kind string) string {
   102  	return fmt.Sprintf(kindColorFmt, t.style.Topology.Kind, kind)
   103  }