1111
1212import { closeSync , constants , openSync , writeSync } from 'fs'
1313
14+ import { getCliEnv } from './env'
15+
1416const MAX_TITLE_LENGTH = 60
1517const TITLE_PREFIX = 'Codebuff: '
1618const OSC_TERMINATOR = '\x07' // BEL
1719
18- function isInTmux ( ) : boolean {
19- return Boolean ( process . env . TMUX )
20+ function isInTmux ( env : ReturnType < typeof getCliEnv > ) : boolean {
21+ return Boolean ( env . TMUX )
2022}
2123
22- function isInScreen ( ) : boolean {
23- if ( process . env . STY ) return true
24- const term = process . env . TERM ?? ''
25- return term . startsWith ( 'screen' ) && ! isInTmux ( )
24+ function isInScreen ( env : ReturnType < typeof getCliEnv > ) : boolean {
25+ if ( env . STY ) return true
26+ const term = env . TERM ?? ''
27+ return term . startsWith ( 'screen' ) && ! isInTmux ( env )
2628}
2729
2830/**
2931 * Build the OSC title sequence with tmux/screen passthrough if needed
3032 */
31- function buildTitleSequence ( title : string ) : string {
33+ function buildTitleSequence ( title : string , env : ReturnType < typeof getCliEnv > ) : string {
3234 const osc = `\x1b]0;${ title } ${ OSC_TERMINATOR } `
3335
3436 // tmux passthrough: wrap in DCS and double ESC characters
35- if ( isInTmux ( ) ) {
37+ if ( isInTmux ( env ) ) {
3638 const escaped = osc . replace ( / \x1b / g, '\x1b\x1b' )
3739 return `\x1bPtmux;${ escaped } \x1b\\`
3840 }
3941
4042 // GNU screen passthrough: wrap in DCS
41- if ( isInScreen ( ) ) {
43+ if ( isInScreen ( env ) ) {
4244 return `\x1bP${ osc } \x1b\\`
4345 }
4446
@@ -89,7 +91,8 @@ export function setTerminalTitle(title: string): void {
8991 : sanitized
9092
9193 const fullTitle = `${ TITLE_PREFIX } ${ truncated } `
92- const sequence = buildTitleSequence ( fullTitle )
94+ const env = getCliEnv ( )
95+ const sequence = buildTitleSequence ( fullTitle , env )
9396
9497 writeToTty ( sequence )
9598}
@@ -100,6 +103,7 @@ export function setTerminalTitle(title: string): void {
100103 */
101104export function resetTerminalTitle ( ) : void {
102105 // Empty title resets to terminal's default behavior
103- const sequence = buildTitleSequence ( '' )
106+ const env = getCliEnv ( )
107+ const sequence = buildTitleSequence ( '' , env )
104108 writeToTty ( sequence )
105109}
0 commit comments