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 { func addFileFilter(s string) error {
var filter zenity.FileFilter var filter zenity.FileFilter
if head, tail, cut := strings.Cut(s, "|"); cut { if name, rest, cut := strings.Cut(s, "|"); cut {
filter.Name = head filter.Name = name
s = tail s = rest
} }
filter.Patterns = strings.Split(strings.Trim(s, " "), " ") filter.Patterns = strings.Fields(s)
fileFilters = append(fileFilters, filter) fileFilters = append(fileFilters, filter)
return nil return nil
} }

View File

@ -25,14 +25,13 @@ func notify(opts ...zenity.Option) error {
ico := zenity.NoIcon ico := zenity.NoIcon
for scanner := bufio.NewScanner(os.Stdin); scanner.Scan(); { for scanner := bufio.NewScanner(os.Stdin); scanner.Scan(); {
line := scanner.Text() line := scanner.Text()
var cmd, msg string cmd, msg, cut := strings.Cut(line, ":")
if n := strings.IndexByte(line, ':'); n >= 0 { if !cut {
cmd = strings.TrimSpace(line[:n])
msg = strings.TrimSpace(zencmd.Unescape(line[n+1:]))
} else {
os.Stderr.WriteString("Could not parse command from stdin\n") os.Stderr.WriteString("Could not parse command from stdin\n")
continue continue
} }
cmd = strings.TrimSpace(cmd)
msg = strings.TrimSpace(zencmd.Unescape(msg))
switch cmd { switch cmd {
case "icon": case "icon":
switch msg { switch msg {
@ -51,9 +50,9 @@ func notify(opts ...zenity.Option) error {
} }
case "message", "tooltip": case "message", "tooltip":
opts := []zenity.Option{ico} opts := []zenity.Option{ico}
if n := strings.IndexByte(msg, '\n'); n >= 0 { if title, rest, cut := strings.Cut(msg, "\n"); cut {
opts = append(opts, zenity.Title(msg[:n])) opts = append(opts, zenity.Title(title))
msg = msg[n+1:] msg = rest
} }
if err := zenity.Notify(msg, opts...); err != nil { if err := zenity.Notify(msg, opts...); err != nil {
return err 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 //sys CoCreateInstance(clsid *GUID, unkOuter *IUnknown, clsContext int32, iid *GUID, address unsafe.Pointer) (res error) = ole32.CoCreateInstance
func guid(s string) *GUID { func guid(s string) *GUID {
// TODO: use unsafe.StringData after 1.20
return (*GUID)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)) return (*GUID)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data))
} }

View File

@ -92,12 +92,10 @@ func getWindowIDs() ([]string, error) {
return nil, err return nil, err
} }
if i := bytes.IndexByte(out, '\t'); i < 0 { if _, out, cut := bytes.Cut(out, []byte("\t")); cut {
return nil, fmt.Errorf("xprop: unexpected output: %q", out) return strings.Split(string(out), ", "), nil
} else {
out = out[i+1:]
} }
return strings.Split(string(out), ", "), nil return nil, fmt.Errorf("xprop: unexpected output: %q", out)
} }
func getWindowPid(id string) (int, error) { func getWindowPid(id string) (int, error) {
@ -106,10 +104,8 @@ func getWindowPid(id string) (int, error) {
return 0, err return 0, err
} }
if i := bytes.IndexByte(out, '\t'); i < 0 { if _, out, cut := bytes.Cut(out, []byte("\t")); cut {
return 0, fmt.Errorf("xprop: unexpected output: %q", out) return strconv.Atoi(string(out))
} else {
out = out[i+1:]
} }
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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if bytes.ContainsAny(data, "`") { if bytes.ContainsRune(data, '`') {
log.Fatalf("illegal character: %s: `", name) log.Fatalf("illegal character: %s: `", name)
} }
_, err = template.New(file.Name()).Funcs(funcs).Parse(string(data)) _, err = template.New(file.Name()).Funcs(funcs).Parse(string(data))

View File

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