Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/tabwriter: Misalignment with non-printable characters #66661

Closed
adamroyjones opened this issue Apr 3, 2024 · 2 comments
Closed

text/tabwriter: Misalignment with non-printable characters #66661

adamroyjones opened this issue Apr 3, 2024 · 2 comments

Comments

@adamroyjones
Copy link

adamroyjones commented Apr 3, 2024

Go version

go version go1.22.1 linux/amd64

Output of go env in your module/workspace:

This shouldn't be relevant, but I've included it below.

go env
GO111MODULE=''
GOARCH='amd64'
GOBIN='/home/adamjones/.local/share/go/bin'
GOCACHE='/home/adamjones/.cache/go-build'
GOENV='/home/adamjones/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/adamjones/.local/share/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/adamjones/.local/share/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/adamjones/.local/share/mise/installs/go/1.22.1'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/adamjones/.local/share/mise/installs/go/1.22.1/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/tmp/tabwriter-reprex/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3233429893=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I noticed that text/tabwriter, which is used for visually aligning text, doesn't seem to account for things being invisible.

There was an abandoned attempt to fix part of this (the ANSI escape codes) here, but I can't see an attempt to revisit it.

Here's a small program that illustrates the problem. This seems insensitive to the flags passed to NewWriter. Note that both the line with NUL bytes and the line with ANSI escape codes are misaligned.

package main

import (
	"fmt"
	"os"
	"strings"
	"text/tabwriter"
)

func main() {
	red := "\033[1m\033[91m"
	reset := "\033[0m"
	nuls := strings.Repeat("\x00", 20)

	w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', tabwriter.StripEscape)
	fmt.Fprintf(w, "KEY\tVALUE\n")
	fmt.Fprintf(w, "%sfoo\t1%s\n", red, reset)
	fmt.Fprintf(w, "bar\t2\n")
	fmt.Fprintf(w, "baz"+nuls+"\t3\n")
	w.Flush()
}

What did you see happen?

A misaligned table was printed. A screenshot follows.

maim-2024-04-03T12-07-50

What did you expect to see?

I expected an aligned table to be printed.

@randall77
Copy link
Contributor

StripEscape only strips the tabwriter-defined escape character, 0xff. It does not strip general unprintable characters.
In any case, text/tabwriter is effectively frozen. I'm going to close this, as I don't think we're going to accept improvements at the moment.

@adamroyjones
Copy link
Author

Right—I now see that the documentation says

The text/tabwriter package is frozen and is not accepting new features.

I'm sorry for wasting your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants