mux-sesh 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ import { spawn } from 'bun'
2
+ import type { Item, SessionDetails, WindowInfo } from '../types'
3
+
4
+ export async function listTmuxSessions(): Promise<Item[]> {
5
+ const proc = spawn([
6
+ 'tmux',
7
+ 'list-sessions',
8
+ '-F',
9
+ '#{session_name}:#{session_attached}:#{session_windows}:#{session_created}:#{session_activity}',
10
+ ])
11
+
12
+ const output = await new Response(proc.stdout).text()
13
+ const lines = output.trim().split('\n').filter(Boolean)
14
+
15
+ const sessions = lines.map(line => {
16
+ const [name, attached, windows, created, activity] = line.split(':')
17
+ return {
18
+ title: name,
19
+ path: name,
20
+ desc: '',
21
+ isSession: true,
22
+ isAttached: attached === '1',
23
+ windowCount: windows,
24
+ createdAt: parseInt(created, 10),
25
+ lastActivity: parseInt(activity, 10),
26
+ }
27
+ })
28
+
29
+ return sessions.sort((a, b) => a.title.localeCompare(b.title))
30
+ }
31
+
32
+ export async function createTmuxSession(name: string, path: string): Promise<void> {
33
+ const sessionName = name.replace(/\./g, '_')
34
+ const insideTmux = !!process.env.TMUX
35
+
36
+ // Check if tmux is running
37
+ const tmuxRunning = spawn(['pgrep', 'tmux'])
38
+ await tmuxRunning.exited
39
+ const isTmuxRunning = tmuxRunning.exitCode === 0
40
+
41
+ if (!insideTmux && !isTmuxRunning) {
42
+ const proc = spawn(['tmux', 'new-session', '-s', sessionName, '-c', path], {
43
+ stdin: 'inherit',
44
+ stdout: 'inherit',
45
+ stderr: 'inherit',
46
+ })
47
+ await proc.exited
48
+ return
49
+ }
50
+
51
+ const hasSession = spawn(['tmux', 'has-session', `-t=${sessionName}`])
52
+ await hasSession.exited
53
+
54
+ if (hasSession.exitCode !== 0) {
55
+ const createProc = spawn(['tmux', 'new-session', '-d', '-s', sessionName, '-c', path])
56
+ await createProc.exited
57
+
58
+ const nvimProc = spawn([
59
+ 'tmux',
60
+ 'send-keys',
61
+ '-t',
62
+ sessionName,
63
+ "nvim -c \"lua vim.defer_fn(function() if pcall(require, 'telescope') then vim.cmd('Telescope find_files') end end, 100)\"",
64
+ 'Enter',
65
+ ])
66
+ await nvimProc.exited
67
+ }
68
+
69
+ const switchProc = spawn(['tmux', 'switch-client', '-t', sessionName])
70
+ await switchProc.exited
71
+ }
72
+
73
+ export async function createNamedTmuxSession(name: string): Promise<void> {
74
+ const sessionName = name.replace(/\./g, '_').replace(/ /g, '_')
75
+ const insideTmux = !!process.env.TMUX
76
+
77
+ const tmuxRunning = spawn(['pgrep', 'tmux'])
78
+ await tmuxRunning.exited
79
+ const isTmuxRunning = tmuxRunning.exitCode === 0
80
+
81
+ if (!insideTmux && !isTmuxRunning) {
82
+ const proc = spawn(['tmux', 'new-session', '-s', sessionName], {
83
+ stdin: 'inherit',
84
+ stdout: 'inherit',
85
+ stderr: 'inherit',
86
+ })
87
+ await proc.exited
88
+ return
89
+ }
90
+
91
+ const hasSession = spawn(['tmux', 'has-session', `-t=${sessionName}`])
92
+ await hasSession.exited
93
+
94
+ if (hasSession.exitCode !== 0) {
95
+ const createProc = spawn(['tmux', 'new-session', '-d', '-s', sessionName])
96
+ await createProc.exited
97
+ }
98
+
99
+ const switchProc = spawn(['tmux', 'switch-client', '-t', sessionName])
100
+ await switchProc.exited
101
+ }
102
+
103
+ export async function switchTmuxSession(name: string): Promise<void> {
104
+ const proc = spawn(['tmux', 'switch-client', '-t', name])
105
+ await proc.exited
106
+ }
107
+
108
+ export async function killTmuxSession(name: string): Promise<void> {
109
+ const proc = spawn(['tmux', 'kill-session', '-t', name])
110
+ await proc.exited
111
+ }
112
+
113
+ export async function renameTmuxSession(oldName: string, newName: string): Promise<void> {
114
+ const sanitizedName = newName.replace(/\./g, '_').replace(/ /g, '_')
115
+ const proc = spawn(['tmux', 'rename-session', '-t', oldName, sanitizedName])
116
+ await proc.exited
117
+ }
118
+
119
+ export async function getSessionDetails(sessionName: string): Promise<SessionDetails> {
120
+ // Get session status
121
+ const statusProc = spawn([
122
+ 'tmux',
123
+ 'list-sessions',
124
+ '-F',
125
+ '#{session_name}:#{session_attached}:#{session_windows}',
126
+ '-f',
127
+ `#{==:#{session_name},${sessionName}}`,
128
+ ])
129
+ const statusOutput = await new Response(statusProc.stdout).text()
130
+ const [, attached, windowCount] = statusOutput.trim().split(':')
131
+
132
+ // Get windows
133
+ const windowsProc = spawn([
134
+ 'tmux',
135
+ 'list-windows',
136
+ '-t',
137
+ sessionName,
138
+ '-F',
139
+ '#{window_index}:#{window_name}',
140
+ ])
141
+ const windowsOutput = await new Response(windowsProc.stdout).text()
142
+ const windowLines = windowsOutput.trim().split('\n').filter(Boolean)
143
+
144
+ const windows: WindowInfo[] = []
145
+
146
+ for (const line of windowLines) {
147
+ const [index, name] = line.split(': ')
148
+
149
+ // Get current directory
150
+ const dirProc = spawn([
151
+ 'tmux',
152
+ 'display-message',
153
+ '-t',
154
+ `${sessionName}:${index}`,
155
+ '-p',
156
+ '#{pane_current_path}',
157
+ ])
158
+ const currentPath = (await new Response(dirProc.stdout).text()).trim()
159
+
160
+ // Get current command
161
+ const cmdProc = spawn([
162
+ 'tmux',
163
+ 'display-message',
164
+ '-t',
165
+ `${sessionName}:${index}`,
166
+ '-p',
167
+ '#{pane_current_command}',
168
+ ])
169
+ const currentCommand = (await new Response(cmdProc.stdout).text()).trim()
170
+
171
+ const displayPath = currentPath.replace(process.env.HOME || '', '~')
172
+
173
+ windows.push({
174
+ index,
175
+ name,
176
+ currentPath: displayPath,
177
+ currentCommand: ['bash', 'zsh', 'fish'].includes(currentCommand) ? '' : currentCommand,
178
+ })
179
+ }
180
+
181
+ return {
182
+ name: sessionName,
183
+ isAttached: attached === '1',
184
+ windowCount,
185
+ windows,
186
+ }
187
+ }
@@ -0,0 +1,55 @@
1
+ import { spawn } from 'bun'
2
+ import { basename } from 'path'
3
+ import { stat } from 'fs/promises'
4
+ import type { Config, Item } from '../types'
5
+
6
+ export async function getProjectItems(config: Config): Promise<Item[]> {
7
+ const items: Item[] = []
8
+
9
+ const existingPaths: string[] = []
10
+ for (const path of config.projectPaths) {
11
+ try {
12
+ await stat(path)
13
+ existingPaths.push(path)
14
+ } catch {}
15
+ }
16
+
17
+ if (existingPaths.length === 0) {
18
+ return items
19
+ }
20
+
21
+ const args = [...existingPaths, '-mindepth', '1', '-maxdepth', '3', '-type', 'd']
22
+
23
+ const proc = spawn(['find', ...args])
24
+ const output = await new Response(proc.stdout).text()
25
+
26
+ const lines = output.trim().split('\n').filter(Boolean)
27
+
28
+ for (const line of lines) {
29
+ const baseName = basename(line)
30
+
31
+ if (
32
+ baseName.startsWith('.') ||
33
+ baseName === 'node_modules' ||
34
+ baseName === 'target' ||
35
+ baseName === 'build' ||
36
+ baseName === 'dist'
37
+ ) {
38
+ continue
39
+ }
40
+
41
+ const name = baseName
42
+ const desc = line.replace(process.env.HOME || '', '~')
43
+
44
+ items.push({
45
+ title: name,
46
+ desc,
47
+ path: line,
48
+ isSession: false,
49
+ })
50
+ }
51
+
52
+ items.sort((a, b) => a.title.localeCompare(b.title))
53
+
54
+ return items
55
+ }
@@ -0,0 +1,80 @@
1
+ // Core types for the application
2
+
3
+ export enum AppMode {
4
+ Normal,
5
+ Search,
6
+ NewSession,
7
+ Rename,
8
+ OpencodeManage,
9
+ }
10
+
11
+ export enum ViewMode {
12
+ Sessions,
13
+ Projects,
14
+ }
15
+
16
+ export type KeybindMode = 'vim' | 'standard'
17
+
18
+ export interface OpencodeSessionStats {
19
+ sessionID?: string
20
+ title?: string
21
+ projectID?: string
22
+ directory?: string
23
+ messageCount?: number
24
+ filesModified?: number
25
+ additions?: number
26
+ deletions?: number
27
+ createdAt?: number
28
+ updatedAt?: number
29
+ // Aggregated stats (for home directory sessions)
30
+ sessionCount?: number
31
+ mostRecentTitle?: string
32
+ // Current session live stats (last message)
33
+ lastMessageTokens?: number
34
+ contextLimit?: number
35
+ sessionTotalCost?: number
36
+ }
37
+
38
+ export interface Item {
39
+ title: string
40
+ desc: string
41
+ path: string
42
+ isSession: boolean
43
+ isAttached?: boolean
44
+ windowCount?: string
45
+ createdAt?: number
46
+ lastActivity?: number
47
+ opencodeStats?: OpencodeSessionStats
48
+ }
49
+
50
+ export interface Config {
51
+ projectPaths: string[]
52
+ reposPath: string
53
+ editor: string
54
+ editorCmd: string
55
+ keybindMode?: KeybindMode
56
+ }
57
+
58
+ export interface WindowInfo {
59
+ index: string
60
+ name: string
61
+ currentPath: string
62
+ currentCommand: string
63
+ }
64
+
65
+ export interface SessionDetails {
66
+ name: string
67
+ isAttached: boolean
68
+ windowCount: string
69
+ windows: WindowInfo[]
70
+ }
71
+
72
+ export interface SearchResult {
73
+ item: Item
74
+ score: number
75
+ }
76
+
77
+ export interface FuzzyMatch {
78
+ score: number
79
+ indices: number[]
80
+ }
@@ -0,0 +1,61 @@
1
+ import { colors } from '../styles/theme'
2
+
3
+ interface Props {
4
+ text: string
5
+ matchIndices?: number[]
6
+ highlightColor?: string
7
+ }
8
+
9
+ /**
10
+ * Renders text with highlighted characters at specified indices
11
+ * Used for fuzzy search match visualization (like fzf/telescope)
12
+ */
13
+ export default function HighlightedText({
14
+ text,
15
+ matchIndices = [],
16
+ highlightColor = colors.action,
17
+ }: Props) {
18
+ // If no matches, return plain text
19
+ if (matchIndices.length === 0) {
20
+ return <>{text}</>
21
+ }
22
+
23
+ // Build array of text segments with their highlight status
24
+ const segments: Array<{ text: string; highlighted: boolean }> = []
25
+ let currentSegment = ''
26
+ let isHighlighted = false
27
+
28
+ for (let i = 0; i < text.length; i++) {
29
+ const shouldHighlight = matchIndices.includes(i)
30
+
31
+ // If highlight status changes, start a new segment
32
+ if (shouldHighlight !== isHighlighted) {
33
+ if (currentSegment) {
34
+ segments.push({ text: currentSegment, highlighted: isHighlighted })
35
+ currentSegment = ''
36
+ }
37
+ isHighlighted = shouldHighlight
38
+ }
39
+
40
+ currentSegment += text[i]
41
+ }
42
+
43
+ // Add the final segment
44
+ if (currentSegment) {
45
+ segments.push({ text: currentSegment, highlighted: isHighlighted })
46
+ }
47
+
48
+ return (
49
+ <>
50
+ {segments.map((segment, i) =>
51
+ segment.highlighted ? (
52
+ <span key={i} style={{ fg: highlightColor }}>
53
+ {segment.text}
54
+ </span>
55
+ ) : (
56
+ <span key={i}>{segment.text}</span>
57
+ )
58
+ )}
59
+ </>
60
+ )
61
+ }
@@ -0,0 +1,93 @@
1
+ import { colors } from '../styles/theme'
2
+ import { AppMode } from '../types'
3
+ import HighlightedText from './HighlightedText'
4
+ import type { Item } from '../types'
5
+
6
+ interface Props {
7
+ items: Item[]
8
+ cursor: number
9
+ appMode: AppMode
10
+ maxItems?: number
11
+ searchQuery?: string
12
+ }
13
+
14
+ export default function ItemList({
15
+ items,
16
+ cursor,
17
+ appMode,
18
+ maxItems = 20,
19
+ searchQuery = '',
20
+ }: Props) {
21
+ const isSearching = searchQuery.trim().length > 0
22
+
23
+ return (
24
+ <>
25
+ {items.slice(0, maxItems).map((item, i) => {
26
+ const titleMatchIndices = (item as any).titleMatchIndices as number[] | undefined
27
+ const descMatchIndices = (item as any).descMatchIndices as number[] | undefined
28
+ const hasTitleMatch = titleMatchIndices && titleMatchIndices.length > 0
29
+ const hasDescMatch = descMatchIndices && descMatchIndices.length > 0
30
+
31
+ return (
32
+ <box
33
+ key={i}
34
+ style={{
35
+ backgroundColor: i === cursor ? colors.backgroundAlt : 'transparent',
36
+ height: 1,
37
+ paddingLeft: 2,
38
+ }}
39
+ >
40
+ {i === cursor && <text> </text>}
41
+ <text>
42
+ {i + 1}{' '}
43
+ {item.isSession ? (
44
+ <>
45
+ <span
46
+ style={{
47
+ fg: item.isAttached ? colors.active : colors.inactive,
48
+ }}
49
+ >
50
+ {item.isAttached ? '●' : '○'}
51
+ </span>{' '}
52
+ {isSearching && hasTitleMatch ? (
53
+ <HighlightedText text={item.title} matchIndices={titleMatchIndices} />
54
+ ) : (
55
+ item.title
56
+ )}{' '}
57
+ <span style={{ fg: colors.inactive }}>({item.windowCount})</span>
58
+ </>
59
+ ) : (
60
+ <>
61
+ {appMode === AppMode.NewSession ? (
62
+ <>
63
+ {/* Show path (desc) with highlighting if it matches */}
64
+ {isSearching && hasDescMatch ? (
65
+ <HighlightedText text={item.desc} matchIndices={descMatchIndices} />
66
+ ) : (
67
+ item.desc
68
+ )}
69
+ </>
70
+ ) : (
71
+ <>
72
+ {isSearching && hasTitleMatch ? (
73
+ <HighlightedText text={item.title} matchIndices={titleMatchIndices} />
74
+ ) : (
75
+ item.title
76
+ )}
77
+ {item.desc && (
78
+ <>
79
+ {' '}
80
+ <span style={{ fg: colors.inactive }}>{item.desc}</span>
81
+ </>
82
+ )}
83
+ </>
84
+ )}
85
+ </>
86
+ )}
87
+ </text>
88
+ </box>
89
+ )
90
+ })}
91
+ </>
92
+ )
93
+ }
@@ -0,0 +1,63 @@
1
+ import { colors } from '../styles/theme'
2
+ import { formatSessionAge } from '../util/time'
3
+ import type { Item, AppMode } from '../types'
4
+ import { AppMode as AppModeEnum } from '../types'
5
+
6
+ interface Props {
7
+ sessions: Item[]
8
+ appMode: AppMode
9
+ cursor: number
10
+ }
11
+
12
+ export default function OpencodeSessionGroup({ sessions, appMode, cursor }: Props) {
13
+ if (sessions.length === 0) return null
14
+
15
+ return (
16
+ <>
17
+ <text style={{ fg: colors.separator, marginTop: 2, marginBottom: 1 }}>
18
+ ─ Opencode Sessions ─
19
+ </text>
20
+ {sessions.map((item, i) => (
21
+ <box
22
+ key={`opencode-${i}`}
23
+ style={{
24
+ height: 1,
25
+ paddingLeft: 2,
26
+ backgroundColor:
27
+ appMode === AppModeEnum.OpencodeManage && i === cursor
28
+ ? colors.backgroundAlt
29
+ : 'transparent',
30
+ }}
31
+ >
32
+ {appMode === AppModeEnum.OpencodeManage && i === cursor && <text> </text>}
33
+ <text>
34
+ {appMode === AppModeEnum.OpencodeManage ? `${i + 1} ` : ' '}
35
+ <span
36
+ style={{
37
+ fg:
38
+ appMode === AppModeEnum.OpencodeManage
39
+ ? item.isAttached
40
+ ? colors.active
41
+ : colors.inactive
42
+ : colors.inactive,
43
+ }}
44
+ >
45
+ {item.isAttached ? '●' : '○'}
46
+ </span>{' '}
47
+ <span
48
+ style={{
49
+ fg: appMode === AppModeEnum.OpencodeManage ? colors.text : colors.inactive,
50
+ }}
51
+ >
52
+ {item.title.padEnd(20)}
53
+ </span>{' '}
54
+ <span style={{ fg: colors.inactive }}>
55
+ {item.createdAt ? formatSessionAge(item.createdAt) : ''}
56
+ </span>
57
+ </text>
58
+ </box>
59
+ ))}
60
+ <box style={{ marginBottom: 2 }} />
61
+ </>
62
+ )
63
+ }