Repository: terminal
Issue: Enable Clickable Directory Paths in Windows Terminal to Seamlessly Open in File Explorer - Just Like URLs
Labels: Issue-Feature,Needs-Triage,Needs-Tag-Fix

### Description of the new feature

Windows Terminal currently displays URLs in a clickable format, allowing users to quickly navigate to web pages directly from the terminal. However, directory paths, such as those displayed in commands like `winget --info` or during output logs, are not clickable. This feature request proposes making directory paths clickable, enabling users to open the corresponding folder in File Explorer with a single click.

# Feature-Use-Benefit Analysis:

## Ease of Access and Workflow Optimization:

Clicking directory paths directly would save users the time and effort of manually copying the path, opening File Explorer, and pasting it in the address bar.
This would be especially beneficial for developers, system administrators, and power users who frequently work with file paths, logs, and configurations, as shown in the screenshot where paths to DiagOutputDir or settings.json are displayed.

## Consistency with Existing Functionality:

Windows Terminal already supports clickable URLs, which enhance usability and workflow efficiency. Extending this functionality to directory paths would create a cohesive and intuitive user experience.
Improved Error Resolution and Debugging:

For users troubleshooting logs or debugging application issues, being able to instantly open a directory reduces the cognitive load and speeds up the process of locating files or folders referenced in the terminal.

## Broader Use Cases and Adoption:

This feature aligns with the needs of diverse user groups, from beginner users navigating the file system to advanced users leveraging tools like winget, as shown in the screenshot.

![Image](https://github.com/user-attachments/assets/4c96ea4a-fd7b-4ff6-bf11-a8c827e38f20)


### Proposed technical implementation details

# Implementation Timeline

## Phase 1: Path Detection
- Develop and test regex for detecting paths.
- Integrate path detection into the rendering pipeline.

## Phase 2: Clickable Rendering
- Implement tokenization and styling for clickable paths.
- Add configuration options for enabling/disabling the feature.

## Phase 3: Event Handling
- Implement mouse click handling and command execution.
- Add error handling and user feedback mechanisms.

## Phase 4: Optimization and Accessibility
- Optimize performance for large outputs.
- Add keyboard navigation and screen reader support.

## Phase 5: Testing and Release
- Conduct extensive testing.
- Release the feature as part of a Windows Terminal Preview build.

# Proposed Technical Implementation

## 1. Directory Path Detection

### Regex for Path Matching:
Create a robust regular expression to detect valid directory paths within terminal output. The regex must:
- Identify Windows-style paths (`C:\Users\username\Documents`).
- Handle spaces, special characters, and network paths (`\\server\share`).
- Consider relative paths like `./subfolder` and `../parentfolder`.

#### Example Regex:
```
(?:(?:[a-zA-Z]:\\)|(?:\\\\[^\s\\]+\\[^\s\\]+))[^<>:"/\\|?*\n\r]*[^<>:"/\\|?*\n\r\\]
```

### Integration into Output Rendering:
- Modify the terminal's rendering pipeline to scan each output line for matches using the regex.
- Detected paths should be tagged with metadata (e.g., `isClickable: true`).

## 2. Rendering Clickable Paths

### Tokenization and Styling:
- Tokenize the terminal output into clickable and non-clickable segments.
- Highlight clickable paths using underline styling or a distinct color, similar to clickable URLs already supported in Windows Terminal.
- Use the existing hyperlink styling engine to render these paths with visual feedback.

### Context Awareness:
- Ensure that clickable paths are contextually accurate:
  - Validate the detected path (e.g., check if it exists).
  - Only render paths as clickable if they are valid on the user’s file system.

## 3. Event Handling for Click Actions

### Mouse Click Handling:
- Extend the existing event-handling system to respond to clicks on clickable paths:
  - Capture the click event and determine if it occurred on a clickable path.
  - Extract the full path string from the metadata associated with the clickable segment.

### Execution of the `explorer` Command:
- Trigger the `explorer` command to open the directory in File Explorer:
  ```cmd
  explorer "C:\path\to\directory"
  ```
- Use secure asynchronous system calls to avoid freezing the terminal during command execution.

### Error Handling:
- If the directory does not exist or the `explorer` command fails, display an appropriate error message in the terminal.

## 4. Configuration Options

### Enable/Disable Clickable Paths:
- Allow users to turn the feature on or off.
#### Example setting in `settings.json`:
```json
"clickablePaths": true
```

### Behavior on Click:
- Define default behavior for clicks:
  - Open in File Explorer.
  - Copy the path to the clipboard.
  - Open the folder in a new terminal tab.

#### Example configuration:
```json
"pathClickBehavior": "openInExplorer" // Options: "openInExplorer", "copyToClipboard", "openInTerminal"
```

## 5. Keyboard Accessibility

### Keyboard Navigation:
- Allow users to navigate between clickable paths using keyboard shortcuts (e.g., Tab or arrow keys).
- Use Enter or a custom key combination (e.g., Ctrl+Enter) to open the selected path.

### Accessibility Considerations:
- Ensure the feature is accessible to screen readers by providing descriptive tooltips or metadata for each clickable path.

## 6. Performance Optimization

### Efficient Path Matching:
- Optimize regex processing to avoid slowing down terminal rendering, especially for outputs with large amounts of text.
- Implement lazy evaluation or process output lines incrementally.

### Caching:
- Cache the results of path validation to minimize repeated checks for the same paths.

## 7. Testing and Validation

### Unit Tests:
- Test regex patterns against a variety of valid and invalid paths to ensure accuracy.
- Validate behavior for edge cases, such as extremely long paths or invalid characters.

### Integration Tests:
- Simulate real-world terminal outputs to verify rendering and click behavior.
- Ensure compatibility across different themes, fonts, and screen resolutions.

### User Testing:
- Collect feedback from beta testers to refine usability and performance.