@@ -139,6 +139,7 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
139
139
namesWithConflict := make (map [string ]bool )
140
140
codespacesByName := make (map [string ]codespaceWithIndex )
141
141
codespacesNames := make ([]string , 0 , len (codespaces ))
142
+ codespacesDirty := make (map [string ]bool )
142
143
for _ , apiCodespace := range codespaces {
143
144
cs := codespace {apiCodespace }
144
145
csName := cs .displayName (false , false )
@@ -155,7 +156,14 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
155
156
156
157
codespacesByName [fullDisplayName ] = codespaceWithIndex {seenCodespace .cs , seenCodespace .idx }
157
158
codespacesNames [seenCodespace .idx ] = fullDisplayNameWithGitStatus
158
- delete (codespacesByName , csName ) // delete the existing map entry with old name
159
+
160
+ // Update the git status dirty map to reflect the new name.
161
+ if cs .hasUnsavedChanges () {
162
+ codespacesDirty [fullDisplayNameWithGitStatus ] = true
163
+ }
164
+
165
+ // delete the existing map entry with old name
166
+ delete (codespacesByName , csName )
159
167
160
168
// All other codespaces with the same name should update
161
169
// to their specific name, this tracks conflicting names going forward
@@ -169,6 +177,10 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
169
177
170
178
codespacesByName [csName ] = codespaceWithIndex {cs , len (codespacesNames )}
171
179
codespacesNames = append (codespacesNames , displayNameWithGitStatus )
180
+
181
+ if cs .hasUnsavedChanges () {
182
+ codespacesDirty [displayNameWithGitStatus ] = true
183
+ }
172
184
}
173
185
174
186
csSurvey := []* survey.Question {
@@ -193,10 +205,28 @@ func chooseCodespaceFromList(ctx context.Context, codespaces []*api.Codespace) (
193
205
// Codespaces are indexed without the git status included as compared
194
206
// to how it is displayed in the prompt, so the git status symbol needs
195
207
// cleaning up in case it is included.
196
- selectedCodespace := strings .Replace (answers .Codespace , gitStatusDirty , "" , - 1 )
208
+ selectedCodespace := answers .Codespace
209
+ isDirty := codespacesDirty [selectedCodespace ]
210
+ if isDirty {
211
+ selectedCodespace = withoutGitStatus (answers .Codespace )
212
+ }
197
213
return codespacesByName [selectedCodespace ].cs .Codespace , nil
198
214
}
199
215
216
+ // withoutGitStatus returns the string without the git status symbol.
217
+ func withoutGitStatus (cname string ) string {
218
+ return replaceLastOccurence (cname , gitStatusDirty , "" )
219
+ }
220
+
221
+ // replaceLastOccurence replaces the last occurence of a string with another string.
222
+ func replaceLastOccurence (str , old , replace string ) string {
223
+ i := strings .LastIndex (str , old )
224
+ if i == - 1 {
225
+ return str
226
+ }
227
+ return str [:i ] + replace + str [i + len (old ):]
228
+ }
229
+
200
230
// getOrChooseCodespace prompts the user to choose a codespace if the codespaceName is empty.
201
231
// It then fetches the codespace record with full connection details.
202
232
// TODO(josebalius): accept a progress indicator or *App and show progress when fetching.
0 commit comments