Skip to content

Commit

Permalink
use string length
Browse files Browse the repository at this point in the history
  • Loading branch information
raspi committed Dec 31, 2019
1 parent 4610591 commit 804dc30
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/display/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
clr "github.com/logrusorgru/aurora"
"github.com/raspi/heksa/pkg/iface"
"io"
"math/bits"
"strings"
)

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 804dc30

Please sign in to comment.