-
Notifications
You must be signed in to change notification settings - Fork 0
/
rev-ind.sh
executable file
·67 lines (55 loc) · 1.64 KB
/
rev-ind.sh
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
#!/bin/bash
# reverse Indenter
# total copyright Andy Wallace 2020
# use this all you want BUT
# if you steal this you're going to JAIL!
# send compliments to andy@andymakes
# do not send any criticism or advice.
input=$1
output=$2
space=" "
max_tabs=0
while IFS= read -r line
do
tab_count=0
for i in $(seq 1 ${#line})
do
this_char=${line:i-1:1}
if [ "$this_char" = "$space" ]
then
tab_count=$((tab_count+1))
else
break
fi
done
#is this the new max?
if [ $tab_count -gt $max_tabs ]
then
max_tabs=$tab_count
fi
done < "$input"
echo "-- max tabs: $max_tabs --"
#now that we have the max tabs we can cut them down
while IFS= read -r line
do
tab_count=0
for i in $(seq 1 ${#line})
do
this_char=${line:i-1:1}
if [ "$this_char" = "$space" ]
then
tab_count=$((tab_count+1))
else
break
fi
done
trim_line=${line:$tab_count}
num_to_add_f=$(bc -l <<< "(1.0-($tab_count/$max_tabs))*$max_tabs")
num_to_add_i=${num_to_add_f%.*}
for (( c=1; c<=$num_to_add_i; c++ ))
do
trim_line=" $trim_line"
done
echo "$trim_line" >> $output
done < "$input"
echo "enjoy your new, reverse indented file."