diff --git a/README.md b/README.md index a8efe4e..d77b81d 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,15 @@ -# trzsz-go - -[trzsz](https://trzsz.github.io/) ( trz / tsz ) is a simple file transfer tools, similar to lrzsz ( rz / sz ), and compatible with tmux. - -Website: [https://trzsz.github.io/go](https://trzsz.github.io/go)  中文文档:[https://trzsz.github.io/cn/go](https://trzsz.github.io/cn/go) +# trzsz-go ( trz / tsz ) - the go version of trzsz [![MIT License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://choosealicense.com/licenses/mit/) [![GitHub Release](https://img.shields.io/github/v/release/trzsz/trzsz-go)](https://github.com/trzsz/trzsz-go/releases) +[![WebSite](https://img.shields.io/badge/WebSite-https%3A%2F%2Ftrzsz.github.io%2Fgo-blue?style=flat)](https://trzsz.github.io/go) +[![中文文档](https://img.shields.io/badge/%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3-https%3A%2F%2Ftrzsz.github.io%2Fcn%2Fgo-blue?style=flat)](https://trzsz.github.io/cn/go) -**_Please check [https://trzsz.github.io](https://trzsz.github.io) for more information of `trzsz`._** +`trzsz-go` makes all terminals that support local shell to support [trzsz](https://trzsz.github.io/) ( trz / tsz ), which similar to ( rz / sz ), and compatible with tmux. -`trzsz-go` is the `go` version of `trzsz`, supports native terminals that support a local shell. +⭐ It's recommended to use the go version of `trzsz` on the server, use [trzsz-ssh ( tssh )](https://trzsz.github.io/ssh) on local. -⭐ It's recommended to use the `go` version of `trzsz` on the server. +_Please check [https://trzsz.github.io](https://trzsz.github.io) for more information about `trzsz ( trz / tsz )`._ ## Installation @@ -125,12 +123,12 @@ Website: [https://trzsz.github.io/go](https://trzsz.github.io/go)  中文文 -- Download from the [Releases](https://github.com/trzsz/trzsz-go/releases) +- Build from source ( Requires go 1.20 or later ) -
Or build and install from the source code ( Requires go 1.20 or later ) +
sudo make install ```sh - git clone https://github.com/trzsz/trzsz-go.git + git clone --depth 1 https://github.com/trzsz/trzsz-go.git cd trzsz-go make sudo make install @@ -138,6 +136,8 @@ Website: [https://trzsz.github.io/go](https://trzsz.github.io/go)  中文文
+- Download from the [Releases](https://github.com/trzsz/trzsz-go/releases) + ## Usage ### Use on the local computer diff --git a/go.mod b/go.mod index 041eeb7..acddec5 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/UserExistsError/conpty v0.1.2 github.com/creack/pty v1.1.21 github.com/klauspost/compress v1.17.4 + github.com/mattn/go-runewidth v0.0.15 github.com/ncruces/zenity v0.10.10 github.com/stretchr/testify v1.8.4 github.com/trzsz/go-arg v1.5.3 @@ -24,6 +25,7 @@ require ( github.com/josephspurrier/goversioninfo v1.4.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect + github.com/rivo/uniseg v0.4.4 // indirect golang.org/x/image v0.15.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index caf44e2..0ef561b 100644 --- a/go.sum +++ b/go.sum @@ -21,12 +21,17 @@ github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7 github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY= github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/ncruces/zenity v0.10.10 h1:V/rtAhr5QLdDThahOkm7EYlnw4RuEsf7oN+Xb6lz1j0= github.com/ncruces/zenity v0.10.10/go.mod h1:k3k4hJ4Wt1MUbeV48y+Gbl7Fp9skfGszN/xtKmuvhZk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 h1:GranzK4hv1/pqTIhMTXt2X8MmMOuH3hMeUR0o9SP5yc= github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844/go.mod h1:T1TLSfyWVBRXVGzWd0o9BI4kfoO9InEgfQe4NV3mLz8= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/trzsz/progress.go b/trzsz/progress.go index 0aec009..cbb0810 100644 --- a/trzsz/progress.go +++ b/trzsz/progress.go @@ -31,20 +31,9 @@ import ( "strings" "sync/atomic" "time" - "unicode/utf8" -) -func getDisplayLength(str string) int { - length := 0 - for _, r := range []rune(str) { // nolint:all - if utf8.RuneLen(r) == 1 { - length++ - } else { - length += 2 - } - } - return length -} + "github.com/mattn/go-runewidth" +) func getEllipsisString(str string, max int) (string, int) { var b strings.Builder @@ -52,19 +41,12 @@ func getEllipsisString(str string, max int) (string, int) { max -= 3 length := 0 for _, r := range []rune(str) { // nolint:all - if utf8.RuneLen(r) > 1 { - if length+2 > max { - b.WriteString("...") - return b.String(), length + 3 - } - length += 2 - } else { - if length+1 > max { - b.WriteString("...") - return b.String(), length + 3 - } - length++ + rlen := runewidth.RuneWidth(r) + if length+rlen > max { + b.WriteString("...") + return b.String(), length + 3 } + length += rlen b.WriteRune(r) } b.WriteString("...") @@ -340,7 +322,7 @@ func (p *textProgressBar) getProgressText(percentage, total, speed, eta string) if p.fileCount > 1 { left = fmt.Sprintf("(%d/%d) %s", p.fileIdx, p.fileCount, p.fileName) } - leftLength := getDisplayLength(left) + leftLength := runewidth.StringWidth(left) right := fmt.Sprintf(" %s | %s | %s | %s", percentage, total, speed, eta) for { diff --git a/trzsz/progress_test.go b/trzsz/progress_test.go index b83039a..485e8fc 100644 --- a/trzsz/progress_test.go +++ b/trzsz/progress_test.go @@ -30,6 +30,7 @@ import ( "regexp" "strings" "testing" + "unicode/utf8" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -37,6 +38,18 @@ import ( var colorRegexp = regexp.MustCompile(`\x1b\[\d+[mD]`) +func getDisplayLength(str string) int { + length := 0 + for _, r := range []rune(str) { // nolint:all + if utf8.RuneLen(r) == 1 { + length++ + } else { + length += 2 + } + } + return length +} + func getProgressLength(text string) int { text = strings.ReplaceAll(text, "\r", "") text = strings.ReplaceAll(text, "\u2588", "*")