From e3dc923165ad391dea8100a3a75bada34cbabafb Mon Sep 17 00:00:00 2001 From: kinggo Date: Mon, 10 Oct 2022 17:59:43 +0800 Subject: [PATCH] fix: solve wrong print width --- go.mod | 2 ++ go.sum | 4 ++++ listen.go | 5 +++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index d6d6fffb087..cf287217d1c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/gofiber/fiber/v2 go 1.19 require ( + github.com/mattn/go-runewidth v0.0.14 github.com/valyala/fasthttp v1.40.0 golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 ) @@ -10,6 +11,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.4 // indirect github.com/klauspost/compress v1.15.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index b96abe70e21..468e23dac2e 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,10 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U= github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.40.0 h1:CRq/00MfruPGFLTQKY8b+8SfdK60TxNztjRMnH0t1Yc= diff --git a/listen.go b/listen.go index c82afc3a201..50b3f68656d 100644 --- a/listen.go +++ b/listen.go @@ -22,6 +22,7 @@ import ( "github.com/gofiber/fiber/v2/internal/colorable" "github.com/gofiber/fiber/v2/internal/isatty" + "github.com/mattn/go-runewidth" ) // Listener can be used to pass a custom listener. @@ -238,11 +239,11 @@ func (app *App) startupMessage(addr string, tls bool, pids string) { } centerValue := func(s string, width int) string { - pad := strconv.Itoa((width - len([]rune(s))) / 2) + pad := strconv.Itoa((width - runewidth.StringWidth(s)) / 2) str := fmt.Sprintf("%"+pad+"s", " ") str += fmt.Sprintf("%s%s%s", colors.Cyan, s, colors.Black) str += fmt.Sprintf("%"+pad+"s", " ") - if len([]rune(s))-10 < width && len([]rune(s))%2 == 0 { + if runewidth.StringWidth(s)-10 < width && runewidth.StringWidth(s)%2 == 0 { // add an ending space if the length of str is even and str is not too long str += " " }