Skip to content

Commit

Permalink
fix: solve wrong print width
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Oct 10, 2022
1 parent 925d5d0 commit 8709f6e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -3,13 +3,15 @@ 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
)

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
)
4 changes: 4 additions & 0 deletions go.sum
Expand Up @@ -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=
Expand Down
10 changes: 5 additions & 5 deletions listen.go
Expand Up @@ -9,6 +9,9 @@ import (
"crypto/x509"
"errors"
"fmt"
"github.com/gofiber/fiber/v2/internal/colorable"
"github.com/gofiber/fiber/v2/internal/isatty"
"github.com/mattn/go-runewidth"
"io/ioutil"
"net"
"os"
Expand All @@ -19,9 +22,6 @@ import (
"strconv"
"strings"
"text/tabwriter"

"github.com/gofiber/fiber/v2/internal/colorable"
"github.com/gofiber/fiber/v2/internal/isatty"
)

// Listener can be used to pass a custom listener.
Expand Down Expand Up @@ -238,11 +238,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 += " "
}
Expand Down

0 comments on commit 8709f6e

Please sign in to comment.