get rid of utils.Cut since we can now rely on strings.Cut instead

This commit is contained in:
Kovid Goyal 2023-03-04 13:37:55 +05:30
parent defac0c061
commit a2887bb9e0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
12 changed files with 24 additions and 35 deletions

View File

@ -7,9 +7,9 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
"kitty/tools/tty" "kitty/tools/tty"
"kitty/tools/utils"
) )
func debug(args ...any) { func debug(args ...any) {
@ -81,7 +81,7 @@ func GenerateCompletions(args []string) error {
} }
shell_state := make(map[string]string, n) shell_state := make(map[string]string, n)
for _, arg := range args { for _, arg := range args {
k, v, found := utils.Cut(arg, "=") k, v, found := strings.Cut(arg, "=")
if !found { if !found {
return fmt.Errorf("Invalid shell state specification: %s", arg) return fmt.Errorf("Invalid shell state specification: %s", arg)
} }

View File

@ -36,7 +36,7 @@ func (self *MatchGroup) remove_common_prefix() string {
} }
} }
} else if len(self.Matches) > 1 && strings.HasPrefix(self.Matches[0].Word, "--") && strings.Contains(self.Matches[0].Word, "=") { } else if len(self.Matches) > 1 && strings.HasPrefix(self.Matches[0].Word, "--") && strings.Contains(self.Matches[0].Word, "=") {
lcp, _, _ := utils.Cut(self.longest_common_prefix(), "=") lcp, _, _ := strings.Cut(self.longest_common_prefix(), "=")
lcp += "=" lcp += "="
if len(lcp) > 3 { if len(lcp) > 3 {
self.remove_prefix_from_all_matches(lcp) self.remove_prefix_from_all_matches(lcp)

View File

@ -76,7 +76,7 @@ func (self *Match) FormatForCompletionList(max_word_len int, f *markup.Context,
return word return word
} }
word_len := wcswidth.Stringwidth(word) word_len := wcswidth.Stringwidth(word)
line, _, _ := utils.Cut(strings.TrimSpace(desc), "\n") line, _, _ := strings.Cut(strings.TrimSpace(desc), "\n")
desc = f.Prettify(line) desc = f.Prettify(line)
multiline := false multiline := false

View File

@ -3,13 +3,13 @@
package at package at
import ( import (
"kitty/tools/utils" "strings"
) )
func parse_key_val_args(args []string) map[escaped_string]escaped_string { func parse_key_val_args(args []string) map[escaped_string]escaped_string {
ans := make(map[escaped_string]escaped_string, len(args)) ans := make(map[escaped_string]escaped_string, len(args))
for _, arg := range args { for _, arg := range args {
key, value, found := utils.Cut(arg, "=") key, value, found := strings.Cut(arg, "=")
if found { if found {
ans[escaped_string(key)] = escaped_string(value) ans[escaped_string(key)] = escaped_string(value)
} else { } else {

View File

@ -80,7 +80,7 @@ func get_pubkey(encoded_key string) (encryption_version string, pubkey []byte, e
return return
} }
} }
encryption_version, encoded_key, found := utils.Cut(encoded_key, ":") encryption_version, encoded_key, found := strings.Cut(encoded_key, ":")
if !found { if !found {
err = fmt.Errorf("KITTY_PUBLIC_KEY environment variable does not have a : in it") err = fmt.Errorf("KITTY_PUBLIC_KEY environment variable does not have a : in it")
return return

View File

@ -48,7 +48,7 @@ func set_color_in_color_map(key, val string, ans map[string]any, check_nullable,
func parse_colors_and_files(args []string) (map[string]any, error) { func parse_colors_and_files(args []string) (map[string]any, error) {
ans := make(map[string]any, len(args)) ans := make(map[string]any, len(args))
for _, arg := range args { for _, arg := range args {
key, val, found := utils.Cut(strings.ToLower(arg), "=") key, val, found := strings.Cut(strings.ToLower(arg), "=")
if found { if found {
err := set_color_in_color_map(key, val, ans, true, false) err := set_color_in_color_map(key, val, ans, true, false)
if err != nil { if err != nil {
@ -63,7 +63,7 @@ func parse_colors_and_files(args []string) (map[string]any, error) {
defer f.Close() defer f.Close()
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(f)
for scanner.Scan() { for scanner.Scan() {
key, val, found := utils.Cut(scanner.Text(), " ") key, val, found := strings.Cut(scanner.Text(), " ")
if found { if found {
set_color_in_color_map(strings.ToLower(key), strings.ToLower(strings.TrimSpace(val)), ans, true, true) set_color_in_color_map(strings.ToLower(key), strings.ToLower(strings.TrimSpace(val)), ans, true, true)
} }

View File

@ -6,8 +6,6 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"kitty/tools/utils"
) )
func parse_set_spacing(args []string) (map[string]any, error) { func parse_set_spacing(args []string) (map[string]any, error) {
@ -24,7 +22,7 @@ func parse_set_spacing(args []string) (map[string]any, error) {
mapper[q+"-bottom"] = []string{q + "bottom"} mapper[q+"-bottom"] = []string{q + "bottom"}
} }
for _, arg := range args { for _, arg := range args {
k, v, found := utils.Cut(arg, "=") k, v, found := strings.Cut(arg, "=")
if !found { if !found {
return nil, fmt.Errorf("%s is not a valid setting", arg) return nil, fmt.Errorf("%s is not a valid setting", arg)
} }

View File

@ -5,8 +5,6 @@ package at
import ( import (
"fmt" "fmt"
"strings" "strings"
"kitty/tools/utils"
) )
var valid_color_names = map[string]bool{"active_fg": true, "active_bg": true, "inactive_fg": true, "inactive_bg": true} var valid_color_names = map[string]bool{"active_fg": true, "active_bg": true, "inactive_fg": true, "inactive_bg": true}
@ -14,7 +12,7 @@ var valid_color_names = map[string]bool{"active_fg": true, "active_bg": true, "i
func parse_tab_colors(args []string) (map[string]any, error) { func parse_tab_colors(args []string) (map[string]any, error) {
ans := make(map[string]any, len(args)) ans := make(map[string]any, len(args))
for _, arg := range args { for _, arg := range args {
key, val, found := utils.Cut(strings.ToLower(arg), "=") key, val, found := strings.Cut(strings.ToLower(arg), "=")
if !found { if !found {
return nil, fmt.Errorf("%s is not a valid setting", arg) return nil, fmt.Errorf("%s is not a valid setting", arg)
} }

View File

@ -269,7 +269,7 @@ func parse_escape_code(etype loop.EscapeCodeType, data []byte) (metadata map[str
func parse_aliases(raw []string) (map[string][]string, error) { func parse_aliases(raw []string) (map[string][]string, error) {
ans := make(map[string][]string, len(raw)) ans := make(map[string][]string, len(raw))
for _, x := range raw { for _, x := range raw {
k, v, found := utils.Cut(x, "=") k, v, found := strings.Cut(x, "=")
if !found { if !found {
return nil, fmt.Errorf("%s is not valid MIME alias specification", x) return nil, fmt.Errorf("%s is not valid MIME alias specification", x)
} }

View File

@ -70,16 +70,16 @@ func parse_identify_record(ans *IdentifyRecord, raw *IdentifyOutput) (err error)
} }
ans.Gap = utils.Max(0, ans.Gap) ans.Gap = utils.Max(0, ans.Gap)
} }
area, pos, found := utils.Cut(raw.Canvas, "+") area, pos, found := strings.Cut(raw.Canvas, "+")
ok := false ok := false
if found { if found {
w, h, found := utils.Cut(area, "x") w, h, found := strings.Cut(area, "x")
if found { if found {
ans.Canvas.Width, err = strconv.Atoi(w) ans.Canvas.Width, err = strconv.Atoi(w)
if err == nil { if err == nil {
ans.Canvas.Height, err = strconv.Atoi(h) ans.Canvas.Height, err = strconv.Atoi(h)
if err == nil { if err == nil {
x, y, found := utils.Cut(pos, "+") x, y, found := strings.Cut(pos, "+")
if found { if found {
ans.Canvas.Left, err = strconv.Atoi(x) ans.Canvas.Left, err = strconv.Atoi(x)
if err == nil { if err == nil {
@ -94,7 +94,7 @@ func parse_identify_record(ans *IdentifyRecord, raw *IdentifyOutput) (err error)
if !ok { if !ok {
return fmt.Errorf("Invalid canvas value in identify output: %s", raw.Canvas) return fmt.Errorf("Invalid canvas value in identify output: %s", raw.Canvas)
} }
w, h, found := utils.Cut(raw.Size, "x") w, h, found := strings.Cut(raw.Size, "x")
ok = false ok = false
if found { if found {
ans.Width, err = strconv.Atoi(w) ans.Width, err = strconv.Atoi(w)
@ -106,7 +106,7 @@ func parse_identify_record(ans *IdentifyRecord, raw *IdentifyOutput) (err error)
if !ok { if !ok {
return fmt.Errorf("Invalid size value in identify output: %s", raw.Size) return fmt.Errorf("Invalid size value in identify output: %s", raw.Size)
} }
x, y, found := utils.Cut(raw.Dpi, "x") x, y, found := strings.Cut(raw.Dpi, "x")
ok = false ok = false
if found { if found {
ans.Dpi.X, err = strconv.ParseFloat(x, 64) ans.Dpi.X, err = strconv.ParseFloat(x, 64)
@ -302,7 +302,7 @@ func Render(path string, ro *RenderOptions, frames []IdentifyRecord) (ans []*ima
min_gap := calc_min_gap(gaps) min_gap := calc_min_gap(gaps)
for _, entry := range entries { for _, entry := range entries {
fname := entry.Name() fname := entry.Name()
p, _, _ := utils.Cut(fname, ".") p, _, _ := strings.Cut(fname, ".")
parts := strings.Split(p, "-") parts := strings.Split(p, "-")
if len(parts) < 5 { if len(parts) < 5 {
continue continue
@ -319,11 +319,11 @@ func Render(path string, ro *RenderOptions, frames []IdentifyRecord) (ans []*ima
if cerr != nil { if cerr != nil {
continue continue
} }
_, pos, found := utils.Cut(parts[3], "+") _, pos, found := strings.Cut(parts[3], "+")
if !found { if !found {
continue continue
} }
px, py, found := utils.Cut(pos, "+") px, py, found := strings.Cut(pos, "+")
if !found { if !found {
continue continue
} }

View File

@ -91,15 +91,15 @@ func parse_place() (err error) {
if opts.Place == "" { if opts.Place == "" {
return nil return nil
} }
area, pos, found := utils.Cut(opts.Place, "@") area, pos, found := strings.Cut(opts.Place, "@")
if !found { if !found {
return fmt.Errorf("Invalid --place specification: %s", opts.Place) return fmt.Errorf("Invalid --place specification: %s", opts.Place)
} }
w, h, found := utils.Cut(area, "x") w, h, found := strings.Cut(area, "x")
if !found { if !found {
return fmt.Errorf("Invalid --place specification: %s", opts.Place) return fmt.Errorf("Invalid --place specification: %s", opts.Place)
} }
l, t, found := utils.Cut(pos, "x") l, t, found := strings.Cut(pos, "x")
if !found { if !found {
return fmt.Errorf("Invalid --place specification: %s", opts.Place) return fmt.Errorf("Invalid --place specification: %s", opts.Place)
} }

View File

@ -10,15 +10,8 @@ import (
"github.com/seancfoley/ipaddress-go/ipaddr" "github.com/seancfoley/ipaddress-go/ipaddr"
) )
func Cut(s string, sep string) (string, string, bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
func ParseSocketAddress(spec string) (network string, addr string, err error) { func ParseSocketAddress(spec string) (network string, addr string, err error) {
network, addr, found := Cut(spec, ":") network, addr, found := strings.Cut(spec, ":")
if !found { if !found {
err = fmt.Errorf("Invalid socket address: %s must be prefix by a protocol such as unix:", spec) err = fmt.Errorf("Invalid socket address: %s must be prefix by a protocol such as unix:", spec)
return return