Shows session name, working folder and git branch in the prompt footer and in the terminal window title

セッション名、作業フォルダ、git ブランチをプロンプト下のフッター左側に表示し、同じ文字列をターミナルのウィンドウタイトルにも設定する、Claude Code の mod です。
session name │ ConsoleProfiles ⎇ main │ ? for shortcuts
セッション名は、/rename で付けた名前を優先します。無ければ自動生成された名前を、それも無ければセッション ID の先頭を使います。detached HEAD は @<commit> と表示します。
ウィンドウタイトルは子プロセスで設定します。Windows では scripts/set-title.ps1 を使い、それ以外では /dev/tty に OSC シーケンスを書き込みます。エンジンにタイトルを設定する手段が無いためです。
claude-code-mods の一部です。
/plugin marketplace add akagaya/claude-code-mods
/plugin install title-bar@claude-code-mods
次に起動した Claude Code のセッションから読み込まれます。
claude plugin validate .
claude plugin test .hooks/register.tsx 233 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface as Engine, Register } from 'claude-code'
3
4import type { Bar } from '../types'
5
6const POLL_MS = 5000
7const BRANCH_MARK = '⎇'
8const TITLE_ENV = 'CLAUDE_TITLE_BAR'
9const SEPARATOR = ' │ '
10const SEPARATOR_GAP = ' '
11const SESSION_CELLS = 32
12// The engine's own pill, as its line reads once flattened to `hint`.
13const PR_PILL = /\b(PR #|MR !)(\d+)\b/
14const REMOTE = /^(?:\w+:\/\/)?(?:[^@/]+@)?([^:/]+)(?::\d+\/|[:/])(.+?)(?:\.git)?\/?$/
15
16const bar = atom({ plugin: 'title-bar', key: 'bar' } as const, null)
17
18let folder = ''
19let sessionId = ''
20let transcriptPath: string | undefined
21let hookTitle: string | undefined
22let session: string | undefined
23let shown: string | undefined
24let repo: string | undefined
25
26export const textOf = ({ session, folder, branch }: Bar): string =>
27 branch === undefined
28 ? `${session} - ${folder}`
29 : `${session} - ${folder} ${BRANCH_MARK} ${branch}`
30
31// The repository's web address, from either spelling of its remote (scp-like or URL).
32export const webOf = (remote: string): string | undefined => {
33 const found = REMOTE.exec(remote)
34
35 return found === null ? undefined : `https://${found[1]}/${found[2]}`
36}
37
38// Drawing our own tree flattens the engine's pill to text: this finds it again to link it.
39export const pillIn = (
40 hint: string,
41 repo: string | undefined,
42): { before: string; label: string; href: string; after: string } | undefined => {
43 const found = PR_PILL.exec(hint)
44
45 if (found === null || repo === undefined) return undefined
46
47 const path = found[1] === 'MR !' ? '-/merge_requests' : 'pull'
48
49 return {
50 before: hint.slice(0, found.index),
51 label: found[0],
52 href: `${repo}/${path}/${found[2]}`,
53 after: hint.slice(found.index + found[0].length),
54 }
55}
56
57const clip = (text: string): string =>
58 text.length > SESSION_CELLS ? `${text.slice(0, SESSION_CELLS - 1)}…` : text
59
60// A name the person gave (/rename) outranks the one the engine generated.
61export const titlesIn = (transcript: string): { custom?: string; ai?: string } => {
62 const found: { custom?: string; ai?: string } = {}
63
64 for (const line of transcript.split('\n')) {
65 if (!line.includes('-title"')) continue
66
67 try {
68 const row = JSON.parse(line)
69
70 if (row.type === 'custom-title' && typeof row.customTitle === 'string') {
71 found.custom = row.customTitle
72 } else if (row.type === 'ai-title' && typeof row.aiTitle === 'string') {
73 found.ai = row.aiTitle
74 }
75 } catch {
76 // A row still being written: the next refresh reads it whole.
77 }
78 }
79
80 return found
81}
82
83const git = async ($: Engine, ...args: string[]): Promise<string | undefined> => {
84 try {
85 const { exitCode, stdout } = await $.process.run(['git', ...args], { timeoutMs: 3000 })
86
87 return exitCode === 0 ? stdout.trim() : undefined
88 } catch {
89 return undefined
90 }
91}
92
93const branchOf = async ($: Engine): Promise<string | undefined> => {
94 const branch = await git($, 'branch', '--show-current')
95
96 if (branch === undefined) return undefined
97 if (branch !== '') return branch
98
99 // Detached HEAD: no branch name, so show the commit instead.
100 const commit = await git($, 'rev-parse', '--short', 'HEAD')
101
102 return commit === undefined || commit === '' ? undefined : `@${commit}`
103}
104
105const sessionOf = async ($: Engine): Promise<string> => {
106 let titles: { custom?: string; ai?: string } = {}
107
108 try {
109 // Rejects past 4 MiB: a long session keeps the name it last read.
110 if (transcriptPath !== undefined) titles = titlesIn(await $.fs.read(transcriptPath))
111 } catch {
112 if (session !== undefined) return session
113 }
114
115 return titles.custom ?? hookTitle ?? titles.ai ?? sessionId.slice(0, 8)
116}
117
118// The engine has no call for the window title, so a child process sets it.
119const setWindowTitle = async ($: Engine, text: string): Promise<void> => {
120 const init = { env: { [TITLE_ENV]: text }, timeoutMs: 15_000 }
121
122 try {
123 if ((await $.env.get('OS')) === 'Windows_NT') {
124 const script = `${$.plugin.root}/scripts/set-title.ps1`
125
126 await $.process.run(
127 ['powershell.exe', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', script],
128 init,
129 )
130 } else {
131 await $.process.run(['sh', '-c', `printf '\\033]0;%s\\007' "$${TITLE_ENV}" > /dev/tty`], init)
132 }
133 } catch {
134 // The band still shows it.
135 }
136}
137
138const refresh = async ($: Engine, hasNewTitle: boolean): Promise<void> => {
139 const branch = await branchOf($)
140
141 if (hasNewTitle || session === undefined) session = await sessionOf($)
142
143 const next: Bar = { session, folder, branch, repo }
144 const text = textOf(next)
145
146 if (text === shown) return
147
148 shown = text
149 await update($, bar, () => next)
150 void setWindowTitle($, text)
151}
152
153export const register: Register = on => {
154 on('session.start', async ($, e, next) => {
155 const started = await next(e)
156 const home = (await $.env.get('USERPROFILE')) ?? (await $.env.get('HOME')) ?? ''
157 const config = (await $.env.get('CLAUDE_CONFIG_DIR')) ?? `${home}/.claude`
158
159 folder = e.cwd.split(/[\\/]/).filter(Boolean).pop() ?? e.cwd
160 sessionId = await $.session.id()
161 transcriptPath ??= `${config}/projects/${e.cwd.replace(/[^a-zA-Z0-9]/g, '-')}/${sessionId}.jsonl`
162
163 // The engine's own title (the topic, a spinner while working) would overwrite this one.
164 await $.env.set('CLAUDE_CODE_DISABLE_TERMINAL_TITLE', '1').catch(() => undefined)
165 repo = webOf((await git($, 'remote', 'get-url', 'origin')) ?? '')
166 await refresh($, true)
167 // A checkout made outside the session (another terminal, an IDE) raises no event here.
168 $.clock.every(POLL_MS, () => void refresh($, false))
169
170 return started
171 })
172
173 on('classic.UserPromptSubmit', async ($, e, next) => {
174 transcriptPath = e.transcript_path || transcriptPath
175 hookTitle = e.session_title ?? hookTitle
176 await refresh($, true)
177
178 return next(e)
179 })
180
181 on('turn.complete', async ($, e, next) => {
182 const completed = await next(e)
183 await refresh($, true)
184
185 return completed
186 })
187
188 on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
189 const ran = await next(e)
190 await refresh($, false)
191
192 return ran
193 })
194
195 on('ui.render', { component: 'PromptHint' }, async ($, e, next) => {
196 const title = await read($, bar)
197
198 if (title === null) {
199 return next(e)
200 }
201
202 const { Box, Text, Link } = $.ui.resolve(e)
203 const rule = <Text dimColor>{SEPARATOR}</Text>
204 const pill = pillIn(e.props.hint, title.repo)
205
206 return (
207 <Box>
208 <Text wrap="truncate">
209 <Text bold>{clip(title.session)}</Text>
210 {rule}
211 <Text color="cyan">{title.folder}</Text>
212 {title.branch === undefined ? null : (
213 <Text color="success">
214 {SEPARATOR_GAP}
215 {BRANCH_MARK} {title.branch}
216 </Text>
217 )}
218 {e.props.hint === '' ? null : rule}
219 {pill === undefined ? (
220 <Text dimColor>{e.props.hint}</Text>
221 ) : (
222 <Text>
223 <Text dimColor>{pill.before}</Text>
224 <Link href={pill.href}>{pill.label}</Link>
225 <Text dimColor>{pill.after}</Text>
226 </Text>
227 )}
228 </Text>
229 </Box>
230 )
231 })
232}
233types/index.d.ts 13 lines1export type Bar = {
2 session: string
3 folder: string
4 branch?: string
5 repo?: string
6}
7
8declare module 'claude-code' {
9 interface PluginState {
10 'title-bar': { bar: Bar | null }
11 }
12}
13