zenity/internal/zenutil/run_unix.go

96 lines
1.8 KiB
Go
Raw Permalink Normal View History

2022-03-24 10:37:37 -04:00
//go:build !windows && !darwin
2020-01-09 06:17:39 -05:00
2020-01-19 06:57:05 -05:00
package zenutil
2020-01-09 06:17:39 -05:00
import (
2021-04-30 14:19:14 -04:00
"bytes"
2020-01-28 07:46:43 -05:00
"context"
2020-01-09 06:17:39 -05:00
"os"
"os/exec"
2020-01-28 07:46:43 -05:00
"strconv"
2022-12-14 18:53:30 -05:00
"sync"
2020-01-09 06:17:39 -05:00
"syscall"
)
2022-12-14 18:53:30 -05:00
var (
tool, path string
pathOnce sync.Once
)
2020-01-09 06:17:39 -05:00
2022-12-14 18:53:30 -05:00
func initPath() {
2020-01-09 21:14:50 -05:00
for _, tool = range [3]string{"qarma", "zenity", "matedialog"} {
2020-01-09 06:17:39 -05:00
path, _ = exec.LookPath(tool)
if path != "" {
return
}
}
tool = "zenity"
}
2022-12-14 18:53:30 -05:00
// IsAvailable is internal.
func IsAvailable() bool {
pathOnce.Do(initPath)
return path != ""
}
2020-01-29 11:45:40 -05:00
// Run is internal.
2020-01-28 07:46:43 -05:00
func Run(ctx context.Context, args []string) ([]byte, error) {
2022-12-14 18:53:30 -05:00
pathOnce.Do(initPath)
2020-01-19 06:57:05 -05:00
if Command && path != "" {
2020-01-28 07:46:43 -05:00
if Timeout > 0 {
args = append(args, "--timeout", strconv.Itoa(Timeout))
}
2020-01-09 06:17:39 -05:00
syscall.Exec(path, append([]string{tool}, args...), os.Environ())
}
2020-01-28 07:46:43 -05:00
if ctx != nil {
2020-01-29 06:09:06 -05:00
out, err := exec.CommandContext(ctx, tool, args...).Output()
if ctx.Err() != nil {
err = ctx.Err()
}
return out, err
2020-01-28 07:46:43 -05:00
}
2020-01-09 06:17:39 -05:00
return exec.Command(tool, args...).Output()
}
2021-04-25 19:36:15 -04:00
// RunProgress is internal.
2023-08-03 07:55:59 -04:00
func RunProgress(ctx context.Context, max int, close bool, extra *string, args []string) (*progressDialog, error) {
2022-12-14 18:53:30 -05:00
pathOnce.Do(initPath)
2021-04-25 19:36:15 -04:00
if Command && path != "" {
if Timeout > 0 {
args = append(args, "--timeout", strconv.Itoa(Timeout))
}
syscall.Exec(path, append([]string{tool}, args...), os.Environ())
}
2021-04-30 14:19:14 -04:00
if ctx == nil {
ctx = context.Background()
}
2021-04-25 19:36:15 -04:00
2021-04-30 14:19:14 -04:00
cmd := exec.CommandContext(ctx, tool, args...)
2021-04-25 19:36:15 -04:00
pipe, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
2021-04-30 14:19:14 -04:00
var out *bytes.Buffer
if extra != nil {
out = &bytes.Buffer{}
cmd.Stdout = out
}
2021-04-29 11:05:28 -04:00
if err := cmd.Start(); err != nil {
2021-04-25 19:36:15 -04:00
return nil, err
}
2021-04-29 11:05:28 -04:00
dlg := &progressDialog{
2021-04-30 14:05:49 -04:00
ctx: ctx,
cmd: cmd,
2021-04-25 19:36:15 -04:00
max: max,
2021-04-30 14:05:49 -04:00
percent: true,
2023-08-03 07:55:59 -04:00
close: close,
2021-04-30 14:05:49 -04:00
lines: make(chan string),
done: make(chan struct{}),
2021-04-25 19:36:15 -04:00
}
2021-04-30 14:05:49 -04:00
go dlg.pipe(pipe)
2021-04-30 14:19:14 -04:00
go dlg.wait(extra, out)
2021-04-29 11:05:28 -04:00
return dlg, nil
2021-04-25 19:36:15 -04:00
}