From 049b18351dc7c1cd137d9b9372abab1b1f2374ed Mon Sep 17 00:00:00 2001 From: n0tori <188390306+n0tori@users.noreply.github.com> Date: Thu, 8 Jan 2026 22:10:08 +0000 Subject: added depth limit of directories --- README.md | 5 +++ langbreak | 110 +++++++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 75 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index e37580b..00a1378 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,11 @@ Export language data to JSON or YAML: ./langbreak --yaml ``` +Limit directory depth: +```bash +./langbreak --max-depth 2 +``` + Redirect to a file: ```bash ./langbreak --json > stats.json diff --git a/langbreak b/langbreak index 8ca4250..110df13 100644 --- a/langbreak +++ b/langbreak @@ -8,7 +8,9 @@ # Date: January 08, 2026 # License: GPLv3 # -# Usage: ./langbreak [--json|--yaml] +# Usage: ./langbreak [OPTIONS] +# ./langbreak --json +# ./langbreak --max-depth 2 # Color constants (ANSI 256-color codes) readonly COLOR_RESET='\033[0m' @@ -16,6 +18,7 @@ declare -A LANGUAGE_COLORS declare -A LANGUAGE_BYTES declare -A LANGUAGE_FILES declare -A EXTENSION_MAP +MAX_DEPTH="" ####################################### # Initialize language-to-color mapping (GitHub linguist colors) @@ -179,6 +182,7 @@ detect_language() { # Globals: # LANGUAGE_BYTES # LANGUAGE_FILES +# MAX_DEPTH # Arguments: # None ####################################### @@ -186,17 +190,13 @@ count_language_bytes() { local file local language local bytes + local find_cmd="find ." - # Find files with exclusions - while IFS= read -r file; do - language=$(detect_language "${file}") + if [[ -n "${MAX_DEPTH}" ]]; then + find_cmd="${find_cmd} -maxdepth ${MAX_DEPTH}" + fi - if [[ -n "${language}" ]]; then - bytes=$(wc -c < "${file}" 2>/dev/null || echo "0") - LANGUAGE_BYTES["${language}"]=$((${LANGUAGE_BYTES[${language}]:-0} + bytes)) - LANGUAGE_FILES["${language}"]=$((${LANGUAGE_FILES[${language}]:-0} + 1)) - fi - done < <(find . -type f \ + find_cmd="${find_cmd} -type f \ ! -path '*/\.*' \ ! -path '*/node_modules/*' \ ! -path '*/vendor/*' \ @@ -205,7 +205,18 @@ count_language_bytes() { ! -path '*/dist/*' \ ! -path '*/build/*' \ ! -path '*/out/*' \ - ! -path '*/target/*') + ! -path '*/target/*'" + + # Find files with exclusions + while IFS= read -r file; do + language=$(detect_language "${file}") + + if [[ -n "${language}" ]]; then + bytes=$(wc -c < "${file}" 2>/dev/null || echo "0") + LANGUAGE_BYTES["${language}"]=$((${LANGUAGE_BYTES[${language}]:-0} + bytes)) + LANGUAGE_FILES["${language}"]=$((${LANGUAGE_FILES[${language}]:-0} + 1)) + fi + done < <(eval "${find_cmd}") } ####################################### @@ -369,39 +380,58 @@ render_output() { ####################################### # Main function # Arguments: -# Command-line arguments (--json, --yaml, --help) +# Command-line arguments (--json, --yaml, --help, --max-depth) ####################################### main() { local export_format="" - case "${1:-}" in - --json) - export_format="json" - ;; - --yaml) - export_format="yaml" - ;; - --help) - echo "Usage: ./langbreak [--json|--yaml]" - echo "" - echo "Display programming language breakdown for the current directory." - echo "" - echo "Options:" - echo " --json Export as JSON to stdout" - echo " --yaml Export as YAML to stdout" - echo " --help Show this help message" - echo "" - echo "Without options, displays a coloured bar visualisation." - exit 0 - ;; - "") - ;; - *) - echo "Error: Unknown option '${1}'" >&2 - echo "Usage: ./langbreak [--json|--yaml]" >&2 - exit 1 - ;; - esac + while [[ $# -gt 0 ]]; do + case "$1" in + --json) + export_format="json" + shift + ;; + --yaml) + export_format="yaml" + shift + ;; + --max-depth) + if [[ -z "${2:-}" || "${2}" =~ ^- ]]; then + echo "Error: --max-depth requires a numeric value" >&2 + exit 1 + fi + if ! [[ "${2}" =~ ^[0-9]+$ ]]; then + echo "Error: --max-depth value must be a positive integer" >&2 + exit 1 + fi + MAX_DEPTH="$2" + shift 2 + ;; + --help) + echo "Usage: ./langbreak [OPTIONS]" + echo "" + echo "Display programming language breakdown for the current directory." + echo "" + echo "Options:" + echo " --json Export as JSON to stdout" + echo " --yaml Export as YAML to stdout" + echo " --max-depth Limit directory traversal to N levels deep" + echo " --help Show this help message" + echo "" + echo "Without options, displays a coloured bar visualisation." + exit 0 + ;; + "") + shift + ;; + *) + echo "Error: Unknown option '${1}'" >&2 + echo "Usage: ./langbreak [OPTIONS]" >&2 + echo "Try './langbreak --help' for more information." >&2 + exit 1 + ;; + esac + done initialize_color_map initialize_extension_map -- cgit v1.2.3