SLOPSHOPPER

session-switcher

A sidebar listing this project's sessions; click one to resume it

panebandcommandtoastprocess
★ 4v0.4.1no licenseupdated 2026-10-10ha-ptt0601/cc-mods/session-switcher
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · session-switcher
│ ┃ Sessions ✕ › fix the failing auth test and add an audit log call │ ┃ ☰ Sessions loading… │ ┃ r: Refresh x: Close s: Search ⏺ Read(src/auth.ts) │ ┃ Search: session name or worktree ⏎ resume fi ⎿ Read 6 lines │ ┃ type /sessions (or ctrl+x tab) to use the ⏺ Update(src/auth.ts) │ ┃ keys ⎿ Added 2 lines, removed 1 line │ ┃ No sessions found in ⏺ Bash(bun test) │ ┃ dir=/.claude/projects/-work-app ⎿ 3 pass, 1 fail │ │ ● Done. refresh now rejects expired claims and logs an audit event. │ │ ✻ Worked for 42s · done 4:20 PM │ │ › /sessions │ ⎿ session-switcher: Sessions: press 1-9 or Enter to resume, x to c │ │ ⟨Claude Code's own drawing⟩ ☰ Sessions ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
⟨Claude Code's own drawing⟩ ☰ Sessions
Pane · Sessions
☰ Sessions loading… r: Refresh x: Close s: Search Search: session name or worktree ⏎ resume first type /sessions (or ctrl+x tab) to use the keys No sessions found in dir=/.claude/projects/-work-app
README

cc-mods

Small Claude Code mods: function-hook plugins that add panes, bands and commands inside Claude Code.

Mods

session-switcher

A sidebar listing the current project's sessions. Pick one to resume it.

The Sessions pane beside a conversation (example data)

  • ☰ Sessions (12) above the prompt, or /sessions, opens the pane with the keyboard on it
  • The pane's header counts the sessions, or the matches while searching (3 / 12)
  • 1–9 or Enter resumes a session, r refreshes, x closes, Esc goes back to the prompt
  • s jumps to the search field: typing filters the list by session name or worktree (case and diacritics ignored, so y tuong finds Ý tưởng), Enter resumes the first match
  • /sessions close hides the pane from the prompt
  • Sessions from the project's git worktrees are listed too, tagged with the worktree's name in cyan ([feat-search] 12m ago, lined up at the right edge)
  • A session is titled by its custom title, its AI title, or its first typed prompt; one with none of them is left out
  • The open session is marked ▸ (this session)

file-tree

An IDE-style file tree beside the conversation, showing what Claude reads, edits and commits.

The file tree in its three styles (example data)

  • ▤ Files above the prompt, or /tree, opens the pane (/tree close hides it); it docks beside the transcript in the fullscreen layout ("tui": "fullscreen" in settings, a terminal 110+ columns wide), else above the prompt
  • Click the tree once, then ↑/↓ move, → opens a folder, ← closes it, Enter or a double-click previews a file
  • Files Claude reads light up purple, edits orange, commits green; folders open to them
  • Git status per file and folder, +N -N line counts, branch and ahead/behind in the header
  • s searches every file, opened folders or not (case and diacritics ignored)
  • t cycles three styles: ide, minimal, rich
  • The preview draws Markdown or highlighted code; ↗ opens the file with your Open with command (code -g {path}, phpstorm {path}) or the default app
  • Settings in /config → file-tree: Style, Preview layout (replace or split), Open with, Follow Claude, Icons (nerd or unicode), Show ignored files

Install

/plugin install session-switcher --marketplace ha-ptt0601/cc-mods
/plugin install file-tree --marketplace ha-ptt0601/cc-mods

Answer y to add the marketplace, then pick the user scope.

Develop

Run a mod straight from its folder, reloading on save:

claude --plugin-dir ./file-tree     # or ./session-switcher

Check it:

