Skip to content

Commit

Permalink
use go-runewidth
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jan 20, 2024
1 parent ddf9b4b commit a734161
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -125,19 +123,21 @@ Website: [https://trzsz.github.io/go](https://trzsz.github.io/go)  中文文
</details>
- Download from the [Releases](https://github.com/trzsz/trzsz-go/releases)
- Build from source ( Requires go 1.20 or later )
<details><summary><code>Or build and install from the source code ( Requires go 1.20 or later )</code></summary>
<details><summary><code>sudo make install</code></summary>
```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
```
</details>
- Download from the [Releases](https://github.com/trzsz/trzsz-go/releases)
## Usage
### Use on the local computer
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
34 changes: 8 additions & 26 deletions trzsz/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,22 @@ 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
b.Grow(max)
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("...")
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions trzsz/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,26 @@ import (
"regexp"
"strings"
"testing"
"unicode/utf8"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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", "*")
Expand Down

0 comments on commit a734161

Please sign in to comment.