-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmux-sessionizer.ts
More file actions
65 lines (48 loc) · 1.33 KB
/
tmux-sessionizer.ts
File metadata and controls
65 lines (48 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { resolve, basename } from 'https://deno.land/std/path/mod.ts'
import tmux from './modules/tmux.ts'
const HOME = Deno.env.get('HOME')
const ROOT_DIR = `${HOME}/sites`
if (!HOME) {
throw Error('no Home')
}
const dirs = []
for await (const dir of Deno.readDir(ROOT_DIR)) {
dirs.push(resolve(ROOT_DIR, dir.name))
}
const fzf = Deno.run({
cmd: ['fzf'],
stdout: 'piped',
stdin: 'piped',
})
const encoder = new TextEncoder()
const decoder = new TextDecoder()
await fzf.stdin.write(encoder.encode(dirs.join('\n')))
await fzf.stdin.close()
const chosenDir = decoder.decode(await fzf.output()).trim()
const sessionName = basename(chosenDir).replaceAll('.', '_').trim()
fzf.close()
await goToSession()
async function goToSession() {
try {
const hasServer = await tmux.hasServer()
const hasSession = await tmux.hasSession(sessionName)
console.log(hasServer, hasSession)
const createSession = () =>
tmux
.createSession()
.name(sessionName)
.directory(chosenDir)
.detached()
.exec()
if (!hasServer) {
await createSession()
return tmux.attach().target(sessionName).directory(chosenDir).exec()
}
if (!hasSession) {
await createSession()
}
return tmux.switchClient().target(sessionName).exec()
} catch (e) {
console.error('Error', e)
}
}