Skip to content

Commit 7e894e9

Browse files
committed
feat: generalize format hook and add floating window docs
Change-Id: I18f2e841ae68c3f4017155367e9a944adf4a4827 Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent e08921f commit 7e894e9

File tree

3 files changed

+163
-23
lines changed

3 files changed

+163
-23
lines changed

.claude/hooks/format-lua.sh renamed to .claude/hooks/format.sh

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
#
3-
# Claude Code Hook: Format Lua Files
4-
# Triggers after Claude edits/writes Lua files and runs nix fmt
3+
# Claude Code Hook: Format Files
4+
# Triggers after Claude edits/writes files and runs nix fmt
55
#
66
# Environment variables provided by Claude Code:
77
# - CLAUDE_PROJECT_DIR: Path to the project directory
@@ -43,27 +43,21 @@ get_file_path() {
4343
return
4444
fi
4545

46-
# Try extracting any .lua file path from the input
47-
local lua_path
48-
lua_path=$(echo "$hook_input" | grep -o '"[^"]*\.lua"' | sed 's/"//g' | head -1)
46+
# Try extracting any file path from the input
47+
local any_file_path
48+
any_file_path=$(echo "$hook_input" | grep -o '"[^"]*\.[^"]*"' | sed 's/"//g' | head -1)
4949

50-
if [ -n "$lua_path" ]; then
51-
echo "$lua_path"
50+
if [ -n "$any_file_path" ]; then
51+
echo "$any_file_path"
5252
return
5353
fi
5454

5555
log "DEBUG: Could not extract file path from hook input"
5656
}
5757

58-
# Check if file is a Lua file
59-
is_lua_file() {
60-
local file="$1"
61-
[[ $file =~ \.lua$ ]]
62-
}
63-
6458
# Main logic
6559
main() {
66-
log "${YELLOW}Claude Code Hook: Lua Formatter${NC}"
60+
log "${YELLOW}Claude Code Hook: File Formatter${NC}"
6761

6862
# Get the file path from tool arguments
6963
FILE_PATH=$(get_file_path)
@@ -75,19 +69,13 @@ main() {
7569

7670
log "Tool: ${CLAUDE_TOOL_NAME:-unknown}, File: $FILE_PATH"
7771

78-
# Check if it's a Lua file
79-
if ! is_lua_file "$FILE_PATH"; then
80-
log "Skipping: Not a Lua file ($FILE_PATH)"
81-
exit 0
82-
fi
83-
8472
# Check if file exists
8573
if [ ! -f "$FILE_PATH" ]; then
8674
log "${RED}Error: File does not exist: $FILE_PATH${NC}"
8775
exit 1
8876
fi
8977

90-
log "${YELLOW}Formatting Lua file with nix fmt...${NC}"
78+
log "${YELLOW}Formatting file with nix fmt...${NC}"
9179

9280
# Change to project directory
9381
cd "${CLAUDE_PROJECT_DIR}"

.claude/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"hooks": [
77
{
88
"type": "command",
9-
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/format-lua.sh"
9+
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/format.sh"
1010
}
1111
]
1212
}

README.md

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ For deep technical details, see [ARCHITECTURE.md](./ARCHITECTURE.md).
151151
split_width_percentage = 0.30,
152152
provider = "auto", -- "auto", "snacks", "native", or custom provider table
153153
auto_close = true,
154-
snacks_win_opts = {}, -- Opts to pass to `Snacks.terminal.open()`
154+
snacks_win_opts = {}, -- Opts to pass to `Snacks.terminal.open()` - see Floating Window section below
155155
},
156156

