util.go
This file
Path
src/util/util.go
165 lines · 3.4 KiB · Go
Last changed by
Fix --gap-line cutting a grapheme clusterJunegunn Choibefore adoption961793c
History of this fileTake it
1package util23import (4 "cmp"5 "math"6 "os"7 "strconv"8 "strings"910 "github.com/mattn/go-isatty"11 "github.com/rivo/uniseg"12)1314// StringWidth returns string width where each CR/LF character takes 1 column15func StringWidth(s string) int {16 return uniseg.StringWidth(s) + strings.Count(s, "\n") + strings.Count(s, "\r")17}1819// RunesWidth returns runes width20func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {21 return StringsWidth(string(runes), prefixWidth, tabstop, limit)22}2324// StringsWidth returns the width of the string25func StringsWidth(str string, prefixWidth int, tabstop int, limit int) (int, int) {26 width := 027 gr := uniseg.NewGraphemes(str)28 idx := 029 for gr.Next() {30 rs := gr.Runes()31 var w int32 if len(rs) == 1 && rs[0] == '\t' {33 w = tabstop - (prefixWidth+width)%tabstop34 } else {35 w = StringWidth(string(rs))36 }37 width += w38 if width > limit {39 return width, idx40 }41 idx += len(rs)42 }43 return width, -144}4546// Truncate returns the truncated runes and its width47func Truncate(input string, limit int) ([]rune, int) {48 runes := []rune{}49 width := 050 gr := uniseg.NewGraphemes(input)51 for gr.Next() {52 rs := gr.Runes()53 w := StringWidth(string(rs))54 if width+w > limit {55 return runes, width56 }57 width += w58 runes = append(runes, rs...)59 }60 return runes, width61}6263func Constrain[T cmp.Ordered](val, minimum, maximum T) T {64 return max(min(val, maximum), minimum)65}6667func AsUint16(val int) uint16 {68 if val > math.MaxUint16 {69 return math.MaxUint1670 } else if val < 0 {71 return 072 }73 return uint16(val)74}7576// IsTty returns true if the file is a terminal77func IsTty(file *os.File) bool {78 fd := file.Fd()79 return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)80}8182// RunOnce runs the given function only once83func RunOnce(f func()) func() {84 once := Once(true)85 return func() {86 if once() {87 f()88 }89 }90}9192// Once returns a function that returns the specified boolean value only once93func Once(nextResponse bool) func() bool {94 state := nextResponse95 return func() bool {96 prevState := state97 state = !nextResponse98 return prevState99 }100}101102// RepeatToFill repeats the given string to fill the given width103func RepeatToFill(str string, length int, limit int) string {104 times := limit / length105 rest := limit % length106 output := strings.Repeat(str, times)107 if rest > 0 {108 // Iterate over grapheme clusters so that we don't cut a cluster in half109 end := 0110 graphemes := uniseg.NewGraphemes(str)111 for rest > 0 && graphemes.Next() {112 width := graphemes.Width()113 if width > rest {114 break115 }116 rest -= width117 _, end = graphemes.Positions()118 }119 output += str[:end]120 }121 return output122}123124// ToKebabCase converts the given CamelCase string to kebab-case125func ToKebabCase(s string) string {126 name := ""127 for i, r := range s {128 if i > 0 && r >= 'A' && r <= 'Z' {129 name += "-"130 }131 name += string(r)132 }133 return strings.ToLower(name)134}135136// CompareVersions compares two version strings137func CompareVersions(v1, v2 string) int {138 parts1 := strings.Split(v1, ".")139 parts2 := strings.Split(v2, ".")140141 atoi := func(s string) int {142 n, e := strconv.Atoi(s)143 if e != nil {144 return 0145 }146 return n147 }148149 for i := 0; i < max(len(parts1), len(parts2)); i++ {150 var p1, p2 int151 if i < len(parts1) {152 p1 = atoi(parts1[i])153 }154 if i < len(parts2) {155 p2 = atoi(parts2[i])156 }157158 if p1 > p2 {159 return 1160 } else if p1 < p2 {161 return -1162 }163 }164 return 0165}