mirror of
https://github.com/wader/fq.git
synced 2024-11-23 09:56:07 +03:00
Merge pull request #260 from wader/mono-unicode-fix
interp: dump: Fix column truncate issue with unicode bars
This commit is contained in:
commit
469116f3b3
@ -3,6 +3,7 @@ package columnwriter
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Column struct {
|
||||
@ -62,9 +63,9 @@ func (c *Column) Flush() {
|
||||
|
||||
// Writer maintins multiple column io.Writer:s. On Flush() row align them.
|
||||
type Writer struct {
|
||||
Columns []*Column
|
||||
LenFn func(s string) int
|
||||
TruncateFn func(s string, l int) string
|
||||
Columns []*Column
|
||||
DisplayLenFn func(s string) int
|
||||
DisplayTruncateFn func(s string, l int) string
|
||||
|
||||
w io.Writer
|
||||
}
|
||||
@ -97,13 +98,16 @@ func New(w io.Writer, widths []int) *Writer {
|
||||
func (w *Writer) Flush() error {
|
||||
const whitespace = " "
|
||||
|
||||
lenFn := w.LenFn
|
||||
// TODO: both fn assume fixed width runes
|
||||
lenFn := w.DisplayLenFn
|
||||
if lenFn == nil {
|
||||
lenFn = func(s string) int { return len(s) }
|
||||
lenFn = func(s string) int { return utf8.RuneCountInString(s) }
|
||||
}
|
||||
truncateFn := w.TruncateFn
|
||||
truncateFn := w.DisplayTruncateFn
|
||||
if truncateFn == nil {
|
||||
truncateFn = func(s string, l int) string { return s[0:l] }
|
||||
truncateFn = func(s string, l int) string {
|
||||
return string(([]rune(s))[0:l])
|
||||
}
|
||||
}
|
||||
|
||||
for _, c := range w.Columns {
|
||||
|
@ -377,8 +377,8 @@ func dump(v *decode.Value, w io.Writer, opts Options) error {
|
||||
buf := make([]byte, 32*1024)
|
||||
|
||||
if opts.Color {
|
||||
cw.LenFn = ansi.Len
|
||||
cw.TruncateFn = ansi.Truncate
|
||||
cw.DisplayLenFn = ansi.Len
|
||||
cw.DisplayTruncateFn = ansi.Truncate
|
||||
}
|
||||
|
||||
return v.WalkPreOrder(makeWalkFn(func(v *decode.Value, rootV *decode.Value, depth int, rootDepth int) error {
|
||||
|
9
pkg/interp/testdata/display.fqtest
vendored
9
pkg/interp/testdata/display.fqtest
vendored
@ -709,3 +709,12 @@ $ fq -n '"broken" | mp3 | d'
|
||||
| | | frames[0:0]:
|
||||
0x0|62 72 6f 6b 65 6e| |broken| | unknown0: raw bits
|
||||
# TODO: add root depth test
|
||||
# test unicode "│" and column truncate
|
||||
$ fq -o unicode=true . test.mp3
|
||||
│00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f│0123456789abcdef│.{}: test.mp3 (mp3)
|
||||
0x000│49 44 33 04 00 00 00 00 00 23 54 53 53 45 00 00│ID3......#TSSE..│ headers[0:1]:
|
||||
* │until 0x2c.7 (45) │ │
|
||||
0x020│ ff fb 40│ ..@│ frames[0:3]:
|
||||
0x030│c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00│................│
|
||||
* │until 0x283.7 (end) (599) │ │
|
||||
│ │ │ footers[0:0]:
|
||||
|
Loading…
Reference in New Issue
Block a user