Use Cut, Fields.

This commit is contained in:
Nuno Cruces 2023-01-10 14:26:14 +00:00
parent b9058d050c
commit 5b4a55e77a
6 changed files with 25 additions and 28 deletions

View file

@ -741,12 +741,12 @@ func addColumn(s string) error {
func addFileFilter(s string) error {
var filter zenity.FileFilter
if head, tail, cut := strings.Cut(s, "|"); cut {
filter.Name = head
s = tail
if name, rest, cut := strings.Cut(s, "|"); cut {
filter.Name = name
s = rest
}
filter.Patterns = strings.Split(strings.Trim(s, " "), " ")
filter.Patterns = strings.Fields(s)
fileFilters = append(fileFilters, filter)
return nil
}

View file

@ -25,14 +25,13 @@ func notify(opts ...zenity.Option) error {
ico := zenity.NoIcon
for scanner := bufio.NewScanner(os.Stdin); scanner.Scan(); {
line := scanner.Text()
var cmd, msg string
if n := strings.IndexByte(line, ':'); n >= 0 {
cmd = strings.TrimSpace(line[:n])
msg = strings.TrimSpace(zencmd.Unescape(line[n+1:]))
} else {
cmd, msg, cut := strings.Cut(line, ":")
if !cut {
os.Stderr.WriteString("Could not parse command from stdin\n")
continue
}
cmd = strings.TrimSpace(cmd)
msg = strings.TrimSpace(zencmd.Unescape(msg))
switch cmd {
case "icon":
switch msg {
@ -51,9 +50,9 @@ func notify(opts ...zenity.Option) error {
}
case "message", "tooltip":
opts := []zenity.Option{ico}
if n := strings.IndexByte(msg, '\n'); n >= 0 {
opts = append(opts, zenity.Title(msg[:n]))
msg = msg[n+1:]
if title, rest, cut := strings.Cut(msg, "\n"); cut {
opts = append(opts, zenity.Title(title))
msg = rest
}
if err := zenity.Notify(msg, opts...); err != nil {
return err

View file

@ -60,5 +60,6 @@ type IBindCtx struct{ IUnknown }
//sys CoCreateInstance(clsid *GUID, unkOuter *IUnknown, clsContext int32, iid *GUID, address unsafe.Pointer) (res error) = ole32.CoCreateInstance
func guid(s string) *GUID {
// TODO: use unsafe.StringData after 1.20
return (*GUID)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data))
}

View file

@ -92,12 +92,10 @@ func getWindowIDs() ([]string, error) {
return nil, err
}
if i := bytes.IndexByte(out, '\t'); i < 0 {
return nil, fmt.Errorf("xprop: unexpected output: %q", out)
} else {
out = out[i+1:]
if _, out, cut := bytes.Cut(out, []byte("\t")); cut {
return strings.Split(string(out), ", "), nil
}
return strings.Split(string(out), ", "), nil
return nil, fmt.Errorf("xprop: unexpected output: %q", out)
}
func getWindowPid(id string) (int, error) {
@ -106,10 +104,8 @@ func getWindowPid(id string) (int, error) {
return 0, err
}
if i := bytes.IndexByte(out, '\t'); i < 0 {
return 0, fmt.Errorf("xprop: unexpected output: %q", out)
} else {
out = out[i+1:]
if _, out, cut := bytes.Cut(out, []byte("\t")); cut {
return strconv.Atoi(string(out))
}
return strconv.Atoi(string(out))
return 0, fmt.Errorf("xprop: unexpected output: %q", out)
}

View file

@ -33,7 +33,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
if bytes.ContainsAny(data, "`") {
if bytes.ContainsRune(data, '`') {
log.Fatalf("illegal character: %s: `", name)
}
_, err = template.New(file.Name()).Funcs(funcs).Parse(string(data))

View file

@ -8,13 +8,14 @@ import (
func notify(text string, opts options) error {
var data zenutil.Notify
data.Text = text
data.Options.Title = opts.title
if i := strings.IndexByte(text, '\n'); i >= 0 && i < len(text) {
data.Options.Subtitle = text[:i]
data.Text = text[i+1:]
if sub, body, cut := strings.Cut(text, "\n"); cut {
data.Options.Subtitle = sub
data.Text = body
} else {
data.Text = text
}
_, err := zenutil.Run(opts.ctx, "notify", data)
if err != nil {
return err