|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('node:fs'); |
| 4 | +const { execSync } = require('node:child_process'); |
| 5 | + |
| 6 | +const CLAUDE_DUMB_ZONE = 59; |
| 7 | + |
| 8 | +function getGitBranch() { |
| 9 | + try { |
| 10 | + return execSync('git branch --show-current 2>/dev/null', { encoding: 'utf-8' }).trim(); |
| 11 | + } catch { |
| 12 | + return ''; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function ansi(text, bgColor, color = 'rgb(255, 255, 255)') { |
| 17 | + const RESET = '\x1b[0m'; |
| 18 | + const bgColorValues = bgColor.match(/\d+/g); |
| 19 | + const bgColorAnsi = `\x1b[48;2;${bgColorValues.join(';')}m`; |
| 20 | + const colorValues = color.match(/\d+/g); |
| 21 | + const colorAnsi = `\x1b[38;2;${colorValues.join(';')}m`; |
| 22 | + return `${RESET}${bgColorAnsi}${colorAnsi}${text}${RESET}`; |
| 23 | +} |
| 24 | + |
| 25 | +function formatSections(sections) { |
| 26 | + |
| 27 | + const LEFT_ROUND = '\ue0b6'; |
| 28 | + const RIGHT_ARROW = '\ue0b0'; |
| 29 | + const RIGHT_ROUND = '\ue0b4'; |
| 30 | + |
| 31 | + const coloredSections = sections.map((section, index) => { |
| 32 | + const { text, bgColor, color } = section; |
| 33 | + const coloredSection = ansi(` ${text} `, bgColor, color); |
| 34 | + |
| 35 | + // First section does not need a leading right arrow |
| 36 | + if (index === 0) { |
| 37 | + return coloredSection; |
| 38 | + } |
| 39 | + |
| 40 | + const prevBgColor = sections[index - 1].bgColor; |
| 41 | + const prefix = `${ansi(RIGHT_ARROW, bgColor, prevBgColor)}`; |
| 42 | + return prefix + coloredSection; |
| 43 | + }); |
| 44 | + |
| 45 | + return [ |
| 46 | + `${ansi(LEFT_ROUND, 'rgb(0,0,0)', sections.at(0).bgColor)}`, |
| 47 | + ...coloredSections, |
| 48 | + `${ansi(RIGHT_ROUND, 'rgb(0,0,0)', sections.at(-1).bgColor)}`, |
| 49 | + ].join(''); |
| 50 | +} |
| 51 | + |
| 52 | +try { |
| 53 | + const sections = []; |
| 54 | + |
| 55 | + // Read data from Claude Code stdin |
| 56 | + const input = JSON.parse(fs.readFileSync(0, 'utf-8')); |
| 57 | + |
| 58 | + const cwd = input.workspace.current_dir; |
| 59 | + const dirName = cwd.split('/').at(-1); |
| 60 | + sections.push({ text: dirName, bgColor: "rgb(52, 86, 164)" }); |
| 61 | + |
| 62 | + const branch = getGitBranch(); |
| 63 | + if (branch) { |
| 64 | + sections.push({ text: branch, bgColor: "rgb(70, 107, 62)" }); |
| 65 | + } |
| 66 | + |
| 67 | + const model = input.model.display_name; |
| 68 | + sections.push({ text: model, bgColor: "rgb(68, 68, 68)" }); |
| 69 | + |
| 70 | + // Context window usage |
| 71 | + const contextWindow = input.context_window; |
| 72 | + |
| 73 | + if (contextWindow?.current_usage && contextWindow?.context_window_size) { |
| 74 | + const usage = contextWindow.current_usage; |
| 75 | + const currentTokens = (usage.input_tokens || 0) + |
| 76 | + (usage.output_tokens || 0) + |
| 77 | + (usage.cache_creation_input_tokens || 0) + |
| 78 | + (usage.cache_read_input_tokens || 0); |
| 79 | + const contextSize = contextWindow.context_window_size; |
| 80 | + const percent = Math.round((currentTokens / contextSize) * 100); |
| 81 | + const isDumbZone = percent > CLAUDE_DUMB_ZONE; |
| 82 | + const bgColor = isDumbZone ? "rgb(226, 0, 0)" : "rgb(217, 119, 87)"; |
| 83 | + const color = isDumbZone ? "rgb(255, 255, 255)" : "rgb(0, 0, 0)"; |
| 84 | + sections.push({ text: `${percent}%`, bgColor, color }); |
| 85 | + } |
| 86 | + |
| 87 | + console.log(formatSections(sections)); |
| 88 | +} catch (error) { |
| 89 | + console.error('Error:', error.message); |
| 90 | + process.exit(1); |
| 91 | +} |
0 commit comments