From 16c314ab7f7806f387af648e3099d18711fb1713 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 28 Apr 2018 15:24:36 +0200 Subject: [PATCH] termstatus: Don't print status if in background --- internal/ui/termstatus/background.go | 9 +++++++++ internal/ui/termstatus/background_linux.go | 21 +++++++++++++++++++++ internal/ui/termstatus/status.go | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 internal/ui/termstatus/background.go create mode 100644 internal/ui/termstatus/background_linux.go diff --git a/internal/ui/termstatus/background.go b/internal/ui/termstatus/background.go new file mode 100644 index 000000000..e371c18df --- /dev/null +++ b/internal/ui/termstatus/background.go @@ -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 +} diff --git a/internal/ui/termstatus/background_linux.go b/internal/ui/termstatus/background_linux.go new file mode 100644 index 000000000..f99091128 --- /dev/null +++ b/internal/ui/termstatus/background_linux.go @@ -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() +} diff --git a/internal/ui/termstatus/status.go b/internal/ui/termstatus/status.go index 25fdcc341..6682430e8 100644 --- a/internal/ui/termstatus/status.go +++ b/internal/ui/termstatus/status.go @@ -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()