mux-sesh 1.8.0 → 1.9.1

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.
@@ -19,14 +19,17 @@ const PROJECT_MARKERS = [
19
19
  'flake.nix',
20
20
  ]
21
21
 
22
+ export const PRUNED_PROJECT_DIRECTORY_NAMES = [
23
+ '.git',
24
+ '.jj',
25
+ 'node_modules',
26
+ 'target',
27
+ 'build',
28
+ 'dist',
29
+ ]
30
+
22
31
  export function isIgnoredProjectDirectory(baseName: string): boolean {
23
- return (
24
- baseName.startsWith('.') ||
25
- baseName === 'node_modules' ||
26
- baseName === 'target' ||
27
- baseName === 'build' ||
28
- baseName === 'dist'
29
- )
32
+ return baseName.startsWith('.') || PRUNED_PROJECT_DIRECTORY_NAMES.includes(baseName)
30
33
  }
31
34
 
32
35
  export function toProjectItem(projectPath: string): Item {
@@ -5,6 +5,7 @@ import {
5
5
  buildCacheKey,
6
6
  getCachedDiscovery,
7
7
  getExistingProjectRoots,
8
+ PRUNED_PROJECT_DIRECTORY_NAMES,
8
9
  toProjectItem,
9
10
  type DiscoveryCacheEntry,
10
11
  } from './project-discovery'
@@ -26,7 +27,24 @@ async function scanDirectories(projectRoots: string[], maxDepth: number): Promis
26
27
  return []
27
28
  }
28
29
 
29
- const args = [...projectRoots, '-mindepth', '1', '-maxdepth', String(maxDepth), '-type', 'd']
30
+ const pruneExpression = PRUNED_PROJECT_DIRECTORY_NAMES.flatMap((name, index) =>
31
+ index === 0 ? ['-name', name] : ['-o', '-name', name]
32
+ )
33
+ const args = [
34
+ ...projectRoots,
35
+ '-mindepth',
36
+ '1',
37
+ '-maxdepth',
38
+ String(maxDepth),
39
+ '(',
40
+ ...pruneExpression,
41
+ ')',
42
+ '-prune',
43
+ '-o',
44
+ '-type',
45
+ 'd',
46
+ '-print',
47
+ ]
30
48
  const proc = spawn(['find', ...args], {
31
49
  stderr: 'pipe',
32
50
  })
