Repository navigation
Expand file tree
/
Copy pathFileExplorerTerminalPathInsertion.swift
More file actions
184 lines (167 loc) · 7.21 KB
/
Copy pathFileExplorerTerminalPathInsertion.swift
File metadata and controls
184 lines (167 loc) · 7.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import AppKit
enum FileExplorerTerminalPathInsertion {
static func insertedText(forPaths paths: [String]) -> String {
TerminalImageTransferPlanner.insertedText(forPathStrings: paths)
}
static func insertedText(forPaths paths: [String], relativeToRootPath rootPath: String) -> String {
insertedText(forPaths: paths.map { relativePath(for: $0, rootPath: rootPath) })
}
static func relativePath(for path: String, rootPath: String) -> String {
let normalizedPath = normalizedFileSystemPath(path)
guard !rootPath.isEmpty else { return normalizedPath }
let normalizedRootPath = normalizedFileSystemPath(rootPath)
if normalizedPath == normalizedRootPath { return "." }
let normalizedRoot = normalizedRootPath == "/" ? "/" : normalizedRootPath + "/"
if normalizedPath.hasPrefix(normalizedRoot) {
return String(normalizedPath.dropFirst(normalizedRoot.count))
}
return normalizedPath
}
private static func normalizedFileSystemPath(_ path: String) -> String {
let path = pathWithoutTrailingSlashes(path)
guard path.hasPrefix("/") else { return path }
return macOSDisplayPath(
pathWithoutTrailingSlashes(URL(fileURLWithPath: path).standardizedFileURL.path)
)
}
private static func macOSDisplayPath(_ path: String) -> String {
let rewrites = [
(privatePath: "/private/tmp", displayPath: "/tmp"),
(privatePath: "/private/var", displayPath: "/var"),
(privatePath: "/private/etc", displayPath: "/etc"),
]
for rewrite in rewrites {
if path == rewrite.privatePath {
return rewrite.displayPath
}
if path.hasPrefix(rewrite.privatePath + "/") {
return rewrite.displayPath + String(path.dropFirst(rewrite.privatePath.count))
}
}
return path
}
private static func pathWithoutTrailingSlashes(_ path: String) -> String {
var result = path
while result.count > 1 && result.hasSuffix("/") {
result.removeLast()
}
return result
}
@MainActor
@discardableResult
static func insert(paths: [String], relativeToRootPath rootPath: String? = nil, intoTerminalFor window: NSWindow?) -> Bool {
let text: String
if let rootPath {
text = insertedText(forPaths: paths, relativeToRootPath: rootPath)
} else {
text = insertedText(forPaths: paths)
}
guard !text.isEmpty else { return false }
guard let terminalPanel = targetTerminalPanel(for: window) else { return false }
terminalPanel.sendText(text)
return true
}
@MainActor
private static func targetTerminalPanel(for window: NSWindow?) -> TerminalPanel? {
guard let appDelegate = AppDelegate.shared else { return nil }
if let window,
let terminalPanel = appDelegate.contextForMainTerminalWindow(window)?.tabManager.selectedWorkspace?.focusedTerminalPanel {
return terminalPanel
}
if let window,
let windowId = appDelegate.mainWindowId(from: window),
let terminalPanel = appDelegate.tabManagerFor(windowId: windowId)?.selectedWorkspace?.focusedTerminalPanel {
return terminalPanel
}
return appDelegate.tabManager?.selectedWorkspace?.focusedTerminalPanel
}
}
extension NSMenu {
func addFileExplorerInsertPathItems(
target: AnyObject,
representedObject: Any,
insertAction: Selector,
insertRelativeAction: Selector
) {
let insertPathItem = NSMenuItem(
title: String(localized: "fileExplorer.contextMenu.insertPath", defaultValue: "Insert Path"),
action: insertAction,
keyEquivalent: ""
)
insertPathItem.target = target
insertPathItem.representedObject = representedObject
addItem(insertPathItem)
let insertRelativePathItem = NSMenuItem(
title: String(localized: "fileExplorer.contextMenu.insertRelativePath", defaultValue: "Insert Relative Path"),
action: insertRelativeAction,
keyEquivalent: ""
)
insertRelativePathItem.target = target
insertRelativePathItem.representedObject = representedObject
addItem(insertRelativePathItem)
}
}
extension FileExplorerPanelView.Coordinator {
@MainActor
private func contextMenuNodes(clicked node: FileExplorerNode) -> [FileExplorerNode] {
guard let outlineView else { return [node] }
let clickedRow = outlineView.clickedRow
let selectedRows = outlineView.selectedRowIndexes
guard clickedRow >= 0, selectedRows.contains(clickedRow) else {
return [node]
}
let nodes = selectedRows.compactMap { row -> FileExplorerNode? in
guard row >= 0, row < outlineView.numberOfRows else { return nil }
return outlineView.item(atRow: row) as? FileExplorerNode
}
return nodes.isEmpty ? [node] : nodes
}
@MainActor
@objc func contextMenuInsertPath(_ sender: NSMenuItem) {
guard let node = sender.representedObject as? FileExplorerNode else { return }
FileExplorerTerminalPathInsertion.insert(
paths: contextMenuNodes(clicked: node).map(\.path),
intoTerminalFor: outlineView?.window ?? containerView?.window
)
}
@MainActor
@objc func contextMenuInsertRelativePath(_ sender: NSMenuItem) {
guard let node = sender.representedObject as? FileExplorerNode else { return }
FileExplorerTerminalPathInsertion.insert(
paths: contextMenuNodes(clicked: node).map(\.path),
relativeToRootPath: store.rootPath,
intoTerminalFor: outlineView?.window ?? containerView?.window
)
}
}
extension FileExplorerContainerView {
@MainActor
private func searchResultsForContextMenu(row: Int) -> [FileSearchResult] {
guard row >= 0, row < searchSnapshot.results.count else { return [] }
let selectedRows = searchResultsView.selectedRowIndexes
guard selectedRows.contains(row) else {
return [searchSnapshot.results[row]]
}
let results = selectedRows.compactMap { selectedRow -> FileSearchResult? in
guard selectedRow >= 0, selectedRow < searchSnapshot.results.count else { return nil }
return searchSnapshot.results[selectedRow]
}
return results.isEmpty ? [searchSnapshot.results[row]] : results
}
@MainActor
@objc func contextMenuInsertSearchResultPath(_ sender: NSMenuItem) {
guard let row = (sender.representedObject as? NSNumber)?.intValue else { return }
FileExplorerTerminalPathInsertion.insert(
paths: searchResultsForContextMenu(row: row).map(\.path),
intoTerminalFor: window
)
}
@MainActor
@objc func contextMenuInsertSearchResultRelativePath(_ sender: NSMenuItem) {
guard let row = (sender.representedObject as? NSNumber)?.intValue else { return }
FileExplorerTerminalPathInsertion.insert(
paths: searchResultsForContextMenu(row: row).map(\.relativePath),
intoTerminalFor: window
)
}
}