From 5fb36d032a0c563c5ddd1d87e3d5daaa7a0bad45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Tr=C3=B6ger?= Date: Sun, 19 Apr 2026 11:23:34 +0200 Subject: [PATCH] statusline-command.sh: Effizienz und Korrektheit verbessern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Alle 7 jq-Aufrufe zu einem einzigen zusammengefasst (Process Substitution + read) - Zwei separate awk-Aufrufe zur Kostenformatierung auf einen reduziert - Fehlerhafte if-Bedingung für git-Branch korrigiert (falsche Operator-Präzedenz) - Unnötige Kommentare für offensichtlichen Code entfernt Co-Authored-By: Claude Sonnet 4.6 --- dotfiles/statusline-command.sh | 49 +++++++++++++--------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/dotfiles/statusline-command.sh b/dotfiles/statusline-command.sh index 5be6b22..447b287 100644 --- a/dotfiles/statusline-command.sh +++ b/dotfiles/statusline-command.sh @@ -1,53 +1,42 @@ #!/bin/bash -# Read JSON input from stdin input=$(cat) -# Extract data from JSON -current_dir=$(echo "$input" | jq -r '.workspace.current_dir // empty') -model=$(echo "$input" | jq -r '.model.display_name // "?"') -total_input=$(echo "$input" | jq -r '.context_window.total_input_tokens // 0') -total_output=$(echo "$input" | jq -r '.context_window.total_output_tokens // 0') -context_size=$(echo "$input" | jq -r '.context_window.context_window_size // 200000') -used_percent=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d'.' -f1) -cost_usd=$(echo "$input" | jq -r '.cost.total_cost_usd // 0') +read -r current_dir model total_input total_output context_size used_percent cost_usd < <( + echo "$input" | jq -r '[ + .workspace.current_dir // "", + .model.display_name // "?", + (.context_window.total_input_tokens // 0), + (.context_window.total_output_tokens // 0), + (.context_window.context_window_size // 200000), + ((.context_window.used_percentage // 0) | floor), + (.cost.total_cost_usd // 0) + ] | @tsv' +) -# Calculate total tokens in K format total_tokens=$(( (total_input + total_output) / 1000 )) +context_limit_k=$(( context_size / 1000 )) -# Format cost (show cents if < $1, otherwise dollars) -# Use LC_NUMERIC=C to ensure decimal point handling -cost_cents=$(LC_NUMERIC=C awk "BEGIN {printf \"%.0f\", $cost_usd * 100}") -if [ "$cost_cents" -lt 100 ]; then - cost_formatted="${cost_cents}¢" -else - cost_dollars=$(LC_NUMERIC=C awk "BEGIN {printf \"%.2f\", $cost_usd}") - cost_formatted="\$${cost_dollars}" -fi +cost_formatted=$(LC_NUMERIC=C awk "BEGIN { + cents = int($cost_usd * 100 + 0.5) + if (cents < 100) printf \"%d¢\", cents + else printf \"\$%.2f\", $cost_usd +}") -# Get user and hostname user=$(whoami) host=$(hostname -s) -# Get git branch if in a git repository git_branch="" -if [ -n "$current_dir" ] && [ -d "$current_dir/.git" ] || git -C "$current_dir" rev-parse --git-dir > /dev/null 2>&1; then +if [ -n "$current_dir" ] && git -C "$current_dir" rev-parse --git-dir > /dev/null 2>&1; then branch=$(git -C "$current_dir" branch --show-current 2>/dev/null) [ -n "$branch" ] && git_branch=" \033[33m($branch)\033[00m" fi -# Check for debian_chroot chroot_prefix="" if [ -r /etc/debian_chroot ]; then - debian_chroot=$(cat /etc/debian_chroot) - chroot_prefix="(${debian_chroot})" + chroot_prefix="($(cat /etc/debian_chroot))" fi -# Format context limit in K -context_limit_k=$(( context_size / 1000 )) - -# Output status line -# Format: user@host:dir (branch) | Model | Tokens/Limit | Cost printf "${chroot_prefix}\033[32m${user}@${host}\033[00m:\033[34m${current_dir}\033[00m${git_branch}" printf " \033[90m|\033[00m \033[36m${model}\033[00m" printf " \033[90m|\033[00m \033[35m${total_tokens}K/${context_limit_k}K (${used_percent}%%)\033[00m"