ui/termstatus: extract background handling code

This commit is contained in:
Michael Eischer
2025-09-07 12:15:27 +02:00
parent 6ff7cd9050
commit 48cbbf9651
11 changed files with 18 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
//go:build unix //go:build unix
package termstatus package terminal
import "github.com/restic/restic/internal/debug" import "github.com/restic/restic/internal/debug"

View File

@@ -1,6 +1,6 @@
//go:build unix //go:build unix
package termstatus package terminal
import ( import (
"os" "os"

View File

@@ -1,4 +1,4 @@
package termstatus package terminal
// IsProcessBackground reports whether the current process is running in the // IsProcessBackground reports whether the current process is running in the
// background. Not implemented for this platform. // background. Not implemented for this platform.

View File

@@ -9,7 +9,6 @@ import (
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/ui/termstatus"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@@ -29,13 +28,13 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
} }
// only move child process to foreground if restic is in the foreground // only move child process to foreground if restic is in the foreground
prev, err := termstatus.Tcgetpgrp(int(tty.Fd())) prev, err := Tcgetpgrp(int(tty.Fd()))
if err != nil { if err != nil {
_ = tty.Close() _ = tty.Close()
return nil, err return nil, err
} }
self := termstatus.Getpgrp() self := Getpgrp()
if prev != self { if prev != self {
debug.Log("restic is not controlling the tty; err = %v", err) debug.Log("restic is not controlling the tty; err = %v", err)
if err := tty.Close(); err != nil { if err := tty.Close(); err != nil {
@@ -56,7 +55,7 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
} }
// move the command's process group into the foreground // move the command's process group into the foreground
err = termstatus.Tcsetpgrp(int(tty.Fd()), cmd.Process.Pid) err = Tcsetpgrp(int(tty.Fd()), cmd.Process.Pid)
if err != nil { if err != nil {
_ = tty.Close() _ = tty.Close()
return nil, err return nil, err
@@ -67,7 +66,7 @@ func startForeground(cmd *exec.Cmd) (bg func() error, err error) {
signal.Reset(unix.SIGTTOU) signal.Reset(unix.SIGTTOU)
// reset the foreground process group // reset the foreground process group
err = termstatus.Tcsetpgrp(int(tty.Fd()), prev) err = Tcsetpgrp(int(tty.Fd()), prev)
if err != nil { if err != nil {
_ = tty.Close() _ = tty.Close()
return err return err

View File

@@ -1,4 +1,4 @@
package termstatus package terminal
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@@ -1,6 +1,6 @@
//go:build unix && !solaris //go:build unix && !solaris
package termstatus package terminal
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@@ -1,4 +1,4 @@
package termstatus package terminal
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@@ -1,6 +1,6 @@
//go:build unix && !linux //go:build unix && !linux
package termstatus package terminal
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@@ -1,4 +1,4 @@
package termstatus package terminal
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@@ -1,6 +1,6 @@
//go:build unix && !aix //go:build unix && !aix
package termstatus package terminal
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@@ -12,6 +12,8 @@ import (
"golang.org/x/term" "golang.org/x/term"
"golang.org/x/text/width" "golang.org/x/text/width"
"github.com/restic/restic/internal/terminal"
) )
// Terminal is used to write messages and display status lines which can be // Terminal is used to write messages and display status lines which can be
@@ -101,14 +103,14 @@ func (t *Terminal) run(ctx context.Context) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
if !IsProcessBackground(t.fd) { if !terminal.IsProcessBackground(t.fd) {
t.writeStatus([]string{}) t.writeStatus([]string{})
} }
return return
case msg := <-t.msg: case msg := <-t.msg:
if IsProcessBackground(t.fd) { if terminal.IsProcessBackground(t.fd) {
// ignore all messages, do nothing, we are in the background process group // ignore all messages, do nothing, we are in the background process group
continue continue
} }
@@ -140,7 +142,7 @@ func (t *Terminal) run(ctx context.Context) {
} }
case stat := <-t.status: case stat := <-t.status:
if IsProcessBackground(t.fd) { if terminal.IsProcessBackground(t.fd) {
// ignore all messages, do nothing, we are in the background process group // ignore all messages, do nothing, we are in the background process group
continue continue
} }