@@ -48,7 +48,7 @@ export function isWithinProjectRoots(projectPath: string, projectRoots: string[]
48
48
 
49
49
  function rankItemsByZoxide(items: Item[], entries: ZoxideEntry[], config: Config): Item[] {
50
50
  if (config.zoxideMode === 'off' || entries.length === 0) {
51
- return items.sort((a, b) => a.title.localeCompare(b.title))
51
+ return [...items].sort((a, b) => a.title.localeCompare(b.title))
52
52
  }
53
53
 
54
54
  const zoxideScores = new Map(
@@ -1,3 +1,5 @@
1
+ import type { OpencodeStats } from '../opencode/types'
2
+
1
3
  export enum AppMode {
2
4
  Normal,
3
5
  Search,
@@ -12,7 +14,7 @@ export enum ViewMode {
12
14
 
13
15
  export type KeybindMode = 'vim' | 'standard'
14
16
  export type ZoxideMode = 'off' | 'rank' | 'merge'
15
- export type ItemKind = 'tmux' | 'configured' | 'project'
17
+ export type ItemKind = 'tmux' | 'configured' | 'project' | 'file'
16
18
  export type SortOrder = 'live-first' | 'configured-first' | 'zoxide-first' | 'alphabetical'
17
19
 
18
20
  export interface IconConfig {
@@ -104,25 +106,7 @@ export interface ProjectWildcard {
104
106
  previewCommand?: string
105
107
  }
106
108
 
107
- export interface OpencodeSessionStats {
108
- sessionID?: string
109
- title?: string
110
- projectID?: string
111
- directory?: string
112
- messageCount?: number
113
- filesModified?: number
114
- additions?: number
115
- deletions?: number
116
- createdAt?: number
117
- updatedAt?: number
118
- // Aggregated stats (for home directory sessions)
119
- sessionCount?: number
120
- mostRecentTitle?: string
121
- // Current session live stats (last message)
122
- lastMessageTokens?: number
123
- contextLimit?: number
124
- sessionTotalCost?: number
125
- }
109
+ export type OpencodeSessionStats = OpencodeStats
126
110
 
127
111
  export type OpencodeStatsState =
128
112
  | { status: 'loading' }
@@ -37,7 +37,11 @@ export default function ItemList({
37
37
  const hasTitleMatch = titleMatchIndices && titleMatchIndices.length > 0
38
38
  const hasDescMatch = descMatchIndices && descMatchIndices.length > 0
39
39
  const icon = getItemIconPresentation(theme, item, icons)
40
- const sessionStatusLabel = item.linkedSessionName ? 'attach' : 'create'
40
+ const sessionStatusLabel = item.linkedSessionName
41
+ ? 'attach'
42
+ : item.itemKind === 'file'
43
+ ? 'open'
44
+ : 'create'
41
45
  const linkedSessionLabel = item.linkedSessionName ? ` -> ${item.linkedSessionName}` : ''
42
46
  const pendingKill = item.isSession && item.title === pendingKillSessionName
43
47
  const selected = absoluteIndex === cursor
@@ -1,5 +1,6 @@
1
1
  import { useEffect, useState } from 'react'
2
- import { getProjectPreview, type ProjectPreview } from '../preview/project'
2
+ import { getFilePreview, getProjectPreview, type ProjectPreview } from '../preview/project'
3
+ import { isFileItem } from '../files/target'
3
4
  import { getSessionDetails } from '../tmux'
4
5
  import { getDetailPanelStyle, useTheme } from '../styles/theme'
5
6
  import type { Config, Item, SessionDetails } from '../types'
@@ -24,6 +25,8 @@ function formatSourceLabel(source: ProjectPreview['source']): string {
24
25
  return 'Wildcard rule'
25
26
  case 'default':
26
27
  return 'Default rule'
28
+ case 'file':
29
+ return 'File target'
27
30
  }
28
31
  }
29
32
 
@@ -179,11 +182,9 @@ export default function SessionDetailsPanel({ selectedItem, config }: Props) {
179
182
  return
180
183
  }
181
184
 
182
- const details = await getProjectPreview(
183
- selectedItem.path,
184
- config,
185
- selectedItem.linkedSessionName
186
- )
185
+ const details = isFileItem(selectedItem)
186
+ ? await getFilePreview(selectedItem.path, config, selectedItem.linkedSessionName)
187
+ : await getProjectPreview(selectedItem.path, config, selectedItem.linkedSessionName)
187
188
  if (!cancelled) {
188
189
  setDetailState({ status: 'project', details })
189
190
  }
@@ -1,4 +1,5 @@
1
1
  import type { SessionSection } from '../items/order'
2
+ import { isOpencodeSessionItem } from '../opencode/session-name'
2
3
  import type { IconConfig, Item, ThemeColors } from '../types'
3
4
 
4
5
  export interface ItemIconPresentation {
@@ -6,6 +7,8 @@ export interface ItemIconPresentation {
6
7
  color: string
7
8
  }
8
9
 
10
+ const FILE_ITEM_GLYPH = '  '
11
+
9
12
  function getIconConfig(icons?: IconConfig): IconConfig {
10
13
  return {
11
14
  tmux: icons?.tmux ?? '',
@@ -29,11 +32,21 @@ export function getItemIconPresentation(
29
32
  }
30
33
  }
31
34
 
35
+ if (item.itemKind === 'file') {
36
+ return {
37
+ glyph: item.icon ?? FILE_ITEM_GLYPH,
38
+ color: item.linkedSessionAttached
39
+ ? theme.active
40
+ : item.linkedSessionName
41
+ ? theme.inactive
42
+ : theme.fileTree,
43
+ }
44
+ }
45
+
32
46
  if (item.isSession) {
33
47
  return {
34
48
  glyph:
35
- item.icon ??
36
- (item.title.startsWith('opencode-') ? resolvedIcons.opencode : resolvedIcons.tmux),
49
+ item.icon ?? (isOpencodeSessionItem(item) ? resolvedIcons.opencode : resolvedIcons.tmux),
37
50
  color: item.isAttached ? theme.active : theme.inactive,
38
51
  }
39
52
  }
@@ -3,6 +3,14 @@ export interface VisibleWindow<T> {
3
3
  startIndex: number
4
4
  }
5
5
 
6
+ export function clampCursorIndex(length: number, cursor: number): number {
7
+ if (length <= 0) {
8
+ return 0
9
+ }
10
+
11
+ return Math.min(Math.max(cursor, 0), length - 1)
12
+ }
13
+
6
14
  export function getVisibleWindow<T>(
7
15
  items: T[],
8
16
  cursor: number,
@@ -17,7 +25,8 @@ export function getVisibleWindow<T>(
17
25
 
18
26
  const visibleCount = Math.max(1, Math.min(maxItems, items.length))
19
27
  const maxStartIndex = Math.max(0, items.length - visibleCount)
20
- const startIndex = Math.min(Math.max(0, cursor - Math.floor(visibleCount / 2)), maxStartIndex)
28
+ const safeCursor = clampCursorIndex(items.length, cursor)
29
+ const startIndex = Math.min(Math.max(0, safeCursor - Math.floor(visibleCount / 2)), maxStartIndex)
21
30
 
22
31
  return {
23
32
  items: items.slice(startIndex, startIndex + visibleCount),
@@ -3,29 +3,65 @@ import { join } from 'path'
3
3
  import { mkdir, stat } from 'fs/promises'
4
4
  import type { Config } from '../types'
5
5
 
6
- export function isGitHubURL(input: string): boolean {
7
- const trimmed = input.trim()
8
- return trimmed.startsWith('https://github.com/') || trimmed.startsWith('git@github.com:')
6
+ interface GitHubRepoRef {
7
+ owner: string
8
+ repo: string
9
9
  }
10
10
 
11
- export function extractRepoName(url: string): string {
12
- const trimmed = url.trim()
11
+ function isSafeGitHubPathSegment(value: string): boolean {
12
+ return value.length > 0 && value !== '.' && value !== '..' && /^[A-Za-z0-9._-]+$/.test(value)
13
+ }
14
+
15
+ function stripGitSuffix(value: string): string {
16
+ return value.endsWith('.git') ? value.slice(0, -4) : value
17
+ }
18
+
19
+ function parseGitHubRepo(input: string): GitHubRepoRef | null {
20
+ const trimmed = input.trim()
21
+
22
+ if (trimmed.startsWith('git@github.com:')) {
23
+ const path = stripGitSuffix(trimmed.slice('git@github.com:'.length))
24
+ const parts = path.split('/').filter(Boolean)
25
+ if (parts.length !== 2 || !parts.every(isSafeGitHubPathSegment)) {
26
+ return null
27
+ }
28
+
29
+ return { owner: parts[0], repo: parts[1] }
30
+ }
31
+
32
+ try {
33
+ const url = new URL(trimmed)
34
+ if (url.protocol !== 'https:' || url.hostname !== 'github.com') {
35
+ return null
36
+ }
13
37
 
14
- if (trimmed.startsWith('https://github.com/')) {
15
- const path = trimmed.replace('https://github.com/', '').replace('.git', '')
16
- const parts = path.split('/')
17
- if (parts.length >= 2) {
18
- return parts[1]
38
+ const parts = url.pathname.split('/').filter(Boolean)
39
+ if (parts.length !== 2) {
40
+ return null
19
41
  }
20
- } else if (trimmed.startsWith('git@github.com:')) {
21
- const path = trimmed.replace('git@github.com:', '').replace('.git', '')
22
- const parts = path.split('/')
23
- if (parts.length >= 2) {
24
- return parts[1]
42
+
43
+ const owner = parts[0]
44
+ const repo = stripGitSuffix(parts[1])
45
+ if (!isSafeGitHubPathSegment(owner) || !isSafeGitHubPathSegment(repo)) {
46
+ return null
25
47
  }
48
+
49
+ return { owner, repo }
50
+ } catch {
51
+ return null
26
52
  }
53
+ }
54
+
55
+ function isMissingPathError(error: unknown): boolean {
56
+ return typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT'
57
+ }
58
+
59
+ export function isGitHubURL(input: string): boolean {
60
+ return parseGitHubRepo(input) !== null
61
+ }
27
62
 
28
- return ''
63
+ export function extractRepoName(url: string): string {
64
+ return parseGitHubRepo(url)?.repo ?? ''
29
65
  }
30
66
 
31
67
  export async function cloneGitHubRepo(url: string, config: Config): Promise<string> {
@@ -40,15 +76,24 @@ export async function cloneGitHubRepo(url: string, config: Config): Promise<stri
40
76
  const targetDir = join(reposDir, repoName)
41
77
 
42
78
  try {
43
- await stat(targetDir)
79
+ const targetStat = await stat(targetDir)
80
+ if (!targetStat.isDirectory()) {
81
+ throw new Error(`Clone target exists but is not a directory: ${targetDir}`)
82
+ }
83
+
44
84
  return targetDir
45
- } catch {}
85
+ } catch (error) {
86
+ if (!isMissingPathError(error)) {
87
+ throw error
88
+ }
89
+ }
46
90
 
47
- const proc = spawn(['git', 'clone', url, targetDir])
91
+ const proc = spawn(['git', 'clone', url, targetDir], { stderr: 'pipe' })
92
+ const stderr = proc.stderr instanceof ReadableStream ? await new Response(proc.stderr).text() : ''
48
93
  await proc.exited
49
94
 
50
95
  if (proc.exitCode !== 0) {
51
- throw new Error('Failed to clone repository')
96
+ throw new Error(stderr.trim() || 'Failed to clone repository')
52
97
  }
53
98
 
54
99
  return targetDir
@@ -1,6 +1,6 @@
1
1
  import { basename, dirname } from 'path'
2
2
 
3
- function toHomeRelativePath(path: string): string {
3
+ export function toHomeRelativePath(path: string): string {
4
4
  const homeDir = process.env.HOME || ''
5
5
 
6
6
  if (!path) {
@@ -0,0 +1,3 @@
1
+ export function quoteShellArg(value: string): string {
2
+ return `'${value.replaceAll("'", "'\\''")}'`
3
+ }
@@ -14,7 +14,7 @@ const TERMINAL_RESET_SEQUENCE = [
14
14
 
15
15
  const state: {
16
16
  renderer?: CliRenderer
17
- exiting?: Promise<never>
17
+ exiting?: Promise<void>
18
18
  listenersInstalled?: boolean
19
19
  } = {}
20
20
 
@@ -60,15 +60,15 @@ export function initializeShutdown(renderer: CliRenderer): void {
60
60
  state.listenersInstalled = true
61
61
  }
62
62
 
63
- export function requestShutdown(code = 0): Promise<never> {
63
+ export function requestShutdown(code = 0): Promise<void> {
64
64
  if (state.exiting) {
65
65
  return state.exiting
66
66
  }
67
67
 
68
68
  state.exiting = (async () => {
69
+ process.exitCode = code
69
70
  destroyRenderer()
70
71
  resetTerminalModes()
71
- process.exit(code)
72
72
  })()
73
73
 
74
74
  return state.exiting