mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
// CenterText takes a string and a width and pads the left and right of the string with
|
|
// empty spaces to ensure that the string is in the middle of the returned value
|
|
//
|
|
// Example:
|
|
//
|
|
// x := CenterText("cat", 11)
|
|
// > " cat "
|
|
//
|
|
func CenterText(str string, width int) string {
|
|
if width < 0 {
|
|
width = 0
|
|
}
|
|
|
|
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
|
|
}
|
|
|
|
// HighlightableHelper pads the given text with blank spaces to the width of the view
|
|
// containing it. This is helpful for extending row highlighting across the entire width
|
|
// of the view
|
|
func HighlightableHelper(view *tview.TextView, input string, idx, offset int) string {
|
|
fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
|
|
_, _, w, _ := view.GetInnerRect()
|
|
fmtStr += input
|
|
fmtStr += PadRow(offset, w+1)
|
|
fmtStr += `[""]` + "\n"
|
|
return fmtStr
|
|
}
|
|
|
|
// PadRow returns a padding for a row to make it the full width of the containing widget.
|
|
// Useful for ensurig row highlighting spans the full width (I suspect tcell has a better
|
|
// way to do this, but I haven't yet found it)
|
|
func PadRow(offset int, max int) string {
|
|
padSize := max - offset
|
|
if padSize < 0 {
|
|
padSize = 0
|
|
}
|
|
|
|
return strings.Repeat(" ", padSize)
|
|
}
|