Skip to content

Commit 23edf0e

Browse files
Stop Modifying User's RC Files (#1753)
1 parent 1e7b9ce commit 23edf0e

15 files changed

+632
-148
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
30AB4EBD2BF71CA800ED4431 /* DeveloperSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30AB4EBC2BF71CA800ED4431 /* DeveloperSettingsView.swift */; };
6969
30AB4EC22BF7253200ED4431 /* KeyValueTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30AB4EC12BF7253200ED4431 /* KeyValueTable.swift */; };
7070
30E6D0012A6E505200A58B20 /* NavigatorSidebarViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E6D0002A6E505200A58B20 /* NavigatorSidebarViewModel.swift */; };
71-
3E0196732A3921AC002648D8 /* codeedit_shell_integration.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */; };
71+
3E0196732A3921AC002648D8 /* codeedit_shell_integration_rc.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196722A3921AC002648D8 /* codeedit_shell_integration_rc.zsh */; };
7272
3E01967A2A392B45002648D8 /* codeedit_shell_integration.bash in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */; };
7373
4E7F066629602E7B00BB3C12 /* CodeEditSplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */; };
7474
4EE96ECB2960565E00FFBEA8 /* DocumentsUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */; };
@@ -314,6 +314,7 @@
314314
6C18620A298BF5A800C663EA /* RecentProjectsListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C186209298BF5A800C663EA /* RecentProjectsListView.swift */; };
315315
6C1CC9982B1E770B0002349B /* AsyncFileIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C1CC9972B1E770B0002349B /* AsyncFileIterator.swift */; };
316316
6C1CC99B2B1E7CBC0002349B /* FindNavigatorIndexBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C1CC99A2B1E7CBC0002349B /* FindNavigatorIndexBar.swift */; };
317+
6C1F3DA22C18C55800F6DEF6 /* ShellIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C1F3DA12C18C55800F6DEF6 /* ShellIntegrationTests.swift */; };
317318
6C2C155829B4F49100EA60A5 /* SplitViewItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C2C155729B4F49100EA60A5 /* SplitViewItem.swift */; };
318319
6C2C155A29B4F4CC00EA60A5 /* Variadic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C2C155929B4F4CC00EA60A5 /* Variadic.swift */; };
319320
6C2C155D29B4F4E500EA60A5 /* SplitViewReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C2C155C29B4F4E500EA60A5 /* SplitViewReader.swift */; };
@@ -324,6 +325,12 @@
324325
6C4104E9297C970F00F472BA /* AboutDefaultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C4104E8297C970F00F472BA /* AboutDefaultView.swift */; };
325326
6C48B5C52C0A2835001E9955 /* FileEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5C42C0A2835001E9955 /* FileEncoding.swift */; };
326327
6C48B5C92C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */; };
328+
6C48B5CE2C0C1BE4001E9955 /* Shell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5CD2C0C1BE4001E9955 /* Shell.swift */; };
329+
6C48B5D12C0D0519001E9955 /* ShellIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */; };
330+
6C48B5D42C0D0743001E9955 /* codeedit_shell_integration_env.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */; };
331+
6C48B5D62C0D08C5001E9955 /* codeedit_shell_integration_profile.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 6C48B5D52C0D08C5001E9955 /* codeedit_shell_integration_profile.zsh */; };
332+
6C48B5D82C0D5DB5001E9955 /* codeedit_shell_integration_login.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 6C48B5D72C0D5DB5001E9955 /* codeedit_shell_integration_login.zsh */; };
333+
6C48B5DA2C0D5FC5001E9955 /* CurrentUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5D92C0D5FC5001E9955 /* CurrentUser.swift */; };
327334
6C48D8F22972DAFC00D6D205 /* Env+IsFullscreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F12972DAFC00D6D205 /* Env+IsFullscreen.swift */; };
328335
6C48D8F42972DB1A00D6D205 /* Env+Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F32972DB1A00D6D205 /* Env+Window.swift */; };
329336
6C48D8F72972E5F300D6D205 /* WindowObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48D8F62972E5F300D6D205 /* WindowObserver.swift */; };
@@ -642,7 +649,7 @@
642649
30AB4EBC2BF71CA800ED4431 /* DeveloperSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperSettingsView.swift; sourceTree = "<group>"; };
643650
30AB4EC12BF7253200ED4431 /* KeyValueTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyValueTable.swift; sourceTree = "<group>"; };
644651
30E6D0002A6E505200A58B20 /* NavigatorSidebarViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigatorSidebarViewModel.swift; sourceTree = "<group>"; };
645-
3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = codeedit_shell_integration.zsh; sourceTree = "<group>"; };
652+
3E0196722A3921AC002648D8 /* codeedit_shell_integration_rc.zsh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = codeedit_shell_integration_rc.zsh; sourceTree = "<group>"; };
646653
3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = codeedit_shell_integration.bash; sourceTree = "<group>"; };
647654
4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeEditSplitViewController.swift; sourceTree = "<group>"; };
648655
4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentsUnitTests.swift; sourceTree = "<group>"; };
@@ -885,6 +892,7 @@
885892
6C186209298BF5A800C663EA /* RecentProjectsListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RecentProjectsListView.swift; sourceTree = "<group>"; };
886893
6C1CC9972B1E770B0002349B /* AsyncFileIterator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncFileIterator.swift; sourceTree = "<group>"; };
887894
6C1CC99A2B1E7CBC0002349B /* FindNavigatorIndexBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNavigatorIndexBar.swift; sourceTree = "<group>"; };
895+
6C1F3DA12C18C55800F6DEF6 /* ShellIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellIntegrationTests.swift; sourceTree = "<group>"; };
888896
6C2C155729B4F49100EA60A5 /* SplitViewItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitViewItem.swift; sourceTree = "<group>"; };
889897
6C2C155929B4F4CC00EA60A5 /* Variadic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Variadic.swift; sourceTree = "<group>"; };
890898
6C2C155C29B4F4E500EA60A5 /* SplitViewReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitViewReader.swift; sourceTree = "<group>"; };
@@ -894,6 +902,12 @@
894902
6C4104E8297C970F00F472BA /* AboutDefaultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDefaultView.swift; sourceTree = "<group>"; };
895903
6C48B5C42C0A2835001E9955 /* FileEncoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileEncoding.swift; sourceTree = "<group>"; };
896904
6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSTextStorage+isEmpty.swift"; sourceTree = "<group>"; };
905+
6C48B5CD2C0C1BE4001E9955 /* Shell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shell.swift; sourceTree = "<group>"; };
906+
6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellIntegration.swift; sourceTree = "<group>"; };
907+
6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */ = {isa = PBXFileReference; lastKnownFileType = text; path = codeedit_shell_integration_env.zsh; sourceTree = "<group>"; };
908+
6C48B5D52C0D08C5001E9955 /* codeedit_shell_integration_profile.zsh */ = {isa = PBXFileReference; lastKnownFileType = text; path = codeedit_shell_integration_profile.zsh; sourceTree = "<group>"; };
909+
6C48B5D72C0D5DB5001E9955 /* codeedit_shell_integration_login.zsh */ = {isa = PBXFileReference; lastKnownFileType = text; name = codeedit_shell_integration_login.zsh; path = ../Features/TerminalEmulator/codeedit_shell_integration_login.zsh; sourceTree = "<group>"; };
910+
6C48B5D92C0D5FC5001E9955 /* CurrentUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUser.swift; sourceTree = "<group>"; };
897911
6C48D8F12972DAFC00D6D205 /* Env+IsFullscreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Env+IsFullscreen.swift"; sourceTree = "<group>"; };
898912
6C48D8F32972DB1A00D6D205 /* Env+Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Env+Window.swift"; sourceTree = "<group>"; };
899913
6C48D8F62972E5F300D6D205 /* WindowObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowObserver.swift; sourceTree = "<group>"; };
@@ -1359,7 +1373,10 @@
13591373
isa = PBXGroup;
13601374
children = (
13611375
3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */,
1362-
3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */,
1376+
3E0196722A3921AC002648D8 /* codeedit_shell_integration_rc.zsh */,
1377+
6C48B5D52C0D08C5001E9955 /* codeedit_shell_integration_profile.zsh */,
1378+
6C48B5D72C0D5DB5001E9955 /* codeedit_shell_integration_login.zsh */,
1379+
6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */,
13631380
);
13641381
path = ShellIntegration;
13651382
sourceTree = "<group>";
@@ -1732,8 +1749,8 @@
17321749
5879827E292ED0FB0085B254 /* TerminalEmulator */ = {
17331750
isa = PBXGroup;
17341751
children = (
1735-
58798280292ED0FB0085B254 /* TerminalEmulatorView.swift */,
1736-
58798281292ED0FB0085B254 /* TerminalEmulatorView+Coordinator.swift */,
1752+
6C48B5DB2C0D664A001E9955 /* Model */,
1753+
6C48B5DC2C0D6654001E9955 /* Views */,
17371754
);
17381755
path = TerminalEmulator;
17391756
sourceTree = "<group>";
@@ -1767,11 +1784,12 @@
17671784
587B60FE293416C900D5CD8F /* Features */ = {
17681785
isa = PBXGroup;
17691786
children = (
1770-
613899BD2B6E70E200A5CAF6 /* Search */,
17711787
283BDCC22972F211002AFF81 /* Acknowledgements */,
17721788
4EE96EC82960562000FFBEA8 /* Documents */,
17731789
583E527429361B39001AB554 /* CodeEditUI */,
17741790
587B612C2934199800D5CD8F /* CodeFile */,
1791+
613899BD2B6E70E200A5CAF6 /* Search */,
1792+
6C1F3DA02C18C53C00F6DEF6 /* TerminalEmulator */,
17751793
);
17761794
path = Features;
17771795
sourceTree = "<group>";
@@ -2442,6 +2460,14 @@
24422460
path = FindNavigatorResultList;
24432461
sourceTree = "<group>";
24442462
};
2463+
6C1F3DA02C18C53C00F6DEF6 /* TerminalEmulator */ = {
2464+
isa = PBXGroup;
2465+
children = (
2466+
6C1F3DA12C18C55800F6DEF6 /* ShellIntegrationTests.swift */,
2467+
);
2468+
path = TerminalEmulator;
2469+
sourceTree = "<group>";
2470+
};
24452471
6C48B5C82C0B5F7A001E9955 /* NSTextStorage */ = {
24462472
isa = PBXGroup;
24472473
children = (
@@ -2451,6 +2477,25 @@
24512477
path = CodeEdit/Utils/Extensions/NSTextStorage;
24522478
sourceTree = SOURCE_ROOT;
24532479
};
2480+
6C48B5DB2C0D664A001E9955 /* Model */ = {
2481+
isa = PBXGroup;
2482+
children = (
2483+
6C48B5D92C0D5FC5001E9955 /* CurrentUser.swift */,
2484+
6C48B5CD2C0C1BE4001E9955 /* Shell.swift */,
2485+
6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */,
2486+
);
2487+
path = Model;
2488+
sourceTree = "<group>";
2489+
};
2490+
6C48B5DC2C0D6654001E9955 /* Views */ = {
2491+
isa = PBXGroup;
2492+
children = (
2493+
58798280292ED0FB0085B254 /* TerminalEmulatorView.swift */,
2494+
58798281292ED0FB0085B254 /* TerminalEmulatorView+Coordinator.swift */,
2495+
);
2496+
path = Views;
2497+
sourceTree = "<group>";
2498+
};
24542499
6C48D8EF2972DAC300D6D205 /* Environment */ = {
24552500
isa = PBXGroup;
24562501
children = (
@@ -3231,12 +3276,15 @@
32313276
B6FF04782B6C08AC002C2C78 /* DefaultThemes in Resources */,
32323277
283BDCBD2972EEBD002AFF81 /* Package.resolved in Resources */,
32333278
B658FB3727DA9E1000EA4DBD /* Preview Assets.xcassets in Resources */,
3234-
3E0196732A3921AC002648D8 /* codeedit_shell_integration.zsh in Resources */,
3279+
3E0196732A3921AC002648D8 /* codeedit_shell_integration_rc.zsh in Resources */,
32353280
58A5DFA529339F6400D1BD5D /* default_keybindings.json in Resources */,
3281+
6C48B5D42C0D0743001E9955 /* codeedit_shell_integration_env.zsh in Resources */,
32363282
3E01967A2A392B45002648D8 /* codeedit_shell_integration.bash in Resources */,
32373283
D7211D4727E06BFE008F2ED7 /* Localizable.strings in Resources */,
3284+
6C48B5D62C0D08C5001E9955 /* codeedit_shell_integration_profile.zsh in Resources */,
32383285
284DC8512978BA2600BF2770 /* .all-contributorsrc in Resources */,
32393286
B658FB3427DA9E1000EA4DBD /* Assets.xcassets in Resources */,
3287+
6C48B5D82C0D5DB5001E9955 /* codeedit_shell_integration_login.zsh in Resources */,
32403288
6C6BD6FC29CD152400235D17 /* codeedit.extension.appextensionpoint in Resources */,
32413289
);
32423290
runOnlyForDeploymentPostprocessing = 0;
@@ -3400,6 +3448,7 @@
34003448
611192022B08CCDC00D4459B /* SearchIndexer+Search.swift in Sources */,
34013449
04BA7C272AE2E9F100584E1C /* GitClient+Push.swift in Sources */,
34023450
B664C3B32B96634F00816B4E /* NavigationSettingsView.swift in Sources */,
3451+
6C48B5D12C0D0519001E9955 /* ShellIntegration.swift in Sources */,
34033452
2B7A583527E4BA0100D25D4E /* AppDelegate.swift in Sources */,
34043453
D7012EE827E757850001E1EF /* FindNavigatorView.swift in Sources */,
34053454
58A5DF8029325B5A00D1BD5D /* GitClient.swift in Sources */,
@@ -3648,6 +3697,7 @@
36483697
B65B11042B09DB1C002852CF /* GitClient+Fetch.swift in Sources */,
36493698
5878DA872918642F00DD95A3 /* AcknowledgementsViewModel.swift in Sources */,
36503699
B6E41C7929DE02800088F9F4 /* AccountSelectionView.swift in Sources */,
3700+
6C48B5CE2C0C1BE4001E9955 /* Shell.swift in Sources */,
36513701
6CA1AE952B46950000378EAB /* EditorInstance.swift in Sources */,
36523702
30AB4EBB2BF718A100ED4431 /* DeveloperSettings.swift in Sources */,
36533703
B6C4F2A92B3CB00100B2B140 /* CommitDetailsHeaderView.swift in Sources */,
@@ -3661,6 +3711,7 @@
36613711
61A53A7E2B4449870093BF8A /* WorkspaceDocument+Find.swift in Sources */,
36623712
6CABB19E29C5591D00340467 /* NSTableViewWrapper.swift in Sources */,
36633713
5879821B292D92370085B254 /* SearchResultMatchModel.swift in Sources */,
3714+
6C48B5DA2C0D5FC5001E9955 /* CurrentUser.swift in Sources */,
36643715
04BA7C1E2AE2D8A000584E1C /* GitClient+Clone.swift in Sources */,
36653716
58F2EB09292FB2B0004A9BDE /* TerminalSettings.swift in Sources */,
36663717
6C578D8429CD343800DC73B2 /* ExtensionDetailView.swift in Sources */,
@@ -3785,6 +3836,7 @@
37853836
587B60F82934124200D5CD8F /* CEWorkspaceFileManagerTests.swift in Sources */,
37863837
6130535F2B23A31300D767E3 /* MemorySearchTests.swift in Sources */,
37873838
587B61012934170A00D5CD8F /* UnitTests_Extensions.swift in Sources */,
3839+
6C1F3DA22C18C55800F6DEF6 /* ShellIntegrationTests.swift in Sources */,
37883840
283BDCC52972F236002AFF81 /* AcknowledgementsTests.swift in Sources */,
37893841
4EE96ECB2960565E00FFBEA8 /* DocumentsUnitTests.swift in Sources */,
37903842
4EE96ECE296059E000FFBEA8 /* NSHapticFeedbackPerformerMock.swift in Sources */,

CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ extension SettingsData {
5454
// Use font settings from Text Editing
5555
var useTextEditorFont: Bool = true
5656

57+
/// If `true`, use injection scripts for terminal features like automatic tab title.
58+
var useShellIntegration: Bool = true
59+
60+
/// If `true`, use a login shell.
61+
var useLoginShell: Bool = true
62+
5763
/// Default initializer
5864
init() {}
5965

@@ -70,6 +76,8 @@ extension SettingsData {
7076
) ?? .block
7177
self.cursorBlink = try container.decodeIfPresent(Bool.self, forKey: .cursorBlink) ?? false
7278
self.useTextEditorFont = try container.decodeIfPresent(Bool.self, forKey: .useTextEditorFont) ?? true
79+
self.useShellIntegration = try container.decodeIfPresent(Bool.self, forKey: .useShellIntegration) ?? true
80+
self.useLoginShell = try container.decodeIfPresent(Bool.self, forKey: .useLoginShell) ?? true
7381
}
7482
}
7583

CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ struct TerminalSettingsView: View {
2828
cursorStyle
2929
cursorBlink
3030
}
31+
Section {
32+
injectionOptions
33+
useLoginShell
34+
}
3135
}
3236
}
3337
}
@@ -38,7 +42,7 @@ private extension TerminalSettingsView {
3842
Text("System Default")
3943
.tag(SettingsData.TerminalShell.system)
4044
Divider()
41-
Text("ZSH")
45+
Text("Zsh")
4246
.tag(SettingsData.TerminalShell.zsh)
4347
Text("Bash")
4448
.tag(SettingsData.TerminalShell.bash)
@@ -81,4 +85,30 @@ private extension TerminalSettingsView {
8185
format: .number
8286
)
8387
}
88+
89+
@ViewBuilder private var injectionOptions: some View {
90+
VStack {
91+
Toggle("Shell Integration", isOn: $settings.useShellIntegration)
92+
// swiftlint:disable:next line_length
93+
.help("CodeEdit supports integrating with common shells such as Bash and Zsh. This enables features like terminal title detection.")
94+
if !settings.useShellIntegration {
95+
HStack {
96+
Image(systemName: "exclamationmark.triangle.fill")
97+
.foregroundStyle(Color(NSColor.systemYellow))
98+
Text("Warning: Disabling integration disables features such as terminal title detection.")
99+
Spacer()
100+
}
101+
}
102+
}
103+
}
104+
105+
@ViewBuilder private var useLoginShell: some View {
106+
if settings.useShellIntegration {
107+
Toggle("Use Login Shell", isOn: $settings.useLoginShell)
108+
// swiftlint:disable:next line_length
109+
.help("Whether or not to use a login shell when starting a terminal session. By default, a login shell is used used similar to Terminal.app.")
110+
} else {
111+
EmptyView()
112+
}
113+
}
84114
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// CurrentUser.swift
3+
// CodeEdit
4+
//
5+
// Created by Khan Winter on 6/2/24.
6+
//
7+
8+
import Foundation
9+
10+
/// Represents the currently logged in user.
11+
///
12+
/// Do not initialize this struct, instead use ``CurrentUser/getCurrentUser()`` to create and fill in the information.
13+
struct CurrentUser {
14+
/// The user's username.
15+
let name: String
16+
/// The path to the user's shell executable.
17+
let shell: String
18+
/// The user's home directory path.
19+
let homeDir: String
20+
/// The users id.
21+
let uid: uid_t
22+
/// The user's group id.
23+
let gid: gid_t
24+
25+
private init(name: String, shell: String, homeDir: String, uid: uid_t, gid: gid_t) {
26+
self.name = name
27+
self.shell = shell
28+
self.homeDir = homeDir
29+
self.uid = uid
30+
self.gid = gid
31+
}
32+
33+
/// Gets the current user using the `getpwuid_r` syscall.
34+
static func getCurrentUser() -> CurrentUser? {
35+
let bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)
36+
guard bufsize != -1 else { return nil }
37+
let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufsize)
38+
defer {
39+
buffer.deallocate()
40+
}
41+
var pwd = passwd()
42+
// points to `pwd`
43+
var result: UnsafeMutablePointer<passwd>? = UnsafeMutablePointer<passwd>.allocate(capacity: 1)
44+
45+
if getpwuid_r(getuid(), &pwd, buffer, bufsize, &result) != 0 { return nil }
46+
47+
return CurrentUser(
48+
name: String(cString: pwd.pw_name),
49+
shell: String(cString: pwd.pw_shell),
50+
homeDir: String(cString: pwd.pw_dir),
51+
uid: pwd.pw_uid,
52+
gid: pwd.pw_gid
53+
)
54+
}
55+
}

0 commit comments

Comments
 (0)