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 / telemetry / hooks / context / terminal-of / terminal-of.ts
74 lines · 2.9 KBRaw
1import type { Facts } from '../../facts'
2import { JETBRAINS_IDES } from './jetbrains-ides'
3
4/**
5 * The row's `terminal` as the CLI names the terminal it runs in, read off
6 * the same variables in the same order.
7 *
8 * The IDE hosts first, then TERM and TERM_PROGRAM, the multiplexers, the
9 * Linux and Windows terminals, WSL, an ssh session, and what TERM still
10 * tells; `non-interactive` with no person at the prompt, else `unknown`.
11 *
12 * @param facts the variables as read once for the session
13 * @param platform the row's platform (the JetBrains check differs on darwin)
14 * @param isInteractive whether a person is at the prompt
15 * @returns the terminal's name
16 */
17export function terminalOf(
18 facts: Facts,
19 platform: string,
20 isInteractive: boolean,
21) {
22 const askpass = (facts.vscodeGitAskpassMain ?? '').toLowerCase()
23 const bundle = (facts.bundleIdentifier ?? '').toLowerCase()
24 const term = facts.term ?? ''
25 const termProgram = facts.termProgram ?? ''
26 const isJediTerm = facts.terminalEmulator === 'JetBrains-JediTerm'
27 const jetbrains = JETBRAINS_IDES.find(ide => bundle.includes(ide))
28
29 const candidates: readonly (readonly [boolean, string])[] = [
30 [isJediTerm && platform !== 'darwin', 'pycharm'],
31 [facts.hasCursorTraceId || askpass.includes('cursor'), 'cursor'],
32 [askpass.includes('windsurf'), 'windsurf'],
33 [askpass.includes('antigravity'), 'antigravity'],
34 [bundle.includes('vscodium'), 'codium'],
35 [bundle.includes('windsurf') || bundle.includes('devin'), 'windsurf'],
36 [bundle.includes('com.google.android.studio'), 'androidstudio'],
37 [jetbrains !== undefined, jetbrains ?? ''],
38 [facts.hasVisualStudioVersion, 'visualstudio'],
39 [isJediTerm, 'pycharm'],
40 [term === 'xterm-ghostty', 'ghostty'],
41 [term.includes('kitty'), 'kitty'],
42 [/^devin([ -]desktop)?$/i.test(termProgram), 'windsurf'],
43 [termProgram !== '', termProgram],
44 [facts.hasTmux, 'tmux'],
45 [facts.hasSty, 'screen'],
46 [facts.hasKonsoleVersion, 'konsole'],
47 [facts.hasGnomeTerminalService, 'gnome-terminal'],
48 [facts.hasXtermVersion, 'xterm'],
49 [facts.hasVteVersion, 'vte-based'],
50 [facts.hasTerminatorUuid, 'terminator'],
51 [facts.hasKittyWindowId, 'kitty'],
52 [facts.hasAlacrittyLog, 'alacritty'],
53 [facts.hasTilixId, 'tilix'],
54 [facts.hasWtSession, 'windows-terminal'],
55 [facts.hasSessionName && term === 'cygwin', 'cygwin'],
56 [facts.msystem !== undefined, (facts.msystem ?? '').toLowerCase()],
57 [
58 facts.hasConEmuAnsi || facts.hasConEmuPid || facts.hasConEmuTask,
59 'conemu',
60 ],
61 [facts.wslDistroName !== undefined, `wsl-${facts.wslDistroName}`],
62 [
63 facts.hasSshConnection || facts.hasSshClient || facts.hasSshTty,
64 'ssh-session',
65 ],
66 [term.includes('alacritty'), 'alacritty'],
67 [term.includes('rxvt'), 'rxvt'],
68 [term.includes('termite'), 'termite'],
69 [term !== '', term],
70 [!isInteractive, 'non-interactive'],
71 ]
72
73 return candidates.find(([isHeld]) => isHeld)?.[1] ?? 'unknown'
74}