Skip to content

Commit

Permalink
added oct viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
raspi committed Dec 29, 2019
1 parent d97306f commit f1de567
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
54 changes: 54 additions & 0 deletions display/oct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package display

import (
"fmt"
clr "github.com/logrusorgru/aurora"
"io"
"math/bits"
"strings"
)

type Oct struct {
fs uint64 // File size
bw uint8 // Bit width calculated from file size
palette map[uint8]clr.Color
offFormat string
}

func (d *Oct) SetFileSize(s int64) {
d.fs = uint64(s)
d.bw = nearest(uint8(bits.Len64(d.fs)))
d.offFormat = fmt.Sprintf(`%%0%vo`, d.bw)
}

func NewOct() *Oct {
return &Oct{}
}

func (d Oct) Display(a []byte) string {
out := ``
for idx, b := range a {
if idx == 8 {
out += ` `
}

color, ok := d.palette[b]
if !ok {
color = clr.BrightFg
}

out += clr.Sprintf(`%03o `, clr.Colorize(b, color))
}

return strings.Trim(out, ` `)
}

// DisplayOffset displays offset as hexadecimal 0x00 - 0xFFFFFFFF....
func (d Oct) DisplayOffset(r io.ReadSeeker) string {
off, _ := r.Seek(0, io.SeekCurrent)
return fmt.Sprintf(d.offFormat, off)
}

func (d *Oct) SetPalette(p map[uint8]clr.Color) {
d.palette = p
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
offsetDisplayS := opt.StringOptional(`offset-display`, `hex`,
opt.Alias(`o`),
opt.ArgName(`offset format`),
opt.Description(`One of: hex, dec`),
opt.Description(`One of: hex, dec, oct, per`),
)

formatS := opt.StringOptional(`format`, `hex,asc`,
Expand Down
3 changes: 3 additions & 0 deletions offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ type offsetViewer uint8
const (
OffsetHex offsetViewer = iota
OffsetDec
OffsetOct
OffsetPercent
)

var offsetViewers = map[offsetViewer]ShowsOffset{
OffsetHex: display.NewHex(),
OffsetDec: display.NewDec(),
OffsetOct: display.NewOct(),
OffsetPercent: display.NewPercent(),
}

var offsetViewersStringToEnumMap = map[string]offsetViewer{
`hex`: OffsetHex,
`dec`: OffsetDec,
`oct`: OffsetOct,
`per`: OffsetPercent,
}

Expand Down
3 changes: 3 additions & 0 deletions viewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type dataViewer uint8
const (
ViewHex dataViewer = iota
ViewDec
ViewOct
ViewASCII
ViewBit
)
Expand All @@ -20,6 +21,7 @@ var viewerEnumMap = map[dataViewer]Views{
ViewASCII: display.NewAscii(),
ViewBit: display.NewBit(),
ViewDec: display.NewDec(),
ViewOct: display.NewOct(),
}

// Get enum from string
Expand All @@ -28,6 +30,7 @@ var viewersStringToEnumMap = map[string]dataViewer{
`asc`: ViewASCII,
`bit`: ViewBit,
`dec`: ViewDec,
`oct`: ViewOct,
}

type Views interface {
Expand Down

0 comments on commit f1de567

Please sign in to comment.