Skip to content

Commit

Permalink
add --buf flag for specifying buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
akavel committed Oct 29, 2020
1 parent 3a63622 commit 8aa1f8f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
noColors = pflag.Bool("no-colors", false, "disable interface colors")
shellFlag = pflag.StringArrayP("exec", "e", nil, "`command` to pass $PIPELINE through; use multiple times to pass multi-word command; defaults to '-e=$SHELL -e=-c'")
initialCmd = pflag.StringP("pipeline", "c", "", "initial command to use as $PIPELINE - editable later")
bufsize = pflag.Int("buf", 40, "input buffer size & pipeline buffer sizes in `megabytes` (MiB)")
)

func main() {
Expand Down Expand Up @@ -138,7 +139,8 @@ func main() {
// We capture data piped to 'up' on standard input into an internal buffer
// When some new data shows up on stdin, we raise a custom signal,
// so that main loop will refresh the buffers and the output.
stdinCapture = NewBuf().StartCapturing(os.Stdin, func() { triggerRefresh(tui) })
stdinCapture = NewBuf(*bufsize*1024*1024).
StartCapturing(os.Stdin, func() { triggerRefresh(tui) })
// Then, we pass this data as input to a subprocess.
// Initially, no subprocess is running, as no command is entered yet
commandSubprocess *Subprocess = nil
Expand Down Expand Up @@ -543,9 +545,8 @@ func count(r io.Reader, b byte) (n int) {
}
}

func NewBuf() *Buf {
func NewBuf(bufsize int) *Buf {
// TODO: make buffer size dynamic (growable by pressing a key)
const bufsize = 40 * 1024 * 1024 // 40 MB
buf := &Buf{bytes: make([]byte, bufsize)}
buf.cond = sync.NewCond(&buf.mu)
return buf
Expand Down Expand Up @@ -676,7 +677,7 @@ func StartSubprocess(shell []string, command string, stdin *Buf, notify func())
ctx, cancel := context.WithCancel(context.TODO())
r, w := io.Pipe()
p := &Subprocess{
Buf: NewBuf().StartCapturing(r, notify),
Buf: NewBuf(len(stdin.bytes)).StartCapturing(r, notify),
cancel: cancel,
}

Expand Down

0 comments on commit 8aa1f8f

Please sign in to comment.