-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvbat.sh
More file actions
99 lines (86 loc) · 2.71 KB
/
Copy pathvbat.sh
File metadata and controls
99 lines (86 loc) · 2.71 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
# vbat.sh: Enhanced bat wrapper for Bash environments
# Supports wildcards, custom encodings, and .encoding sidecar files.
# Default encoding is strictly set to UTF-8.
show_help() {
echo "Usage:"
echo " ./vbat.sh [filename/pattern] [--encoding=value] [-e value]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -e, --encoding= Specify file encoding (e.g., UTF-8, CP949, EUC-KR)"
echo " (Defaults to UTF-8 if omitted)"
exit 0
}
file_pattern=""
enc_override=""
# 1. Parse Arguments Safely
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
;;
--encoding=*)
enc_override="${1#*=}"
shift
;;
-e|--encoding)
if [[ -n "$2" && "$2" != -* ]]; then
enc_override="$2"
shift 2
else
echo "Error: Argument for $1 is missing." >&2
exit 1
fi
;;
*)
file_pattern="$1"
shift
;;
esac
done
# 2. Validation: Check Missing Path
if [[ -z "$file_pattern" ]]; then
echo "Error: Missing filename or pattern. Type \`./vbat.sh -h\` for help." >&2
exit 1
fi
# 3. Process Files (Handles Wildcards natively via Bash Expansion)
shopt -s nullglob
files=($file_pattern)
shopt -u nullglob
if [[ ${#files[@]} -eq 0 ]]; then
echo "Error: File(s) not found: $file_pattern" >&2
exit 1
fi
for file in "${files[@]}"; do
if [[ ! -f "$file" ]]; then
continue
fi
$current_enc="UTF-8" # Default Fallback
if [[ -n "$enc_override" ]]; then
current_enc="$enc_override"
else
meta_file="${file}.encoding"
if [[ -f "$meta_file" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^[[:space:]]*encoding[[:space:]]*=[[:space:]]*(.+)$ ]]; then
current_enc=$(echo "${BASH_REMATCH[1]}" | xargs)
break
fi
done < "$meta_file"
fi
fi
# Normalize Encoding Names for iconv compatibility
if [[ "${current_enc,,}" == "utf8" || "${current_enc,,}" == "default" ]]; then
current_enc="UTF-8"
fi
# 4. Validate Encoding with iconv
if ! iconv -l | grep -qi "^${current_enc}$"; then
if ! iconv -f "$current_enc" -t UTF-8 <<< "" &>/dev/null; then
echo "Error: Unsupported encoding '$current_enc' for file $(basename "$file")" >&2
continue
fi
fi
# 5. Read, Convert, and Pipeline to 'bat'
iconv -f "$current_enc" -t UTF-8 "$file" 2>/dev/null | bat --file-name "$(basename "$file")"
done