termstatus: Don't print status if in background

This commit is contained in:
Alexander Neumann 2018-04-28 15:24:36 +02:00
parent 1449d7dc29
commit 16c314ab7f
3 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,9 @@
// +build !linux
package termstatus
// IsProcessBackground reports whether the current process is running in the
// background. Not implemented for this platform.
func IsProcessBackground() bool {
return false
}

View File

@ -0,0 +1,21 @@
package termstatus
import (
"syscall"
"unsafe"
"github.com/restic/restic/internal/debug"
)
// IsProcessBackground reports whether the current process is running in the background.
func IsProcessBackground() bool {
var pid int
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), syscall.TIOCGPGRP, uintptr(unsafe.Pointer(&pid)))
if err != 0 {
debug.Log("Can't check if we are in the background. Using default behaviour. Error: %s\n", err.Error())
return false
}
return pid != syscall.Getpgrp()
}

View File

@ -95,6 +95,10 @@ func (t *Terminal) run(ctx context.Context) {
for {
select {
case <-ctx.Done():
if IsProcessBackground() {
// ignore all messages, do nothing, we are in the background process group
continue
}
t.undoStatus(statusLines)
err := t.wr.Flush()
@ -105,6 +109,10 @@ func (t *Terminal) run(ctx context.Context) {
return
case msg := <-t.msg:
if IsProcessBackground() {
// ignore all messages, do nothing, we are in the background process group
continue
}
t.undoStatus(statusLines)
var dst io.Writer
@ -144,6 +152,10 @@ func (t *Terminal) run(ctx context.Context) {
}
case stat := <-t.status:
if IsProcessBackground() {
// ignore all messages, do nothing, we are in the background process group
continue
}
t.undoStatus(statusLines)
statusBuf.Reset()