From efebbc7a955ca2013e9576cd2d7ae2609ffa45d6 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Sat, 14 Feb 2026 21:49:54 -0600 Subject: [PATCH] Add PostToolUse hook for auto-formatting Python with uvx black MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - New hooks/format-code.sh: reads file path from stdin JSON, dispatches by extension (*.py → uvx black, *.js/ts → prettier) - Updated settings.json: PostToolUse hook on Edit/Write/MultiEdit Co-Authored-By: Claude Opus 4.6 --- hooks/format-code.sh | 12 ++++++++++++ settings.json | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100755 hooks/format-code.sh diff --git a/hooks/format-code.sh b/hooks/format-code.sh new file mode 100755 index 0000000..0726d25 --- /dev/null +++ b/hooks/format-code.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Read tool input from stdin (JSON with tool_input.file_path) +FILE=$(jq -r '.tool_input.file_path // empty' 2>/dev/null) + +[ -z "$FILE" ] && exit 0 + +case "$FILE" in + *.py) + uvx black --quiet "$FILE" ;; + *.js|*.ts|*.jsx|*.tsx) + prettier --write "$FILE" 2>/dev/null ;; +esac diff --git a/settings.json b/settings.json index 4468ec0..8c4ef18 100644 --- a/settings.json +++ b/settings.json @@ -103,6 +103,18 @@ }, "effortLevel": "medium", "hooks": { + "PostToolUse": [ + { + "matcher": "Edit|Write|MultiEdit", + "hooks": [ + { + "type": "command", + "command": "/home/cal/.claude/hooks/format-code.sh", + "timeout": 10 + } + ] + } + ], "SessionEnd": [ { "hooks": [