spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / ask / fitted-ask-text-of.ts
37 lines · 835 BRaw
1import { ASK_CUT_NOTE } from './ask-cut-note'
2
3/**
4 * An armed ask's text as it fits in the room a prompt's context has left:
5 * whole when it fits, else its first lines and ASK_CUT_NOTE.
6 *
7 * @param text the armed ask's text
8 * @param room the characters the context has left
9 * @returns the text to attach, or undefined when not even its first line fits
10 */
11export function fittedAskTextOf(
12 text: string,
13 room: number,
14): string | undefined {
15 if (text.length <= room) {
16 return text
17 }
18
19 const kept: string[] = []
20
21 let used = ASK_CUT_NOTE.length
22
23 for (const line of text.split('\n')) {
24 const cost = line.length + 1
25
26 if (used + cost > room) {
27 break
28 }
29
30 kept.push(line)
31 used += cost
32 }
33
34 const hasBody = kept.length > 1
35
36 return hasBody ? `${kept.join('\n')}\n${ASK_CUT_NOTE}` : undefined
37}