πŸ“„ File detail

ink/hooks/use-terminal-title.ts

🧩 .tsπŸ“ 32 linesπŸ’Ύ 1,020 bytesπŸ“ text
← Back to All Files

🎯 Use case

This file lives under β€œink/”, which covers Ink terminal UI (layouts, TTY IO, keyboard, renderer components). On the API surface it exposes useTerminalTitle β€” mainly functions, hooks, or classes. Dependencies touch React UI and strip-ansi. It composes internal code from termio and useTerminalNotification (relative imports).

Generated from folder role, exports, dependency roots, and inline comments β€” not hand-reviewed for every path.

🧠 Inline summary

import { useContext, useEffect } from 'react' import stripAnsi from 'strip-ansi' import { OSC, osc } from '../termio/osc.js' import { TerminalWriteContext } from '../useTerminalNotification.js'

πŸ“€ Exports (heuristic)

  • useTerminalTitle

πŸ“š External import roots

Package roots from from "…" (relative paths omitted).

  • react
  • strip-ansi

πŸ–₯️ Source preview

import { useContext, useEffect } from 'react'
import stripAnsi from 'strip-ansi'
import { OSC, osc } from '../termio/osc.js'
import { TerminalWriteContext } from '../useTerminalNotification.js'

/**
 * Declaratively set the terminal tab/window title.
 *
 * Pass a string to set the title. ANSI escape sequences are stripped
 * automatically so callers don't need to know about terminal encoding.
 * Pass `null` to opt out β€” the hook becomes a no-op and leaves the
 * terminal title untouched.
 *
 * On Windows, uses `process.title` (classic conhost doesn't support OSC).
 * Elsewhere, writes OSC 0 (set title+icon) via Ink's stdout.
 */
export function useTerminalTitle(title: string | null): void {
  const writeRaw = useContext(TerminalWriteContext)

  useEffect(() => {
    if (title === null || !writeRaw) return

    const clean = stripAnsi(title)

    if (process.platform === 'win32') {
      process.title = clean
    } else {
      writeRaw(osc(OSC.SET_TITLE_AND_ICON, clean))
    }
  }, [title, writeRaw])
}