Skip to content

Commit 228d23a

Browse files
authored
feat: Use terminal title as the terminal tab title (#1298) (#1302)
1 parent b945e9b commit 228d23a

File tree

7 files changed

+504
-6
lines changed

7 files changed

+504
-6
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
2B7AC06B282452FB0082A5B8 /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2B7AC06A282452FB0082A5B8 /* Media.xcassets */; };
6363
2BE487EF28245162003F3F64 /* FinderSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BE487EE28245162003F3F64 /* FinderSync.swift */; };
6464
2BE487F428245162003F3F64 /* OpenWithCodeEdit.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 2BE487EC28245162003F3F64 /* OpenWithCodeEdit.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
65+
3E0196732A3921AC002648D8 /* codeedit_shell_integration.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */; };
66+
3E01967A2A392B45002648D8 /* codeedit_shell_integration.bash in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */; };
6567
474397C52893AC4B00518C8C /* codeedit-midnight.json in Resources */ = {isa = PBXBuildFile; fileRef = 474397C42893AC4B00518C8C /* codeedit-midnight.json */; };
6668
4E7F066629602E7B00BB3C12 /* CodeEditSplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */; };
6769
4EE96ECB2960565E00FFBEA8 /* DocumentsUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */; };
@@ -518,6 +520,8 @@
518520
2BE487EC28245162003F3F64 /* OpenWithCodeEdit.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = OpenWithCodeEdit.appex; sourceTree = BUILT_PRODUCTS_DIR; };
519521
2BE487EE28245162003F3F64 /* FinderSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FinderSync.swift; sourceTree = "<group>"; };
520522
2BE487F028245162003F3F64 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
523+
3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = codeedit_shell_integration.zsh; sourceTree = "<group>"; };
524+
3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = codeedit_shell_integration.bash; sourceTree = "<group>"; };
521525
474397C42893AC4B00518C8C /* codeedit-midnight.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "codeedit-midnight.json"; sourceTree = "<group>"; };
522526
4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeEditSplitViewController.swift; sourceTree = "<group>"; };
523527
4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentsUnitTests.swift; sourceTree = "<group>"; };
@@ -1101,6 +1105,15 @@
11011105
path = OpenWithCodeEdit;
11021106
sourceTree = "<group>";
11031107
};
1108+
3E0196712A392170002648D8 /* ShellIntegration */ = {
1109+
isa = PBXGroup;
1110+
children = (
1111+
3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */,
1112+
3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */,
1113+
);
1114+
path = ShellIntegration;
1115+
sourceTree = "<group>";
1116+
};
11041117
4EE96EC82960562000FFBEA8 /* Documents */ = {
11051118
isa = PBXGroup;
11061119
children = (
@@ -2220,6 +2233,7 @@
22202233
B658FB2E27DA9E0F00EA4DBD /* CodeEdit */ = {
22212234
isa = PBXGroup;
22222235
children = (
2236+
3E0196712A392170002648D8 /* ShellIntegration */,
22232237
5831E3C52933E6CB00D5A6D2 /* Features */,
22242238
D7211D4427E066D4008F2ED7 /* Localization */,
22252239
B658FB3527DA9E1000EA4DBD /* Preview Content */,
@@ -2597,8 +2611,10 @@
25972611
283BDCBD2972EEBD002AFF81 /* Package.resolved in Resources */,
25982612
B658FB3727DA9E1000EA4DBD /* Preview Assets.xcassets in Resources */,
25992613
28A51006281701B40087B0CC /* codeedit-github-dark.json in Resources */,
2614+
3E0196732A3921AC002648D8 /* codeedit_shell_integration.zsh in Resources */,
26002615
28F43DE029738792008BBA45 /* codeedit-solarized-dark.json in Resources */,
26012616
58A5DFA529339F6400D1BD5D /* default_keybindings.json in Resources */,
2617+
3E01967A2A392B45002648D8 /* codeedit_shell_integration.bash in Resources */,
26022618
28A51002281673530087B0CC /* codeedit-xcode-light.json in Resources */,
26032619
28A51005281701B40087B0CC /* codeedit-github-light.json in Resources */,
26042620
D7211D4727E06BFE008F2ED7 /* Localizable.strings in Resources */,

CodeEdit/Features/DebugArea/Views/DebugAreaTerminalTab.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,22 @@ struct DebugAreaTerminalTab: View {
2121
private var isFocused: Bool
2222

2323
var body: some View {
24+
var terminalTitle = Binding<String>(
25+
get: {
26+
self.terminal.title
27+
}, set: {
28+
if $0.trimmingCharacters(in: .whitespaces) == "" && !isFocused {
29+
self.terminal.title = self.terminal.terminalTitle
30+
self.terminal.customTitle = false
31+
} else {
32+
self.terminal.title = $0
33+
self.terminal.customTitle = true
34+
}
35+
}
36+
)
37+
2438
Label {
25-
TextField("Name", text: $terminal.title)
39+
TextField("Name", text: terminalTitle)
2640
.focused($isFocused)
2741
.padding(.leading, -8)
2842
.background {

CodeEdit/Features/DebugArea/Views/DebugAreaTerminalView.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ struct DebugAreaTerminal: Identifiable, Equatable {
1111
var id: UUID
1212
var url: URL?
1313
var title: String
14+
var terminalTitle: String
1415
var shell: String
16+
var customTitle: Bool
1517

1618
init(id: UUID, url: URL, title: String, shell: String) {
1719
self.id = id
1820
self.title = title
21+
self.terminalTitle = title
1922
self.url = url
2023
self.shell = shell
24+
self.customTitle = false
2125
}
2226
}
2327

@@ -107,7 +111,10 @@ struct DebugAreaTerminalView: View {
107111
title: String? = nil
108112
) {
109113
if let newTitle = title {
110-
terminal.title = newTitle
114+
if !terminal.customTitle {
115+
terminal.title = newTitle
116+
}
117+
terminal.terminalTitle = newTitle
111118
}
112119
}
113120

CodeEdit/Features/TerminalEmulator/TerminalEmulatorView+Coordinator.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ extension TerminalEmulatorView {
1414
@State
1515
private var url: URL
1616

17-
init(url: URL) {
17+
public var onTitleChange: (_ title: String) -> Void
18+
19+
init(url: URL, onTitleChange: @escaping (_ title: String) -> Void) {
1820
self._url = .init(wrappedValue: url)
21+
self.onTitleChange = onTitleChange
1922
super.init()
2023
}
2124

2225
func hostCurrentDirectoryUpdate(source: TerminalView, directory: String?) {}
2326

2427
func sizeChanged(source: LocalProcessTerminalView, newCols: Int, newRows: Int) {}
2528

26-
func setTerminalTitle(source: LocalProcessTerminalView, title: String) {}
29+
func setTerminalTitle(source: LocalProcessTerminalView, title: String) {
30+
onTitleChange(title)
31+
}
2732

2833
func processTerminated(source: TerminalView, exitCode: Int32?) {
2934
guard let exitCode else {

CodeEdit/Features/TerminalEmulator/TerminalEmulatorView.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ struct TerminalEmulatorView: NSViewRepresentable {
9292
}
9393
}
9494

95+
private func setupShellTitle(shell: String) {
96+
if let shellSetupScript = Bundle.main.url(forResource: "codeedit_shell_integration", withExtension: shell) {
97+
let scriptPath = (shellSetupScript.absoluteString[7..<shellSetupScript.absoluteString.count]) ?? ""
98+
terminal.send(txt: "source \(scriptPath)\n")
99+
}
100+
101+
terminal.send(txt: "clear\n")
102+
}
103+
95104
private func getTerminalCursor() -> CursorStyle {
96105
let blink = terminalSettings.cursorBlink
97106
switch terminalSettings.cursorStyle {
@@ -195,7 +204,9 @@ struct TerminalEmulatorView: NSViewRepresentable {
195204
terminal.getTerminal().silentLog = true
196205
if TerminalEmulatorView.lastTerminal[url.path] == nil {
197206
let shell = getShell()
198-
let shellIdiom = "-" + NSString(string: shell).lastPathComponent
207+
let shellName = NSString(string: shell).lastPathComponent
208+
onTitleChange(shellName)
209+
let shellIdiom = "-" + shellName
199210

200211
// changes working directory to project root
201212
// TODO: Get rid of FileManager shared instance to prevent problems
@@ -214,6 +225,8 @@ struct TerminalEmulatorView: NSViewRepresentable {
214225
terminal.cursorStyleChanged(source: terminal.getTerminal(), newStyle: getTerminalCursor())
215226
terminal.layer?.backgroundColor = .clear
216227
terminal.optionAsMetaKey = optionAsMeta
228+
229+
setupShellTitle(shell: shellName)
217230
}
218231
terminal.appearance = colorAppearance
219232
scroller?.isHidden = true
@@ -251,6 +264,6 @@ struct TerminalEmulatorView: NSViewRepresentable {
251264
}
252265

253266
func makeCoordinator() -> Coordinator {
254-
Coordinator(url: url)
267+
Coordinator(url: url, onTitleChange: onTitleChange)
255268
}
256269
}

0 commit comments

Comments
 (0)