feat(tx): attach or switch client

This commit is contained in:
2024-01-04 01:35:09 +02:00
parent ffee236b4c
commit a628b55168
3 changed files with 43 additions and 14 deletions

View File

@@ -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<Opts & { bare?: boolean; sessions?: boolean }
return
}
const sessionsOutput = await getCommandOutput(opts, 'tmux ls')
const sessions = sessionsOutput.output.replace(/\(created ([^)]+)\)/g, '$1')
let sessions = sessionsOutput.output.replace(/\(created ([^)]+)\)/g, '$1')
sessions = sessions
.split('\n')
.map((line) => line.replace(/^([^:]+):/, '$1').trim())
.join('\n')
const tbl = await getCommandOutput(
opts,
`echo ${JSON.stringify(
@@ -111,13 +116,13 @@ const listCmd = new MassargCommand<Opts & { bare?: boolean; sessions?: boolean }
console.log('tmux config files:\n')
console.log(
' - ' +
Object.entries(configs)
.map(([key, config]) =>
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<Opts>({
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
}

View File

@@ -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)
}

View File

@@ -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<bo
}
}
export async function fzf(opts: Opts, inputs: string[]): Promise<string> {
export async function fzf(_opts: Opts, inputs: string[]): Promise<string> {
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<string> {
})
})
}
export async function attachToSession(opts: Opts, sessionName: string): Promise<void> {
if (process.env.TMUX) {
await runCommand(opts, `tmux switch-client -t ${sessionName}`)
return
}
await runCommand(opts, `tmux attach -t ${sessionName}`)
return
}