go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/utils/stringx/indent.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package stringx
     5  
     6  import (
     7  	"bufio"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  func Indent(indent int, message string) string {
    13  	indentTxt := ""
    14  	for i := 0; i < indent; i++ {
    15  		indentTxt += " "
    16  	}
    17  
    18  	var sb strings.Builder
    19  	scanner := bufio.NewScanner(strings.NewReader(message))
    20  	for scanner.Scan() {
    21  		sb.WriteString(indentTxt)
    22  		sb.WriteString(scanner.Text())
    23  		sb.WriteString("\n")
    24  	}
    25  	return sb.String()
    26  }
    27  
    28  func MaxLines(lines int, message string) string {
    29  	res := strings.Split(message, "\n")
    30  	if len(res) <= lines {
    31  		return message
    32  	}
    33  
    34  	n := len(res) - lines
    35  
    36  	return strings.Join(res[:lines], "\n") + "\n... " + strconv.Itoa(n) + " more lines ..."
    37  }