Skip to content

Instantly share code, notes, and snippets.

@tecnologer
Created July 4, 2024 16:44
Show Gist options
  • Select an option

  • Save tecnologer/c34685b9cdc604f8d33322f20602c87b to your computer and use it in GitHub Desktop.

Select an option

Save tecnologer/c34685b9cdc604f8d33322f20602c87b to your computer and use it in GitHub Desktop.

Code Stats

This script checks your code stats of the current dir. If previous stats are stored, they could be compared to them.

Preconditions

Requires tokei, you could install it using:

  • Debian/Ubuntu: apt install tokei
  • Mac: brew install tokei

Tip

Create an alias in your ~/.*rc (.bashrc, .zshrc, etc) file.

Example

  1. Open the shell config file with your favorite editor, for example: nano ~/.zshrc.
  2. If the script is stored at ~/code_stats.sh, add alias stats='~/code_stats.sh' to the end of the file ~/.zshrc.
  3. Save changes.
  4. Reinitialize the shell environment with source ~/.zshrc.
  5. Test your command stats -h.
#!/bin/bash
# check if the first argument is --help or -h
if [[ $1 == "--help" || $1 == "-h" ]]; then
echo "Usage: stats [date]"
echo "Example: stats 20210101"
echo ""
echo "Prints the code statistics for the current day or the specified date."
echo "If no date is provided, the statistics for the previous working day are shown."
echo "Requires tokei to be installed. More info at https://github.com/XAMPPRocky/tokei"
return
fi
# Get the current day of the week (1 = Monday, 7 = Sunday)
day_of_week=$(date +%u)
today=$(date "+%Y%m%d")
# Check if a date was passed as an argument
if [[ -n $1 ]]; then
# Use the provided date
previous_working_day=$1
else
# Check if today is Monday (day_of_week = 1)
if [[ $day_of_week -eq 1 ]]; then
# If today is Monday, get last Friday
previous_working_day=$(date -v-3d "+%Y%m%d")
else
# Otherwise, get the previous weekday
previous_working_day=$(date -v-1d "+%Y%m%d")
fi
fi
prev_filename="stats_$previous_working_day.json"
if [[ -f $prev_filename ]]; then
tokei --input "$prev_filename"
fi
tokei --output json > "stats_$today.json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment