Skip to content

Instantly share code, notes, and snippets.

@Mario-SO
Created April 20, 2023 16:33
Show Gist options
  • Select an option

  • Save Mario-SO/409ca4b38692741420ba1793423c7435 to your computer and use it in GitHub Desktop.

Select an option

Save Mario-SO/409ca4b38692741420ba1793423c7435 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
# Made by @mariodev__
clear
# Check if the directory is provided
if [ -z "$1" ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# Check if the directory exists
if [ ! -d "$1" ]; then
echo "Error: Directory \"$1\" does not exist."
exit 1
fi
# ANSI escape codes for colors
HEADER_COLOR="\033[1;34m" # Bold blue
DATA_COLOR="\033[1;32m" # Bold green
LINE_COLOR="\033[1;31m" # Bold red
RESET_COLOR="\033[0m" # Reset to default color
# Create a temporary file to store filenames and line counts
temp_file=$(mktemp)
# Loop through the files in the directory
for file in "$1"/*; do
if [ -f "$file" ]; then
line_count=$(wc -l < "$file")
file_name=$(basename "$file")
printf "%-40s %12s\n" "$file_name" "$line_count" >> "$temp_file"
fi
done
# Function to draw a horizontal line
draw_horizontal_line() {
printf "${LINE_COLOR}+%-40s+%-12s+${RESET_COLOR}\n" "$(printf '%0.1s' -{1..40})" "$(printf '%0.1s' -{1..12})"
}
# Header of the table
draw_horizontal_line
printf "${HEADER_COLOR}${LINE_COLOR}|${RESET_COLOR}${HEADER_COLOR}%-40s${RESET_COLOR}${LINE_COLOR}|${RESET_COLOR}${HEADER_COLOR}%12s${RESET_COLOR}${LINE_COLOR}|${RESET_COLOR}\n" " Filename" " Line Count "
draw_horizontal_line
# Sort the temporary file by line count and print the result
while read -r line; do
file_name=$(echo "$line" | awk '{ print $1 }')
line_count=$(echo "$line" | awk '{ print $2 }')
printf "${LINE_COLOR}|${RESET_COLOR}${DATA_COLOR}%-40s${RESET_COLOR}${LINE_COLOR}|${RESET_COLOR}${DATA_COLOR}%12s${RESET_COLOR}${LINE_COLOR}|${RESET_COLOR}\n" " $file_name" "$line_count "
done < <(sort -n -k 2 "$temp_file")
# Footer of the table
draw_horizontal_line
# Remove the temporary file
rm "$temp_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment