|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +ccusage.nvim is a minimal Neovim plugin that integrates the ccusage CLI tool into lualine statuslines. It provides real-time Claude Code usage monitoring, displaying token counts and costs with customizable formatting. |
| 8 | + |
| 9 | +## Commands & Development Workflow |
| 10 | + |
| 11 | +### Testing |
| 12 | +- **Run all tests**: `busted .` |
| 13 | +- **Single test file**: `busted spec/ccusage_spec.lua` |
| 14 | +- **Test with specific pattern**: `busted --pattern="specific_test_name"` |
| 15 | + |
| 16 | +### Development Testing |
| 17 | +- **Health check**: `:checkhealth ccusage` (in Neovim) |
| 18 | +- **Minimal reproduction**: `nvim -u repro/repro.lua` |
| 19 | +- **Test command**: `:CCUsage` (displays usage stats notification) |
| 20 | + |
| 21 | +### CLI Dependencies |
| 22 | +- **ccusage CLI**: Required external dependency (`npm install -g ccusage`) |
| 23 | +- **Commands used**: `ccusage blocks --json --offline`, `ccusage --version` |
| 24 | + |
| 25 | +## Architecture |
| 26 | + |
| 27 | +### Core Module Structure |
| 28 | +- **`lua/ccusage/init.lua`**: Main entry point with `setup()` function |
| 29 | +- **`lua/ccusage/config.lua`**: Configuration management with defaults and user options merging |
| 30 | +- **`lua/ccusage/cli.lua`**: CLI interface with caching (5-second cache), async job management via `vim.fn.jobstart` |
| 31 | +- **`lua/ccusage/data.lua`**: Data processing layer that coordinates CLI and stats computation |
| 32 | +- **`lua/ccusage/utils.lua`**: Utility functions for time parsing, command checking, and stats calculation |
| 33 | + |
| 34 | +### Plugin Integration Points |
| 35 | +- **`plugin/ccusage.lua`**: Defines `:CCUsage` command with notification system |
| 36 | +- **`lua/lualine/components/ccusage.lua`**: Lualine component integration |
| 37 | +- **`lua/ccusage/health.lua`**: Health check implementation for `:checkhealth ccusage` |
| 38 | + |
| 39 | +### Data Flow Architecture |
| 40 | +1. **CLI Layer** (`cli.lua`): Executes `ccusage` command asynchronously, handles JSON parsing and caching |
| 41 | +2. **Data Layer** (`data.lua`): Coordinates between CLI data and computed statistics |
| 42 | +3. **Utils Layer** (`utils.lua`): Computes stats from raw blocks data (max_tokens, usage_ratio, time_ratio, cost) |
| 43 | +4. **Formatter Layer** (`formatters/*.lua`): Transforms data into display strings with color coding |
| 44 | +5. **Display Layer** (lualine component/command): Presents formatted data to user |
| 45 | + |
| 46 | +### Key Data Structures |
| 47 | +- **CCUsage.Block**: Raw block data from ccusage CLI with tokenCounts, costUSD, timestamps |
| 48 | +- **CCUsage.Stats**: Computed statistics (usage_ratio, time_ratio, max_tokens, cost) |
| 49 | +- **CCUsage.FormatterContext**: Combined data/stats object passed to formatter functions |
| 50 | + |
| 51 | +### Formatter System |
| 52 | +- **Default formatter** (`formatters/default.lua`): Shows "time 75% | tok 45%" with color coding |
| 53 | +- **Verbose formatter** (`formatters/verbose.lua`): Detailed stats for `:CCUsage` command |
| 54 | +- **Custom formatters**: User-configurable via `formatter` option in setup |
| 55 | + |
| 56 | +### Error Handling Strategy |
| 57 | +- **Graceful degradation**: Missing CLI/data shows appropriate fallback messages |
| 58 | +- **Unified error handling**: `data.get_formatter_context()` centralizes error states |
| 59 | +- **User feedback**: Clear messages for missing dependencies or invalid data |
| 60 | + |
| 61 | +## Context7 Documentation Resources |
| 62 | + |
| 63 | +Available documentation for integration and development: |
| 64 | +- **Neovim**: `/neovim/neovim` - Core Neovim APIs and patterns |
| 65 | +- **Busted**: `lunarmodules_github_io-busted` - Testing framework documentation |
| 66 | +- **lualine**: `/nvim-lualine/lualine.nvim` - Statusline component patterns |
| 67 | +- **ccusage**: `/ryoppippi/ccusage` - CLI tool data structures and usage |
| 68 | + |
| 69 | +## Testing Architecture |
| 70 | + |
| 71 | +- **Busted framework**: Uses spies and mocks for external dependencies |
| 72 | +- **Mock strategy**: Mocks `io.popen`, `os.time`, `os.date` for deterministic tests |
| 73 | +- **Sample data**: Comprehensive test fixtures matching real ccusage CLI output |
| 74 | +- **Module isolation**: Each test reloads modules for clean state |
| 75 | +- **Coverage areas**: Config merging, CLI handling, stats computation, formatters, error conditions |
0 commit comments