157157
-- Diff Integration
@@ -167,6 +167,158 @@ For deep technical details, see [ARCHITECTURE.md](./ARCHITECTURE.md).
167167
}
168168
```
169169

170+
## Floating Window Configuration
171+
172+
The `snacks_win_opts` configuration allows you to create floating Claude Code terminals with custom positioning, sizing, and key bindings. Here are several practical examples:
173+
174+
### Basic Floating Window with Ctrl+, Toggle
175+
176+
```lua
177+
local toggle_key = "<C-,>"
178+
return {
179+
{
180+
"coder/claudecode.nvim",
181+
dependencies = { "folke/snacks.nvim" },
182+
keys = {
183+
{ toggle_key, "<cmd>ClaudeCodeFocus<cr>", desc = "Claude Code", mode = { "n", "x" } },
184+
},
185+
opts = {
186+
terminal = {
187+
---@module "snacks"
188+
---@type snacks.win.Config|{}
189+
snacks_win_opts = {
190+
position = "float",
191+
width = 0.9,
192+
height = 0.9,
193+
keys = {
194+
claude_hide = {
195+
toggle_key,
196+
function(self)
197+
self:hide()
198+
end,
199+
mode = "t",
200+
desc = "Hide",
201+
},
202+
},
203+
},
204+
},
205+
},
206+
},
207+
}
208+
```
209+
210+
### Alternative with Meta+, (Alt+,) Toggle
211+
212+
```lua
213+
local toggle_key = "<M-,>" -- Alt/Meta + comma
214+
return {
215+
{
216+
"coder/claudecode.nvim",
217+
dependencies = { "folke/snacks.nvim" },
218+
keys = {
219+
{ toggle_key, "<cmd>ClaudeCodeFocus<cr>", desc = "Claude Code", mode = { "n", "x" } },
220+
},
221+
opts = {
222+
terminal = {
223+
snacks_win_opts = {
224+
position = "float",
225+
width = 0.8,
226+
height = 0.8,
227+
border = "rounded",
228+
keys = {
229+
claude_hide = { toggle_key, function(self) self:hide() end, mode = "t", desc = "Hide" },
230+
},
231+
},
232+
},
233+
},
234+
},
235+
}
236+
```
237+
238+
### Centered Floating Window with Custom Styling
239+
240+
```lua
241+
require("claudecode").setup({
242+
terminal = {
243+
snacks_win_opts = {
244+
position = "float",
245+
width = 0.6,
246+
height = 0.6,
247+
border = "double",
248+
backdrop = 80,
249+
keys = {
250+
claude_hide = { "<Esc>", function(self) self:hide() end, mode = "t", desc = "Hide" },
251+
claude_close = { "q", "close", mode = "n", desc = "Close" },
252+
},
253+
},
254+
},
255+
})
256+
```
257+
258+
### Multiple Key Binding Options
259+
260+
```lua
261+
{
262+
"coder/claudecode.nvim",
263+
dependencies = { "folke/snacks.nvim" },
264+
keys = {
265+
{ "<C-,>", "<cmd>ClaudeCodeFocus<cr>", desc = "Claude Code (Ctrl+,)", mode = { "n", "x" } },
266+
{ "<M-,>", "<cmd>ClaudeCodeFocus<cr>", desc = "Claude Code (Alt+,)", mode = { "n", "x" } },
267+
{ "<leader>tc", "<cmd>ClaudeCodeFocus<cr>", desc = "Toggle Claude", mode = { "n", "x" } },
268+
},
269+
opts = {
270+
terminal = {
271+
snacks_win_opts = {
272+
position = "float",
273+
width = 0.85,
274+
height = 0.85,
275+
border = "rounded",
276+
keys = {
277+
-- Multiple ways to hide from terminal mode
278+
claude_hide_ctrl = { "<C-,>", function(self) self:hide() end, mode = "t", desc = "Hide (Ctrl+,)" },
279+
claude_hide_alt = { "<M-,>", function(self) self:hide() end, mode = "t", desc = "Hide (Alt+,)" },
280+
claude_hide_esc = { "<C-\\><C-n>", function(self) self:hide() end, mode = "t", desc = "Hide (Ctrl+\\)" },
281+
},
282+
},
283+
},
284+
},
285+
}
286+
```
287+
288+
### Window Position Variations
289+
290+
```lua
291+
-- Bottom floating (like a drawer)
292+
snacks_win_opts = {
293+
position = "bottom",
294+
height = 0.4,
295+
width = 1.0,
296+
border = "single",
297+
}
298+
299+
-- Side floating panel
300+
snacks_win_opts = {
301+
position = "right",
302+
width = 0.4,
303+
height = 1.0,
304+
border = "rounded",
305+
}
306+
307+
-- Small centered popup
308+
snacks_win_opts = {
309+
position = "float",
310+
width = 120, -- Fixed width in columns
311+
height = 30, -- Fixed height in rows
312+
border = "double",
313+
backdrop = 90,
314+
}
315+
```
316+
317+
For complete configuration options, see:
318+
319+
- [Snacks.nvim Terminal Documentation](https://github.com/folke/snacks.nvim/blob/main/docs/terminal.md)
320+
- [Snacks.nvim Window Documentation](https://github.com/folke/snacks.nvim/blob/main/docs/win.md)
321+
170322
## Custom Terminal Providers
171323

172324
You can create custom terminal providers by passing a table with the required functions instead of a string provider name:

0 commit comments

Comments
 (0)