mirror of
https://github.com/wader/fq.git
synced 2024-12-26 23:15:04 +03:00
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
|
package interp
|
||
|
|
||
|
import (
|
||
|
"fq/internal/asciiwriter"
|
||
|
"fq/internal/hexdump"
|
||
|
"fq/internal/hexpairwriter"
|
||
|
"fq/internal/num"
|
||
|
"fq/pkg/bitio"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
func hexdumpRange(bbr bufferRange, w io.Writer, opts Options) error {
|
||
|
bitsByteAlign := bbr.r.Start % 8
|
||
|
bb, err := bbr.bb.BitBufRange(bbr.r.Start-bitsByteAlign, bbr.r.Len+bitsByteAlign)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
br := bitio.NewSectionBitReader(bb, 0, bb.Len())
|
||
|
|
||
|
d := opts.Decorator
|
||
|
hw := hexdump.New(
|
||
|
w,
|
||
|
(bbr.r.Start-bitsByteAlign)/8,
|
||
|
num.DigitsInBase(bitio.BitsByteCount(bbr.r.Stop()+bitsByteAlign), true, opts.AddrBase),
|
||
|
opts.AddrBase,
|
||
|
opts.LineBytes,
|
||
|
func(b byte) string { return d.ByteColor(b).Wrap(hexpairwriter.Pair(b)) },
|
||
|
func(b byte) string { return d.ByteColor(b).Wrap(asciiwriter.SafeASCII(b)) },
|
||
|
func(s string) string { return d.DumpHeader.Wrap(s) },
|
||
|
func(s string) string { return d.DumpAddr.Wrap(s) },
|
||
|
d.Column,
|
||
|
)
|
||
|
aw := &bitio.AlignBitWriter{W: hw, N: 8}
|
||
|
// TODO: ugly, AlignBitWriter take Closer? some other way?
|
||
|
defer hw.Close()
|
||
|
defer aw.Close()
|
||
|
if _, err = bitio.Copy(aw, br); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|