Skip to content

Commit

Permalink
fix progress bar on tmux control mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Dec 9, 2023
1 parent 4537ecb commit a64ab46
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/klauspost/compress v1.17.4
github.com/ncruces/zenity v0.10.10
github.com/stretchr/testify v1.8.4
github.com/trzsz/go-arg v1.5.2
github.com/trzsz/go-arg v1.5.3
github.com/trzsz/promptui v0.10.5
golang.org/x/sys v0.15.0
golang.org/x/term v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/trzsz/go-arg v1.5.2 h1:zGxCuTKvtC3jBf7HbvNk0HooUjv8uKAy2mY+bHVhRas=
github.com/trzsz/go-arg v1.5.2/go.mod h1:IC6Z/FiVH7uYvcbp1/gJhDYCFPS/GkL0APYakVvgY4I=
github.com/trzsz/go-arg v1.5.3 h1:eIDwDEmvSahtr5HpQOLrSa+YMqWQQ0H20xx60XgXQJw=
github.com/trzsz/go-arg v1.5.3/go.mod h1:IC6Z/FiVH7uYvcbp1/gJhDYCFPS/GkL0APYakVvgY4I=
github.com/trzsz/promptui v0.10.5 h1:tlzJkx+JOeE0sqKWmqgaoToZiYqj5G1Mz+QDV97VFu8=
github.com/trzsz/promptui v0.10.5/go.mod h1:GMZtu6ZTzU73CBFkzGtmB4wnTROIAbv4GFA74fV8V8g=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
Expand Down
33 changes: 20 additions & 13 deletions trzsz/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ func (filter *TrzszFilter) chooseUploadPaths(directory bool) ([]string, error) {
return files, nil
}

func (filter *TrzszFilter) createProgressBar(quiet bool, tmuxPaneColumns int32) {
if quiet {
filter.progress.Store(nil)
return
}
filter.progress.Store(newTextProgressBar(filter.clientOut, filter.options.TerminalColumns,
tmuxPaneColumns, filter.trigger.tmuxPrefix))
}

func (filter *TrzszFilter) resetProgressBar() {
if progress := filter.progress.Load(); progress != nil {
progress.showCursor()
}
filter.progress.Store(nil)
}

func (filter *TrzszFilter) downloadFiles(transfer *trzszTransfer) error {
path, err := filter.chooseDownloadPath()
if err == errUserCanceled {
Expand All @@ -332,12 +348,8 @@ func (filter *TrzszFilter) downloadFiles(transfer *trzszTransfer) error {
return err
}

filter.progress.Store(nil)
if !config.Quiet {
filter.progress.Store(newTextProgressBar(filter.clientOut, filter.options.TerminalColumns,
config.TmuxPaneColumns, filter.trigger.tmuxPrefix))
defer filter.progress.Store(nil)
}
filter.createProgressBar(config.Quiet, config.TmuxPaneColumns)
defer filter.resetProgressBar()

localNames, err := transfer.recvFiles(path, filter.progress.Load())
if err != nil {
Expand Down Expand Up @@ -378,12 +390,8 @@ func (filter *TrzszFilter) uploadFiles(transfer *trzszTransfer, directory bool)
}
}

filter.progress.Store(nil)
if !config.Quiet {
filter.progress.Store(newTextProgressBar(filter.clientOut, filter.options.TerminalColumns,
config.TmuxPaneColumns, filter.trigger.tmuxPrefix))
defer filter.progress.Store(nil)
}
filter.createProgressBar(config.Quiet, config.TmuxPaneColumns)
defer filter.resetProgressBar()

remoteNames, err := transfer.sendFiles(files, filter.progress.Load())
if err != nil {
Expand All @@ -401,7 +409,6 @@ func (filter *TrzszFilter) handleTrzsz() {

defer func() {
transfer.cleanup()
showCursor(filter.clientOut)
filter.transfer.CompareAndSwap(transfer, nil)
}()

Expand Down
15 changes: 10 additions & 5 deletions trzsz/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (p *textProgressBar) onNum(num int64) {
return
}
p.fileCount = int(num)
hideCursor(p.writer)
p.hideCursor()
}

func (p *textProgressBar) onName(name string) {
Expand Down Expand Up @@ -258,9 +258,6 @@ func (p *textProgressBar) onDone() {
if p == nil {
return
}
if p.fileIdx == p.fileCount {
showCursor(p.writer)
}
if p.fileSize == 0 {
return
}
Expand All @@ -281,11 +278,19 @@ func (p *textProgressBar) setPause(pausing bool) {
return
}
if !pausing {
hideCursor(p.writer)
p.hideCursor()
}
p.pausing.Store(pausing)
}

func (p *textProgressBar) hideCursor() {
p.writeProgress("\x1b[?25l")
}

func (p *textProgressBar) showCursor() {
p.writeProgress("\x1b[?25h")
}

func (p *textProgressBar) writeProgress(progress string) {
data := []byte(progress)
if p.tmuxPrefix != "" {
Expand Down
10 changes: 5 additions & 5 deletions trzsz/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ func TestProgressWithMultiFiles(t *testing.T) {
progress.onDone()

assert.Equal(6, *callTimeNowCount)
writer.assertBufferCount(6)
writer.assertBufferCount(5)
writer.assertProgressText(1, 100, []string{"(1/2) 中文😀test.txt [", "] 10% | 100 B | 100 B/s | 00:09 ETA"})
writer.assertProgressText(2, 100, []string{"(1/2) 中文😀test.txt [", "] 100% | 1000 B | 1000 B/s | 00:00 ETA"})
writer.assertProgressText(3, 80, []string{"(2/2) 英文😀test.txt [", "] 15% | 300 B | 150 B/s | 00:11 ETA"})
writer.assertProgressText(5, 80, []string{"(2/2) 英文😀test.txt [", "] 100% | 1000 B/s | 00:00 ETA"})
writer.assertProgressText(4, 80, []string{"(2/2) 英文😀test.txt [", "] 100% | 1000 B/s | 00:00 ETA"})
}

func TestProgressInTmuxPane(t *testing.T) {
Expand All @@ -372,7 +372,7 @@ func TestProgressInTmuxPane(t *testing.T) {
progress.onDone()

assert.Equal(7, *callTimeNowCount)
writer.assertBufferCount(7)
writer.assertBufferCount(6)
writer.assertProgressText(1, 79, []string{"(1/2) 中文😀test.txt [", "] 10% | 100 B | 100 B/s | 00:09 ETA"})
assert.NotContains(writer.buffer[1], "\r")
assert.NotContains(writer.buffer[1], "\x1b[79D")
Expand All @@ -385,8 +385,8 @@ func TestProgressInTmuxPane(t *testing.T) {

writer.assertProgressText(4, 120, []string{"(2/2) 中文😀test2.txt [", "] 30% | 300 B | 300 B/s | 00:02 ETA"})

writer.assertProgressText(6, 120, []string{"(2/2) 中文😀test2.txt [", "] 100% | 1000 B | 1000 B/s | 00:00 ETA"})
assert.Contains(writer.buffer[6], "\r")
writer.assertProgressText(5, 120, []string{"(2/2) 中文😀test2.txt [", "] 100% | 1000 B | 1000 B/s | 00:00 ETA"})
assert.Contains(writer.buffer[5], "\r")
}

func TestProgressEllipsisString(t *testing.T) {
Expand Down

0 comments on commit a64ab46

Please sign in to comment.