A small, tested dotfiles framework: symlink-managed zsh, tmux, Ghostty, Git and Neovim config, with safe backups, a doctor, and a privacy scan.

English | 简体中文
A terminal workbench for AI coding agents. Claude Code, Codex, Gemini CLI, Grok CLI and the like are command-line programs that read and write plain text, so the best place to run them is a terminal you control: tmux sessions per project, an editor one key away from the agent, ripgrep, fzf and jq for text, and every setting in a Git repository you can read, diff and fork. This repository is that environment, deployed with chezmoi.
prefix + P -> pick a project -> one tmux session, one window per repository
┌───────────────────────────────────┐
│ claude / codex / gemini / grok │ the first installed agent starts here,
│ │ in the project's directory
├───────────────────────────────────┤
│ shell │
└───────────────────────────────────┘
prefix + e / prefix + g: Neovim in a popup, to browse code or review changes
| Project → session, agent included | A directory under ~/dev/projects is a workspace. A folder holding several repositories is one workspace with a window per repository, each with its own agent pane. Nothing to register: it is discovered from the directory tree. Auto-detects claude, codex, gemini, grok (extend the list with WORKSPACE_SWITCH_AGENTS, pick one with WORKSPACE_SWITCH_AGENT, turn off with none). |
| Plain text all the way down | Settings, history, notes and agent instructions are files. rg, fzf, jq, nvim and git are the tools; there is no GUI state and no database to lose. One instruction text is deployed to every agent (CLAUDE.md, AGENTS.md, GEMINI.md), with your private rules appended from an untracked file. |
| Safe to make public | Agents need API keys, and keys leak through dotfiles. scripts/privacy-scan runs as a pre-commit hook and again in CI, and recognises Anthropic (Claude), OpenAI, Google (Gemini) and xAI (Grok) keys, GitHub and AWS tokens, private keys, personal paths and e-mail addresses. A pre-push hook scans commit metadata (author, committer, message) too. tmux pane contents are not saved to disk by default. Secrets live in an untracked file. |
| Reversible | Before every chezmoi apply, the files it would replace are backed up to ~/.cli-workbench-backup/ with a restore note. |
| The promises are tested | The quick start below is run end to end in a throwaway home directory on macOS and Ubuntu. The agent pane (with stand-in agents), the backup and the privacy scanner each have their own tests. |
Agent-specific today: the workspace switcher starts any of the four agents; the shared instruction text reaches Claude Code, Codex and Gemini CLI (Grok CLI is not wired up: the location of its instruction file is not known); the status line (~/.claude/statusline.sh) and the mods (below) are for Claude Code only. Full agent settings files are deliberately not tracked. Codex's portable UI preferences are merged into its local configuration, see below. The agents themselves are not installed by this repository.
What this is: a personal-dotfiles repository laid out as a chezmoi source tree. You fork it, edit
home/, and keep it as your own.What this is not: a package manager, a one-click installer, a theme pack, or an agent framework. It does not install software and it does not manage secrets.
| Platform | macOS is the primary platform (tested on macOS 26, Apple Silicon). The zsh and tmux configs also pass the full test suite and the first-run flow on Ubuntu 24.04 (checked in a container); the Ghostty config and the Homebrew casks are macOS-oriented. Windows is not supported. |
| Shell | zsh 5.8 or newer (tested with 5.9). The helper scripts are bash 3.2 compatible, i.e. the macOS system bash is enough. |
| tmux | 3.2 or newer recommended (tested with 3.5a). Older versions still load the config, minus the workspace switcher key. |
| Required | git and chezmoi (brew install chezmoi) |
| Optional | fzf (0.48+ for the shell integration), zoxide, starship, zsh-syntax-highlighting, jq (status line), translate-shell (the translation popup), Ghostty and a Nerd Font, Homebrew |
What it changes on your machine: exactly the files under home/ that chezmoi deploys (the table below), a zsh completion cache in ~/.cache/zsh/, and backups of the files it replaces in ~/.cli-workbench-backup/<timestamp>/ (chezmoi itself keeps none; see Safety model).
brew install chezmoi
git clone https://github.com/dishangyijiao/cli-workbench.git ~/dev/cli-workbench # or your fork; any location works
chezmoi init --source ~/dev/cli-workbench # use this clone as the source, and install the backup hook (below)
chezmoi diff # read-only: what would change in $HOME
chezmoi apply ~/.tmux.conf # apply ONE file first, then look at the result
chezmoi apply # then everything
Or let chezmoi do the clone: chezmoi init <your-github-user>/cli-workbench, then the same diff and apply.
What gets deployed (the layout follows chezmoi's naming: dot_ becomes ., executable_ sets the mode, private_ makes the directory mode 700):
Source in home/ | Target | Notes |
|---|---|---|
dot_tmux.conf, dot_tmux/scripts/ | ~/.tmux.conf, ~/.tmux/scripts | prefix Ctrl-a, vi keys, mouse, workspace switcher with the agent pane |
dot_zshrc, dot_config/private_zsh/ | ~/.zshrc, ~/.config/zsh/{path,tmux-autostart}.zsh | replaces your .zshrc; move your own tweaks to ~/.config/zsh/local.zsh first. Installers (nvm, bun, ...) append to ~/.zshrc; chezmoi diff shows that, so move such lines into local.zsh |
dot_config/ghostty/config | ~/.config/ghostty/config | Catppuccin Mocha, Nerd Font, macOS tabs title bar |
dot_claude/executable_statusline.sh | ~/.claude/statusline.sh | the Claude Code status line (project, branch, model, context, cost, rate limits); only if you use Claude Code |
dot_claude/workbench-mods/ | ~/.claude/workbench-mods/ | four Claude Code mods (chezmoi-guard, reply-polish, agent-state, md-open) as a local marketplace; deployed, not installed: see "Claude Code mods" |
dot_codex/modify_private_config.toml | ~/.codex/config.toml | merge portable status-line and completion-bell preferences; preserve other local values |
.chezmoitemplates/agent-instructions.md, dot_claude/CLAUDE.md.tmpl, dot_codex/AGENTS.md.tmpl, dot_gemini/GEMINI.md.tmpl | ~/.claude/CLAUDE.md, ~/.codex/AGENTS.md, ~/.gemini/GEMINI.md | the same short text for every agent: how this workbench works (config lives in the repo, secrets stay out, one tmux session per project). Your own rules: see "Agent instructions" |
dot_config/git/config | ~/.config/git/config | portable Git settings; Git reads this file by itself, and ~/.gitconfig (identity, credentials) stays yours |
dot_config/nvim/ | ~/.config/nvim | optional Neovim setup (lazy.nvim, LSP, Telescope, Git, debugging; no AI plugin: agents run in their own pane); plugins install on first launch and need network access. Delete the directory from your fork if you have your own |
Suggested order and the manual steps (Homebrew tools, tmux plugin manager) are in docs/bootstrap.md.
These are opinions, not requirements. Edit the files in home/ and run chezmoi apply (or chezmoi edit --apply ~/.tmux.conf).
Ctrl-a (not Ctrl-b); vi-style copy mode; mouse on; windows numbered from 1. Pane contents are not saved to disk by default (that would store anything printed in a pane, tokens included); see the comment next to @resurrect-capture-pane-contents to turn it on.starship, zoxide, fzf and zsh-syntax-highlighting only if installed.SauceCodePro Nerd Font Mono (install the font or change the line).templates/local.zsh.example to ~/.config/zsh/local.zsh. chezmoi does not manage it and it is loaded last.templates/secrets.zsh.example to ~/.config/zsh/secrets.zsh, mode 600. Never commit it. The repository must never contain keys or tokens.chezmoi add ~/.config/<tool>/config copies the file into home/; commit it.lazy-lock.json after :Lazy update): the copy in $HOME changes, the repository does not. chezmoi diff shows it; chezmoi re-add pulls it back into home/.home/dot_config/nvim from your fork.chezmoi diff | status | verify # what differs between home/ and $HOME (verify exits non-zero if anything does)
chezmoi doctor # chezmoi's own health check
tests/run.sh # the repository's tests, including an end-to-end run of this quick start in a throwaway HOME
scripts/privacy-scan [--all] # secrets, personal paths and e-mail addresses in staged (or all tracked) files, or in commit metadata (--commits)
scripts/lint-shell # shellcheck (warning level and above) over every bash/sh script in the repository
chezmoi diff and chezmoi apply --dry-run change nothing.chezmoi init installs a hook (scripts/backup-before-apply) that copies every file the apply is about to replace, including files you edited after chezmoi wrote them, to ~/.cli-workbench-backup/<timestamp>/ first. Directory mode 700, symlinks kept as symlinks, a RESTORE note with one copy-paste command per file. If nothing would change, nothing is created. If the backup fails, the apply is refused. --dry-run has no side effects.chezmoi init writes. If you only create chezmoi.toml by hand, or run chezmoi apply --source ... without having run init, there is no backup.chezmoi destroy).scripts/privacy-scan guards what you commit; see "Safe to make public" above.Claude Code, Codex and Gemini CLI each read a plain-text instruction file from their home directory. Here they are generated from one source, home/.chezmoitemplates/agent-instructions.md, so the agents get the same facts about the machine and the same engineering principles: one source of truth with layers that only add differences, least privilege, files over conversation, the simplest thing that works, and evidence before "done" with a way back.
~/.config/cli-workbench/agent-instructions.local.md. It is not tracked, so personal preferences never reach a public fork. It is appended after the shared text in all three files. Remove the file and the next chezmoi apply removes its text.~/.claude/CLAUDE.md (and the others) are replaced on apply and backed up first. Move their content into the local file beforehand if you want to keep it as it is.~/.claude/CLAUDE.md is overwritten at the next apply. The generated files cannot be pulled back with chezmoi re-add.settings.json, config.toml, auth.json, histories, sessions and databases. They hold machine paths, proxies and credentials, and the tools rewrite them. A test fails if such a file appears under home/.home/dot_codex/modify_private_config.toml merges five UI settings into ~/.codex/config.toml: the status line (model, directory, session name, five-hour and weekly remaining limits), status-line colors, and completion notifications using a terminal bell regardless of focus. With the existing tmux bell settings, Ghostty can mark the background tab with a bell.
On a new machine, install Codex separately and run the usual chezmoi init / chezmoi apply setup, then sign in to Codex. The configuration is created if absent. Existing model choices, local paths, notification commands, project trust, and other settings are preserved; credentials and sessions are not migrated. Restart an existing Codex CLI after applying (use codex resume --last to continue).
Edit the merge template to change these shared preferences. Changes to these five settings made inside Codex are replaced by the next apply. Do not use chezmoi add or re-add to copy the full local file into the repository. When values need changing, the TOML is reserialized, so comments and formatting are lost; matching files remain unchanged. The existing pre-apply backup hook keeps the original before replacement. Invalid TOML stops the merge without overwriting the file.
A mod is a Claude Code plugin whose behavior is a small TypeScript file that Claude Code calls when something happens: a tool is about to run, a reply is about to be drawn. This repository ships four, as a local marketplace (a folder Claude Code installs plugins from) in home/dot_claude/workbench-mods/:
| Mod | What it does |
|---|---|
chezmoi-guard | Refuses Edit, Write and NotebookEdit on a file chezmoi deploys (~/.zshrc, ~/.tmux.conf, ...) and names the source file to edit instead, so the next chezmoi apply cannot overwrite the change. Shows a line above the prompt while the deployed files differ from the source (chezmoi status). It cannot see edits made through Bash (sed -i, > file). If chezmoi is missing or fails, it blocks nothing. |
agent-state | Records, in one small file per tmux pane, whether the agent in it is working, waiting for you or idle, so that the overview popup (see "Agent overview") can list them. It runs ~/.tmux/scripts/agent-state.sh on Claude Code events and changes nothing else: outside tmux, or if the script fails, nothing happens, and a hook waits for the script for at most one second. |
md-open | Adds /md: a pane lists the Markdown files this conversation mentioned (paths in tool calls, Bash commands, replies, your prompts and tool results; absolute, ~/ and relative to the session's directory), newest first, each once, only those that exist, with ~ for your home directory. Pick one with Enter or its digit (1-9) and it opens read-only (nvim -R) in a tmux popup, rendered by render-markdown.nvim; quitting Neovim closes the popup. The path goes to tmux as an argument, never through a shell. Outside tmux, or when tmux fails, a toast shows the path instead. It needs no model turn, and changes no file. |
reply-polish | Lays out the assistant's replies for a wide terminal. The text is one column, at most 80 cells (about 40 Chinese characters) and at most 72% of the window, left-aligned and centered on the screen. Headings are bold, the first two levels in cyan; lists use • and ◦; a table is drawn with box lines when it fits the column and becomes a list when it does not; lines are cut so that a number stays with its unit and closing punctuation never starts a line; a code block longer than 30 lines is shortened to 12. Only the drawing changes: the stored reply, and ctrl+o, keep the original. |
Install once per machine (needs Claude Code 2.1.287 or later; chezmoi apply only deploys the files, it does not install anything):
chezmoi apply # deploys ~/.claude/workbench-mods
claude plugin marketplace add ~/.claude/workbench-mods
claude plugin install chezmoi-guard@cli-workbench --scope user
claude plugin install reply-polish@cli-workbench --scope user
claude plugin install agent-state@cli-workbench --scope user
claude plugin install md-open@cli-workbench --scope user
Then restart Claude Code, or run /reload-plugins in a running session.
home/dot_claude/workbench-mods/, run chezmoi apply, then /reload-plugins. A marketplace that is a folder is read from the folder itself, so no version bump is needed. claude plugin disable <name> turns one off, claude plugin uninstall <name> removes it.tests/mods.test.sh always checks the layout of the marketplace. With Claude Code installed it also runs claude plugin validate and each mod's own tests (claude plugin test); without it that part is skipped.md-open with 2.1.295). Mods do not load under --safe-mode or --bare..claude-plugin/types/ when it loads the mod.The keys you use most, on one page to print: docs/keybindings.md. A test keeps it in step with the config.
Press prefix then P (Ctrl-a P) for a picker over ~/dev/projects. Every directory directly under a root is a workspace, opened as one tmux session: an AI agent on top, a shell under it. There is no editor pane; prefix e and prefix g open Neovim in a popup when you want to read code (see below). A directory that is not a repository but contains several (a multi-repo product) is one workspace with one window per repository. Choosing an existing workspace only switches to it. It needs tmux 3.2+ and fzf.
claude codex gemini grok is started in the project directory. If none is installed, the window has just a shell. WORKSPACE_SWITCH_AGENT="claude --continue" picks one (with arguments), none turns it off, WORKSPACE_SWITCH_AGENTS="aider claude" changes the candidates and their order. Set these with set-environment -g in home/dot_tmux.conf, next to WORKSPACE_ROOTS.WORKSPACE_ROOTS in home/dot_tmux.conf to scan other directories.home/dot_tmux/scripts/executable_workspace-switch.sh.Read code without leaving the agent's pane. Both keys open Neovim in a popup over the current pane, in that pane's directory; closing Neovim closes the popup and you are back where you were. They need tmux 3.2+.
prefix then e: browse the project with the usual Neovim keys (<leader>ff find a file, <leader>fg search, <leader>e file tree). :qa closes it.<leader>mp switches the rendering off and on. The text is a centred column of about 80 characters, so it stays readable on a wide screen.prefix then g: review changes. A Telescope list of every file the branch changed since it left the default branch (origin/HEAD, else origin/main, main and so on): committed, uncommitted, deleted and untracked files, each marked, with a colored diff as the preview. Enter opens the file in a new tab beside its version at the fork point, in Neovim's own diff mode (a deleted file: its old version beside an empty side) (]c/[c jump between changes). Inside Neovim the same list is :Changes or <leader>gv. :qa closes the popup. Outside a Git repository it says so. No extra plugin is needed.home/dot_tmux/scripts/executable_code-popup.sh; tmux passes it only the pane id, never a directory name.Select some English text with the mouse (or v ... y in copy mode), then press prefix then t (Ctrl-a t). A popup shows a translation, so you do not leave the pane you are reading. One word gives a dictionary entry; anything longer gives its translation. Close the popup with q.
translate-shell (brew install translate-shell; without it the popup says so).LOOKUP_LANG sets the target language (default zh-CN). Details are in the header of home/dot_tmux/scripts/executable_lookup.sh.Write an idea down the moment you have it, without leaving the pane or interrupting the agent. Press prefix then a (Ctrl-a a), type one line, press Enter; the popup closes and you are back where you were. An empty line cancels. In a shell, idea some text does the same (or idea alone to be asked). It needs tmux 3.2+.
~/.config/cli-workbench/inbox.md (private, mode 600, not in this repository), for all projects: - [ ] 2026-10-06 17:42 · ~/dev/projects/foo · the idea. The project is the repository's top directory, for a worktree the repository it belongs to; ? if the pane was gone.- [ ] to - [x], never deleting; afterwards check that every line from before is still there; ask before anything leaves the machine, such as opening a GitHub issue.When several agents work at once, the question is which one needs you. Press prefix then O (Ctrl-a O) for a popup that lists every agent session, one row each: state, project, branch and how long it has been in that state. Waiting comes first (a permission prompt or a question; the longest wait on top), then working, then idle. Enter jumps to that pane (its session, window and pane); Esc closes the popup. With fzf it is a picker you can type into; without it, a numbered menu. Plain text, no colors. It needs tmux 3.2+ and jq.
agent-state mod (see "Claude Code mods") runs ~/.tmux/scripts/agent-state.sh: a turn starts, working; a permission prompt or an MCP question appears, waiting; a turn ends for any reason (an answer, an interrupt, an error), idle; a session starts, idle (a compaction does not count); a session ends, the record is removed. When a tool finishes, fails, is denied, or a dialog is answered while the pane still says waiting, it goes back to working. Without the mod the popup is empty.${XDG_STATE_HOME:-~/.local/state}/cli-workbench/sessions/<tmux server>/<pane id>.json, with state, project, branch, since and session id. The server directory is the tmux server's pid and start time, so a pane id reused after a tmux restart never meets an old record. The last event wins; the file is replaced atomicallhooks/register.ts 124 lines1import type { Register, RenderElement } from 'claude-code'
2
3import { displayWidth, layout, polish, reflow } from './format'
4import type { Segment } from './format'
5
6// Layout rules, in the order of the four design principles.
7//
8// Contrast: headings are bold, the first two levels in the accent colour; table borders are dim,
9// the table header takes the accent colour; body text stays plain.
10// Repetition: one accent colour, one heading style per level, one table style, one reply marker.
11// Alignment: one left edge for everything: text, headings and tables start at the same cell, and a
12// table is never wider than the text column. The column itself is centred on the screen.
13// Proximity: a heading sits right above what it introduces and a blank line apart from what came
14// before; other blocks are one blank line apart.
15const ACCENT = 'cyan'
16// Cells across the text column: about 40 Chinese characters or 80 Latin ones, a comfortable measure.
17const MEASURE = 80
18// The column takes at most this share of the window.
19const FILL = 0.72
20// A nested list item moves in by this many cells.
21const INDENT = 2
22// One Markdown block is at most this long, or the whole tree is refused.
23const MAX_BLOCK = 10000
24// The reply's own marker (the bullet) takes this many cells at the left.
25const MARKER = 2
26
27const text = (props: Record<string, unknown>, content: string): RenderElement =>
28 ({ type: 'Text', props: { wrap: 'truncate-end', ...props }, children: [content] }) as RenderElement
29
30const box = (props: Record<string, unknown>, children: RenderElement[]): RenderElement =>
31 ({ type: 'Box', props, children }) as RenderElement
32
33export const register: Register = on => {
34 // A rewrite changes the drawing only; the stored reply, and ctrl+o, keep the original.
35 on('ui.render', { component: 'AssistantMessage' }, ($, e, next) => {
36 const columns = e.viewport?.columns
37 // At most MEASURE cells, and a share of a narrow window, so the margins on both sides stay visible.
38 const measure = columns === undefined ? undefined : Math.max(30, Math.min(MEASURE, Math.floor(columns * FILL)))
39 const segments = measure === undefined ? [] : layout(e.props.text, measure)
40 const isDrawable = segments.every(segment => segment.kind !== 'text' || segment.text.length <= MAX_BLOCK) &&
41 segments.every(segment => segment.kind !== 'list' || segment.items.every(item => item.text.length <= MAX_BLOCK))
42
43 if (columns === undefined || measure === undefined || !isDrawable) {
44 const rewritten = polish(e.props.text)
45 return next(rewritten === e.props.text ? e : { ...e, props: { ...e.props, text: rewritten } })
46 }
47
48 const left = Math.max(0, Math.floor((columns - MARKER - measure) / 2))
49
50 const draw = (segment: Segment): RenderElement => {
51 if (segment.kind === 'text') {
52 return { type: 'Markdown', props: { text: reflow(segment.text, measure) } } as RenderElement
53 }
54
55 if (segment.kind === 'list') {
56 // Markers share one column so the item texts line up; each text hangs after its marker.
57 const markerWidth = Math.max(...segment.items.map(item => displayWidth(item.marker))) + 1
58
59 return box(
60 { flexDirection: 'column' },
61 segment.items.map(item => {
62 const indent = INDENT * item.depth
63 const itemWidth = measure - indent - markerWidth
64
65 return box({ flexDirection: 'row', marginLeft: indent }, [
66 text({ color: ACCENT }, item.marker.padEnd(markerWidth)),
67 box({ width: itemWidth }, [
68 { type: 'Markdown', props: { text: reflow(item.text, itemWidth) } } as RenderElement,
69 ]),
70 ])
71 }),
72 )
73 }
74
75 if (segment.kind === 'heading') {
76 const isTop = segment.level <= 2
77 const title = text({ bold: true, ...(isTop ? { color: ACCENT } : {}) }, segment.text)
78 const rule = text({ dimColor: true }, '─'.repeat(Math.min(displayWidth(segment.text), measure)))
79
80 return box({ flexDirection: 'column' }, segment.level === 1 ? [title, rule] : [title])
81 }
82
83 return box(
84 { flexDirection: 'column' },
85 segment.cells.map((row, k) => {
86 if (row === null) return text({ dimColor: true }, segment.lines[k] ?? '')
87
88 const isHeader = k >= 1 && k <= segment.headerLines
89 const sources = segment.sources[k] ?? []
90 // A body cell is drawn as markdown in a box of the column's width, so code spans and bold keep
91 // their style; the header is plain text in the accent colour.
92 const draw = (cell: string, c: number): RenderElement => {
93 const source = sources[c] ?? ''
94 if (isHeader) return text({ bold: true, color: ACCENT }, cell)
95 if (source === '') return text({}, cell)
96 return box({ width: segment.widths[c] ?? 0 }, [
97 { type: 'Markdown', props: { text: source } } as RenderElement,
98 ])
99 }
100 const pieces = row.flatMap((cell, c) => [text({ dimColor: true }, c === 0 ? '│ ' : ' │ '), draw(cell, c)])
101
102 return box({ flexDirection: 'row' }, [...pieces, text({ dimColor: true }, ' │')])
103 }),
104 )
105 }
106
107 const children = segments.map((segment, i) => {
108 const isFirst = i === 0 && e.props.isFirstOfReply
109 const isTight = (i === 0 && !isFirst) || (i > 0 && segments[i - 1]?.kind === 'heading')
110 // The marker of a reply is kept: it sits in the gutter just left of the text column.
111 const hasMarker = isFirst && left >= MARKER
112 const block = box({ width: measure }, [draw(segment)])
113
114 // A reply starts one blank line below whatever came before it, a hook's notice for one.
115 return box(
116 { flexDirection: 'row', marginLeft: hasMarker ? left - MARKER : left, marginTop: isTight ? 0 : 1 },
117 hasMarker ? [text({}, '● '), block] : [block],
118 )
119 })
120
121 return box({ flexDirection: 'column' }, children)
122 })
123}
124hooks/format.ts 436 lines1// Pure text rewriting for an assistant reply. The result is still markdown: the engine's own
2// renderer draws it, so themes, links and code highlighting are kept.
3
4export const MAX_CODE_LINES = 30
5export const KEEP_CODE_LINES = 12
6// Without a measured width, a table stays a table when its widest row fits and it has few columns.
7export const WIDE_TABLE = 72
8export const MAX_COLUMNS = 3
9// A table segment carries what `drawTable` returns, line by line (see there).
10export type ListItem = { depth: number; marker: string; text: string }
11
12export type Segment =
13 | { kind: 'text'; text: string }
14 | { kind: 'heading'; level: number; text: string }
15 | { kind: 'list'; items: ListItem[] }
16 | ({ kind: 'table' } & Drawn)
17
18const OPEN_FENCE = /^\s*(`{3,}|~{3,})/
19const LIST_ITEM = /^(\s*)([-*+]|\d{1,3}[.)])\s+(.*)$/
20const BULLETS = ['•', '◦', '▪']
21// Characters that may not start a line (closing punctuation) or end one (opening punctuation).
22const NO_START = ',。、;:!?)】》」』”’…%,.;:!?)]}'
23const NO_END = '(【《「『“‘([{'
24const HEADING = /^(#{1,6})\s+(.*?)\s*#*\s*$/
25const SEPARATOR = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/
26
27const PIPE = '\u0000'
28
29const cells = (line: string): string[] =>
30 line
31 .trim()
32 .replace(/\\\|/g, PIPE)
33 .replace(/^\|/, '')
34 .replace(/\|$/, '')
35 .split('|')
36 .map(cell => cell.split(PIPE).join('|').trim())
37
38const closesFence = (line: string, marker: string): boolean => {
39 const text = line.trim()
40 const mark = text[0] ?? ''
41 return text.length >= marker.length && mark === marker[0] && text === mark.repeat(text.length)
42}
43
44const isTableStart = (lines: string[], i: number): boolean => {
45 const line = lines[i] ?? ''
46 const next = lines[i + 1]
47 return (
48 next !== undefined &&
49 line.includes('|') &&
50 next.includes('|') &&
51 SEPARATOR.test(next) &&
52 cells(line).length >= 2 &&
53 cells(line).length === cells(next).length
54 )
55}
56
57const isWide = (cp: number): boolean =>
58 (cp >= 0x1100 && cp <= 0x115f) ||
59 (cp >= 0x2e80 && cp <= 0xa4cf) ||
60 (cp >= 0xac00 && cp <= 0xd7a3) ||
61 (cp >= 0xf900 && cp <= 0xfaff) ||
62 (cp >= 0xfe30 && cp <= 0xfe6f) ||
63 (cp >= 0xff00 && cp <= 0xff60) ||
64 (cp >= 0xffe0 && cp <= 0xffe6) ||
65 (cp >= 0x1f300 && cp <= 0x1faff)
66
67// Terminal cells a string takes: CJK and emoji are two cells wide.
68export const displayWidth = (text: string): number => {
69 let width = 0
70 for (const char of text) width += isWide(char.codePointAt(0) ?? 0) ? 2 : 1
71 return width
72}
73
74// A cell or a heading is drawn as plain text, so inline markdown is reduced to what it shows.
75const plain = (cell: string): string =>
76 cell
77 .replace(/\*\*(.+?)\*\*/g, '$1')
78 .replace(/`([^`]+)`/g, '$1')
79 .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
80
81// A line cut to `width` cells. A Latin word or number stays with the Chinese character after it
82// ("20个"), closing punctuation never starts a line and opening punctuation never ends one.
83export const wrapLine = (line: string, width: number): string[] => {
84 if (displayWidth(line) <= width) return [line]
85
86 const pieces: string[] = []
87 let run = ''
88 for (const char of line) {
89 if (char === ' ') {
90 if (run !== '') pieces.push(run)
91 run = ''
92 pieces.push(' ')
93 } else if (isWide(char.codePointAt(0) ?? 0)) {
94 pieces.push(run + char)
95 run = ''
96 } else {
97 run += char
98 }
99 }
100 if (run !== '') pieces.push(run)
101
102 const units: string[] = []
103 for (const piece of pieces) {
104 const prev = units[units.length - 1]
105 const isGlued =
106 prev !== undefined &&
107 prev !== ' ' &&
108 piece !== ' ' &&
109 (NO_START.includes(piece.charAt(0)) || NO_END.includes(prev.charAt(prev.length - 1)))
110 if (isGlued) units[units.length - 1] = prev + piece
111 else units.push(piece)
112 }
113
114 const out: string[] = []
115 let current = ''
116 let used = 0
117 // Backticks are never shown; `**` is hidden as bold markup, but shown inside a code span.
118 let isInCode = false
119 for (const unit of units) {
120 if (unit === ' ') {
121 if (current !== '') {
122 current += ' '
123 used += 1
124 }
125 continue
126 }
127 const ticks = (unit.match(/`/g) ?? []).length
128 const shown = isInCode || ticks > 0 ? unit.replace(/`/g, '') : unit.replace(/\*\*/g, '')
129 if (ticks % 2 === 1) isInCode = !isInCode
130 const size = displayWidth(shown)
131 if (used + size > width && current.trim() !== '') {
132 out.push(current.trimEnd())
133 current = ''
134 used = 0
135 }
136 current += unit
137 used += size
138 }
139 if (current.trim() !== '') out.push(current.trimEnd())
140
141 return out
142}
143
144// Paragraph lines cut to `width`; code, quotes, tables and indented lines are left as written.
145export const reflow = (text: string, width: number): string => {
146 let isFenced = false
147
148 return text
149 .split('\n')
150 .flatMap(raw => {
151 if (OPEN_FENCE.test(raw)) {
152 isFenced = !isFenced
153 return [raw]
154 }
155 // One to three leading spaces mean nothing in markdown; four or more make a code block.
156 const line = isFenced || !/^ {1,3}\S/.test(raw) ? raw : raw.trimStart()
157 return isFenced || /^(\s|>|\||<)/.test(line) ? [line] : wrapLine(line, width)
158 })
159 .join('\n')
160}
161
162// A table with this many body rows or more gets a rule between its rows, so a long table stays readable.
163export const ROW_RULES_FROM = 7
164// A column is never squeezed below this many cells; a table that needs less goes to a list.
165export const MIN_COLUMN = 4
166
167type Drawn = {
168 lines: string[]
169 // Per line: the padded plain cells of a content line, null for a rule.
170 cells: (string[] | null)[]
171 // Per line: the cells as written in markdown (code spans, bold), null for a rule.
172 sources: (string[] | null)[]
173 widths: number[]
174 width: number
175 // How many lines the header takes, from line 1 on; a wrapped header takes more than one.
176 headerLines: number
177}
178
179// Cuts at most `width` cells off the front of `text`, ignoring the markup that is not shown.
180const hardSplit = (text: string, width: number): string[] => {
181 const out: string[] = []
182 let current = ''
183 let used = 0
184 let i = 0
185 while (i < text.length) {
186 const isMark = text[i] === '`' || text.startsWith('**', i)
187 const step = text.startsWith('**', i) ? 2 : 1
188 const char = isMark ? text.slice(i, i + step) : String.fromCodePoint(text.codePointAt(i) ?? 32)
189 const size = isMark ? 0 : displayWidth(char)
190 if (used + size > width && current !== '') {
191 out.push(current)
192 current = ''
193 used = 0
194 }
195 current += char
196 used += size
197 i += char.length
198 }
199 if (current !== '') out.push(current)
200 return out
201}
202
203// A cell as lines of at most `width` cells. A code span or bold run cut by a line break is closed
204// at the end of its line and opened again at the start of the next, so each line draws on its own.
205export const wrapCell = (source: string, width: number): string[] => {
206 const text = source.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
207 if (text === '') return ['']
208
209 const lines = wrapLine(text, width).flatMap(line => (displayWidth(line.replace(/`|\*\*/g, '')) > width ? hardSplit(line, width) : [line]))
210 let isCode = false
211 let isBold = false
212
213 return lines.map(line => {
214 const prefix = (isCode ? '`' : '') + (isBold ? '**' : '')
215 for (const part of line.split(/(`|\*\*)/)) {
216 if (part === '`') isCode = !isCode
217 else if (part === '**' && !isCode) isBold = !isBold
218 }
219 return prefix + line + (isBold ? '**' : '') + (isCode ? '`' : '')
220 })
221}
222
223// The widest column width `cap` such that the columns, each cut to `cap`, fit in `available`.
224const capColumns = (natural: number[], available: number): number[] | null => {
225 if (natural.reduce((sum, width) => sum + width, 0) <= available) return natural
226 for (let cap = Math.max(...natural) - 1; cap >= MIN_COLUMN; cap -= 1) {
227 const widths = natural.map(width => Math.min(width, cap))
228 if (widths.reduce((sum, width) => sum + width, 0) <= available) return widths
229 }
230 return null
231}
232
233// The table as box-drawing lines, columns sized to their widest cell. Line 1 is the header. With
234// `maxWidth`, the widest columns are narrowed and their cells wrap onto more lines; null when even
235// the narrowest columns do not fit.
236export const drawTable = (header: string[], rows: string[][], maxWidth?: number): Drawn | null => {
237 const raw = [header, ...rows.map(row => header.map((_, k) => row[k] ?? ''))]
238 const grid = raw.map(row => row.map(plain))
239 const natural = header.map((_, k) => Math.max(...grid.map(row => displayWidth(row[k] ?? ''))))
240 const widths = maxWidth === undefined ? natural : capColumns(natural, maxWidth - (3 * header.length + 1))
241 if (widths === null) return null
242
243 const pad = (text: string, width: number) => text + ' '.repeat(Math.max(0, width - displayWidth(text)))
244 const rule = (left: string, mid: string, right: string) =>
245 left + widths.map(width => '─'.repeat(width + 2)).join(mid) + right
246 const [top, middle, bottom] = [rule('┌', '┬', '┐'), rule('├', '┼', '┤'), rule('└', '┴', '┘')]
247
248 // Each table row as visual lines: [sources of each cell] per line.
249 const wrapped = raw.map(row => {
250 const pieces = row.map((cell, k) => (displayWidth(plain(cell)) <= (widths[k] ?? 0) ? [cell] : wrapCell(cell, widths[k] ?? 0)))
251 const height = Math.max(...pieces.map(cell => cell.length))
252 return Array.from({ length: height }, (_, line) => pieces.map(cell => cell[line] ?? ''))
253 })
254 const isWrapped = wrapped.some(row => row.length > 1)
255 const hasRowRules = rows.length >= ROW_RULES_FROM || isWrapped
256
257 type Entry = { rule: string } | { source: string[] }
258 const entries: Entry[] = [{ rule: top }]
259 wrapped.forEach((row, r) => {
260 entries.push(...row.map(source => ({ source })))
261 if (r === 0 || (hasRowRules && r < wrapped.length - 1)) entries.push({ rule: middle })
262 })
263 entries.push({ rule: bottom })
264
265 const padded = (source: string[]) => source.map((cell, k) => pad(plain(cell), widths[k] ?? 0))
266
267 return {
268 lines: entries.map(entry => ('rule' in entry ? entry.rule : '│ ' + padded(entry.source).join(' │ ') + ' │')),
269 cells: entries.map(entry => ('rule' in entry ? null : padded(entry.source))),
270 sources: entries.map(entry => ('rule' in entry ? null : entry.source)),
271 widths,
272 width: displayWidth(top),
273 headerLines: wrapped[0]?.length ?? 1,
274 }
275}
276
277// Two columns: one bullet per row, "- **first** — second".
278// More columns: a bullet per row with one sub-bullet per cell, " - head: cell".
279// Returns null when the table is small enough to stay as it is, unless `force` is set.
280const tableItems = (header: string[], rows: string[][]): ListItem[] =>
281 rows.flatMap((row): ListItem[] => {
282 const [first = '', ...rest] = header.map((_, k) => row[k] ?? '')
283 const title = first === '' ? '' : first.includes('**') ? first : `**${first}**`
284
285 if (rest.length === 1) {
286 const only = rest[0] ?? ''
287 return [{ depth: 0, marker: '•', text: `${title}${title !== '' && only !== '' ? ' — ' : ''}${only}` }]
288 }
289
290 const parts = rest.flatMap((cell, k): ListItem[] =>
291 cell === '' ? [] : [{ depth: 1, marker: '◦', text: `${header[k + 1] ?? ''}: ${cell}` }],
292 )
293 return [{ depth: 0, marker: '•', text: title }, ...parts]
294 })
295
296const tableToList = (header: string[], rows: string[][], rawLines: string[], force = false): string[] | null => {
297 const widest = Math.max(...rawLines.map(line => line.length))
298 if (!force && widest <= WIDE_TABLE && header.length <= MAX_COLUMNS) return null
299
300 return tableItems(header, rows).map(item => `${' '.repeat(item.depth)}- ${item.text}`)
301}
302
303// The reply as text blocks, headings and tables. With `width` (the cells a table may take, the text
304// column's) a table that fits is returned as a table to draw and one that does not becomes a list,
305// and headings are blocks of their own; without it, only a wide table becomes a list, headings
306// become bold lines and the rest stays markdown for the engine.
307export const layout = (text: string, width?: number): Segment[] => {
308 const lines = text.split('\n')
309 const out: string[] = []
310 const segments: Segment[] = []
311 const lastIsBlank = () => out.length > 0 && (out[out.length - 1] ?? '').trim() === ''
312 const flush = () => {
313 const block = out.join('\n').replace(/^\n+|\n+$/g, '')
314 if (block !== '') segments.push({ kind: 'text', text: block })
315 out.length = 0
316 }
317 let i = 0
318
319 while (i < lines.length) {
320 const line = lines[i] ?? ''
321 const fence = OPEN_FENCE.exec(line)
322
323 if (fence) {
324 const marker = fence[1] ?? '```'
325 let end = i + 1
326 while (end < lines.length && !closesFence(lines[end] ?? '', marker)) end += 1
327 const isClosed = end < lines.length
328 const body = lines.slice(i + 1, end)
329
330 out.push(line)
331 if (isClosed && body.length > MAX_CODE_LINES) {
332 out.push(...body.slice(0, KEEP_CODE_LINES))
333 out.push(`… ${body.length - KEEP_CODE_LINES} more lines hidden (ctrl+o shows the whole reply)`)
334 } else {
335 out.push(...body)
336 }
337 if (isClosed) out.push(lines[end] ?? '')
338 i = isClosed ? end + 1 : lines.length
339 continue
340 }
341
342 if (isTableStart(lines, i)) {
343 let end = i + 2
344 while (end < lines.length && (lines[end] ?? '').trim() !== '' && (lines[end] ?? '').includes('|')) end += 1
345 const raw = lines.slice(i, end)
346 const header = cells(line)
347 const rows = lines.slice(i + 2, end).map(cells)
348 const drawn = width === undefined ? null : drawTable(header, rows, width)
349
350 if (drawn !== null) {
351 flush()
352 segments.push({ kind: 'table', ...drawn })
353 } else if (width !== undefined) {
354 flush()
355 segments.push({ kind: 'list', items: tableItems(header, rows) })
356 } else {
357 out.push(...(tableToList(header, rows, raw) ?? raw))
358 }
359 i = end
360 continue
361 }
362
363 const heading = HEADING.exec(line)
364 if (heading) {
365 const level = (heading[1] ?? '#').length
366 const title = (heading[2] ?? '').replace(/^\*\*(.+)\*\*$/, '$1')
367
368 if (width !== undefined) {
369 flush()
370 segments.push({ kind: 'heading', level, text: plain(title) })
371 } else {
372 if (level <= 2 && out.length > 0 && !lastIsBlank()) out.push('')
373 out.push(`**${title}**`)
374 }
375 i += 1
376 continue
377 }
378
379 if (width !== undefined && LIST_ITEM.test(line)) {
380 flush()
381 const items: ListItem[] = []
382 const indents: number[] = []
383 let end = i
384
385 while (end < lines.length) {
386 const current = lines[end] ?? ''
387 const item = LIST_ITEM.exec(current)
388
389 if (item) {
390 const indent = (item[1] ?? '').length
391 while (indents.length > 0 && indent < (indents[indents.length - 1] ?? 0)) indents.pop()
392 if (indents.length === 0 || indent > (indents[indents.length - 1] ?? 0)) indents.push(indent)
393 const depth = Math.min(indents.length - 1, BULLETS.length - 1)
394 const mark = item[2] ?? '-'
395 items.push({
396 depth,
397 marker: /^\d/.test(mark) ? mark : (BULLETS[depth] ?? '•'),
398 text: (item[3] ?? '').trim(),
399 })
400 } else if (items.length > 0 && /^\s{2,}\S/.test(current) && !OPEN_FENCE.test(current)) {
401 const last = items[items.length - 1]
402 if (last !== undefined) last.text += ' ' + current.trim()
403 } else {
404 break
405 }
406 end += 1
407 }
408
409 segments.push({ kind: 'list', items })
410 i = end
411 continue
412 }
413
414 if (line.trim() === '') {
415 if (!lastIsBlank()) out.push('')
416 } else {
417 out.push(line)
418 }
419 i += 1
420 }
421
422 flush()
423 return segments.length > 0 ? segments : [{ kind: 'text', text: '' }]
424}
425
426// The reply as one markdown string, for a surface that cannot be measured.
427export const polish = (text: string): string =>
428 layout(text)
429 .map(segment => {
430 if (segment.kind === 'text') return segment.text
431 if (segment.kind === 'heading') return `**${segment.text}**`
432 if (segment.kind === 'list') return segment.items.map(item => `${item.marker} ${item.text}`).join('\n')
433 return segment.lines.join('\n')
434 })
435 .join('\n')
436