From a628b551684ae7c8b63b51ef273f5ebd73442600 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 4 Jan 2024 01:35:09 +0200 Subject: [PATCH] feat(tx): attach or switch client --- utils/src/tmux/cmd.ts | 27 ++++++++++++++++++--------- utils/src/tmux/tmux.ts | 17 ++++++++++++++--- utils/src/tmux/utils.ts | 13 +++++++++++-- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/utils/src/tmux/cmd.ts b/utils/src/tmux/cmd.ts index c6d81d12..e82b18c5 100644 --- a/utils/src/tmux/cmd.ts +++ b/utils/src/tmux/cmd.ts @@ -7,6 +7,7 @@ import { indent, strConcat } from 'massarg/utils' import { format } from 'massarg/style' import { MassargCommand } from 'massarg/command' import { + attachToSession, getTmuxConfig, getTmuxConfigFileInfo, nameFix, @@ -95,7 +96,11 @@ const listCmd = new MassargCommand line.replace(/^([^:]+):/, '$1').trim()) + .join('\n') const tbl = await getCommandOutput( opts, `echo ${JSON.stringify( @@ -111,13 +116,13 @@ const listCmd = new MassargCommand - config && key !== 'merged' ? key + ': ' + config.filepath : undefined, - ) - .filter(Boolean) - .join('\n - ') + - '\n', + Object.entries(configs) + .map(([key, config]) => + config && key !== 'merged' ? key + ': ' + config.filepath : undefined, + ) + .filter(Boolean) + .join('\n - ') + + '\n', ) console.log('tmux configurations:\n') console.log(' - ' + keys.join('\n - ')) @@ -244,7 +249,11 @@ const attachCmd = new MassargCommand({ if (!(await sessionExists(opts, sessionName))) { throw new Error(`tmux session ${sessionName} does not exist`) } - await runCommand(opts, `tmux attach -t ${sessionName}`) + return attachToSession(opts, sessionName) + } + + if (process.env.TMUX) { + log(opts, 'Already in tmux and no key specified, not attaching') return } diff --git a/utils/src/tmux/tmux.ts b/utils/src/tmux/tmux.ts index 00f8db9b..dacb6591 100644 --- a/utils/src/tmux/tmux.ts +++ b/utils/src/tmux/tmux.ts @@ -1,5 +1,13 @@ import { Opts } from '../common' -import { TmuxLayout, fzf, getTmuxConfig, getTmuxConfigFileInfo, parseConfig } from './utils' +import { + TmuxLayout, + attachToSession, + fzf, + getTmuxConfig, + getTmuxConfigFileInfo, + parseConfig, + sessionExists, +} from './utils' import { createFromConfig } from './command_builder' const defaultLayout: TmuxLayout = { @@ -31,6 +39,9 @@ export async function main(opts: Opts) { throw new Error(`tmux config item ${key} not found`) } - const tmuxConfig = parseConfig(item) - createFromConfig(opts, tmuxConfig) + const parsed = parseConfig(item) + if (await sessionExists(opts, parsed.name)) { + return attachToSession(opts, parsed.name) + } + return createFromConfig(opts, parsed) } diff --git a/utils/src/tmux/utils.ts b/utils/src/tmux/utils.ts index f62516be..8bba0d27 100644 --- a/utils/src/tmux/utils.ts +++ b/utils/src/tmux/utils.ts @@ -1,7 +1,7 @@ import { CosmiconfigResult, cosmiconfig } from 'cosmiconfig' import * as path from 'node:path' import * as os from 'node:os' -import { Opts, getCommandOutput } from '../common' +import { Opts, getCommandOutput, runCommand } from '../common' import { spawn } from 'node:child_process' const searchDirs = [ @@ -223,7 +223,7 @@ export async function sessionExists(opts: Opts, sessionName: string): Promise { +export async function fzf(_opts: Opts, inputs: string[]): Promise { const fzf = spawn(`echo "${inputs.join('\n')}" | fzf`, { stdio: ['inherit', 'pipe', 'inherit'], shell: true, @@ -243,3 +243,12 @@ export async function fzf(opts: Opts, inputs: string[]): Promise { }) }) } + +export async function attachToSession(opts: Opts, sessionName: string): Promise { + if (process.env.TMUX) { + await runCommand(opts, `tmux switch-client -t ${sessionName}`) + return + } + await runCommand(opts, `tmux attach -t ${sessionName}`) + return +}