Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

1.8 KB
Raw Blame
import { isWindowsAbsolutePathLike } from './cross-platform-path'
import {
  resolveTerminalFileUrlTarget,
  type TerminalFileUrlTarget
} from './terminal-file-url-target'
import { toParsedTerminalFileLink } from './terminal-file-link-detection-ranges'
import type { ParsedTerminalFileLink } from './terminal-links'

export type TerminalOscLinkTarget =
  | { kind: 'url'; url: string }
  | { kind: 'file-url'; target: TerminalFileUrlTarget }
  | { kind: 'path'; link: ParsedTerminalFileLink }

export type TerminalOscLinkTargetOptions = {
  /** URL parsing reads a drive letter as a scheme, so Windows hosts try the path reading first. */
  windowsPathsFirst: boolean
  allowUncHost: boolean
}

/** What an OSC 8 hyperlink's URI points at; callers resolve paths against their own cwd. */
export function classifyTerminalOscLinkTarget(
  rawText: string,
  options: TerminalOscLinkTargetOptions
): TerminalOscLinkTarget | null {
  // Why: a hyperlink names its whole target, so the path is taken as written, never detected in it.
  const pathTarget = (): TerminalOscLinkTarget | null => {
    const link = rawText.includes('://')
      ? null
      : toParsedTerminalFileLink({ text: rawText, startIndex: 0, endIndex: rawText.length })
    return link ? { kind: 'path', link } : null
  }
  const windowsPath =
    options.windowsPathsFirst && isWindowsAbsolutePathLike(rawText) ? pathTarget() : null
  if (windowsPath) {
    return windowsPath
  }
  let parsed: URL
  try {
    parsed = new URL(rawText)
  } catch {
    return pathTarget()
  }
  if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
    return { kind: 'url', url: parsed.toString() }
  }
  if (parsed.protocol !== 'file:') {
    return null
  }
  const target = resolveTerminalFileUrlTarget(parsed, { allowUncHost: options.allowUncHost })
  return target ? { kind: 'file-url', target } : null
}