Skip to content

Commit

Permalink
allow output hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
liamg committed Jul 27, 2020
1 parent 8363482 commit 436519a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
17 changes: 14 additions & 3 deletions pkg/terminal/lazycopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ const BackOffReadInitialSleepDuration = time.Millisecond
const BackOffReadMaxSleepDuration = time.Millisecond * 16

// similar to io.Copy with sleep when no data is received
func lazyCopy(dst io.Writer, src io.Reader) error {
func lazyCopy(dst io.Writer, src io.Reader, hideFuncs ...func() bool) error {

buffer := make([]byte, 4096)

backOffDelay := BackOffReadInitialSleepDuration

var hide bool

for {
size, err := src.Read(buffer)
if size > 0 {
if _, err := dst.Write(buffer[:size]); err != nil {
return err
hide = false
for _, f := range hideFuncs {
if f() {
hide = true
break
}
}
if !hide {
if _, err := dst.Write(buffer[:size]); err != nil {
return err
}
}
backOffDelay = BackOffReadInitialSleepDuration
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"os/signal"
"sync"
"syscall"

"github.com/liamg/shox/pkg/decorators"
Expand All @@ -22,6 +23,8 @@ type Terminal struct {
proxy *proxy.Proxy
pty *os.File
enableNesting bool
hideOutput bool
outputMutex sync.RWMutex
}

// NewTerminal creates a new terminal instance
Expand Down Expand Up @@ -130,8 +133,26 @@ func (t *Terminal) Run() error {

// Copy stdin to the pty and the pty to stdout.
go func() { _ = lazyCopy(t.pty, os.Stdin) }()
go func() { _ = lazyCopy(os.Stdout, t.proxy) }()
go func() { _ = lazyCopy(os.Stdout, t.proxy, t.canOutput) }()
_ = lazyCopy(t.proxy, t.pty)
fmt.Printf("\r\n")
return nil
}

func (t *Terminal) HideOutput() {
t.outputMutex.Lock()
defer t.outputMutex.Unlock()
t.hideOutput = true
}

func (t *Terminal) ShowOutput() {
t.outputMutex.Lock()
defer t.outputMutex.Unlock()
t.hideOutput = false
}

func (t *Terminal) canOutput() bool {
t.outputMutex.RLock()
defer t.outputMutex.RUnlock()
return !t.hideOutput
}

0 comments on commit 436519a

Please sign in to comment.