github.com/wtfutil/wtf@v0.43.0/utils/text.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"strings"
     7  
     8  	"golang.org/x/text/message"
     9  
    10  	"github.com/rivo/tview"
    11  )
    12  
    13  // CenterText takes a string and a width and pads the left and right of the string with
    14  // empty spaces to ensure that the string is in the middle of the returned value
    15  //
    16  // Example:
    17  //
    18  //	x := CenterText("cat", 11)
    19  //	> "    cat    "
    20  func CenterText(str string, width int) string {
    21  	if width < 0 {
    22  		width = 0
    23  	}
    24  
    25  	return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
    26  }
    27  
    28  // FindBetween finds and returns the text between two strings
    29  //
    30  // Example:
    31  //
    32  //	a := "{ cat } { dog }"
    33  //	b := FindBetween(a, "{", "}")
    34  //	> [" cat ", " dog "]
    35  func FindBetween(input string, left string, right string) []string {
    36  	out := []string{}
    37  
    38  	i := 0
    39  	for i >= 0 {
    40  		i = strings.Index(input, left)
    41  		if i == -1 {
    42  			break
    43  		}
    44  
    45  		i += len(left)
    46  
    47  		e := strings.Index(input[i:], right)
    48  		if e == -1 {
    49  			break
    50  		}
    51  
    52  		if e <= i {
    53  			break
    54  		}
    55  
    56  		chunk := input[i : e+1]
    57  		input = input[i+e+1:]
    58  
    59  		out = append(out, chunk)
    60  
    61  		i = i + e
    62  
    63  	}
    64  
    65  	return out
    66  }
    67  
    68  // HighlightableHelper pads the given text with blank spaces to the width of the view
    69  // containing it. This is helpful for extending row highlighting across the entire width
    70  // of the view
    71  func HighlightableHelper(view *tview.TextView, input string, idx, offset int) string {
    72  	_, _, w, _ := view.GetInnerRect()
    73  
    74  	fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
    75  	fmtStr += input
    76  	fmtStr += RowPadding(offset, w)
    77  	fmtStr += `[""]` + "\n"
    78  
    79  	return fmtStr
    80  }
    81  
    82  // RowPadding returns a padding for a row to make it the full width of the containing widget.
    83  // Useful for ensuring row highlighting spans the full width (I suspect tcell has a better
    84  // way to do this, but I haven't yet found it)
    85  func RowPadding(offset int, max int) string {
    86  	padSize := max - offset
    87  	if padSize < 0 {
    88  		padSize = 0
    89  	}
    90  
    91  	return strings.Repeat(" ", padSize)
    92  }
    93  
    94  // Truncate chops a given string at len length. Appends an ellipse character if warranted
    95  func Truncate(src string, maxLen int, withEllipse bool) string {
    96  	if len(src) < 1 || maxLen < 1 {
    97  		return ""
    98  	}
    99  
   100  	if maxLen == 1 {
   101  		return src[:1]
   102  	}
   103  
   104  	var runeCount = 0
   105  	for idx := range src {
   106  		runeCount++
   107  		if runeCount > maxLen {
   108  			if withEllipse {
   109  				return src[:idx-1] + "…"
   110  			}
   111  
   112  			return src[:idx]
   113  		}
   114  	}
   115  	return src
   116  }
   117  
   118  // PrettyNumber formats number as string with 1000 delimiters and, if necessary, rounds it to 2 decimals
   119  func PrettyNumber(prtr *message.Printer, number float64) string {
   120  	if number == math.Trunc(number) {
   121  		return prtr.Sprintf("%.0f", number)
   122  	}
   123  
   124  	return prtr.Sprintf("%.2f", number)
   125  }