From 804dc305de16778422c229a89b77b632f6e8d44e Mon Sep 17 00:00:00 2001 From: raspi Date: Tue, 31 Dec 2019 14:45:21 +0200 Subject: [PATCH] use string length --- pkg/display/hex.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/display/hex.go b/pkg/display/hex.go index 1482d30..806ac15 100644 --- a/pkg/display/hex.go +++ b/pkg/display/hex.go @@ -5,7 +5,6 @@ import ( clr "github.com/logrusorgru/aurora" "github.com/raspi/heksa/pkg/iface" "io" - "math/bits" "strings" ) @@ -14,9 +13,9 @@ Hex displays bytes in hexadecimal format 00-ff. */ type Hex struct { fs uint64 // File size - bw uint8 // Bit width calculated from file size offFormat string // Format for offset column sb strings.Builder + zeroes int } func NewHex() *Hex { @@ -28,8 +27,12 @@ func NewHex() *Hex { func (d *Hex) SetFileSize(s int64) { d.fs = uint64(s) - d.bw = nearest(uint8(bits.Len64(d.fs))) - d.offFormat = fmt.Sprintf(`%%0%vx`, d.bw/8) + d.zeroes = len(fmt.Sprintf(`%x`, d.fs)) + if d.zeroes&1 != 0 { + d.zeroes++ + } + + d.offFormat = fmt.Sprintf(`%%0%vx`, d.zeroes) } func (d *Hex) Format(b byte, color clr.Color) string { @@ -51,7 +54,7 @@ func (d *Hex) EofStr() string { } func (d *Hex) OffsetHeader() string { - return strings.Repeat(`_`, int(d.bw/8)) + return strings.Repeat(`_`, d.zeroes) } func (d *Hex) Header() string {