-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbin-graph-hexdump.sh
More file actions
executable file
·72 lines (63 loc) · 2.16 KB
/
bin-graph-hexdump.sh
File metadata and controls
executable file
·72 lines (63 loc) · 2.16 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env bash
#
# Copyright 2025 8dcc. All Rights Reserved.
#
# This file is part of bin-graph.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------
#
# Generate multiple binary graphs, and merge them together into a big image.
set -e
HEXDUMP='xxd'
HEXDUMP_ARGS=(-R always)
BIN_GRAPH='bin-graph'
BIN_GRAPH_ARGS=(--width 16 --zoom 2 --output-format 'escaped-text')
PASTE='paste'
if [ $# -lt 1 ]; then
echo "Usage: $(basename "$0") [OPTION...] INPUT" 1>&2
exit 1
fi
assert_cmd() {
if [ ! "$(command -v "$1")" ]; then
echo "$(basename "$0"): The '$1' command is not installed." 1>&2
exit 1
fi
}
assert_cmd "$HEXDUMP"
assert_cmd "$BIN_GRAPH"
assert_cmd "$PASTE"
# Last argument must be the input file.
input_file="${*: -1}"
# Final string of arguments for 'bin-graph'.
bin_graph_args=("${BIN_GRAPH_ARGS[@]}" "${@:1:$#-1}")
# In order to redirect the standard input to both commands, it needs to be
# buffered in a temporary file.
if [ "$input_file" = "-" ]; then
tmp_file="$(mktemp --tmpdir "bin-graph-hexdump.XXXXX.bin")"
cat "$input_file" > "$tmp_file"
input_file="$tmp_file"
fi
# Merge the output of the hexdump command with the ANSI-encoded output of
# 'bin-graph'.
#
# FIXME: If the last hexdump line is partial, the output of 'bin-graph' is not
# aligned.
"$PASTE" <("$HEXDUMP" "${HEXDUMP_ARGS[@]}" "$input_file") \
<("$BIN_GRAPH" "${bin_graph_args[@]}" "$input_file" '-')
# We are done with the temporary file, delete it.
if [ -n "$tmp_file" ]; then
rm "$tmp_file"
fi