Skip to content

Commit 3c00f87

Browse files
committed
fix: solve wrong print width
1 parent 925d5d0 commit 3c00f87

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

example/main.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "github.com/gofiber/fiber/v2"
4+
import "log"
5+
6+
func main() {
7+
// Steps to reproduce
8+
app := fiber.New(fiber.Config{
9+
CaseSensitive: true,
10+
StrictRouting: true,
11+
GETOnly: false,
12+
ServerHeader: "01-Matrix",
13+
AppName: "零壹矩阵管理平台 V0.3",
14+
})
15+
16+
log.Fatal(app.Listen(":3000"))
17+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ module github.com/gofiber/fiber/v2
33
go 1.19
44

55
require (
6+
github.com/mattn/go-runewidth v0.0.14
67
github.com/valyala/fasthttp v1.40.0
78
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9
89
)
910

1011
require (
1112
github.com/andybalholm/brotli v1.0.4 // indirect
1213
github.com/klauspost/compress v1.15.0 // indirect
14+
github.com/rivo/uniseg v0.2.0 // indirect
1315
github.com/valyala/bytebufferpool v1.0.0 // indirect
1416
github.com/valyala/tcplisten v1.0.0 // indirect
1517
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY
22
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
33
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
44
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
5+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
6+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
7+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
8+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
59
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
610
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
711
github.com/valyala/fasthttp v1.40.0 h1:CRq/00MfruPGFLTQKY8b+8SfdK60TxNztjRMnH0t1Yc=

listen.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/gofiber/fiber/v2/internal/colorable"
2424
"github.com/gofiber/fiber/v2/internal/isatty"
25+
"github.com/mattn/go-runewidth"
2526
)
2627

2728
// Listener can be used to pass a custom listener.
@@ -238,11 +239,11 @@ func (app *App) startupMessage(addr string, tls bool, pids string) {
238239
}
239240

240241
centerValue := func(s string, width int) string {
241-
pad := strconv.Itoa((width - len([]rune(s))) / 2)
242+
pad := strconv.Itoa((width - runewidth.StringWidth(s)) / 2)
242243
str := fmt.Sprintf("%"+pad+"s", " ")
243244
str += fmt.Sprintf("%s%s%s", colors.Cyan, s, colors.Black)
244245
str += fmt.Sprintf("%"+pad+"s", " ")
245-
if len([]rune(s))-10 < width && len([]rune(s))%2 == 0 {
246+
if runewidth.StringWidth(s)-10 < width && runewidth.StringWidth(s)%2 == 0 {
246247
// add an ending space if the length of str is even and str is not too long
247248
str += " "
248249
}

0 commit comments

Comments
 (0)