import { exec } from 'child_process' ; import * as util from 'util' ; const execAsync = util . promisify ( exec ) ; const pid2title_linux = async ( PID ) : Promise < string > => { const { stdout : stdout1 } = await execAsync ( 'xprop -root _NET_CLIENT_LIST | cut -d "#" -f 2' ) ; const WIDs = stdout1 . split ( ',' ) . map ( ( s ) => s . trim ( ) ) ; const findWidCommand = `ids="${ WIDs . join ( ' ' ) }"\n` + 'for wid in ${ids}; do\n' + "xprop -id ${wid} _NET_WM_PID _NET_WM_NAME | awk -F ' = ' '{print $2}'" + ';done' ; const { stdout : stdout2 } = await execAsync ( findWidCommand ) ; const blocks = stdout2 . split ( '\n' ) ; while ( blocks . length ) { const [ pid , name ] = blocks . splice ( 0 , 2 ) ; if ( Number ( pid ) === PID ) return name . trim ( ) . slice ( 1 , - 1 ) ; } throw new Error ( `Can not detect a window of process: ${ PID }` ) ; } ; const pid2title_win = async ( PID ) : Promise < string > => { const cmd = `powershell.exe (Get-Process -id ${ PID } -ErrorAction SilentlyContinue).MainWindowTitle` ; const { stdout } = await execAsync ( cmd ) ; return stdout ; } ; export const pid2title = async ( PID ) : Promise < string > => { if ( process . platform === 'win32' ) return pid2title_win ( PID ) ; if ( process . platform === 'linux' ) return pid2title_linux ( PID ) ; return '' ; } ;