aboutsummaryrefslogtreecommitdiff
path: root/cmd/explain-pack/fmt.go
blob: a3d1b33397467f1dc0437acfee30a860868e281f (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
	"bytes"
	"encoding/hex"
	"io"

	"lindenii.org/go/furgit/internal/utils"
)

func indentBlock(out io.Writer, indent string, block []byte) {
	lines := bytes.Split(block, []byte("\n"))
	if n := len(lines); n > 0 && len(lines[n-1]) == 0 {
		lines = lines[:n-1]
	}

	for _, line := range lines {
		utils.BestEffortFprintf(out, "%s%s\n", indent, line)
	}
}

func hexBlock(out io.Writer, indent string, data []byte) {
	var buf bytes.Buffer

	dumper := hex.Dumper(&buf)
	_, _ = dumper.Write(data)
	_ = dumper.Close()

	indentBlock(out, indent, buf.Bytes())
}