Skip to content

Commit 4beead1

Browse files
committed
CheckIfInteractive
1 parent 7c3438e commit 4beead1

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,28 @@ protected void SaveDataCollectionProfile()
225225
WriteWarning(string.Format(Resources.DataCollectionSaveFileInformation, fileFullPath));
226226
}
227227

228+
protected bool CheckIfInteractive()
229+
{
230+
bool interactive = true;
231+
try
232+
{
233+
var test = this.Host.UI.RawUI.CursorSize;
234+
}
235+
catch (HostException ex)
236+
{
237+
if (ex.Message.StartsWith("A command that prompts the user failed"))
238+
{
239+
interactive = false;
240+
}
241+
else
242+
{
243+
throw ex;
244+
}
245+
}
246+
247+
return interactive;
248+
}
249+
228250
/// <summary>
229251
/// Prompt for the current data collection profile
230252
/// </summary>
@@ -234,7 +256,7 @@ protected void PromptForDataCollectionProfileIfNotExists()
234256
// Initialize it from the environment variable or profile file.
235257
InitializeDataCollectionProfile();
236258

237-
if (GeneralUtilities.CheckIfInteractive() && !_dataCollectionProfile.EnableAzureDataCollection.HasValue)
259+
if (CheckIfInteractive() && !_dataCollectionProfile.EnableAzureDataCollection.HasValue)
238260
{
239261
WriteWarning(Resources.DataCollectionPrompt);
240262

@@ -259,7 +281,7 @@ protected void PromptForDataCollectionProfileIfNotExists()
259281
bool enabled = false;
260282
if (this.Host.UI.RawUI.KeyAvailable)
261283
{
262-
KeyInfo keyInfo = this.Host.UI.RawUI.ReadKey(ReadKeyOptions.NoEcho);
284+
KeyInfo keyInfo = this.Host.UI.RawUI.ReadKey(ReadKeyOptions.NoEcho | ReadKeyOptions.AllowCtrlC | ReadKeyOptions.IncludeKeyDown);
263285
enabled = (keyInfo.Character == 'Y' || keyInfo.Character == 'y');
264286
}
265287

src/Common/Commands.Common/GeneralUtilities.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,5 @@ public static void EnsureDefaultProfileDirectoryExists()
430430
AzureSession.DataStore.CreateDirectory(AzureSession.ProfileDirectory);
431431
}
432432
}
433-
434-
/// <summary>
435-
/// Check if the PS program is run in interactive mode.
436-
/// </summary>
437-
public static bool CheckIfInteractive()
438-
{
439-
var arguments = Environment.GetCommandLineArgs();
440-
return arguments.Any(s => !string.Equals(s, "-noninteractive"));
441-
}
442433
}
443434
}

0 commit comments

Comments
 (0)