-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermdiff.sh
More file actions
executable file
·80 lines (72 loc) · 1.61 KB
/
termdiff.sh
File metadata and controls
executable file
·80 lines (72 loc) · 1.61 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
#!/usr/bin/env bash
# Configs
save="true"
output="false"
diff="true"
errors="false"
global="false"
# Set Configs by command line args
while [[ $1 == -* ]]; do
case $1 in
"-t" | "--temporary")
save="false"
;;
"-o" | "--output")
output="true"
;;
"-s" | "--silent")
diff="false"
;;
"-e" | "--errors")
errors="true"
;;
"-g" | "--global")
global="true"
;;
*)
echo "Unknown Argument: $1"
exit
esac
shift
done
# Make sure we were given some command
if [[ ! $1 ]]
then
echo "Must provide some command to termdiff!"
exit
fi
# Determine where to save output
if [[ "$global" == "true" ]]
then
dir="/tmp/termdiff/1"
else
# PPID is PID of Parent i.e. terminal that called the script
dir="/tmp/termdiff/$PPID"
fi
# Create temporary dir
mkdir -p "$dir"
# Run command passed as args, store/display output/errors
if [[ "$output" == "true" && "$errors" == "true" ]]
then
$@ |& tee "$dir/new"
elif [[ "$output" == "true" ]] # Errors is false
then
$@ | tee "$dir/new"
elif [[ "$errors" == "true" ]] # Output is false
then
$@ &> "$dir/new"
else # Both are False
$@ > "$dir/new"
fi
# If there's a previously stored output, and we want a diff
if [[ -f "$dir/old" && "$diff" == "true" ]]
then
# Diff outputs
diff -d -U 1 --color=always "$dir/old" "$dir/new" | tail -n +3
fi
# If we want to save the current output for later diffing
if [[ "$save" == "true" ]]
then
# Store last output
mv "$dir/new" "$dir/old"
fi