Skip to content

Commit

Permalink
Added --print-relative-offset which can be used when seeking a file t…
Browse files Browse the repository at this point in the history
…o print additional relative offset position
  • Loading branch information
raspi committed May 18, 2021
1 parent 3be88ef commit e4d72b1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var requiredColorGroupNames = []string{
}

// Parse command line arguments
func getParams() (source iface.ReadSeekerCloser, offsetViewer []reader.OffsetFormatter, colorGroupings map[string]string, limit uint64, filesize int64, fg base.FormatterGroup) {
func getParams() (source iface.ReadSeekerCloser, offsetViewer []reader.OffsetFormatter, colorGroupings map[string]string, limit uint64, filesize int64, fg base.FormatterGroup, printRelative bool) {
opt := getoptions.New()

opt.HelpSynopsisArgs(`<filename> or STDIN`)
Expand All @@ -59,6 +59,11 @@ func getParams() (source iface.ReadSeekerCloser, offsetViewer []reader.OffsetFor
),
)

argPrintRelativeOffset := opt.Bool(`print-relative-offset`, false,
opt.Alias(`r`),
opt.Description(`Print relative offset(s) starting from 0 (file only)`),
)

argFormat := opt.StringOptional(`format`, `hex,asc`,
opt.Alias(`f`),
opt.ArgName(`fmt1,fmt2,..`),
Expand Down Expand Up @@ -99,6 +104,7 @@ func getParams() (source iface.ReadSeekerCloser, offsetViewer []reader.OffsetFor
_, _ = fmt.Fprintln(os.Stdout, ` - You can use prefixes for seek, limit and width. 0x = hex, 0b = binary, 0o = octal`)
_, _ = fmt.Fprintln(os.Stdout, ` - Use '--seek \-1234' for seeking from end of file`)
_, _ = fmt.Fprintln(os.Stdout, ` - Limit and seek parameters supports units (KB, KiB, MB, MiB, GB, GiB, TB, TiB)`)
_, _ = fmt.Fprintln(os.Stdout, ` - --print-relative-offset can be used when seeking to certain offset to also print extra offset position starting from zero`)
_, _ = fmt.Fprintln(os.Stdout, ` - Offset formatters:`)
_, _ = fmt.Fprintln(os.Stdout, ` - Disable formatter output with 'no' or ''`)
_, _ = fmt.Fprintln(os.Stdout, ` - 'humiec' (IEC: 1024 B) and 'humsi' (SI: 1000 B) displays offset in human form (n KiB/KB)`)
Expand Down Expand Up @@ -245,11 +251,11 @@ func getParams() (source iface.ReadSeekerCloser, offsetViewer []reader.OffsetFor

fGroup := base.New(formatters, palette, colorGroupings[`Splitter`], colorGroupings[`Padding`], width, uint8(*argSplitter))

return source, offsetViewer, colorGroupings, limit, filesize, fGroup
return source, offsetViewer, colorGroupings, limit, filesize, fGroup, *argPrintRelativeOffset
}

func main() {
source, offViewer, colorGroupings, limit, filesize, fGroup := getParams()
source, offViewer, colorGroupings, limit, filesize, fGroup, printRelative := getParams()
usingLimit := limit > 0

binfo := offFormatters.BaseInfo{
Expand All @@ -272,7 +278,12 @@ func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)

r := reader.New(source, offormatters, colors, fGroup, filesize == -1)
isStdin := filesize == -1
if isStdin {
printRelative = false
}

r := reader.New(source, offormatters, colors, fGroup, isStdin, printRelative)

// Dump hex
for {
Expand Down
68 changes: 46 additions & 22 deletions pkg/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,40 @@ type ReaderColors struct {
}

type Reader struct {
r iface.ReadSeekerCloser
offsetFormatters []offFormatters.OffsetFormatter // offset formatters (max 2) first one is displayed on the left side and second one on the right side
offsetFormatterCount int // shorthand for len(offsetFormatters), for speeding up
isStdin bool // Are we reading from STDIN? if so, we can't ask for offset position from file
readTotalBytes uint64 // How many bytes Reader has been reading so far (for limit)
sb strings.Builder // Faster than concatenating strings
Splitter string // Splitter character for columns
growHint int // Grow hint for sb strings.Builder variable for speed
formatterGroup base.FormatterGroup
colors ReaderColors
isEven bool // change background color for printed line
r iface.ReadSeekerCloser
offsetFormatters []offFormatters.OffsetFormatter // offset formatters (max 2) first one is displayed on the left side and second one on the right side
offsetFormatterCount int // shorthand for len(offsetFormatters), for speeding up
isStdin bool // Are we reading from STDIN? if so, we can't ask for offset position from file
readTotalBytes uint64 // How many bytes Reader has been reading so far (for limit)
readRelativeTotalBytes uint64 // How many bytes Reader has been reading relatively
sb strings.Builder // Faster than concatenating strings
Splitter string // Splitter character for columns
growHint int // Grow hint for sb strings.Builder variable for speed
formatterGroup base.FormatterGroup
colors ReaderColors
isEven bool // change background color for printed line
printRelativeOffset bool // Print relative offset?
}

func New(r iface.ReadSeekerCloser, offsetFormatter []offFormatters.OffsetFormatter, colors ReaderColors, formatterGroup base.FormatterGroup, isStdin bool) *Reader {
func New(r iface.ReadSeekerCloser, offsetFormatter []offFormatters.OffsetFormatter, colors ReaderColors, formatterGroup base.FormatterGroup, isStdin bool, useRelativeOffset bool) *Reader {

if isStdin {
useRelativeOffset = false
}

reader := &Reader{
r: r,
isStdin: isStdin,
offsetFormatters: offsetFormatter,
readTotalBytes: 0, // How many bytes we've read
sb: strings.Builder{},
Splitter: `┊`, // Splitter character between different columns
offsetFormatterCount: len(offsetFormatter),
formatterGroup: formatterGroup,
colors: colors,
isEven: false,
r: r,
isStdin: isStdin,
offsetFormatters: offsetFormatter,
readTotalBytes: 0, // How many bytes we've read
readRelativeTotalBytes: 0, // How many bytes we've read relatively (always starts from zero)
sb: strings.Builder{},
Splitter: `┊`, // Splitter character between different columns
offsetFormatterCount: len(offsetFormatter),
formatterGroup: formatterGroup,
colors: colors,
isEven: false,
printRelativeOffset: useRelativeOffset,
}

for _, f := range reader.offsetFormatters {
Expand Down Expand Up @@ -99,6 +108,10 @@ func (r *Reader) Read() (string, error) {

offsetLeft := r.getoffsetLeft(offset)
offsetRight := r.getoffsetRight(offset)

offsetLeftRelative := r.getoffsetLeft(r.readRelativeTotalBytes)
offsetRightRelative := r.getoffsetRight(r.readRelativeTotalBytes)

r.sb.Reset()
r.sb.Grow(r.growHint)

Expand All @@ -110,6 +123,7 @@ func (r *Reader) Read() (string, error) {
}

r.readTotalBytes += uint64(bytesReadCount)
r.readRelativeTotalBytes += uint64(bytesReadCount)

// Change between two background colors
if r.isEven {
Expand All @@ -122,9 +136,19 @@ func (r *Reader) Read() (string, error) {
// Offset on the left
r.sb.WriteString(offsetLeft)

if r.printRelativeOffset {
// Print relative offset
r.sb.WriteString(offsetLeftRelative)
}

// Print the formatted bytes
r.sb.WriteString(r.formatterGroup.Print(tmp[0:bytesReadCount]))

if r.printRelativeOffset {
// Print relative offset
r.sb.WriteString(offsetRightRelative)
}

// Offset on the right
r.sb.WriteString(offsetRight)

Expand Down

0 comments on commit e4d72b1

Please sign in to comment.