-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodometer.sh
More file actions
executable file
·88 lines (74 loc) · 2.41 KB
/
Codometer.sh
File metadata and controls
executable file
·88 lines (74 loc) · 2.41 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
#!/bin/bash
#
# Codometer
# © 2013 Eric Magnuson <eric@ericmagnuson.me>
# MIT License (http://opensource.org/licenses/MIT)
#
# Calculate how many miles of code are written in a directory and its
# subdirectories, as if each character were one after the other on one
# line. Defaults to 12 characters per 1 inch of screen space.
#
# Instructions: Save and run this script in the topmost directory of
# your project. Alternatively, run the script from Github (without
# saving it to your computer) by using the following command:
#
# bash <(curl -s https://raw.githubusercontent.com/ericmagnuson/Codometer/master/Codometer.sh)
white=$(tput setaf 7)
blue=$(tput setaf 6)
green=$(tput setaf 2)
red=$(tput setaf 1)
nc=$(tput sgr0) # No Color
function progress() {
echo ""
echo -n "Calculating the distance..."
while true
do
echo -n "."
sleep 1
done
}
# Ask for chars per inch variable
read -e -p "${blue}In your text editor, how many characters
fit into one inch on your screen (if no
answer is given, this will default to 12)?${nc} " chars_per_inch
chars_per_inch=${chars_per_inch:-12}
# If $chars_per_inch is an int
if [[ $chars_per_inch =~ ^-?[0-9]+$ ]]; then
# Show progress text and save PID to kill it later
progress &
PROGRESS_PID=$!
# Get the number of chars in the entire directory
chars=$(find . \( ! -regex '.*/\..*' \) \
! -name 'Codometer.sh' -type f -exec wc -m {} \; 2> /dev/null \
| awk '{total += $1} END{print total}')
# Stop the progress text
echo ""
kill $PROGRESS_PID
wait $! 2>/dev/null
# Check if bc is installed for doing decimal work.
# If not, we will fall back to expr (no decimals).
bc_installed=true
hash bc 2>/dev/null || bc_installed=false
if $bc_installed; then
inches=$(echo "scale=4; $chars / $chars_per_inch" | bc)
feet=$(echo "scale=4; $chars / $chars_per_inch / 12" | bc)
miles=$(echo "scale=4; $chars / $chars_per_inch / 12 / 5280" | bc)
else
inches=$(expr $chars / $chars_per_inch)
feet=$(expr $chars / $chars_per_inch / 12)
miles=$(expr $chars / $chars_per_inch / 12 / 5280)
fi
echo ""
echo "${white}➤ ${green}$miles miles of code"
echo "...or $feet feet of code"
echo "...or $inches inches of code.${nc}"
if ! $bc_installed; then
echo ""
echo "(You don't have bc installed, so we can't"
echo "calculate decimals. Please install bc for"
echo "more precision in the above distances.)"
fi
else
echo "${red}Please try again with a valid number.${nc}"
exit;
fi