|
| 1 | +import * as vscode from "vscode" |
| 2 | + |
| 3 | +export interface TerminalProfile { |
| 4 | + name: string |
| 5 | + path?: string |
| 6 | + args?: string[] |
| 7 | + icon?: string |
| 8 | + color?: string |
| 9 | + env?: Record<string, string> |
| 10 | +} |
| 11 | + |
| 12 | +export interface TerminalProfileInfo { |
| 13 | + name: string |
| 14 | + displayName: string |
| 15 | + shellPath?: string |
| 16 | + isDefault?: boolean |
| 17 | +} |
| 18 | + |
| 19 | +export class TerminalProfileService { |
| 20 | + /** |
| 21 | + * Gets the current platform identifier for terminal profiles |
| 22 | + */ |
| 23 | + private static getPlatform(): string { |
| 24 | + switch (process.platform) { |
| 25 | + case "win32": |
| 26 | + return "windows" |
| 27 | + case "darwin": |
| 28 | + return "osx" |
| 29 | + case "linux": |
| 30 | + return "linux" |
| 31 | + default: |
| 32 | + return "linux" |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets all available terminal profiles for the current platform |
| 38 | + */ |
| 39 | + public static getAvailableProfiles(): TerminalProfileInfo[] { |
| 40 | + const platform = this.getPlatform() |
| 41 | + const config = vscode.workspace.getConfiguration("terminal.integrated") |
| 42 | + |
| 43 | + // Get profiles for the current platform |
| 44 | + const profiles = config.get<Record<string, TerminalProfile>>(`profiles.${platform}`) || {} |
| 45 | + |
| 46 | + // Get the default profile name |
| 47 | + const defaultProfile = config.get<string>(`defaultProfile.${platform}`) |
| 48 | + |
| 49 | + // Convert profiles to our format |
| 50 | + const profileInfos: TerminalProfileInfo[] = Object.entries(profiles).map(([name, profile]) => ({ |
| 51 | + name, |
| 52 | + displayName: name, |
| 53 | + shellPath: profile.path, |
| 54 | + isDefault: name === defaultProfile, |
| 55 | + })) |
| 56 | + |
| 57 | + // If no profiles are configured, return an empty array |
| 58 | + // VSCode will use its built-in defaults |
| 59 | + return profileInfos |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the default profile for the current platform |
| 64 | + */ |
| 65 | + public static getDefaultProfile(): TerminalProfileInfo | undefined { |
| 66 | + const platform = this.getPlatform() |
| 67 | + const config = vscode.workspace.getConfiguration("terminal.integrated") |
| 68 | + |
| 69 | + const defaultProfileName = config.get<string>(`defaultProfile.${platform}`) |
| 70 | + |
| 71 | + if (defaultProfileName) { |
| 72 | + const profiles = this.getAvailableProfiles() |
| 73 | + return profiles.find((profile) => profile.name === defaultProfileName) |
| 74 | + } |
| 75 | + |
| 76 | + return undefined |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets a specific profile by name |
| 81 | + */ |
| 82 | + public static getProfileByName(name: string): TerminalProfileInfo | undefined { |
| 83 | + const profiles = this.getAvailableProfiles() |
| 84 | + return profiles.find((profile) => profile.name === name) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets the shell path for a given profile name |
| 89 | + * Returns undefined if profile doesn't exist or doesn't specify a path |
| 90 | + */ |
| 91 | + public static getShellPathForProfile(profileName: string): string | undefined { |
| 92 | + if (!profileName) { |
| 93 | + return undefined |
| 94 | + } |
| 95 | + |
| 96 | + // Check regular profiles |
| 97 | + const profile = this.getProfileByName(profileName) |
| 98 | + return profile?.shellPath |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the full profile configuration from terminal.integrated.profiles.{platform} |
| 103 | + */ |
| 104 | + public static getProfileConfiguration(profileName: string): Partial<vscode.TerminalOptions> | undefined { |
| 105 | + if (!profileName) { |
| 106 | + return undefined |
| 107 | + } |
| 108 | + |
| 109 | + const platform = this.getPlatform() |
| 110 | + const config = vscode.workspace.getConfiguration("terminal.integrated") |
| 111 | + const profiles = config.get<Record<string, TerminalProfile>>(`profiles.${platform}`) || {} |
| 112 | + |
| 113 | + const profile = profiles[profileName] |
| 114 | + if (!profile) { |
| 115 | + return undefined |
| 116 | + } |
| 117 | + |
| 118 | + // Convert VSCode terminal profile to vscode.TerminalOptions |
| 119 | + const terminalOptions: Partial<vscode.TerminalOptions> = {} |
| 120 | + |
| 121 | + if (profile.path) { |
| 122 | + terminalOptions.shellPath = profile.path |
| 123 | + } |
| 124 | + |
| 125 | + if (profile.args) { |
| 126 | + terminalOptions.shellArgs = profile.args |
| 127 | + } |
| 128 | + |
| 129 | + if (profile.env) { |
| 130 | + terminalOptions.env = profile.env |
| 131 | + } |
| 132 | + |
| 133 | + if (profile.icon) { |
| 134 | + terminalOptions.iconPath = new vscode.ThemeIcon(profile.icon) |
| 135 | + } |
| 136 | + |
| 137 | + if (profile.color) { |
| 138 | + terminalOptions.color = new vscode.ThemeColor(profile.color) |
| 139 | + } |
| 140 | + |
| 141 | + return terminalOptions |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets the terminal options that Roo should use for terminals |
| 146 | + * Priority: 1. User's preferred profile, 2. Default profile, 3. undefined (VSCode default) |
| 147 | + */ |
| 148 | + public static getTerminalOptionsForRoo(preferredProfile?: string): Partial<vscode.TerminalOptions> | undefined { |
| 149 | + // 1. Check user's preferred profile |
| 150 | + if (preferredProfile) { |
| 151 | + const profileConfig = this.getProfileConfiguration(preferredProfile) |
| 152 | + if (profileConfig) { |
| 153 | + return profileConfig |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // 2. Check default profile |
| 158 | + const defaultProfile = this.getDefaultProfile() |
| 159 | + if (defaultProfile) { |
| 160 | + const profileConfig = this.getProfileConfiguration(defaultProfile.name) |
| 161 | + if (profileConfig) { |
| 162 | + return profileConfig |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // 3. Let VSCode use its default |
| 167 | + return undefined |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Gets the shell path that Roo should use for terminals (backward compatibility) |
| 172 | + * Priority: 1. User's preferred profile, 2. Default profile, 3. undefined (VSCode default) |
| 173 | + */ |
| 174 | + public static getShellPathForRoo(preferredProfile?: string): string | undefined { |
| 175 | + const terminalOptions = this.getTerminalOptionsForRoo(preferredProfile) |
| 176 | + return terminalOptions?.shellPath |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Gets all profiles that should be shown in the UI |
| 181 | + */ |
| 182 | + public static getAllSelectableProfiles(): TerminalProfileInfo[] { |
| 183 | + const profiles = this.getAvailableProfiles() |
| 184 | + |
| 185 | + // Add a "Default" option that represents using VSCode's default behavior |
| 186 | + profiles.unshift({ |
| 187 | + name: "", |
| 188 | + displayName: "Default (VSCode Default)", |
| 189 | + isDefault: false, |
| 190 | + }) |
| 191 | + |
| 192 | + return profiles |
| 193 | + } |
| 194 | +} |
0 commit comments