claude plugin validate ./file-tree
claude plugin test ./file-tree
Source 2 files
hooks/register.tsx 342 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { SessionRow } from '../types'
5
6const PANE = 'sessions'
7const LIMIT = 30
8const sessions = atom({ plugin: 'session-switcher', key: 'sessions' } as const, [])
9const isLoading = atom({ plugin: 'session-switcher', key: 'isLoading' } as const, false)
10const note = atom({ plugin: 'session-switcher', key: 'note' } as const, '')
11const query = atom({ plugin: 'session-switcher', key: 'query' } as const, '')
12
13/**
14 * The text of a transcript's user message, or '' when it is not a typed prompt
15 * (a tool result, a command's output, a hook's injected context).
16 *
17 * @param line one JSONL line of a transcript
18 */
19export function promptText(line: string): string {
20  try {
21    const row = JSON.parse(line) as { type?: string; isMeta?: boolean; message?: { content?: unknown } }
22    if (row.type !== 'user' || row.isMeta) return ''
23    const content = row.message?.content
24    const text = typeof content === 'string'
25      ? content
26      : Array.isArray(content)
27        ? content.map(b => (b && typeof b === 'object' && 'text' in b ? String(b.text) : '')).join(' ')
28        : ''
29    const trimmed = text.trim()
30
31    return trimmed.startsWith('<') ? '' : trimmed.replace(/\s+/g, ' ')
32  } catch {
33    return ''
34  }
35}
36
37/**
38 * The value of `key` on a JSONL line, or '' when the line does not parse.
39 */
40function field(line: string, key: string): string {
41  try {
42    return String((JSON.parse(line) as Record<string, unknown>)[key] ?? '')
43  } catch {
44    return ''
45  }
46}
47
48/**
49 * A transcript's title: its last custom title, else its last AI title, else
50 * its first typed prompt; '' when it has none of them (nothing worth listing).
51 * A session started by a slash command has no typed prompt but is titled.
52 *
53 * @param titleLines the transcript's custom-title and ai-title lines
54 * @param userLines the transcript's first user lines
55 */
56export function sessionTitle(titleLines: string[], userLines: string[]): string {
57  const custom = titleLines.map(l => field(l, 'customTitle')).filter(Boolean).at(-1)
58  const ai = titleLines.map(l => field(l, 'aiTitle')).filter(Boolean).at(-1)
59
60  return custom || ai || userLines.map(promptText).find(Boolean) || ''
61}
62
63/**
64 * Groups `grep -H` output (`path:line`) by the transcript's session id.
65 */
66function byFile(stdout: string): Map<string, string[]> {
67  const found = new Map<string, string[]>()
68  for (const hit of stdout.split('\n')) {
69    const at = hit.indexOf('.jsonl:')
70    if (at < 0) continue
71    const id = hit.slice(0, at).split('/').pop() ?? ''
72    found.set(id, [...(found.get(id) ?? []), hit.slice(at + 7)])
73  }
74
75  return found
76}
77
78/**
79 * The paths of every worktree in `git worktree list --porcelain` output, the
80 * main one first.
81 */
82export function worktreePaths(porcelain: string): string[] {
83  return porcelain
84    .split('\n')
85    .filter(line => line.startsWith('worktree '))
86    .map(line => line.slice('worktree '.length).trim())
87    .filter(Boolean)
88}
89
90/**
91 * The folder Claude Code keeps a directory's transcripts in.
92 */
93export function projectDir(home: string, path: string): string {
94  return `${home}/.claude/projects/${path.replace(/[^a-zA-Z0-9]/g, '-')}`
95}
96
97/**
98 * Lists this project's sessions, its worktrees' included, newest first: one
99 * row per transcript that holds at least one typed prompt, titled by its
100 * custom or AI title.
101 */
102async function loadSessions($: EngineInterface, trace: string[]): Promise<SessionRow[]> {
103  const root = await $.session.root()
104  const env = await $.process.run(['printenv', 'HOME']).catch(() => ({ stdout: '' }))
105  const home = env.stdout.trim() || (/^\/(?:Users|home)\/[^/]+/.exec(root)?.[0] ?? '')
106  const git = await $.process.run(['git', 'worktree', 'list', '--porcelain'], { cwd: root })
107    .catch(() => ({ exitCode: 1, stdout: '' }))
108  const listed = git.exitCode === 0 ? worktreePaths(git.stdout) : []
109  const trees = listed.includes(root) ? listed : [root, ...listed]
110  trace.push(`root=${root}`, `home=${home}`, `dir=${projectDir(home, root)}`, `worktrees=${trees.length}`)
111
112  const found = await Promise.all(trees.map(async tree => {
113    const dir = projectDir(home, tree)
114    if (!(await $.fs.exists(dir))) return []
115
116    return (await $.fs.list(dir))
117      .filter(f => f.kind === 'file' && f.name.endsWith('.jsonl'))
118      .map(f => ({ ...f, path: `${dir}/${f.name}`, worktree: tree === root ? undefined : tree.split('/').pop() }))
119  }))
120  const files = found.flat().sort((a, b) => b.mtimeMs - a.mtimeMs).slice(0, LIMIT)
121  trace.push(`transcripts=${files.length}`)
122  if (files.length === 0) return []
123
124  const paths = files.map(f => f.path)
125  const grep = async (pattern: string, max: string) =>
126    byFile((await $.process.run(['grep', '-H', '-m', max, '-e', pattern, ...paths])).stdout)
127  const [titles, prompts] = await Promise.all([
128    grep('"type":"\\(custom-title\\|ai-title\\)"', '50'),
129    grep('"type":"user"', '20'),
130  ])
131  trace.push(`titled=${titles.size}`, `with prompts=${prompts.size}`)
132
133  return files.flatMap(f => {
134    const id = f.name.slice(0, -'.jsonl'.length)
135    const title = sessionTitle(titles.get(id) ?? [], prompts.get(id) ?? [])
136
137    return title ? [{ id, title, mtimeMs: f.mtimeMs, worktree: f.worktree }] : []
138  })
139}
140
141/**
142 * Lowercase text with Vietnamese and other diacritics removed, so `y tuong`
143 * finds `Ý tưởng`.
144 */
145function fold(text: string): string {
146  return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/đ/gi, 'd').toLowerCase()
147}
148
149/**
150 * Whether a session matches a search: every word of `search` is in its title
151 * or its worktree's name, ignoring case and diacritics. An empty search
152 * matches every session.
153 */
154export function matches(row: Pick<SessionRow, 'title' | 'worktree'>, search: string): boolean {
155  const haystack = fold(`${row.title} ${row.worktree ?? ''}`)
156
157  return fold(search).split(/\s+/).filter(Boolean).every(word => haystack.includes(word))
158}
159
160/**
161 * Resumes a session, saying so in a toast.
162 */
163function resume($: EngineInterface, row: SessionRow) {
164  $.ui.toast(`Resuming: ${row.title.slice(0, 40)}`)
165  void $.command.run({ command: 'resume', args: row.id })
166}
167
168/**
169 * How long ago `ms` was, in the largest whole unit: `5m`, `3h`, `2d`.
170 */
171export function ago(ms: number, now: number): string {
172  const minutes = Math.max(0, Math.round((now - ms) / 60000))
173  if (minutes < 60) return `${minutes}m`
174  if (minutes < 60 * 24) return `${Math.round(minutes / 60)}h`
175
176  return `${Math.round(minutes / 60 / 24)}d`
177}
178
179/**
180 * The count in the pane's header: `12 sessions`, or `3 / 12` while a search
181 * filters the list.
182 */
183export function countLabel(shown: number, total: number, search: string): string {
184  if (search.trim() !== '') return `${shown} / ${total}`
185
186  return `${total} session${total === 1 ? '' : 's'}`
187}
188
189/**
190 * The label of the button above the prompt: `☰ Sessions (12)`, with no
191 * count until the list has loaded.
192 */
193export function bandLabel(count: number): string {
194  return count > 0 ? `☰ Sessions (${count})` : '☰ Sessions'
195}
196
197/**
198 * Reloads the session list into state, flagging the load while it runs.
199 */
200async function refresh($: EngineInterface) {
201  const trace: string[] = []
202  await update($, isLoading, () => true)
203  try {
204    const rows = await loadSessions($, trace)
205    await update($, sessions, () => rows)
206    await update($, note, () => (rows.length === 0 ? `No sessions found in ${trace[2] ?? '?'}` : ''))
207  } catch (error) {
208    trace.push(`error: ${String(error)}`)
209    await update($, note, () => `Could not list sessions: ${String(error)}`)
210  } finally {
211    await update($, isLoading, () => false)
212  }
213}
214
215/**
216 * Opens the sessions pane, or brings an open one forward, with the keyboard
217 * on it and the list reloaded.
218 *
219 * @returns a line saying what to press next
220 */
221async function show($: EngineInterface): Promise<string> {
222  await update($, query, () => '')
223  await $.ui.open({ id: PANE, title: 'Sessions', focus: true, columns: 60 })
224  void refresh($)
225
226  return 'Sessions: press 1-9 or Enter to resume, x to close, Esc to go back (/sessions close hides it).'
227}
228
229export const register: Register = on => {
230  on('session.start', async ($, e, next) => {
231    const started = await next(e)
232    await $.command.register({
233      name: 'sessions',
234      description: "Open this project's sessions and pick one to resume (close: hide it)",
235      argumentHint: '[close]',
236    })
237    void refresh($)
238
239    return started
240  })
241
242  on('command.run', { command: 'sessions' }, async ($, e) => {
243    if (/^(close|hide|x)$/i.test(e.args.trim())) {
244      await $.ui.close({ id: PANE })
245
246      return { text: 'Sessions pane closed.' }
247    }
248
249    return { text: await show($) }
250  })
251
252  // The "icon": one small button above the prompt that opens the pane, drawn
253  // beside what other plugins put there, not over it.
254  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
255    if (e.props.hasSurvey) return next(e)
256    const below = await next(e)
257    const { Box, Button } = $.ui.resolve(e)
258
259    return (
260      <Box gap={2}>
261        {below}
262        <Button key="toggle" plain label={bandLabel((await read($, sessions)).length)} dimColor onPress={() => void show($)} />
263      </Box>
264    )
265  })
266
267  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
268    const { Box, Button, Input, Text } = $.ui.resolve(e)
269    const search = await read($, query)
270    const all = await read($, sessions)
271    const rows = all.filter(row => matches(row, search))
272    const current = await $.session.id()
273    const now = await $.clock.now()
274    const width = Math.max(10, (e.props.bodyColumns ?? 40) - 6)
275    const others = rows.filter(row => row.id !== current)
276
277    return (
278      <Box flexDirection="column">
279        <Box justifyContent="space-between">
280          <Text bold color="magenta">☰ Sessions</Text>
281          {(await read($, isLoading))
282            ? <Text dimColor>loading…</Text>
283            : <Text bold>{countLabel(rows.length, all.length, search)}</Text>}
284        </Box>
285        <Box gap={2}>
286          <Button key="refresh" plain hotkey="r" label="Refresh" dimColor onPress={() => void refresh($)} />
287          <Button key="close" plain hotkey="x" label="Close" dimColor onPress={() => void $.ui.close({ id: PANE })} />
288          <Button key="find" plain hotkey="s" label="Search" dimColor onPress={() => void $.ui.focus({ requestId: PANE, key: 'search' })} />
289        </Box>
290        <Input
291          key="search"
292          label="Search"
293          placeholder="session name or worktree"
294          value={search}
295          submitLabel="resume first"
296          onInput={value => void update($, query, () => value)}
297          onSubmit={value => {
298            const first = others.find(row => matches(row, value))
299            if (first) resume($, first)
300          }}
301        />
302        {e.props.isFocused
303          ? <Text dimColor>1-9 resume · s search · r refresh · x close · Esc back</Text>
304          : <Text dimColor>type /sessions (or ctrl+x tab) to use the keys</Text>}
305        {(await read($, note)) !== '' && <Text color="yellow">{await read($, note)}</Text>}
306        {search !== '' && rows.length === 0 && <Text dimColor>No session matches "{search}"</Text>}
307        {rows.map(row => {
308          const isCurrent = row.id === current
309          const when = `${row.worktree ? `[${row.worktree}] ` : ''}${ago(row.mtimeMs, now)} ago`
310          const title = row.title.slice(0, Math.max(5, width - when.length - 4))
311          if (isCurrent) {
312            return (
313              <Box key={row.id} justifyContent="space-between" gap={2}>
314                <Text color="green" bold>▸ {title}</Text>
315                <Text dimColor>(this session)</Text>
316              </Box>
317            )
318          }
319          const slot = others.indexOf(row)
320
321          return (
322            <Box key={row.id} justifyContent="space-between" gap={2}>
323              <Button
324                key={row.id}
325                plain
326                hotkey={slot < 9 ? String(slot + 1) : undefined}
327                autoFocus={slot === 0 ? true : undefined}
328                label={slot < 9 ? title : `   ${title}`}
329                onPress={() => resume($, row)}
330              />
331              <Box flexShrink={0}>
332                {row.worktree && <Text color="cyan">[{row.worktree}] </Text>}
333                <Text dimColor>{ago(row.mtimeMs, now)} ago</Text>
334              </Box>
335            </Box>
336          )
337        })}
338      </Box>
339    )
340  })
341}
342
types/index.d.ts 8 lines
1export type SessionRow = { id: string; title: string; mtimeMs: number; worktree?: string }
2
3declare module 'claude-code' {
4  interface PluginState {
5    'session-switcher': { sessions: SessionRow[]; isLoading: boolean; note: string; query: string }
6  }
7}
8