feat(tx): use fzf to ask session when no key

fix: session name
This commit is contained in:
2023-12-23 23:06:38 +02:00
parent bc2efdddcd
commit 8855ef7ced
4 changed files with 36 additions and 5 deletions

View File

@@ -40,7 +40,6 @@ const mainCmd = massarg<Opts>({
aliases: ['k'],
description: 'The tmux session to open',
isDefault: true,
required: true,
})
.help({
bindOption: true,

View File

@@ -17,7 +17,7 @@ export async function createFromConfig(opts: Opts, tmuxConfig: ParsedTmuxConfigI
const commands: string[] = []
let sessionName = tmuxConfig.name
let sessionName = nameFix(tmuxConfig.name)
log(opts, 'Session name:', sessionName)

View File

@@ -1,5 +1,5 @@
import { Opts } from '../common'
import { TmuxLayout, getTmuxConfig, parseConfig } from './utils'
import { Opts, getCommandOutput } from '../common'
import { TmuxLayout, fzf, getTmuxConfig, getTmuxConfigFileInfo, parseConfig } from './utils'
import { createFromConfig } from './command_builder'
const defaultLayout: TmuxLayout = {
@@ -14,7 +14,17 @@ const defaultLayout: TmuxLayout = {
}
export async function main(opts: Opts) {
const { key } = opts
let { key } = opts
if (!key) {
const {
merged: { config },
} = await getTmuxConfigFileInfo()
const output = await fzf(opts, Object.keys(config))
if (!output || !(output in config)) {
throw new Error('tmux config item not found')
}
key = output
}
const config = await getTmuxConfig()
const item = config[key]
if (!item) {

View File

@@ -2,6 +2,7 @@ import { CosmiconfigResult, cosmiconfig } from 'cosmiconfig'
import * as path from 'node:path'
import * as os from 'node:os'
import { Opts, getCommandOutput } from '../common'
import { spawn } from 'node:child_process'
const searchDirs = [
process.cwd(),
@@ -221,3 +222,24 @@ export async function sessionExists(opts: Opts, sessionName: string): Promise<bo
return false
}
}
export async function fzf(opts: Opts, inputs: string[]): Promise<string> {
const fzf = spawn(`echo "${inputs.join('\n')}" | fzf`, {
stdio: ['inherit', 'pipe', 'inherit'],
shell: true,
})
fzf.stdout.setEncoding('utf-8')
return new Promise((resolve, reject) => {
fzf.stdout.on('readable', function () {
const value = fzf.stdout.read()
if (value !== null) {
resolve(value.toString().trim())
return
}
reject()
})
})
}