Skip to content

Commit

Permalink
initial work for issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
raspi committed Oct 9, 2021
1 parent 67f9cd8 commit d36023c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
34 changes: 31 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -285,6 +286,10 @@ func main() {

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

isFirst := true
lastData := make([]byte, fGroup.Width)
repeatedCount := 0

// Dump hex
for {
select {
Expand All @@ -296,26 +301,49 @@ func main() {
s, err := r.Read()
if err != nil {
if errors.Is(err, io.EOF) {
// End of File
break
}

_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf(`error while reading file: %v`, err))
os.Exit(1)
}

// Print formatted line
// <optional offset formatter #1><split><format 1><split><format N...><optional split><optional offset formatter #2>
_, _ = fmt.Println(s)
data := r.GetData()

if !isFirst && bytes.Equal(data, lastData) {
repeatedCount++
} else {
if repeatedCount > 0 {
_, _ = fmt.Println("\t" + fmt.Sprintf(`-- last line repeated %[1]d times (%[2]d bytes (0x%04[2]x))`, 1+repeatedCount, (1+repeatedCount)*fGroup.Width))
}

repeatedCount = 0

// Print formatted line
// <optional offset formatter #1><split><format 1><split><format N...><optional split><optional offset formatter #2>
_, _ = fmt.Println(s)
}

if usingLimit && r.GetReadBytes() >= limit {
// Limit is set and found
break
}

lastData = data
isFirst = false
}

err := source.Close()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `couldn't close file: %v`, err)
os.Exit(1)
}

if repeatedCount > 0 {
_, _ = fmt.Println("\t" + fmt.Sprintf(`-- last line repeated %[1]d times (%[2]d bytes (0x%04[2]x))`, repeatedCount, repeatedCount*fGroup.Width))
}

_, _ = fmt.Println()
_, _ = fmt.Println(fmt.Sprintf(`Read %d bytes total`, r.GetReadBytes()))
}
17 changes: 12 additions & 5 deletions pkg/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type Reader struct {
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?
isEven bool // change background color for printed line
printRelativeOffset bool // Print relative offset?
data []byte // Raw bytes data for accessing repeating data, etc
}

func New(r io.ReadSeekCloser, offsetFormatter []offFormatters.OffsetFormatter, colors ReaderColors, formatterGroup base.FormatterGroup, isStdin bool, useRelativeOffset bool) *Reader {
Expand All @@ -51,6 +52,7 @@ func New(r io.ReadSeekCloser, offsetFormatter []offFormatters.OffsetFormatter, c
colors: colors,
isEven: false,
printRelativeOffset: useRelativeOffset,
data: make([]byte, formatterGroup.Width),
}

for _, f := range reader.offsetFormatters {
Expand Down Expand Up @@ -115,8 +117,8 @@ func (r *Reader) Read() (string, error) {
r.sb.Grow(r.growHint)

// Fetch bytes with selected formatters
tmp := make([]byte, r.formatterGroup.Width)
bytesReadCount, err := r.r.Read(tmp)
r.data = make([]byte, r.formatterGroup.Width)
bytesReadCount, err := r.r.Read(r.data)
if err != nil {
return ``, err
}
Expand All @@ -141,7 +143,7 @@ func (r *Reader) Read() (string, error) {
}

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

if r.printRelativeOffset {
// Print relative offset
Expand All @@ -160,3 +162,8 @@ func (r *Reader) Read() (string, error) {
func (r *Reader) GetReadBytes() uint64 {
return r.readTotalBytes
}

// GetData return raw data read in Read()
func (r *Reader) GetData() []byte {
return r.data
}

0 comments on commit d36023c

Please sign in to comment.