github.com/oam-dev/kubevela@v1.9.11/pkg/utils/strings.go (about)

     1  /*
     2  Copyright 2021 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 utils
    18  
    19  import (
    20  	"strings"
    21  )
    22  
    23  // GetBoxDrawingString get line drawing string, see https://en.wikipedia.org/wiki/Box-drawing_character
    24  // nolint:gocyclo
    25  func GetBoxDrawingString(up bool, down bool, left bool, right bool, padLeft int, padRight int) string {
    26  	var c rune
    27  	switch {
    28  	case up && down && left && right:
    29  		c = '┼'
    30  	case up && down && left && !right:
    31  		c = '┤'
    32  	case up && down && !left && right:
    33  		c = '├'
    34  	case up && down && !left && !right:
    35  		c = '│'
    36  	case up && !down && left && right:
    37  		c = '┴'
    38  	case up && !down && left && !right:
    39  		c = '┘'
    40  	case up && !down && !left && right:
    41  		c = '└'
    42  	case up && !down && !left && !right:
    43  		c = '╵'
    44  	case !up && down && left && right:
    45  		c = '┬'
    46  	case !up && down && left && !right:
    47  		c = '┐'
    48  	case !up && down && !left && right:
    49  		c = '┌'
    50  	case !up && down && !left && !right:
    51  		c = '╷'
    52  	case !up && !down && left && right:
    53  		c = '─'
    54  	case !up && !down && left && !right:
    55  		c = '╴'
    56  	case !up && !down && !left && right:
    57  		c = '╶'
    58  	case !up && !down && !left && !right:
    59  		c = ' '
    60  	}
    61  	sb := strings.Builder{}
    62  	writePadding := func(connect bool, width int) {
    63  		for i := 0; i < width; i++ {
    64  			if connect {
    65  				sb.WriteRune('─')
    66  			} else {
    67  				sb.WriteRune(' ')
    68  			}
    69  		}
    70  	}
    71  	writePadding(left, padLeft)
    72  	sb.WriteRune(c)
    73  	writePadding(right, padRight)
    74  	return sb.String()
    75  }
    76  
    77  // Sanitize the inputs by removing line endings
    78  func Sanitize(s string) string {
    79  	s = strings.ReplaceAll(s, "\n", "")
    80  	s = strings.ReplaceAll(s, "\r", "")
    81  	return s
    82  }
    83  
    84  // IgnoreVPrefix removes the leading "v" from a string
    85  func IgnoreVPrefix(s string) string {
    86  	return strings.TrimPrefix(s, "v")
    87  }